How to copy specific columns of a character array to another character array?

2 visualizzazioni (ultimi 30 giorni)
I have a character array named 'seq' as following:
MRSLMVLALLAVAALCLCLAGPADAKPSSAESRKGGATFVSKREGSEVVRRLRRYLDSGL
MRTPMLLALLALAT--LCLAGRADAKPGDAESGK-GAAFVSKQEGSEVVKRLRRYLDHWL
MRALTLLALLALAT--LCITGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL
MRALTLLALLALAA--LCIAGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL
MRALTLLALLALAA--LCIAGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL
I want to remove those columns which has more than 50% gaps (-) in them. For example, column 15 has 4 gaps out of 5, so I want to remove that column. After removing these columns, I want to take the rest to another character array (e.g 'mod_seq').
I tried with this code:
[rowLen, colLen] = size(seq);
count = 0;
mod_col_no = 1;
mod_seq = zeros(rowLen,colLen);
end
for i=1:colLen
for j=1:rowLen
if seq(j,i) == '-'
count = count+1;
end
end
if count > ceil(rowLen/2)
continue;
else
mod_seq(:,mod_col_no) = seq(:,i);
mod_col_no=+1;
end
end
mod_seq = char(mod_seq);
I have tried like this because I cannot initialize 'mod_seq'. I can't simply make the two arrays equal because when I delete columns, mod_seq will have less dimension.

Risposta accettata

Guillaume
Guillaume il 22 Ott 2015
Modificato: Guillaume il 22 Ott 2015
The way to fix your loop would be to gather the list of columns to delete inside the loop and perform the deletion after the loop.
Even simpler is to not bother with loops at all when a single line of code can do the same:
seq=['MRSLMVLALLAVAALCLCLAGPADAKPSSAESRKGGATFVSKREGSEVVRRLRRYLDSGL';
'MRTPMLLALLALAT--LCLAGRADAKPGDAESGK-GAAFVSKQEGSEVVKRLRRYLDHWL';
'MRALTLLALLALAT--LCITGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL';
'MRALTLLALLALAA--LCIAGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL';
'MRALTLLALLALAA--LCIAGQAGAKPSGAESSK-GAAFVSKQEGSEVVKRPRRYLYQWL';]
mod_seq = seq(:, sum(seq == '-') < size(seq, 1)/2)
sum(seq == '-') gives you straight away the count of '-' per column.
  1 Commento
Rasif Ajwad
Rasif Ajwad il 22 Ott 2015
Modificato: Rasif Ajwad il 22 Ott 2015
Thanks. It worked. :)
But can you please explain this command as I am not familiar with it.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by