[!INDEX]

  1. remaining part
  2. two occurance data
  3. finding weblinks
  4. Three stop codon return lenght
  5. ThreeStopCodonORAND + return -1
  6. Print All Gene Cls
  7. Storage Resource For All Gene
  8. c and g ration

1. remaining part


public class RemainingPart {

    public String lastPart(String x,String y) {
        String result ;
        int pos = y.indexOf(x);
        int len = x.length();          
        if(pos != -1){
            String z = y.substring(pos+len);
            result = z;
            System.out.println(result); 
         }
        else{
            result =y;
            System.out.println(result);         
        }       
     return result;
    }     
    public void test2(){
        String xx = lastPart("by","byA story by Abb Long");
    }
}

2. two occurance data

// twoOccurrences(String a,String b) - method returns true if stringa appears at least twice in stringb,
import edu.duke.*;
public class TwoOccurrencesDna {
     String output;
     public String twoOccurrences(String a,String b){     
     int firstpos = b.indexOf(a);
     if(firstpos != -1){
         firstpos= firstpos+a.length()-1;
         int secondpos = b.indexOf(a,firstpos);
             if(secondpos != -1){
                    output = "yes";
             }
        }
     return output;
    } 
    public void checkOccurance () {
        String xx = twoOccurrences("bye","aaabyedaaaby");
        if(xx=="yes"){
            System.out.println("it has two occurance , using method twooccurance");
        }
        else{
            System.out.println("it has one occurance , using method twooccurance");    
        }    
    }
}

3. finding weblinks

import edu.duke.*;
public class FindingWebLinks { 
 public void checkurlwords(){
     String url = "https://www.dukelearntoprogram.com/course2/data/manylinks.html";
     String dq = "\"";
// Create a URLResource object to read from the URL
        URLResource urlResource = new URLResource(url);
        // Loop through each word in the file
        for (String word : urlResource.words()) {
            if(word.contains("www.youtube.com")){    
                int firstPos =  word.indexOf(dq);
                int lastPos =  word.lastIndexOf(dq);     
                String toPrint = word.substring(firstPos+1,lastPos);
                System.out.println(toPrint);
            }
        }
    }
    public void getData(){     
        FindingWebLinks reader = new FindingWebLinks();
        reader.checkurlwords();
    }
}

4. three stop codon return lenght


public class ThreeStopCondon {
// finds the gene index , later we will match it
    public int findStopCodon(String dnaStr, int startindex, String stopcodon ){
          int currentindex = dnaStr.indexOf(stopcodon,startindex+3);
          while(currentindex!= -1){
              int diff = currentindex-startindex;
              if(diff % 3 == 0){       
              return currentindex;  
              }
            else{
            currentindex = dnaStr.indexOf(stopcodon, currentindex+1);// if not ok then start form this index further
            }            
          }
     return dnaStr.length(); // this return the biggest number , easy for finding the smallest one
    //return -1; // it is the samllest number it will not give proper result
    }    

     public String findGene(String dna){
        int startindex = dna.indexOf("ATG");            
        if(startindex == -1){
            return "";
        }
        int taaindex = findStopCodon(dna,startindex,"TAA");
        int tagindex = findStopCodon(dna,startindex,"TAG");
        int tgaindex = findStopCodon(dna,startindex,"TGA");     

       //using one line
        int temp = Math.min(taaindex,Math.min(tagindex,tgaindex)); 

        int minIdex=0;
        return dna.substring(startindex,minIdex+3);
    }
    // below is just for testing purpose
    public void testFindGeneSimple(){
        String dna = "*********TAA*********TAAs";
        System.out.println("Dna strand is "+dna);
        int getindex = findStopCodon(dna, 0, "TAA"); // must return 9
        if(getindex != 9 )System.out.println("error on line 9 ");// 9 not equal to 9, false, dont print

        getindex = findStopCodon(dna, 9, "TAA"); // must return 21
        if(getindex != 21 )System.out.println("error on line 21 ");// 21 not equals to 21, false, dont print

        getindex = findStopCodon(dna, 1, "TAA"); // 
        System.out.println(getindex);
       // if(getindex != 26 )System.out.println("error on line 26 ");// true statement

        System.out.println("Testing is done : All works fine" );        
    }
}

5. Three Stop Codon OR AND + return -1

public class ThreStopCodonORAND {
// finds the gene index , later we will match it
    public int findStopCodon(String dnaStr, int startindex, String stopcodon ){
          int currentindex = dnaStr.indexOf(stopcodon,startindex+3);
          while(currentindex!= -1){
              int diff = currentindex-startindex;
              if(diff % 3 == 0){       
              return currentindex;  
              }
            else{
            currentindex = dnaStr.indexOf(stopcodon, currentindex+1);// if not ok then start form this index further
            }            
          }
    return -1; // it is the samllest number it will not give proper result
    }    

     public String findGene(String dna){
        int startindex = dna.indexOf("ATG");            
        if(startindex == -1){
            return "";
        }
        int taaindex = findStopCodon(dna,startindex,"TAA");
        int tagindex = findStopCodon(dna,startindex,"TAG");
        int tgaindex = findStopCodon(dna,startindex,"TGA");    

       // using and or operators
            int minIdex=0;
            if(taaindex == -1 || (tgaindex != -1 && tgaindex < taaindex)){
                minIdex=tgaindex;
            }
            else{
                minIdex=taaindex;
            }

            if(minIdex == -1 || (tagindex !=-1 && tagindex < minIdex)){
                minIdex = tagindex;

            }
            if(minIdex == -1){
                return "";
            }
        return dna.substring(startindex,minIdex+3);
    }
    // below is just for testing purpose
    public void testFindGeneSimple(){
        String dna = "*********TAA*********TAAs";
        System.out.println("Dna strand is "+dna);
        int getindex = findStopCodon(dna, 0, "TAA"); // must return 9
        if(getindex != 9 )System.out.println("error on line 9 ");// 9 not equal to 9, false, dont print

        getindex = findStopCodon(dna, 9, "TAA"); // must return 21
        if(getindex != 21 )System.out.println("error on line 21 ");// 21 not equals to 21, false, dont print

        getindex = findStopCodon(dna, 1, "TAA"); // 
        if(getindex != -1 )System.out.println("error on line 26 ");// false statement

        System.out.println("Testing is done : All works fine" );

    }
}

6. Print All Gene Cls

public class PrintAllGeneCls {
//finds the gene index , later we will match it
    public int findStopCodon(String dnaStr, int startindex, String stopcodon ){
          int currentindex = dnaStr.indexOf(stopcodon,startindex+3);
          while(currentindex!= -1){
              int diff = currentindex-startindex;
              if(diff % 3 == 0){       
              return currentindex;  
              }
            else{
            currentindex = dnaStr.indexOf(stopcodon, currentindex+1);// if not ok then start form this index further
            }            
          }
    return -1; // it is the samllest number it will not give proper result
    }   
    public void printAllGene(String dna){
        int startindex = 0 ;
        while(true){
        String currentGene = findGene(dna, startindex) ;
            if(currentGene.isEmpty()){
            break;
            }
            System.out.println(currentGene);
            startindex = dna.indexOf(currentGene,startindex)+currentGene.length();
        }
    }
    //return the shortest gene with characters
     public String findGene(String dna, int where){ // where to start lookin in the middle of the string
        int startindex = dna.indexOf("ATG", where);            
        if(startindex == -1){
            return "";
        }
        int taaindex = findStopCodon(dna,startindex,"TAA");
        int tagindex = findStopCodon(dna,startindex,"TAG");
        int tgaindex = findStopCodon(dna,startindex,"TGA");    
       // using and or operators
            int minIdex=0;
            if(taaindex == -1 || (tgaindex != -1 && tgaindex < taaindex)){
                minIdex=tgaindex;
            }
            else{
                minIdex=taaindex;
            }
            if(minIdex == -1 || (tagindex !=-1 && tagindex < minIdex)){
                minIdex = tagindex;        
            }
            if(minIdex == -1){
                return "";
            }
        return dna.substring(startindex,minIdex+3);
    }
    public void testOn(String dna){
        System.out.println("testing all gene");
        printAllGene(dna);
    }
    // below is just for testing purpose
    public void test(){
        String dna = "ATG******TAA***ATG******TGA";
        testOn(dna);        
    }
}

7. Storage Resource For All Gene

import edu.duke.*;

public class StorageResourceForAllGene {
//finds the gene index , later we will match it
    public int findStopCodon(String dnaStr, int startindex, String stopcodon ){
          int currentindex = dnaStr.indexOf(stopcodon,startindex+3);
          while(currentindex!= -1){
              int diff = currentindex-startindex;
              if(diff % 3 == 0){       
              return currentindex;  
              }
            else{
            currentindex = dnaStr.indexOf(stopcodon, currentindex+1);// if not ok then start form this index further
            }            
          }
    return -1; // it is the samllest number it will not give proper result
    }   

    public void printAllGene(String dna){
        int startindex = 0 ;
        while(true){
        String currentGene = findGene(dna, startindex) ;
            if(currentGene.isEmpty()){
            break;
            }
            System.out.println(currentGene);
            startindex = dna.indexOf(currentGene,startindex)+currentGene.length();
        }
    }

    public StorageResource getAllGenes(String dna){
        StorageResource geneList = new StorageResource();

        int startindex = 0 ;
        while(true){
        String currentGene = findGene(dna, startindex) ;
            if(currentGene.isEmpty()){
            break;
            }
            geneList.add(currentGene);
//            System.out.println(currentGene);
            startindex = dna.indexOf(currentGene,startindex)+currentGene.length();
        }
        return geneList;
    }
    //return the shortest gene with characters
     public String findGene(String dna, int where){ // where to start lookin in the middle of the string
        int startindex = dna.indexOf("ATG", where);            
        if(startindex == -1){
            return "";
        }
        int taaindex = findStopCodon(dna,startindex,"TAA");
        int tagindex = findStopCodon(dna,startindex,"TAG");
        int tgaindex = findStopCodon(dna,startindex,"TGA");    

       // using and or operators
            int minIdex=0;
            if(taaindex == -1 || (tgaindex != -1 && tgaindex < taaindex)){
                minIdex=tgaindex;
            }
            else{
                minIdex=taaindex;
            }

            if(minIdex == -1 || (tagindex !=-1 && tagindex < minIdex)){
                minIdex = tagindex;

            }
            if(minIdex == -1){
                return "";
            }
        return dna.substring(startindex,minIdex+3);
    }
    public void testOn(String dna){
        System.out.println("testing all gene");
        StorageResource gene = getAllGenes(dna);
        for(String g : gene.data()){
            System.out.println(g);
        }
    }
    // below is just for testing purpose
    public void test(){
        String dna = "ATG******TAA***ATG******TGA";
        testOn(dna);        
    }
}

8. c and g ration

import java.text.DecimalFormat;
public class CgRatioCls {
    public double cgRation(String dna){
        float dnalength = dna.length();
        float cCount = (float)calculate("C",dna);
        float gCount = (float)calculate("G",dna);        
        float result = (cCount+gCount)/dnalength;
        double roundedValue = Math.round(result * 1000.0) / 1000.0;

        return roundedValue;
    }
    public int calculate(String x, String dna){
        int startindex = 0;
        int count =0;
        String Charcter = x;
        while(true){
          int findC = dna.indexOf(Charcter,startindex);
              if(findC == -1){
                break;
              }
              else{
                  count = count+1;    
                  startindex = findC+1;
              }
        }
        return count;
    }
    // for run the code 
    public void check(){
        String dna = "ATGCCATAG"; // 
        System.out.println("cg ration is = " + cgRation(dna));
    }
    public void testcase(){
        String dna = "ATGCCATAG"; // 
        if(cgRation(dna)!= 0.444 )System.out.println("error generate step-1 ");
        dna = "ATGCCATAGG";
        if(cgRation(dna)!= 0.5 )System.out.println("error generate step-2 ");

        System.out.println("code is check and found ok");
    }    
}