필터 지우기
필터 지우기

how to replace characters into digits

조회 수: 3 (최근 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 CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by