Page 1 of 2 12 LastLast
Results 1 to 20 of 31
Like Tree5Likes

Thread: DNA class- bioinformatics(

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default DNA class- bioinformatics(

    I have made a class named DNA performing several functions on a DNA sequence. I am not satisfied with my code. Can you guys run it and tell me how to improve it?

    The major pitfalls which i can see are:

    1. Method readingFrames() and ORF():
      The readingFrames() method invokes the ORF() method and it needs to pass in six different reading frames of the DNA sequence to the ORF(). For that the reading frame has to store all six reading frames of the DNA sequence in six different strings.
      so it has that many statements. Would that it wouldnt need to store the six reading reading frames in six different strings and them then pass in to the ORF(), it could easily have done the job in a single for loop, containing two statements- one printing a reading frame and the other printing the reverse of the first reading frame( giving the 2nd reading frame).

      Then in the ORF(), i have separately check for each reading frame if it is an open reading frame. For that i have to pass each of the six string to a another method which returns codons(triplets) made from the sequence.
      Can't I do that in a loop? Can't i just achieve the passing in of a different string to the invoked method in each iteration of a loop?

      Because this long code makes my code less efficient. :(



    Java Code:
    void readingFrames(){
          
           String s1, s2, s3, s4, s5, s6;
          
           System.out.println("\nReading Frame  1:" + seq.substring(0,seq.length()-1));
           s1=seq.substring(0,seq.length()-1);
           System.out.println("\nReading Frame  -1:" + new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString());
           s2= new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString();
           System.out.println("\nReading Frame  2:" + seq.substring(0,seq.length()-1));
           s3=seq.substring(0,seq.length()-1);
           System.out.println("\nReading Frame  -2:" + new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString());
           s4= new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString();
           System.out.println("\nReading Frame  3:" + seq.substring(0,seq.length()-1));
           s5=seq.substring(0,seq.length()-1);
           System.out.println("\nReading Frame  -3:" + new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString());
           s6= new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString();
    
    
          System.out.println("\n\nLET's SEE IF AN OPEN READING FRAME IS PRESENT IN THESE READING FRAMES:\n");
           ORF(s1,s2,s3,s4,s5,s6);
    
    
       }
    
         void ORF(String s1,String s2,String s3,String s4,String s5,String s6){
    
            String[] make_codons= makeCodons(s1);
            ArrayList al= new ArrayList();
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
       }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s1);
            }
    
             make_codons= makeCodons(s2);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
       }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
             System.out.println("An ORF found :)\n" + s2);
            }
    
             make_codons= makeCodons(s3);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
    }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s3);
            }
    
            make_codons= makeCodons(s4);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
    }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s4);
            }
    
            make_codons= makeCodons(s5);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
    } if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s5);
            }
    
            make_codons= makeCodons(s6);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
       }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s6);
            }
         }


    [LIST=2]
    Then in another method named displayFasta(), i have to display the sequence of DNA in fasta format, given the name of the sequence and the sequence. In the fasta format,
    • The name should be followed by the tokenizer ">", followed by the sequence. I have achieved that.
    The number of characters in each line should be less than 80. Then the control should shift to the next line.How to achieve this?

    [LIST]



    Java Code:
    String displayInFasta(){
            String b=new String();
            b= name.concat(">");
            return b.concat(seq).toUpperCase();
        }

    1. MOST IMPORTANT:
      The number of amino acids formed is quite less than the number of codons. Why so? While an amino acid is formed against each gene, except one stop codon that is present at the end of the gene.



    Java Code:
    String[] makeCodons(String gene){
           String make_codons[]= new String[(gene.length()/3)];
        int b=0;
        for( int a=0; a<gene.length()-(gene.length()%3); a+=3){
    
             make_codons[b]= gene.substring(a, a+3);
            //System.out.println(make_codons[b]);
           b++;
        }
    
        return make_codons;
        }
          
    
       String toProtein(String gene){
          
           String make_codons[]= new String[(gene.length()/3)];
            make_codons= makeCodons(gene);
    
           StringBuilder al  = new StringBuilder();
           
          
           
            for(int c=0; c<make_codons.length; c++){
    
                if(make_codons[c].equals("atg"))
                al= al.append("M");
                 //System.out.println("Methionine");
                else if(make_codons[c].equals("gtt")|| make_codons[c].equals("gtc")|| make_codons[c].equals("gta")|| make_codons[c].equals("gtg"))
                al= al.append("V");
                    //System.out.println("Valine");
                else if(make_codons[c].equals("att")|| make_codons[c].equals("atc")|| make_codons[c].equals("ata"))
                al= al.append("I");
                   // System.out.println("Isoleucine");
                else if(make_codons[c].equals("ttg")|| make_codons[c].equals("tta")|| make_codons[c].equals("ctg")|| make_codons[c].equals("cta")|| make_codons[c].equals("ctc")|| make_codons[c].equals("ctt"))
                 al= al.append("L");
                    //System.out.println("Leucine");
                else if(make_codons[c].equals("tct")|| make_codons[c].equals("tcc")|| make_codons[c].equals("tca")|| make_codons[c].equals("tcg")||make_codons[c].equals("agt")|| make_codons[c].equals("agc"))
                 al= al.append("S");
                   // System.out.println("Serine");
                else if(make_codons[c].equals("cca")|| make_codons[c].equals("ccc")|| make_codons[c].equals("cct")|| make_codons[c].equals("ccg"))
             al= al.append("P");
                   // System.out.println("Proline");
                else if(make_codons[c].equals("act")|| make_codons[c].equals("acc")|| make_codons[c].equals("aca")|| make_codons[c].equals("acg"))
                  al= al.append("T");
                    //System.out.println("Threonine");
                else if(make_codons[c].equals("gct")|| make_codons[c].equals("gcc")|| make_codons[c].equals("gca")|| make_codons[c].equals("gcg"))
                  al= al.append("A");
                    //System.out.println("Alanine");
                else if(make_codons[c].equals("tat")|| make_codons[c].equals("tac"))
                  al= al.append("Y");
                   // System.out.println("Tyrosine");
                else if(make_codons[c].equals("cat")|| make_codons[c].equals("cac"))
                 al= al.append("H");
                    //System.out.println("Histidine");
                 else if(make_codons[c].equals("caa")|| make_codons[c].equals("cag"))
                  al= al.append("Q");
                     //System.out.println("Glutamine");
                 else if(make_codons[c].equals("aat")|| make_codons[c].equals("aac"))
                  al= al.append( "N");
                    // System.out.println("Asparagine");
                 else if(make_codons[c].equals("aag")|| make_codons[c].equals("aaa"))
                  al= al.append( "K");
                    // System.out.println("Lysine");
                 else if(make_codons[c].equals("gac")|| make_codons[c].equals("gat"))
                  al= al.append( "D");
                     //System.out.println("Aspartic acid");
                 else if(make_codons[c].equals("gaa")|| make_codons[c].equals("gag"))
                al= al.append( "E");
                     //System.out.println("Glutamic acid");
                 else if(make_codons[c].equals("tgt")|| make_codons[c].equals("tgc"))
                 al= al.append( "C");
                     //System.out.println("Cystine");
                 else if(make_codons[c].equals("tgg"))
                 al= al.append("W");
                    // System.out.println("Tryptophan");
                 else if(make_codons[c].equals("agt")|| make_codons[c].equals("cga")|| make_codons[c].equals("ctg")|| make_codons[c].equals("cta")|| make_codons[c].equals("cgt")|| make_codons[c].equals("cgg"))
                 al= al.append( "R");
                    // System.out.println("Arginine");
                 else if(make_codons[c].equals("gga")|| make_codons[c].equals("ggc")|| make_codons[c].equals("ggt"))
                  al= al.append("G");
                     // System.out.println("Glycine");
                 else if(make_codons[c].equals("tga")||make_codons[c].equals("taa")||make_codons[c].equals("tag"))
                     break;
                   
    
            }
           return al.toString();}[/COLOR]
    
    
    
    
    [COLOR="#ff00ff"]public static void main(String[] args) {
            DNA c= new DNA();
            DNA dna= new DNA("BB002260","aggggggatgggggccccccgcgcctccgcgcgcgcgctcgctgcggcgtcgcgcgcgcccccccaaaaaatgagcgcatccgggcgggcggcggtct");
    System.out.println("Polypeptide chain:"+ dna.toProtein("atgggggccccccgcgcctccgcgcgcgcgctcgctgcggcgtcgcgcgcgcccccccaaaaaatga
    "));
    dna.readingFrames();
    [/COLOR]
    
    The constructor is
    [COLOR="#ff00ff"] DNA(){}
    
        DNA(String name, String seq){
            this.name=name;
            this.seq=seq;
        }
    The class is named DNA.
    Last edited by Nazneen Ali; 08-04-2011 at 01:48 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    Please edit your code and wrap it in code tags. Use the # icon above the input box.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    That big blob of code (in green) is simply a mapping from an encoding triplet to an amino acid; why don't you first fill a Map with the encoding triplets and their associated amino acid and simply do something like this:

    Java Code:
    String triplet= ...;
    String amino= ecodingMap.get(triplet);
    if (amino != null)
       al.append(amino);
    kind regards,

    Jos
    Nazneen Ali likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Just some mistakes:
    - Variabele c in main is never read.
    - Readingframe 1 has to be a copy of the inputstring (dna). If you want to use the substringfunction, which is not nescessary here, you should use seq.substring( 0,seq.length() ). Now you chop of 1 base (same for the others).
    - You don't shift the readingframes correctly (only shifting 0 or 1 position).
    - But worst (biologically speaking) is that you take the reverse-function to obtain the backward sequence: you should take the complement strand by switching c for g, g for c, a for t and t for a, and then reverse of course.
    (Small detail: if you chop of 1 or two bases from the start of the string and then reverse, the sequences are stil in phase, i.e. not frameshifted. )
    Last edited by Jodokus; 07-23-2011 at 11:47 PM.
    Nazneen Ali likes this.
    No bug ever had to calculate its fitnessfunction.

  5. #5
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Please edit your code and wrap it in code tags. Use the # icon above the input box.
    Java Code:
    //COPY THE SEQUENCE BEFORE RUNNING THE FILE.
    //             COPY THE SEQUENCE BEFORE RUNNING THE FILE.
    //COPY THE SEQUENCE BEFORE RUNNING THE FILE.
    //BB002260>aggggggatgggggccccccgcgcctccgcgcgcgcgctcgctgcggcgtcgcgcgcgcccccccaaaaaatgagcgcatccgggcgggcggcggtct/////////////
    
    
    
    
    
    package dna;
    
    import java.io.*;
    import java.util.ArrayList;
    import javax.swing.*;
    
    
    public class DNA {
        String name;
        String seq;
    
        DNA(){}
    
        DNA(String name, String seq){
            this.name=name;
            this.seq=seq;
        }
    
        String getName(){
        return JOptionPane.showInputDialog(this,"Name of the sequence:");
       }
    
        String getSeq(){
        return JOptionPane.showInputDialog(this,"Sequence:");
    
        }
    
    
        String displayInFasta(){
            String b=new String();
            b= name.concat(">");
            return b.concat(seq).toUpperCase();
        }
    
    
       void writeToFile(String h){
        try
        {
           FileWriter fw = new FileWriter ( "FastaDNA.txt" );
           BufferedWriter  bw = new BufferedWriter ( fw );
    
    
           String stringFile =h;
           bw.write(stringFile);
           bw.close( );
        }
            catch(Exception e) {
          System.out.println("Exception: " + e);
        }
        }
    
    
    
        void readFromFile(){
           String stringFromFile= new String();
    
            try
           {
               FileReader fro = new FileReader( "G:\\FastaDNA.txt" );
               BufferedReader bro = new BufferedReader( fro );
              String strline= bro.readLine( );
    
              stringFromFile= "";
              while( strline!= null )
              {  stringFromFile+= strline;
              strline= bro.readLine( );
              }
              
              bro.close( );}
           catch( FileNotFoundException filenotfoundexxption)
           {
               System.out.println( "FastaDNA.txt, does not exist" );
           }
           catch( IOException ioexception)
           {
               ioexception.printStackTrace( );
           }
    
    
           System.out.println("To test if the file, FastaDNA.text located in G drive, has been read:\nSEQUENCE NAME:" + stringFromFile.substring(0, stringFromFile.indexOf(">")));
           System.out.println("SEQUENCE:"+ stringFromFile.substring(stringFromFile.indexOf(">")+1,stringFromFile.length()));
    
    
        }
    
    
    
    
    
        String complement(){
         String com="";
            for(int a=0; a < seq.length(); a++){
                if(seq.charAt(a)=='a'){
                    com= com.concat("t");
                }
                else if(seq.charAt(a)=='t'){
                    com= com.concat("a");
                }
                else if(seq.charAt(a)=='g'){
                    com= com.concat("c");
                }
                else if(seq.charAt(a)=='c'){
                    com= com.concat("g");
            }
          }
            return com;
        }
    
    
        String revCom(String argcom){
           String com="";
            for(int a=0; a < argcom.length(); a++){
                if(argcom.charAt(a)=='a'){
                    com= com.concat("t");
                }
                else if(argcom.charAt(a)=='t'){
                    com= com.concat("a");
                }
                else if(argcom.charAt(a)=='g'){
                    com= com.concat("c");
                }
                else if(argcom.charAt(a)=='c'){
                    com= com.concat("g");
            }
          }
            return com;
        }
    
    
         String RNA(String compl){
            String theta= compl.replace("t", "u");
            return theta;
        }
    
    
         String gene(){
             int position=0;
    
             if(seq.indexOf("tag")!=-1){
               if(seq.indexOf("tga")!=-1){
                 if(seq.indexOf("tag")<seq.indexOf("tga")) {
                   if(seq.indexOf("taa")!=-1){
                     if(seq.indexOf("tag")<seq.indexOf("taa")){
                             position=seq.indexOf("tag");}
    
                     position=seq.indexOf("taa");}
                      position=seq.indexOf("tag");}
                        position=seq.indexOf("tga");}
    
                       if(seq.indexOf("taa")!=-1){
                     if(seq.indexOf("tag")<seq.indexOf("taa")){
                             position=seq.indexOf("tag");}
                     position= seq.indexOf("taa");}
                     }
    
              if(seq.indexOf("tga")!=-1){
               if(seq.indexOf("tag")!=-1){
                 if(seq.indexOf("tga")<seq.indexOf("tag")) {
                   if(seq.indexOf("taa")!=-1){
                     if(seq.indexOf("tga")<seq.indexOf("taa")){
                             position=seq.indexOf("tga");}
    
                     position=seq.indexOf("taa");}
                      position=seq.indexOf("tga");}
                        position=seq.indexOf("tag");}
    
                       if(seq.indexOf("taa")!=-1){
                     if(seq.indexOf("tga")<seq.indexOf("taa")){
                             position=seq.indexOf("tga");}
                     position= seq.indexOf("taa");}
                     }
    
              if(seq.indexOf("taa")!=-1){
               if(seq.indexOf("tga")!=-1){
                 if(seq.indexOf("taa")<seq.indexOf("tga")) {
                   if(seq.indexOf("tag")!=-1){
                     if(seq.indexOf("taa")<seq.indexOf("tag")){
                             position=seq.indexOf("taa");}
    
                     position=seq.indexOf("tag");}
                      position=seq.indexOf("taa");}
                        position=seq.indexOf("tga");}
    
                       if(seq.indexOf("tag")!=-1){
                     if(seq.indexOf("taa")<seq.indexOf("tag")){
                             position=seq.indexOf("taa");}
                     position= seq.indexOf("tag");}
                     }
    
    
    
    
            return seq.substring(seq.indexOf("atg"),position);//CHECKS LAGAO: SAY STOP CODON MILY HI NAHN. YA STOP CODON PEHLY HO START CODON SE PHIR? ASK ODA GENETICS PPL FOR CHECKS REQUIRED.
        }
    
    
    
          int length(){
          return seq.length();
          }
    
    
    
          float percentageOfA(){
            float count=0;
            for(int i=0; i<seq.length(); i++){
                if(seq.charAt(i) == 'a'){
                    count++;
                }
            }
            return count/seq.length();
            }
    
    
    String[] makeCodons(String gene){
           String make_codons[]= new String[(gene.length()/3)];
        int b=0;
        for( int a=0; a<gene.length()-(gene.length()%3); a+=3){
    
             make_codons[b]= gene.substring(a, a+3);
            //System.out.println(make_codons[b]);
           b++;
        }
    
        return make_codons;
        }
          
    
       String toProtein(String gene){
          
           String make_codons[]= new String[(gene.length()/3)];
            make_codons= makeCodons(gene);
    
           StringBuilder al  = new StringBuilder();
           
          
           
            for(int c=0; c<make_codons.length; c++){
    
                if(make_codons[c].equals("atg"))
                al= al.append("M");
                 //System.out.println("Methionine");
                else if(make_codons[c].equals("gtt")|| make_codons[c].equals("gtc")|| make_codons[c].equals("gta")|| make_codons[c].equals("gtg"))
                al= al.append("V");
                    //System.out.println("Valine");
                else if(make_codons[c].equals("att")|| make_codons[c].equals("atc")|| make_codons[c].equals("ata"))
                al= al.append("I");
                   // System.out.println("Isoleucine");
                else if(make_codons[c].equals("ttg")|| make_codons[c].equals("tta")|| make_codons[c].equals("ctg")|| make_codons[c].equals("cta")|| make_codons[c].equals("ctc")|| make_codons[c].equals("ctt"))
                 al= al.append("L");
                    //System.out.println("Leucine");
                else if(make_codons[c].equals("tct")|| make_codons[c].equals("tcc")|| make_codons[c].equals("tca")|| make_codons[c].equals("tcg")||make_codons[c].equals("agt")|| make_codons[c].equals("agc"))
                 al= al.append("S");
                   // System.out.println("Serine");
                else if(make_codons[c].equals("cca")|| make_codons[c].equals("ccc")|| make_codons[c].equals("cct")|| make_codons[c].equals("ccg"))
             al= al.append("P");
                   // System.out.println("Proline");
                else if(make_codons[c].equals("act")|| make_codons[c].equals("acc")|| make_codons[c].equals("aca")|| make_codons[c].equals("acg"))
                  al= al.append("T");
                    //System.out.println("Threonine");
                else if(make_codons[c].equals("gct")|| make_codons[c].equals("gcc")|| make_codons[c].equals("gca")|| make_codons[c].equals("gcg"))
                  al= al.append("A");
                    //System.out.println("Alanine");
                else if(make_codons[c].equals("tat")|| make_codons[c].equals("tac"))
                  al= al.append("Y");
                   // System.out.println("Tyrosine");
                else if(make_codons[c].equals("cat")|| make_codons[c].equals("cac"))
                 al= al.append("H");
                    //System.out.println("Histidine");
                 else if(make_codons[c].equals("caa")|| make_codons[c].equals("cag"))
                  al= al.append("Q");
                     //System.out.println("Glutamine");
                 else if(make_codons[c].equals("aat")|| make_codons[c].equals("aac"))
                  al= al.append( "N");
                    // System.out.println("Asparagine");
                 else if(make_codons[c].equals("aag")|| make_codons[c].equals("aaa"))
                  al= al.append( "K");
                    // System.out.println("Lysine");
                 else if(make_codons[c].equals("gac")|| make_codons[c].equals("gat"))
                  al= al.append( "D");
                     //System.out.println("Aspartic acid");
                 else if(make_codons[c].equals("gaa")|| make_codons[c].equals("gag"))
                al= al.append( "E");
                     //System.out.println("Glutamic acid");
                 else if(make_codons[c].equals("tgt")|| make_codons[c].equals("tgc"))
                 al= al.append( "C");
                     //System.out.println("Cystine");
                 else if(make_codons[c].equals("tgg"))
                 al= al.append("W");
                    // System.out.println("Tryptophan");
                 else if(make_codons[c].equals("agt")|| make_codons[c].equals("cga")|| make_codons[c].equals("ctg")|| make_codons[c].equals("cta")|| make_codons[c].equals("cgt")|| make_codons[c].equals("cgg"))
                 al= al.append( "R");
                    // System.out.println("Arginine");
                 else if(make_codons[c].equals("gga")|| make_codons[c].equals("ggc")|| make_codons[c].equals("ggt"))
                  al= al.append("G");
                     // System.out.println("Glycine");
                 else if(make_codons[c].equals("tga")||make_codons[c].equals("taa")||make_codons[c].equals("tag"))
                     break;
                   
    
            }
           return al.toString();
       }
    
          boolean isValidSeq(){
              boolean n=true;
            for(int a=0; a < seq.length(); a++){
               if(seq.charAt(a)!='a'){
                  if( seq.charAt(a)!='g'){
                 if( seq.charAt(a)!='c'){
                 if( seq.charAt(a)!='t'){
                     n= false;
    
                  }}}}
              }
             return n;
            }
    
    
          String removeErrorInSequence(){
               for(int a=0; a < seq.length(); a++){
               if(seq.charAt(a)!='a'){
                  if( seq.charAt(a)!='g'){
                 if( seq.charAt(a)!='c'){
                 if( seq.charAt(a)!='t'){
                   seq=(new StringBuilder(seq).deleteCharAt(a)).toString();//convert seq into sb, then delete chatAt(a) from that sb, then convert sb into string, then save that string in seq
               }}}}
          } return seq;
          }
          
          
          int countA(){
              int count=0;
              for(int a=0; a < seq.length(); a++){
               if(seq.charAt(a)=='a'){
                   count++;
               }}
              return count;
          }
    
    int countG(){
              int count=0;
              for(int a=0; a < seq.length(); a++){
               if(seq.charAt(a)=='g'){
                   count++;
               }}
              return count;
          }
    
    int countC(){
              int count=0;
              for(int a=0; a < seq.length(); a++){
               if(seq.charAt(a)=='c'){
                   count++;
               }}
              return count;
          }
    
    int countT(){
              int count=0;
              for(int a=0; a < seq.length(); a++){
               if(seq.charAt(a)=='t'){
                   count++;
               }}
              return count;
          }
    
    String insertSeq(){
    String strIn= JOptionPane.showInputDialog(this,"Enter the sequence to be inserted");
    int pos= Integer.parseInt(JOptionPane.showInputDialog(this,"Enter position of the sequence to be inserted:"));
    while(pos>seq.length()||pos<0){
        JOptionPane.showMessageDialog(null,"You have given an invalid input,either poition is greater than the length" +
                " of the sequence, or less than zero. Enter Again!");
    pos= Integer.parseInt(JOptionPane.showInputDialog(this,"Enter position of the sequence to be inserted:"));
    }
    return (new StringBuilder(seq).insert(pos, strIn)).toString();
    }
    
    
    String deleteSeq(){
    
    int sPos= Integer.parseInt(JOptionPane.showInputDialog(this,"Enter start position of the sequence to be deleted:"));
    int ePos= Integer.parseInt(JOptionPane.showInputDialog(this,"Enter end position(inclusive) of the sequence to be deleted:"));
    while(ePos<sPos ||ePos>seq.length()-1||sPos>seq.length()-1||sPos<0||ePos<0){
        JOptionPane.showMessageDialog(null,"You have given an invalid input,either start or end poition is greater than the length" +
                " of the sequence, or less than zero, or endpostion is less than the start position. Enter Again!");
    sPos= Integer.parseInt(JOptionPane.showInputDialog(this,"Enter start position of the sequence to be deleted:"));
    ePos= Integer.parseInt(JOptionPane.showInputDialog(this,"Enter end position(inclusive) of the sequence to be deleted:"));
    }
    return (new StringBuilder(seq).delete(sPos, ePos)).toString();
    }
    
    String appendSeq(){
     String strAp= JOptionPane.showInputDialog(this,"Enter the sequence to be appended:");
    return new StringBuilder(seq).append(strAp).toString();
    }
    
    void searchSequence(){
        String searchSeq= JOptionPane.showInputDialog(this,"Enter the sequence to be searched:");
       if( seq.indexOf(searchSeq)==-1){
           System.out.println("Bad Luck:( The sequence you wanted to searched was Not found!");
       }
       else
    System.out.println("The sequence you entered lies at position " + seq.indexOf(searchSeq));
    }
    
    
    
    
    /*void readingFrames(){
        for(int a=0; a<3; a++){
           System.out.println("\nReading Frame:" + seq.substring(a,seq.length()-1));
           System.out.println("\nReading Frame:" + new StringBuilder( seq.substring(a,seq.length()-1)).reverse().toString());
            }
    }
    
    void ORF(int frameno){
        switch (frameno){
            case 1:
                System.out.println("\nOpen Reading Frame(ORF):" + seq.substring(0,seq.length()-1));
            case 2:
                System.out.println("\nOpen Reading Frame(ORF):" + seq.substring(1,seq.length()-1));
            case 3:
                System.out.println("\nOpen Reading Frame(ORF):" + seq.substring(2,seq.length()-1));
            case 4:
               System.out.println("\nOpen Reading Frame(ORF):" + new StringBuilder( seq.substring(0,seq.length()-1)).reverse().toString());
            case 5:
                System.out.println("\nOpen eading Frame(ORF):" + new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString());
            default:
                System.out.println("\nOpen Reading Frame(ORF):" + new StringBuilder( seq.substring(2,seq.length()-1)).reverse().toString());
        }
    
    }*/
    
     void readingFrames(){
          
           String s1, s2, s3, s4, s5, s6;
          
           System.out.println("\nReading Frame  1:" + seq.substring(0,seq.length()-1));
           s1=seq.substring(0,seq.length()-1);
           System.out.println("\nReading Frame  -1:" + new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString());
           s2= new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString();
           System.out.println("\nReading Frame  2:" + seq.substring(0,seq.length()-1));
           s3=seq.substring(0,seq.length()-1);
           System.out.println("\nReading Frame  -2:" + new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString());
           s4= new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString();
           System.out.println("\nReading Frame  3:" + seq.substring(0,seq.length()-1));
           s5=seq.substring(0,seq.length()-1);
           System.out.println("\nReading Frame  -3:" + new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString());
           s6= new StringBuilder( seq.substring(1,seq.length()-1)).reverse().toString();
    
    
          System.out.println("\n\nLET's SEE IF AN OPEN READING FRAME IS PRESENT IN THESE READING FRAMES:\n");
           ORF(s1,s2,s3,s4,s5,s6);
    
    
       }
    
         void ORF(String s1,String s2,String s3,String s4,String s5,String s6){
    
            String[] make_codons= makeCodons(s1);
            ArrayList al= new ArrayList();
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
       }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s1);
            }
    
             make_codons= makeCodons(s2);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
       }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
             System.out.println("An ORF found :)\n" + s2);
            }
    
             make_codons= makeCodons(s3);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
    }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s3);
            }
    
            make_codons= makeCodons(s4);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
    }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s4);
            }
    
            make_codons= makeCodons(s5);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
    } if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s5);
            }
    
            make_codons= makeCodons(s6);
    for (int ix=0; ix<make_codons.length; ix++) {
       al.add(make_codons[ix]);
       }if(al.contains("tag")||al.contains("tga")||al.contains("taa")){ }
            else{
                System.out.println("An ORF found :)\n" + s6);
            }
         }
    
    
    
    
    
    
    String updateSeq(){
            char op= JOptionPane.showInputDialog(this, "Enter i to insert, a to append and d to delete a substring.").charAt(0);
            String delta="";
       switch (op){
           case 'i':
               delta= insertSeq();
               break;
           case 'a':
               delta= appendSeq();
               break;
           default:
               delta= deleteSeq();
       }
       return delta;
    }
    
    
    
    
    
        void Printing(){
            System.out.println("\nName of the sequence:" + name);
            System.out.println("\nSequence:" + seq);
    
            System.out.println("\nIs the sequence valid.... => " + isValidSeq());
            if(isValidSeq()==false){
            System.out.println("Since the sequence was erroneous, the corrected one is as follows: \n" + removeErrorInSequence() );}
    
    
            System.out.println("\nCount of A: " + countA());
            System.out.println("Count of G: " + countG());
            System.out.println("Count of C: " + countC());
            System.out.println("Count of T: " + countT());
            System.out.println("\nPercentage of A: " + percentageOfA());
    
            System.out.println("\nFASTA FORMAT:" + displayInFasta());
            System.out.println("\nLength of the sequence: " + length());
            
            System.out.println("\nThe substring has been deleted from the sequence, viz. " + deleteSeq());
    
            
    
            
            System.out.println("\nThe sequence of your choice has been appended viz. " + appendSeq());
    
           
    
            System.out.println("\nThe sequence of your choice has been inserted viz. " + insertSeq());
    
            
            System.out.println("The sequence has been updated, viz. " + updateSeq());
            
            searchSequence();
    
            System.out.println("\nComplement:" + complement());
            System.out.println("\nReverse Complement: " + revCom(complement()));
            System.out.println("\nRNA:" + RNA(complement()) );
            System.out.println("\nGene:" + seq.substring(seq.indexOf("atg"), seq.indexOf("tga")+3) + "\n");
            
            System.out.println("\ngene length:"+ gene().length() + "\nnumber of codons that should be made:"+ gene().length()/3 + "\nnumber of codons made here:" + makeCodons(gene()).length);
            System.out.println("\nPolypeptide chain: " + toProtein(gene()) + "\n");
    
            
            }
        
    
    
        public static void main(String[] args) {
            DNA c= new DNA();
            DNA dna= new DNA(c.getName(), c.getSeq());
            dna.Printing();
    
            
            dna.writeToFile(dna.displayInFasta());
           dna.readFromFile();
             System.out.println("\n\nREADING FRAMES\n");
            dna.readingFrames();
    
    
    
             //int frameno= Integer.parseInt(JOptionPane.showInputDialog(null,"Enter reading frame number you want to be printed as an ORF:"));
           // dna.ORF(frameno);
        }}
    This is the entire class. Can you please see for one more thing? Sometimes, it gives an exception after the third last statement in the method printing() executes. The exception occurs when i try to enter a sub-sequence to be searched which is not present in the sequence. e.g to run it, use the sequence at the top of the code in comments, initially. And enter gggggggggggggggggggg when it asks you to enter the sequence you want to search, while i have applied a check which does print something like "sequence not found" when the it is not there.

  6. #6
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    - Your ORF()-function (open reading frame) does six times the same, once for every argument. The idea of a method is that you can call it multiple times, once on each parameter.
    - It would be more realistic to define an ORF as containing a minimum length of DNA without stop-codons in stead of no stop-codons at all (I suppose you plan to work with longer strands then this, and then you'll hardly find any open reading frames at all this way.)
    The local arrayList al is never used. If you plan to store codons in an ArrayList there is no reason to first collect them in an array in makeCodons(). toProtein() could also work with an arrayList. I advise to make a choice: work with arrays or arraylists. I think ArrayLists are better because now you risk to work with empty codons and have to test for them. ArrayList should be parameterized ( ArrayList<String>() ) if you don't run an outdated Javaversion.

    (Edit: I was too slow. I reacted on the situation before the last post by the OP.)

    Edit 2: I quit helping here. I try to help people when they're stuck or on the wrong track, NOT to code along with them. Use the debugger.
    Last edited by Jodokus; 07-24-2011 at 12:46 AM.
    Nazneen Ali likes this.
    No bug ever had to calculate its fitnessfunction.

  7. #7
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    Dear Jodokus!
    I don't know how to use a debugger. I am really poor at java... indeed, at programming and at everything, well now you must be knowing that. I am trying to work. But I am so short of time, I am lagging far behind my mates in my work. If I would have planned properly before working, and if I would have much time to read as much as I want and need to, I might have done better.
    I want to work and work well, though I am not sure if I really can. I am poor at time management as well. I am worn out right now.

  8. #8
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    Quote Originally Posted by Jodokus View Post
    -

    Edit 2: I quit helping here. I try to help people when they're stuck or on the wrong track, NOT to code along with them. Use the debugger.
    Dear Jodokus!
    I don't know how to use a debugger. I am really poor at java... indeed, at programming and at everything, well now you must be knowing that. I am trying to work. But I am so short of time, I am lagging far behind my mates in my work. If I would have planned properly before working, and if I would have much time to read as much as I want and need to, I might have done better.
    I want to work and work well, though I am not sure if I really can. I am poor at time management as well. I am worn out right now.

  9. #9
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @OP,
    Don't take it too harsh.
    But imagine I see some code with a lot of mistakes. I jump in to rescue you. Then when I press the submit button I see a bulk of totally new much bigger code, with some of the things I suggested implemented, and a lot of new features, without you referring to my advise (could be that you didn't use or need that anyway but still) and a new cry for help.

    My reaction then is that you clearly don't need help.
    Normally you work on a SSCCE (small self contained compiling example), stripped to the bare essentials to work something out, not on proliferating code with a lot of new problems. I'll help you if you have small well defined problems (and I happen to know the solution) .
    Last edited by Jodokus; 07-24-2011 at 11:52 AM.
    No bug ever had to calculate its fitnessfunction.

  10. #10
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    @Jodokus:
    Let me tell you what it is. Well, the right way was to make small test programs for the code i needed to ask things about, and then post them. But two things... firstly I was really exhausted after having worked continuously for many hours and secondly the stress of the work and shortage of time... So what I did was that i copied those parts of my code and pasted them here, along with the problems in them.

    Then in the end of my first post, hastily creating a main() method and constructors which would be enough for the parts of my code I had pasted, I made my code more problematic.

    Then Norm had asked to edit my code and wrap it. so I posted the entire class here.

    Just some mistakes:
    - Variabele c in main is never read.
    - Readingframe 1 has to be a copy of the inputstring (dna). If you want to use the substringfunction, which is not nescessary here, you should use seq.substring( 0,seq.length() ). Now you chop of 1 base (same for the others).
    - You don't shift the readingframes correctly (only shifting 0 or 1 position).
    - But worst (biologically speaking) is that you take the reverse-function to obtain the backward sequence: you should take the complement strand by switching c for g, g for c, a for t and t for a, and then reverse of course.
    (Small detail: if you chop of 1 or two bases from the start of the string and then reverse, the sequences are stil in phase, i.e. not frameshifted. )

    Among these, its just the first one about variable c which has been corrected in the next post, just because I had pasted the entire class this time and c is being utilized to call the methods getName() and getSequence().
    Rest of the mistakes are still there.

    I do need help. I need to be helped and I need to be mentored. I am just too busted. And I want to do better now.

    I am highly obliged indeed, because you always reply so fast. And you are such a brilliant programmer. Believe me I was thinking if I can ever be a programmer like you.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,332
    Rep Power
    25

    Default

    A suggestion for easier testing. Preload the required answers to the prompts.
    For example change these two methods like this:
    Java Code:
        String getName(){
    //      return JOptionPane.showInputDialog(this,"Name of the sequence:");
          return "BB002260>aggggggatgggggccccccgcgcctccgcgcgcgcgctcgctgcggcgtcgcgcgcgcccccccaaaaaatgagcgcatccgggcgggcggcggtct/////////////";
    
       }
    
        String getSeq(){
          return "gggggggggggggggggggg";
    //      return JOptionPane.showInputDialog(this,"Sequence:");
    
        }

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Of course my tip from reply #3 is completely ignored again; oh well, I guess it has something to do with invisible ink ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    Java Code:
    Of course my tip from reply #3 is completely ignored again; oh well, I guess it has something to do with invisible ink ...
    No, the tip is great to make my code shorter, cleaner and efficient. I am going to apply all the tips by you and the other guys. Actually, I am messed up because my code needs alot of improvement and I am left with no time. I have to develop a GUI for this, which is something too difficult for me. That's why, I could not respond well.

    Anyways, I am always grateful to you guys for the great tips.
    Last edited by Nazneen Ali; 07-24-2011 at 08:51 AM. Reason: spelling mistake

  14. #14
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    @Jos: nice belly!
    @OP: I didn't sleep all night because I didn't advise you to implement Jos' suggestions first.
    I had some regrets and started to scrutinize your code again.

    - Made the privates private (no pun intended)
    - main(): It is a strange construction to instantiate a DNA just to obtain a name and sequence. Removed this (and adapted the defaultconstructor a bit).
    - Implemented Norm's suggestion (used the second constructor with the sequence)
    - Ignored Jos' suggestion (because of the invisible ink)
    - Was confronted with a popup. This is from a deleteSeq() initiated by the Print()-function. This function merrily tells you what has been deleted, but first expects you to supply a start and an end-position (how should the user know? That's programmers business). Also not nice: the instructions to the user are in the textfield, where the defaultinput belongs.
    Gave up (what any user would do after such a popup).......
    Last edited by Jodokus; 07-24-2011 at 01:54 PM.
    Nazneen Ali likes this.
    No bug ever had to calculate its fitnessfunction.

  15. #15
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    That big blob of code (in green) is simply a mapping from an encoding triplet to an amino acid; why don't you first fill a Map with the encoding triplets and their associated amino acid and simply do something like this:


    Java Code:
    String triplet= ...;
    String amino= ecodingMap.get(triplet);
    if (amino != null)
       al.append(amino);
    kind regards,

    Jos

    What is this piece of statement doing?
    Java Code:
    ecodingMap.get(triplet)
    What I understand is that encodingMap has got to be an object of the main class named DNA. It invokes a method to which we are passing a codon/triplet as an argument. What does this method do?
    Don't forget to smile :-)

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nazneen Ali View Post
    What is this piece of statement doing?
    Java Code:
    ecodingMap.get(triplet)
    What I understand is that encodingMap has got to be an object of the main class named DNA. It invokes a method to which we are passing a codon/triplet as an argument. What does this method do?
    The Map maps a triplet String to an amino acid String, e.g. "atg" --> "M"; it replaces that big blob of code; of course you have to fill the map before you can use it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    Made the privates private
    Does that matter when our program contains only one class?
    Don't forget to smile :-)

  18. #18
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    The Map maps a triplet String to an amino acid String, e.g. "atg" --> "M"; it replaces that big blob of code; of course you have to fill the map before you can use it.
    - And get(triplet), what does this method return? What I am getting from this is that get(triplet) gets the triplet and then encodingMap is invoked which maps the triplet to the corresponding amino acid. If that is the case, the big blob of code shall still remain in the program, except that it will be in another method named encodingMap? So we are aiming at making the code more organized basically.. is it so?? I mean am I getting it right?
    Don't forget to smile :-)

  19. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Nazneen Ali View Post
    - And get(triplet), what does this method return? What I am getting from this is that get(triplet) gets the triplet and then encodingMap is invoked which maps the triplet to the corresponding amino acid. If that is the case, the big blob of code shall still remain in the program, except that it will be in another method named encodingMap? So we are aiming at making the code more organized basically.. is it so?? I mean am I getting it right?
    Yep, true; writing out in code what can be done by a simple Map is so Fortanesque; that's why I mentioned it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  20. #20
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default

    Yep, true; writing out in code what can be done by a simple Map is so Fortanesque
    Fortanesque??
    What does that mean?
    Don't forget to smile :-)

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  2. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  3. Replies: 8
    Last Post: 04-14-2010, 05:49 AM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •