Skip to content

Instantly share code, notes, and snippets.

@elvismdev
Created January 11, 2016 08:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save elvismdev/ae37ab3445b9e51b8f97 to your computer and use it in GitHub Desktop.
Save elvismdev/ae37ab3445b9e51b8f97 to your computer and use it in GitHub Desktop.
A Java small class to find all the genes in a DNA string. The library edu.duke is a dependency for the class to work, it should be added into the Java IDE to compile with no errors. Download link http://www.dukelearntoprogram.com/downloads/archives/courserajava.jar
/**
* Find all the genes in a DNA string.
*
* @author (Elvis Morales)
* @version (1.0)
*/
public class FindMultipleGenes {
public int findStopIndex(String dna, int index) {
int stop1 = dna.indexOf("tga", index);
if ( stop1 == -1 || ( stop1-index ) % 3 != 0 ) {
stop1 = dna.length();
}
int stop2 = dna.indexOf("taa", index);
if ( stop2 == -1 || ( stop2-index ) % 3 != 0 ) {
stop2 = dna.length();
}
int stop3 = dna.indexOf("tag", index);
if ( stop3 == -1 || ( stop3-index ) % 3 != 0 ) {
stop3 = dna.length();
}
return Math.min( stop1, Math.min(stop2, stop3) );
}
public void printAll(String dna) {
String dnaLow = dna.toLowerCase();
int start = 0;
while (true) {
int loc = dnaLow.indexOf( "atg", start );
if ( loc == -1 ) {
break;
}
int stop = findStopIndex( dnaLow, loc+3 );
if ( stop != dna.length() ) {
System.out.println( dna.substring(loc, stop+3) );
start = stop + 3;
} else {
start = start + 3;
}
}
}
public void testFinder() {
String dna = "CATGTAATAGATGAATGACTGATAGATATGCTTGTATGCTATGAAAATGTGAAATGACCCA";
System.out.println("DNA string is: \n" +dna);
System.out.println("Genes found are:");
printAll(dna);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment