开发者

Function, in MATLAB dna replication

开发者 https://www.devze.com 2023-02-28 03:10 出处:网络
I\'m trying to figure out the function command in Matlab and im having some difficulties. I\'m trying to write a Matlab function named dna_replicate. It will replicate a given 开发者_StackOverflowstra

I'm trying to figure out the function command in Matlab and im having some difficulties. I'm trying to write a Matlab function named dna_replicate. It will replicate a given 开发者_StackOverflowstrand and return its partner strand

For example if the user enters ATGCATGCAHGCAGTC, it should return TACGTACGT CGTCAG

A-->T G-->C if the user enters other than these 4 letters, there should be blank in the partner strand. Thank you for your help


This implementation should be faster, involving only a simple table look-up. Note that the table t is constructed only once when the function is first called.

function out = dna_replicate(in)
  persistent t
  if isempty(t)
    t = blanks(256);
    t('ATGC') = 'TACG';
  end
  out = t(in);
end


How about:

function out = dna_replicate(in)
in = upper(in); % Ensures all same case
A = in=='A';
T = in=='T';
C = in=='C';
G = in=='G';
out = in;
out(A) = 'T';
out(T) = 'A';
out(C) = 'G';
out(G) = 'C';
out(~(A|T|C|G)) = ' ';

while @Jirka cigler answer works, it uses a for loop as well as dynamically growing vector 'out'. As matlab is optimized for vector operations, this answer should perform better.


You can create a simple vectorized solution using the function ISMEMBER:

function outString = dna_replicate(inString)
  [~,index] = ismember(upper(inString),'ACGT');  %# Find the indices of inStrings
                                                 %#   letters in string `ACGT`
  outString = 'ACGT ';             %# Initialize outString to `ACGT` and a blank
  outString = outString(5-index);  %# Use inverted and shifted index to expand
                                   %#   outString to the size of inString
end

And here's a test:

>> dna_replicate('ATGCATGCAHGCAGTC')

ans =

TACGTACGT CGTCAG


I think it can be implemented as follows:

   function out=dna_replicate(in)
for i=1:numel(in)
    switch in(i)
        case 'A'
            out(i)= 'T';
        case 'G'
            out(i)= 'C';
        case 'T'
            out(i)='A';
        case 'C'
            out(i)='G';
        otherwise
            out(i)=' ';
    end
end

this function has argument of type char

in='ATGCATGCAHGCAGTC'

and you can run

out=dna_replicate(in)

to get the same result as you want :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消