Last active
January 11, 2016 04:23
-
-
Save elvismdev/23fdd92d41e8c84afb1d to your computer and use it in GitHub Desktop.
A Java small class to find a protein within a strand of DNA represented as a string of C, G, T, A letters. 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Finds a protein within a strand of DNA represented as a string of c,g,t,a letters. | |
* A protein is a part of the DNA strand marked by start and stop codons (DNA triples) | |
* that is a multiple of 3 letters long. | |
* | |
* @author Duke Software Team | |
*/ | |
import edu.duke.*; | |
import java.io.*; | |
public class FindGene { | |
public String findProtein(String dna) { | |
dna = dna.toLowerCase(); | |
int start = dna.indexOf("atg"); | |
if (start == -1) { | |
return ""; | |
} | |
int stopTag = dna.indexOf("tag", start+3); | |
int stopTga = dna.indexOf("tga", start+3); | |
int stopTaa = dna.indexOf("taa", start+3); | |
if ( ( stopTag - start ) % 3 == 0 ) { | |
return dna.substring( start, stopTag+3 ); | |
} else if ( ( stopTga - start ) % 3 == 0 ) { | |
return dna.substring( start, stopTga+3 ); | |
} else if ( ( stopTaa - start ) % 3 == 0 ) { | |
return dna.substring( start, stopTaa+3 ); | |
} else { | |
return ""; | |
} | |
} | |
public void testing() { | |
String a = "ATGAAATGA"; | |
String ap = "atggggtttaaataataatag"; | |
//String a = "atgcctag"; | |
//String ap = ""; | |
//String a = "ATGCCCTAG"; | |
//String ap = "ATGCCCTAG"; | |
String result = findProtein(a); | |
if (ap.equals(result)) { | |
System.out.println("success for " + ap + " length " + ap.length()); | |
System.out.println("Stop Codon: " + stopCodon(result).toUpperCase() + " length " + result.length()); | |
} | |
else { | |
System.out.println("mistake for input: " + a); | |
System.out.println("got: " + result); | |
System.out.println("not: " + ap); | |
System.out.println("Stop Codon: " + stopCodon(result).toUpperCase() + " length " + result.length()); | |
} | |
} | |
public String stopCodon(String gene) { | |
String answer = findProtein(gene); | |
int size = answer.length(); | |
if ( size > 6 ) { | |
return answer.substring(size - 3, size); | |
} | |
else { | |
return ""; | |
} | |
} | |
public void realTesting() { | |
DirectoryResource dr = new DirectoryResource(); | |
for (File f : dr.selectedFiles()) { | |
FileResource fr = new FileResource(f); | |
String s = fr.asString(); | |
System.out.println("read " + s.length() + " characters"); | |
String result = findProtein(s); | |
System.out.println("found " + result); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment