how to replace characters into digits

1 回表示 (過去 30 日間)
Sharen H
Sharen H 2013 年 2 月 27 日
i have to replace the each characters using the following digits s=ACGT
i have to replace as
'A' then 11
'C' then 00
'G' then 01
'T' then 10
  1 件のコメント
Jan
Jan 2013 年 2 月 27 日
You write 11 without surrounding quotes. This isn't an accident, correctly? What do you want as output? How long is the input?

サインインしてコメントする。

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 2 月 27 日
編集済み: Azzi Abdelmalek 2013 年 2 月 27 日
clear
s='ACGT'
e=['11';'00';'01';'10']
in='AGCTAG' % Your initial data
out=in
for k=1:numel(s)
out=regexprep(out,s(k),e(k,:))
end
  3 件のコメント
Jan
Jan 2013 年 2 月 28 日
STRREP is less powerful, but much faster than REGEXPREP.
Cedric
Cedric 2013 年 2 月 28 日
STRREP is probably the best solution. It is a little slower than my solution, but more memory friendly.

サインインしてコメントする。

その他の回答 (3 件)

Jan
Jan 2013 年 2 月 28 日
And a lookup table:
seqIn = 'ACGTTGCA'
table = repmat('0', 2, 255);
table(1, 'AT') = '1';
table(2, 'AG') = '1';
result = reshape(table(:, seqIn), 1, []);
Does this work? I do not have access to Matlab currently.
  3 件のコメント
Cedric
Cedric 2013 年 2 月 28 日
編集済み: Cedric 2013 年 2 月 28 日
Actually STRREP still wins up to me. The solution based on ISMEMBER is profiled between REGEXP and the two solutions based on array indexing. But the latter require much more memory during the indexing operation than STRREP. You start seeing that with ~1e8 chars. On my laptop with 8GB RAM for example, only STRREP can treat more than 2e8 chars without swapping (or being killed by Process Lasso).
Jan
Jan 2013 年 2 月 28 日
@Azzi: Is this a typo?! Your function needs 14 secs with REGEXPREP and 0.007 secs with STRREP? Then my minor suggestion caused a speedup of a factor 1900? Wow, this would be the most efficient suggestion I ever gave. And it would be a strong hint to warn for the low efficiency of regexprep in this forum.

サインインしてコメントする。


Cedric
Cedric 2013 年 2 月 27 日
編集済み: Cedric 2013 年 2 月 27 日
If you need to process long sequences, you might want to optimize a little the efficiency.. a MEX-based solution would be most efficient I guess, but here is one way you could go using basic MATLAB only..
If you want to replace character 'A' with a numeric array (1,1) and so on, you can do:
aa = 'ACGT' ;
seq = 'AAGCTCAGGTTCA' ;
rep = zeros(2, max(aa), 'uint8') ;
rep(:,aa) = [1 0 0 1; 1 0 1 0] ;
result = reshape(rep(:,seq), 1, []) ;
This outputs the numeric array:
result =
1 1 1 1 0 1 0 0 1 0 0 0 1 1 0 1 0 1 1 0 1 0 0 0 1 1
If you want to replace character 'A' with characters '11' and so on, you can do:
aa = 'ACGT' ;
seq = 'AAGCTCAGGTTCA' ;
rep = zeros(2, max(aa), 'uint8') ;
rep(:,aa) = ['11'; '00'; '01'; '10'].' ;
result = char(reshape(rep(:,seq), 1, [])) ;
This outputs the string '11110100100011010110100011'.
EDIT: note that there are slightly different ways of doing this a little slower but with a more memory-friendly approach.
Cheers,
Cedric

Jos (10584)
Jos (10584) 2013 年 2 月 28 日
Here is a simpler approach than looping over REGEXPREP or STRREP:
seqIn = 'ACGTTGCA' % input sequence
letters = 'ACGT' ;
symb = {'11','00','10','01'} ; % stored as a cell array of strings!
[tf,idx] = ismember(seqIn,letters) ;
seqOut = [symb{idx}]

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by