Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3 . You will then implement another class, DNAtools, in which you will implement

ID: 3695000 • Letter: 3

Question

3. You will then implement another class, DNAtools, in which you will implement the following methods:
a. ReadSequencesFromFile: this method takes the name of a file as parameter: the given file contains information about DNA sequences (one sequence per line, including name and sequence). It reads the information and fills an array of DNAsequences (a 1D array of elements of type DNAsequence) with this information. It returns the array of DNAsequences.


b. ReadTargetsFromFile: this method takes the name of a file as parameter: the given file contains information about DNA sequences (one sequence per line, which is just one string per line – no name in this case). It reads the information and fills an array of Strings with this information. It returns this array.


c. PrintSequenceArray: this method takes a 1D array of DNAsequences and prints it out (using the print method from the DNAsequence class).


d. FindBestMatchSequence: this method takes a 1D array of DNAsequences and a 1D array of strings (the target strings), and identifies the DNAsequence (within the given 1D array of DNAsequences) whose average of the number of occurrences in each of the target strings is highest.


e. SortByBestOccurrenceAverage: this method takes a 1D array of DNAsequences and a 1D array of strings (the target strings), and sorts the 1D array of DNAsequences by the average of their number of occurrences in each of the target strings (highest to lowest).


f. SortByLetter: this method takes a 1D array of DNAsequences and a letter (among A, C, G, or T) as an input and sorts the 1D array of DNAsequences by the number of such letter in each DNAsequence (in descending order of such letter).


g. The main method in which you will implement the following.


1. You ask the user for a file name 2. You read the file and retrieve information about DNAsequences by calling method ReadSequencesFromFile 3. You ask the user for a file name 4. You read the file and retrieve information about target strings by calling method ReadTargetsFromFile 5. You run the relevant class methods from DNAsequence to fill the attributes. 6. You sort the array obtained in Step 2 using method SortByBestOccurrenceAverage and print it out. 7. You sort the array obtained in Step 6 using method SortByLetter where the letter is A, and print it out.
8. You sort the array obtained in Step 7 using method SortByLetter where the letter is C, and print it out. 9. You sort the array obtained in Step 8 using method SortByLetter where the letter is G, and print it out. 10. You sort the array obtained in Step 9 using method SortByLetter where the letter is T, and print it out

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class DNAtools {
  
   /**********************************************************************************
   * ReadSequencesFromFile:
* This method takes the name of a file as parameter:
* the given file contains information about DNA sequences (one sequence per
* line, including name and sequence).
* It reads the information and fills an array of DNAsequences (a 1D array of
* elements of type DNAsequence) with this information.
* It returns the array of DNAsequences.
   ***********************************************************************************/
   public static DNAsequence[] ReadSequencesFromFile(String filename) throws IOException {
      
       FileReader fr = new FileReader(filename);
       BufferedReader textReader = new BufferedReader(fr);
       textReader.mark(1000); // here we assume that the file won't be longer than 1000 lines
      
       // here we count how many DNA sequences we have: the number of DNA sequences is the number of lines in the file
       // because for each sequence there is one sequence information per line
       while (textReader.readLine()!=null) {
           numOfLines++;
       }
  
DNAsequence[] myDNAsequences = new DNAsequence[numOfLines];
       int index = 0;
       String name;
       String sequence;
      
       for (int i=0; i<numOfLines; i++) {
           String info = textReader.readLine();
           index = info.lastIndexOf(' ');
           name = info.substring(0,index);
           sequence = info.substring(index + 1,info.length());
          
           myDNAsequences[i] = new DNAsequence();
           myDNAsequences[i].setName(name);
           myDNAsequences[i].setSequence(sequence);
       }
// your code goes here
  
       textReader.close();
       return myDNAsequences;
   }
  
   /**********************************************************************************
   * ReadTargetsFromFile:
* This method takes the name of a file as parameter:
* The given file contains information about DNA sequences (one sequence per
* line, which is just one string per line – no name in this case).
* It reads the information and fills an array of Strings with this information.
* It returns this array.
   ***********************************************************************************/
   public static String[] ReadTargetsFromFile(String filename) throws IOException {
      
       FileReader fr = new FileReader(filename);
       BufferedReader textReader = new BufferedReader(fr);
       textReader.mark(1000); // here we assume that the file won't be longer than 1000 lines
      
       // here we count how many DNA sequences we have: the number of DNA sequences is the number of lines in the file
       // because for each sequence there is one sequence per line
       int numOfLines = 0;
       while (textReader.readLine()!=null) {
           numOfLines++;
       }
  
String[] myTargets = new String[numOfLines];

// your code goes here
  
       textReader.close();
       return myTargets;
   }
  
  
   /**********************************************************************************
* PrintSequenceArray:
* This method takes a 1D array of DNAsequences and prints it out (using the print
* method from the DNAsequence class).
   ***********************************************************************************/
public static void PrintSequenceArray(DNAsequence[] A) {
// your code goes here   
}
  
   /**********************************************************************************
* FindBestMatchSequence:
* This method takes a 1D array of DNAsequences and a 1D array of strings (the target
* strings), and identifies the DNAsequence (within the given 1D array of DNAsequences)
* whose average of the number of occurrences in each of the target strings is highest.
   ***********************************************************************************/
public static DNAsequence FindBestMatchSequence(DNAsequence[] A, String[] targets) {
// your code goes here   
}

   /**********************************************************************************
* SortByBestOccurrenceAverage:
* This method takes a 1D array of DNAsequences and a 1D array of strings (the
* target strings), and sorts the 1D array of DNAsequences by the average of their
* number of occurrences in each of the target strings (highest to lowest).
   ***********************************************************************************/
public static void SortByBestOccurrenceAverage(DNAsequence[] A, String[] targets) {
// your code goes here   
}


   /**********************************************************************************
* SortByLetter:
* This method takes a 1D array of DNAsequences and a letter (among A, C, G, or T)
* as an input and sorts the 1D array of DNAsequences by the number of such letter
* in each DNAsequence (in descending order of such letter).
   ***********************************************************************************/
public static void SortByLetter(DNAsequence[] A, char c) {
// your code goes here   
}
  
  
   /**********************************************************************************
   * The main method should go as follows:
* 1.   You ask the user for a file name
* 2.   You read the file and retrieve information about DNAsequences by calling method ReadSequencesFromFile
* 3.   You ask the user for a file name
* 4.   You read the file and retrieve information about target strings by calling method ReadTargetsFromFile
* 5.   You run the relevant class methods from DNAsequence to fill the attributes.
* 6.   You sort the array obtained in Step 2 using method SortByBestOccurrenceAverage and print it out.
* 7.   You sort the array obtained in Step 6 using method SortByLetter where the letter is A, and print it out.
* 8.   You sort the array obtained in Step 7 using method SortByLetter where the letter is C, and print it out.
* 9.   You sort the array obtained in Step 8 using method SortByLetter where the letter is G, and print it out.
* 10.   You sort the array obtained in Step 9 using method SortByLetter where the letter is T, and print it out.
   ***********************************************************************************/
   public static void main(String[] args) throws IOException {
// your code goes here
   }

}

Explanation / Answer

I wrote following methods and classes

1)DNAsequence class is missing , wrote the class

Wrote following methods

2)PrintSequenceArray(DNAsequence[] A)

3) private static float getAvgOccurrence(DNAsequence dnaseq, String[] targets) -to count avg no of occurences

4)FindBestMatchSequence

5)SortByBestOccurrenceAverage

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class DNAtools {
  
    /**********************************************************************************
     * ReadSequencesFromFile:
     * This method takes the name of a file as parameter:
     *      the given file contains information about DNA sequences (one sequence per
     *      line, including name and sequence).
     * It reads the information and fills an array of DNAsequences (a 1D array of
     * elements of type DNAsequence) with this information.
     * It returns the array of DNAsequences.
     ***********************************************************************************/
    public static DNAsequence[] ReadSequencesFromFile(String filename) throws IOException {
      
        FileReader fr = new FileReader(filename);
        BufferedReader textReader = new BufferedReader(fr);
        textReader.mark(1000); // here we assume that the file won't be longer than 1000 lines
        int numOfLines = 0;
        // here we count how many DNA sequences we have: the number of DNA sequences is the number of lines in the file
        // because for each sequence there is one sequence information per line
        while (textReader.readLine()!=null) {
            numOfLines++;
        }
      
        DNAsequence[] myDNAsequences = new DNAsequence[numOfLines];
        int index = 0;
        String name;
        String sequence;
      
        for (int i=0; i<numOfLines; i++) {
            String info = textReader.readLine();
            index = info.lastIndexOf(' ');
            name = info.substring(0,index);
            sequence = info.substring(index + 1,info.length());
          
            myDNAsequences[i] = new DNAsequence();
            myDNAsequences[i].setName(name);
            myDNAsequences[i].setSequence(sequence);
        }
        // your code goes here
      
        textReader.close();
        return myDNAsequences;
    }
  
    /**********************************************************************************
     * ReadTargetsFromFile:
     * This method takes the name of a file as parameter:
     *      The given file contains information about DNA sequences (one sequence per
     *      line, which is just one string per line – no name in this case).
     * It reads the information and fills an array of Strings with this information.
     * It returns this array.
     ***********************************************************************************/
    public static String[] ReadTargetsFromFile(String filename) throws IOException {
      
        FileReader fr = new FileReader(filename);
        BufferedReader textReader = new BufferedReader(fr);
        textReader.mark(1000); // here we assume that the file won't be longer than 1000 lines
      
        // here we count how many DNA sequences we have: the number of DNA sequences is the number of lines in the file
        // because for each sequence there is one sequence per line
        int numOfLines = 0;
        while (textReader.readLine()!=null) {
            numOfLines++;
        }
      
        String[] myTargets = new String[numOfLines];

        // your code goes here
      
        textReader.close();
        return myTargets;
    }
  
  
    /**********************************************************************************
     * PrintSequenceArray:
     * This method takes a 1D array of DNAsequences and prints it out (using the print
     * method from the DNAsequence class).
     ***********************************************************************************/
    public static void PrintSequenceArray(DNAsequence[] A) {
        // your code goes here
       if(A != null && A.length > 0) {
           for(DNAsequence dseq: A)
               dseq.print();
       }
    }
  
    private static float getAvgOccurrence(DNAsequence dnaseq, String[] targets) {
       float avg = 0;
       int count = 0;
       int index = 0;
       int noOfTargets = 0;
       for(String tg: targets) {
           index = 0;
           if(((tg.indexOf(dnaseq.getSequence(), index)) != -1))
               noOfTargets++;
             while ((index = tg.indexOf(dnaseq.getSequence(), index)) != -1)
             {
               index++;
                count++;
             }
       }
       avg = (float)count/noOfTargets;
       return avg;
    }
  
    /**********************************************************************************
     * FindBestMatchSequence:
     * This method takes a 1D array of DNAsequences and a 1D array of strings (the target
     * strings), and identifies the DNAsequence (within the given 1D array of DNAsequences)
     * whose average of the number of occurrences in each of the target strings is highest.
     ***********************************************************************************/
    public static DNAsequence FindBestMatchSequence(DNAsequence[] A, String[] targets) {
        // your code goes here
       DNAsequence maxSeq = null;
       float maxAvg = 0;
       float avg = 0;
       int index = 0;

       if(A != null && A.length > 0) {
           for(DNAsequence dseq: A) {
               avg = getAvgOccurrence(dseq,targets);
               if(avg > maxAvg) {
                   maxAvg = avg;
                   maxSeq = dseq;
                 }
           }
              
       }
       return maxSeq;
    }

    /**********************************************************************************
     * SortByBestOccurrenceAverage:
     * This method takes a 1D array of DNAsequences and a 1D array of strings (the
     * target strings), and sorts the 1D array of DNAsequences by the average of their
     * number of occurrences in each of the target strings (highest to lowest).
     ***********************************************************************************/
    public static void SortByBestOccurrenceAverage(DNAsequence[] A, String[] targets) {
        // your code goes here
      
       int index = 0;
       float avgOccurences[] = null;
       if(A != null && A.length > 0) {
           avgOccurences = new float[A.length];
          
           for(DNAsequence dseq: A) {
               avgOccurences[index++] = getAvgOccurrence(dseq,targets);
              
           }
           //now sort it
           DNAsequence temp = null;
           for(int i = 0; i < A.length;i++) {
               for(int j = 0; j < A.length; j++) {
                   if(avgOccurences[j] > avgOccurences[i]) {
                       temp = A[i];
                       A[i] =A[j];
                       A[j] =temp;
                      
                   }
               }
           }
              
       }
    }


    /**********************************************************************************
     * SortByLetter:
     * This method takes a 1D array of DNAsequences and a letter (among A, C, G, or T)
     * as an input and sorts the 1D array of DNAsequences by the number of such letter
     * in each DNAsequence (in descending order of such letter).
     ***********************************************************************************/
    public static void SortByLetter(DNAsequence[] A, char c) {
        // your code goes here
    }
  
  
    /**********************************************************************************
     * The main method should go as follows:
     * 1.    You ask the user for a file name
     * 2.    You read the file and retrieve information about DNAsequences by calling method ReadSequencesFromFile
     * 3.    You ask the user for a file name
     * 4.    You read the file and retrieve information about target strings by calling method ReadTargetsFromFile
     * 5.    You run the relevant class methods from DNAsequence to fill the attributes.
     * 6.    You sort the array obtained in Step 2 using method SortByBestOccurrenceAverage and print it out.
     * 7.    You sort the array obtained in Step 6 using method SortByLetter where the letter is A, and print it out.
     * 8.    You sort the array obtained in Step 7 using method SortByLetter where the letter is C, and print it out.
     * 9.    You sort the array obtained in Step 8 using method SortByLetter where the letter is G, and print it out.
     * 10.    You sort the array obtained in Step 9 using method SortByLetter where the letter is T, and print it out.
     ***********************************************************************************/
    public static void main(String[] args) throws IOException {
        // your code goes here
    }

}

class DNAsequence {
   private String name;
   private String sequence;
  
   public DNAsequence() {
       name = null;
       sequence = null;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getSequence() {
       return sequence;
   }

   public void setSequence(String sequence) {
       this.sequence = sequence;
   }
  
   public void print() {
       System.out.println(name+" : "+sequence);
   }
  
  
}