How do I make function which gets as input arguments matrix of chars work?

2 views (last 30 days)
I am trying to write function which converts RNA codons into amino acids. The codons is matrix of chars for example 10x3. There is still is an error output arguments "aminoacids" (and maybe others) not assigned during call to "translation". I don/t know how to fix it. Tried to by replacing aminoacids(m) with result(m) and then typing just above the end of the function "aminoacids=result" but it still doesn't work. Please help.
function [aminoacids] = translation(codons)
for m = 1:length(codons)
switch (codons(m))
case {'GCU', 'GCC', 'GCA', 'GCG'}
aminoacids(m) = 'Ala';
case {'UGU', 'UGC'}
aminoacids(m) = 'Cys';
case {'GAU', 'GAC'}
aminoacids(m) = 'Asp';
case {'GAA', 'GAG'}
aminoacids(m) = 'Glu';
case {'UUU', 'UUC'}
aminoacids(m) = 'Phe';
case {'GGU', 'GGC', 'GGA', 'GGG'}
aminoacids(m) = 'Gly';
case {'CAU', 'CAC'}
aminoacids(m) = 'His';
case {'AUU', 'AUC', 'AUA'}
aminoacids(m) = 'Ile';
case {'AAA', 'AAG'}
aminoacids(m) = 'Lys';
case {'UUA', 'UUG', 'CUU', 'CUC', 'CUA', 'CUG'}
aminoacids(m) = 'Leu';
case 'AUG'
aminoacids(m) = 'Met';
case {'AAU', 'AAC'}
aminoacids(m) = 'Asn';
case {'CCU', 'CCC', 'CCA', 'CCG'}
aminoacids(m) = 'Pro';
case {'CAA', 'CAG'}
aminoacids(m) = 'Gln';
case {'CGU', 'CGC', 'CGA', 'CGG', 'AGA', 'AGG'}
aminoacids(m) = 'Arg';
case {'UCU', 'UCC', 'UCA', 'UCG', 'AGU', 'AGC'}
aminoacids(m) = 'Ser';
case {'ACU', 'ACC', 'ACA', 'ACG'}
aminoacids(m) = 'Thr';
case {'GUU', 'GUC', 'GUA', 'GUG'}
aminoacids(m) = 'Val';
case 'UGG'
aminoacids(m) = 'Trp';
case {'UAU', 'UAC'}
aminoacids(m) = 'Tyr';
case {'UAA', 'UAG', 'UGA'}
aminoacids(m) = 'Stop';
end
end
aminoacids = aminoacids;
end

Accepted Answer

TADA
TADA on 25 Jan 2019
Edited: TADA on 25 Jan 2019
Kevin's Answer Is Correct Of Course,
But If that Was The Problem You Were Facing You Would Have Had A Different Error About Indexing Mismatch or SWITCH expression must be a scalar or a character vector.
I suspect you called the translation function with a character vector input, something like:
a = translation('GCUGAC')
the problem is that when you test the value at index m of that codons vector you get a single character 'G', 'C', 'U' or 'A' which is never equal to any of the sequences in your switch case.
what you need to do is send the codon sequence as a cell array of character vectors:
a = translation({'GCU' 'GAC'})
then you need to index that inside the loop with curly bracers like kevin said:
switch (codons{m})
and the output should also be a cell array proobably (like kevin suggested)
moreover, it's always a good idea to add a default value for output arguments, add
aminoacids = {};
just before the loop.
  2 Comments
Drugie Konto
Drugie Konto on 25 Jan 2019
Thank you so much for your explanation! Yeah, it works with cell arrays. Getting to work on my 'split into codons' function right now
TADA
TADA on 25 Jan 2019
Edited: TADA on 25 Jan 2019
a different approach if you insist on a char vector rather than a cell array for whatever reason,
you can take advantage of the fact that all codons for a single amino acid are 3 bases long (I think... is that correct?) and of the fact that you are using the 3-letter abbreviation for amino acid code, and make the loop index have intervals of 3:
% preallocate out argument
aminoacids = char(zeros(1,length(codons)));
% loop in intervals of 3 characters - single amino acid codon length
for m = 1:3:length(codons)
% extract current codon
codValue = codons(m:(m+2));
switch (codValue)
case {'GCU', 'GCC', 'GCA', 'GCG'}
% current codon encoded to alanine
% set the output value at mth index to alanine code
aminoacids(m:(m+2)) = 'Ala';
% more of the same...
end
end

Sign in to comment.

More Answers (1)

Kevin Phung
Kevin Phung on 25 Jan 2019
Use curly brackets.
example:
case {'GUU', 'GUC', 'GUA', 'GUG'}
aminoacids{m} = 'Val';
I also dont think you need the line aminoacids=aminoacids
  1 Comment
Drugie Konto
Drugie Konto on 25 Jan 2019
Yeah, that's right, it was there by mistake. Thank you for your answer, it still doesn't work unfortunately. I guess the brackets wasn't the problem with aminoacids, but can't figure it out what is

Sign in to comment.

Categories

Find more on Genomics and Next Generation Sequencing in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!