1.FindGeneSimple
介绍:
利用密码子ATG
,TAA
找到DNA链中Gene的简易程序
Java
public class FindGeneSimpleAndTest {
public String findGeneSimple(String dna){
String result = "";
int startIndex = dna.indexOf("ATG");
if(startIndex == -1) // no ATG
{
return "";
}
int stopIndex = dna.indexOf("TAA", startIndex + 3);
if(stopIndex == -1){ // no TTA
return "";
}
result = dna.substring(startIndex, stopIndex + 3);
return result;
}
public void testFindGeneSimple(){
String dna = "AATGCGTAATATGGT";
System.out.println("DNA strand is " + dna);
String gene = findGeneSimple(dna);
System.out.println("Gene is " + gene);
dna = "AATGCTAGGGTAATATGGT";
System.out.println("DNA strand is " + dna);
gene = findGeneSimple(dna);
System.out.println("Gene is " + gene);
dna = "ATCCTATGCTTCGGCTGCTCTAATATGGT";
System.out.println("DNA strand is " + dna);
gene = findGeneSimple(dna);
System.out.println("Gene is " + gene);
dna = "TTATTA";
System.out.println("DNA strand is " + dna);
gene = findGeneSimple(dna);
System.out.println("Gene is " + gene);
dna = "CGATGGTTTG";
System.out.println("DNA strand is " + dna);
gene = findGeneSimple(dna);
System.out.println("Gene is " + gene);
}
public static void main(String[] args) {
FindGeneSimpleAndTest findGeneSimpleAndTest = new FindGeneSimpleAndTest();
findGeneSimpleAndTest.testFindGeneSimple();
}
}
2.FindGeneWhile
介绍:
利用密码子ATG
,TAA
找到DNA链中Gene的简易程序,同时考虑基因长度必须为3的倍数(1个密码子有3个碱基)
Java
public class FindGeneWhile {
public String findGene(String dna){
int startIndex = dna.indexOf("ATG");
int currIndex = dna.indexOf("TAA", startIndex + 3);
while(currIndex != -1){
if( (currIndex - startIndex) % 3 == 0){
//if so
return dna.substring(startIndex, currIndex + 3);
}
//if no
else{
currIndex = dna.indexOf("TAA", currIndex + 1);
}
}
return "";
}
public void testFindGeneSimple(){
String dna = "AATGCGTAATTAATCG";
System.out.println("DNA strand is ");
String gene = findGene(dna);
System.out.println("Gene is " + gene);
}
public static void main(String[] args) {
FindGeneWhile findGeneWhile = new FindGeneWhile();
findGeneWhile.testFindGeneSimple();
}
}
3.AllCodons
介绍:
利用密码子ATG
找到DNA链中Gene的简易程序,同时考虑基因长度必须为3的倍数(1个密码子有3个碱基),并且ATG
对应3个密码子TAA
,TAG
,TGA
Java
public class AllCodons {
public int findStopCondon(String dnaStr, int startIndex, String stopCodon){
int currIndex = dnaStr.indexOf(stopCodon, startIndex+3);
while(currIndex != -1){
int diff = currIndex - startIndex;
if(diff % 3 == 0){
return currIndex;
}
else {
currIndex = dnaStr.indexOf(stopCodon, currIndex + 1);
}
}
return dnaStr.length();
}
public String findGene(String dna){
int startIndex = dna.indexOf("ATG");
if(startIndex == -1){
return "";
}
int taaIndex = findStopCondon(dna, startIndex, "TAA");
int tagIndex = findStopCondon(dna, startIndex, "TAG");
int tgaIndex = findStopCondon(dna, startIndex, "TGA");
int temp = Math.min(taaIndex, tagIndex);
int minIndex = Math.min(temp, tgaIndex);
if(minIndex == dna.length()){//nothing found
return "";
}
return dna.substring(startIndex, minIndex + 3);
}
public void testFindStop(){
String dna = "xxxyyyzzzTAAxxxyyyzzzTAAxx";
int dex = findStopCondon(dna, 0, "TAA");
if(dex != 9) System.out.println("error on 9");
dna = "AATGCGTAATATGGT";
System.out.println("DNA strand is " + dna);
String gene = findGene(dna);
System.out.println("Gene is " + gene);
dna = "AATGCTAGGGTAATATGGT";
System.out.println("DNA strand is " + dna);
gene = findGene(dna);
System.out.println("Gene is " + gene);
dna = "ATCCTATGCTTCGGCTGCTCTAATATGGT";
System.out.println("DNA strand is " + dna);
gene = findGene(dna);
System.out.println("Gene is " + gene);
dna = "TTATTA";
System.out.println("DNA strand is " + dna);
gene = findGene(dna);
System.out.println("Gene is " + gene);
dna = "CGATGGTTTG";
System.out.println("DNA strand is " + dna);
gene = findGene(dna);
System.out.println("Gene is " + gene);
}
public static void main(String[] args) {
AllCodons allCodons = new AllCodons();
allCodons.testFindStop();
}
}
4. AllCodons(printAllGenes)
介绍:
利用密码子ATG
找到DNA链中Gene的简易程序,同时考虑基因长度必须为3的倍数(1个密码子有3个碱基),并且ATG
对应3个密码子TAA
,TAG
,TGA
。并且可以在一条dna链中找到多个基因。
Java
public class AllCodons {
public int findStopCondon(String dnaStr, int startIndex, String stopCodon){
int currIndex = dnaStr.indexOf(stopCodon, startIndex+3);
while(currIndex != -1){
int diff = currIndex - startIndex;
if(diff % 3 == 0){
return currIndex;
}
else {
currIndex = dnaStr.indexOf(stopCodon, currIndex + 1);
}
}
return dnaStr.length();
}
public String findGene(String dna, int where){
int startIndex = dna.indexOf("ATG", where);
//System.out.println("hapens or not");
if(startIndex == -1){
return "";
}
//System.out.println("hapens or not 2");
int taaIndex = findStopCondon(dna, startIndex, "TAA");
int tagIndex = findStopCondon(dna, startIndex, "TAG");
int tgaIndex = findStopCondon(dna, startIndex, "TGA");
// System.out.println("hapens or not 3");
int minIndex = 0;
if( taaIndex == -1 || (tgaIndex != -1 && tgaIndex < taaIndex) ){
minIndex = tgaIndex;
}else {
minIndex = taaIndex;
}
if(minIndex == -1 || (tgaIndex != -1 && tagIndex < minIndex)){
minIndex = tagIndex;
}
if(minIndex == -1){
return "";
}
//System.out.println("hapens or not 4");
//System.out.println(minIndex);
return dna.substring(startIndex, minIndex + 3);
}
public void printAllGenes(String dna){
int startIndex = 0;
String currentGene;
while (true){
currentGene = findGene(dna, startIndex);
//System.out.println("happens");
if(currentGene.isEmpty()){
break;
}
System.out.println(currentGene);
//System.out.println("before sI= " + startIndex);
startIndex = dna.indexOf(currentGene, startIndex) + currentGene.length();
// System.out.println("after sI= " + startIndex);
}
}
public void testFindStop(String dna){
System.out.println("Testing printAllGenes on " + dna);
printAllGenes(dna);
}
public static void main(String[] args) {
AllCodons allCodons = new AllCodons();
allCodons.testFindStop("ATGATCTAATTTATGCTGCAACGGTGAAGA");
allCodons.testFindStop("");
allCodons.testFindStop("ATGATCATAAGAAGATAATAGAGGGCCATGTAA");
}
}