Matthew Matthew - 6 months ago 9
Java Question

Program is suppose to split DNA is groups of 3 but instead sees the sequence as too long

For simplicities sake I pasted the whole thing in but the test case that we are suppose to use is CAGTTTAGCTAGAAA and when I do this I get an error because it says its too long but where the error is, that is only suppose to group them into 3 so later on they can be convertated based on their groups.

public class Soutar_bioinformatics
{
public static void main (String[] args)
{
//Variable declaration
int DNAstrandlength, DNAsplit;
String strandDNA;
Scanner DNAInfo = new Scanner (System.in);
//Welcome
System.out.println ("**********************************************");
System.out.println ("Welcome to Matthew's Bioinformatics");
System.out.println ("**********************************************");

//Input
System.out.println ("Please enter a DNA Strand!");
strandDNA = DNAInfo.next();
DNAstrandlength = strandDNA.length();

if (DNAstrandlength % 3 != 0)
{
System.out.println ("The DNA sequence enterered is not valid, please insert a correct sequence.");
strandDNA = DNAInfo.next();
}

//Processing
ArrayList<String> DNAarray = DNAsplit(strandDNA); // Calls the DNA split method

//Goodbye
System.out.println ("**********************************************");
System.out.println ("Thank you for using Matthew's Program");
System.out.println ("**********************************************");

}
private static ArrayList<String> DNAsplit (String text)
{
ArrayList<String> DNAsplit = new ArrayList <String>();

for (int i = 0; 1 < text.length(); i += 3)
{
DNAsplit.add(text.substring(i, Math.min(i + 3, text.length())));
}

String [] codonArray = DNAsplit.toArray (new String[DNAsplit.size()]);
System.out.print ("\f");
System.out.println ("The Amino acids are: ");

for (int j = 0; j < codonArray.length; j = j + 1)
{


//First Column
codonArray[j] = codonArray[j].replace ("TTT", "F");
codonArray[j] = codonArray[j].replace("TCT", "S");
codonArray[j] = codonArray[j].replace("TAT", "Y");
codonArray[j] = codonArray[j].replace("TGT","C");
codonArray[j] = codonArray[j].replace("CTT", "L");
codonArray[j] = codonArray[j].replace("CCT", "P");
codonArray[j] = codonArray[j].replace("CAT", "H");
codonArray[j] = codonArray[j].replace("CGT", "R");
codonArray[j] = codonArray[j].replace("ATT" , "I");
codonArray[j] = codonArray[j].replace("ACT", "T");
codonArray[j] = codonArray[j].replace("AAT", "N");
codonArray[j] = codonArray[j].replace("AGT", "S");
codonArray[j] = codonArray[j].replace("GTT", "V");
codonArray[j] = codonArray[j].replace("GCT" , "A");
codonArray[j] = codonArray[j].replace("GAT" , "D");
codonArray[j] = codonArray[j].replace("GGT", "G");
codonArray[j] = codonArray[j].replace ("TTT", "F");
//Second Column
codonArray[j] = codonArray[j].replace("TCT", "S");
codonArray[j] = codonArray[j].replace("TAT", "Y");
codonArray[j] = codonArray[j].replace("TGT", "C");
codonArray[j] = codonArray[j].replace("CTT", "L");
codonArray[j] = codonArray[j].replace("CCT", "P");
codonArray[j] = codonArray[j].replace("CAT", "H");
codonArray[j] = codonArray[j].replace("CGT", "R");
codonArray[j] = codonArray[j].replace("ATT" , "I");
codonArray[j] = codonArray[j].replace("ACT", "T");
codonArray[j] = codonArray[j].replace("AAT", "N");
codonArray[j] = codonArray[j].replace("AGT", "S");
codonArray[j] = codonArray[j].replace("GTT", "V");
codonArray[j] = codonArray[j].replace("GCT" ,"A");
codonArray[j] = codonArray[j].replace("GAT" , "D");
codonArray[j] = codonArray[j].replace("GGT", "G");
//Third Coloumn
codonArray[j] = codonArray[j].replace ("TTA", "L");
codonArray[j] = codonArray[j].replace ("TCA", "S");
codonArray[j] = codonArray[j].replace ("TAA", "STOP");
codonArray[j] = codonArray[j].replace ("TGA", "STOP");
codonArray[j] = codonArray[j].replace ("CTA", "L");
codonArray[j] = codonArray[j].replace ("CCA", "P");
codonArray[j] = codonArray[j].replace ("CAA", "Q");
codonArray[j] = codonArray[j].replace ("CGA", "R");
codonArray[j] = codonArray[j].replace ("ATA", "I");
codonArray[j] = codonArray[j].replace ("ACA", "T");
codonArray[j] = codonArray[j].replace ("AAA", "K");
codonArray[j] = codonArray[j].replace ("AGA", "R");
codonArray[j] = codonArray[j].replace ("GTA", "V");
codonArray[j] = codonArray[j].replace ("GCA", "A");
codonArray[j] = codonArray[j].replace ("GAA", "E");
codonArray[j] = codonArray[j].replace ("GGA", "E");
//Fourth Column
codonArray[j] = codonArray[j].replace ("TTG", "L");
codonArray[j] = codonArray[j].replace ("TCG", "S");
codonArray[j] = codonArray[j].replace ("TAG", "STOP");
codonArray[j] = codonArray[j].replace ("TGG", "W");
codonArray[j] = codonArray[j].replace ("CTG", "L");
codonArray[j] = codonArray[j].replace ("CCG", "P");
codonArray[j] = codonArray[j].replace ("CAG", "Q");
codonArray[j] = codonArray[j].replace ("CGG", "R");
codonArray[j] = codonArray[j].replace ("ATG", "M");
codonArray[j] = codonArray[j].replace ("ACG", "T");
codonArray[j] = codonArray[j].replace ("AAG", "K");
codonArray[j] = codonArray[j].replace ("AGG", "R");
codonArray[j] = codonArray[j].replace ("GTG", "V");
codonArray[j] = codonArray[j].replace ("GCG", "A");
codonArray[j] = codonArray[j].replace ("GAG", "E");
codonArray[j] = codonArray[j].replace ("GGG", "G");


System.out.print (codonArray[j] + " ");
if (codonArray[j].contains ("STOP"))
{
break;
}

}
return DNAsplit;
}
}

Answer

There is a simple error on your code in DNASplit Function.

        for (int i = 0; 1 < text.length(); i += 3)
        {
            DNAsplit.add(text.substring(i, Math.min(i + 3, text.length())));
        }

that 1 in for loop should be i.