How do I connect my strings to my function?

조회 수: 1 (최근 30일)
Natalie Merchant
Natalie Merchant 2022년 5월 11일
댓글: Natalie Merchant 2022년 5월 11일
I need to use the input nucleotide to make a string of amino acids. I am unsure how to use a for loop to do this, if someone could please help me. My code is below:
function [aminoAcidChain] = synthesise(nucleotide)
nucleotide = input('Please enter a nucelotide chain: ','s');
%Make sure code is all uppercase and 3 characters long
nucleotide = upper(nucleotide);
while ~all(ismember(nucleotide, 'AGUC'))
error('Character entered is invalid.');
end
%Divide the nucleotide chain into groups of 3 and discard leftover
%characters
if length (nucleotide)<3
aminoAcidChain= char([]);
end
codonChain = floor(length(nucleotide)/3);
aminoAcidChain = char(zeros(codonChain,3));
for i = 1:codonChain
start(i)= 3<(i-1)+1;
finish(i) = start+2;
CCodons = codonChain(start(i) : finish(i));
aminoAcidChain(i,:) = CCodons;
end
%convert codons to amino acids
UUC = {'F'};
UUU = {'F'};
UUA = {'L'};
UUG = {'L'};
CUU = {'L'};
CUC = {'L'};
CUG = {'L'};
AUU = {'I'};
AUC = {'I'};
AUA = {'I'};
AUG = {'M'};
GUU = {'V'};
GUC = {'V'};
GUA = {'V'};
GUG = {'V'};
UCU = {'S'};
UCC = {'S'};
UCA = {'S'};
UCG = {'S'};
CCU = {'P'};
CCC = {'P'};
CCA = {'P'};
CCG = {'P'};
ACU = {'T'};
ACC = {'T'};
ACA = {'T'};
ACG = {'T'};
GCU = {'A'};
GCC = {'A'};
GCA = {'A'};
GCG = {'A'};
UAU = {'Y'};
UAC = {'Y'};
CAU = {'H'};
CAC = {'H'};
CAA = {'Q'};
CAG = {'Q'};
AAU = {'N'};
AAC = {'N'};
AAA = {'K'};
AAG = {'K'};
GAU = {'D'};
GAC = {'D'};
GAA = {'E'};
GAG = {'E'};
UGU = {'C'};
UGC = {'C'};
UGG = {'W'};
CGU = {'R'};
CGC = {'R'};
CGA = {'R'};
CGC = {'R'};
AGU = {'S'};
AGC = {'S'};
AGA = {'R'};
AGG = {'R'};
GGU = {'G'};
GGC = {'G'};
GGA = {'G'};
GGG = {'G'};
UAA = {'STOP'};
UAG = {'STOP'};
UGA = {'STOP'};
fprintf('The resulting amino acid chain from the nucleotide is %s.',codonChain);
end

채택된 답변

Geoff Hayes
Geoff Hayes 2022년 5월 11일
편집: Geoff Hayes 2022년 5월 11일
@Natalie Merchant - I may not have grasped exactly what you want, but I think from the code and your question, you are looking for a way to create a string of (multiple) three letter codes that are one of the characters AGUC. I wasn't sure if you are prompting the user to enter a 3 character chain at a time or a string whose length is a multiple of three. If the former, you could store the 3 character codes in a cell array like
nucleotides = {};
k = 1;
while true
nucleotide = input('Please enter a nucelotide chain: ','s');
%Make sure code is all uppercase and 3 characters long
nucleotide = upper(nucleotide);
if strcmpi(nucleotide, 'exit')
break;
elseif length(nucleotide) ~= 3
fprintf('Nucleotide string is not 3 characters long.\n');
elseif ~all(ismember(nucleotide, 'AGUC'))
fprintf('Nucleotide string has invalid character.\n');
end
nucleotides{k} = nucleotide;
k = k + 1;
end
You can then iterate over this cell array and convert each cell to the amino acid equivalents (which you may want to store in a containers.Map where the key is the 3 digit code ('UUC' for example) and the value is the amino acid string 'F').
Looking closer at your function
function [aminoAcidChain] = synthesise(nucleotide)
nucleotide = input('Please enter a nucelotide chain: ','s');
why do you allow the user to pass in the parameter nucleotide only to overwrite it with user input?
  댓글 수: 1
Natalie Merchant
Natalie Merchant 2022년 5월 11일
Thank you for your help. Yes sorry should of given more detail but the user will give a nucleotide chain that is longer than three. And not sure why I overwrite that not sure I did my function correctly!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by