Java! - When you can’t find it, you build it! (User-defined types) As a genetics
ID: 3687723 • Letter: J
Question
Java! - When you can’t find it, you build it! (User-defined types)
As a genetics researcher, you want to inventory DNA sequences. Any time you access a sequence, you would like to have access to a range of information about it and similarly, you would like to have an array of dedicated tools for it.
Each DNA sequence is compared to an array of other DNA sequences (in the fashion we did it for lab 8: with key and target sequences). You would like to be able to access information about the occurrences of your DNA sequence in other known target DNA sequences.
In this activity, you will have to define a new type, DNAsequence, to allow you to do just that.
Each element of type DNAsequence carries the following information:
1. Design a class DNAsequence (in a file VideoGame.java) that has the following list of attributes (all of them private):
a. A name: of type string
b. A DNA sequence: of type string
c. A length: of type int
d. A number of A’s: of type int
e. A number of C’s: of type int
f. A number of G’s: of type int
g. A number of T’s: of type int
This DNAsequence class will provide you with a blueprint to create DNAsequence objects.
2. To complete the class DNAsequence, you will also be required to implement the following class methods* (these are non-static methods):
a. Constructors (essential to create instances of your blueprint DNAsequence)
You will implement two constructors: the default constructor as well as a constructor that takes 3 parameters (a string – the name of the sequence, a string – the sequence, and an integer – the length of the sequence).
b. Accessors: one method per attribute so that the value of each attribute can be accessed.
c. Setters / Mutators: one method per attribute so that the value of each attribute can be set / modified.
d. Additional methods:
1/ Method Print: this method prints the name of the sequence followed by the sequence itself, then prints its length and the number of A’s, C’s, G’s, and T’s.
2/ Method countSubStringMatch: as defined in Lab 8, except for the parameters: this method only takes one parameter, the target (the key is the object on which the method is applied). Note: You are free to use your iterative or recursive method.
*Note: Some of the above methods are already provided to you in VideoGame.java to “show you the way”.
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
GIVEN JAVA!!!!!:
public class DNAsequence {
/**************** ATTRIBUTES *******************************************
/* Here go your attributes, i.e., the information that is contained in
* your new "type"
* We can also see these new types as "blue-prints" of "things" we are
* going to build
* Expected attributes are:
* a. A name: of type string
* b. A DNA sequence: of type string
* c. A length: of type int
* d. A number of A’s: of type int
* e. A number of C’s: of type int
* f. A number of G’s: of type int
* g. A number of T’s: of type int
***********************************************************************/
private String name;
private String sequence;
private int length;
private int As;
private int Cs;
private int Gs;
private int Ts;
/***************** METHODS *********************************************
* Note that none of the methods below are static.
* It means that once you have defined a object of type DNAsequence,
* let's say mySequence, then you will call these methods as:
* mySequence.printSequence(), or mySequence.setName("this new name"),
* etc.
***********************************************************************/
/**************** CONSTRUCTORS *****************************************
* Note that the signatures are different from those we are used to
***********************************************************************/
/* default constructor: provided to you. You should not touch the next two lines of code */
public DNAsequence() {
}
public DNAsequence(String seqName, String seq, int seqlength) {
// your code goes here
}
/***************** SETTERS / MUTATORS **********************************
* Methods that allow to set or modify the values of the attributes
* One method per attribute
* Note that the methods are not static (to be explained -- much -- later)
***********************************************************************/
/* one per attribute: we provide you with two, you have to provide the others */
public void setName(String seqName) {
name = seqName;
}
public void setSequence(String seqSequence) {
sequence = seqSequence;
}
public void setlength(int seqlength) {
// your code goes here
}
// complete here with more mutators, as relevant
/**************** GETTERS / ACCESSORS **********************************
* Methods that allow to access the values of the attributes
* One method per attribute
* Note that the methods are not static (to be explained -- much -- later)
***********************************************************************/
/* one per attribute: we provide you with the one, you have to provide the others */
public String getName() {
return name;
}
public double getSequence() {
return sequence;
}
public int getLength() {
// your code goes here
}
// complete here with more accessors, as relevant
/***********************************************************************
* Other methods, depending on needs
***********************************************************************/
/***********************************************************************
* Here we are asking you to design a method that prints the information
* about any DNAsequence
* Method Print: this method prints the name of the sequence followed by
* the sequence itself, then prints its length and the number of A’s,
* C’s, G’s, and T’s.
***********************************************************************/
public void Print() {
// your code goes here
}
/***********************************************************************
* Method countSubStringMatch:
* as defined in Lab 8, except for the parameters: this method only takes
* one parameter, the target (the key is the object on which the method
* is applied). Note: You are free to use your iterative or recursive method.
***********************************************************************/
public int countSubStringMatch(String target) {
int result = 0;
// your code goes here
return result;
}
}
Explanation / Answer
//DNAtools.java
package dnaapplication;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class DNAtools extends DNAsequence{
/*
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. */
public DNAsequence[] ReadSequencesFromFile(String Seqfilename) throws FileNotFoundException
{
Scanner infile=new Scanner(new FileReader(Seqfilename));
DNAsequence dnaSequences[]=new DNAsequence[100];
int n=0;
while(infile.hasNextLine())
{
String info[]=infile.nextLine().split(" ");
dnaSequences[n++]=new DNAsequence(info[0], info[1],info[1].length());
}
return dnaSequences;
}
/*
b. ReadTargetsFromFile: this method takes the name of a file as parameter:
It reads the information and fills an array of Strings with this information.
It returns this array.
*/
public String[] ReadTargetsFromFile(String Targetfilename) throws FileNotFoundException
{
Scanner infile=new Scanner(new FileReader(Targetfilename));
String targets[]=new String[100];
int n=0;
while(infile.hasNextLine())
{
targets[n++]=infile.nextLine();
}
return targets;
}
/*
c. PrintSequenceArray: this method takes a 1D array of DNAsequences and prints it out
(using the print method from the DNAsequence class).
*/
public void PrintSequenceArray(DNAsequence dnaSequence[])
{
System.out.println("List of Sequences");
for(int i=0;i<dnaSequence.length;i++)
{
dnaSequence[i].Print();
}
}
/*
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.
*/
public void FindBestMatchSequence(DNAsequence dnaSequence[], String targets[])
{
//finding occurences
int Occurences[]=new int[dnaSequence.length];
for(int i=0;i<dnaSequence.length;i++)
{
Occurences[i]=0;
for(int j=0;j<targets.length;j++)
{
Occurences[i]+=dnaSequence[i].countSubStringMatch(targets[j]);
}
}
//find highest Ocurrence containing sequence
int high=Occurences[0];
int pos=0;
for(int k=1;k<Occurences.length;k++)
{
if(Occurences[k]>high)
{
high=Occurences[k];
pos=k;
}
}
System.out.println("Best Match Sequence is : "+dnaSequence[pos]);
}
/*
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).
*/
public void SortByBestOccurrenceAverage(DNAsequence dnaSequence[], String targets[])
{
//finding occurences
int Occurences[]=new int[dnaSequence.length];
for(int i=0;i<dnaSequence.length;i++)
{
Occurences[i]=0;
for(int j=0;j<targets.length;j++)
{
Occurences[i]+=dnaSequence[i].countSubStringMatch(targets[j]);
}
}
//sorting
for(int k=0;k<dnaSequence.length;k++)
{
for(int m=0;m<dnaSequence.length;m++)
if(Occurences[k]>Occurences[m])
{
DNAsequence dna=dnaSequence[k];
dnaSequence[k]=dnaSequence[m];
dnaSequence[m]=dna;
}
}
}
/*
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
*/
public static void main(String[] args) {
}
}
////////////////////////////////////////////////////////
//DNAsequence.java
package dnaapplication;
public class DNAsequence {
/**************** ATTRIBUTES *******************************************
/* Here go your attributes, i.e., the information that is contained in
* your new "type"
* We can also see these new types as "blue-prints" of "things" we are
* going to build
* Expected attributes are:
* a. A name: of type string
* b. A DNA sequence: of type string
* c. A length: of type int
* d. A number of A’s: of type int
* e. A number of C’s: of type int
* f. A number of G’s: of type int
* g. A number of T’s: of type int
***********************************************************************/
private String name;
private String sequence;
private int length;
private int As;
private int Cs;
private int Gs;
private int Ts;
/***************** METHODS *********************************************
* Note that none of the methods below are static.
* It means that once you have defined a object of type DNAsequence,
* let's say mySequence, then you will call these methods as:
* mySequence.printSequence(), or mySequence.setName("this new name"),
* etc.
***********************************************************************/
/**************** CONSTRUCTORS *****************************************
* Note that the signatures are different from those we are used to
***********************************************************************/
/* default constructor: provided to you. You should not touch the next two lines of code */
public DNAsequence() {
}
public DNAsequence(String seqName, String seq, int seqlength) {
name=seqName;
sequence=seq;
length=seqlength;
As=getCount('A');
Cs=getCount('C');
Gs=getCount('G');
Ts=getCount('T');
}
/***************** SETTERS / MUTATORS **********************************
* Methods that allow to set or modify the values of the attributes
* One method per attribute
* Note that the methods are not static (to be explained -- much -- later)
* @param seqName
***********************************************************************/
/* one per attribute: we provide you with two, you have to provide the others */
public void setName(String seqName) {
name = seqName;
}
public void setSequence(String seqSequence) {
sequence = seqSequence;
}
public void setlength(int seqlength) {
// your code goes here
length=seqlength;
}
// complete here with more mutators, as relevant
/**************** GETTERS / ACCESSORS **********************************
* Methods that allow to access the values of the attributes
* One method per attribute
* Note that the methods are not static (to be explained -- much -- later)
***********************************************************************/
/* one per attribute: we provide you with the one, you have to provide the others */
public String getName() {
return name;
}
public String getSequence() {
return sequence;
}
public int getLength() {
return length;
}
// complete here with more accessors, as relevant
/***********************************************************************
* Other methods, depending on needs
***********************************************************************/
/***********************************************************************
* Here we are asking you to design a method that prints the information
* about any DNAsequence
* Method Print: this method prints the name of the sequence followed by
* the sequence itself, then prints its length and the number of A's,
* C's, G's, and T's.
***********************************************************************/
public void Print() {
System.out.println("Name of the sequence: "+name);
System.out.println("Sequence: "+sequence);
System.out.println("Length: "+length);
System.out.println("A's: "+As);
System.out.println("C's: "+Cs);
System.out.println("G's: "+Gs);
System.out.println("A's: "+As);
}
/***********************************************************************
* Method countSubStringMatch:
* as defined in Lab 8, except for the parameters: this method only takes
* one parameter, the target (the key is the object on which the method
* is applied). Note: You are free to use your iterative or recursive method.
* @param target
***********************************************************************/
public int countSubStringMatch(String target) {
int result = 0;
int i=0;
while(i<target.length())
{
if(target.substring(i, i+5).equalsIgnoreCase(sequence))
{
result++;
}
i++;
}
return result;
}
private int getCount(char c) {
int count=0;
for(int i=0;i<sequence.length();i++)
{
if(sequence.charAt(i)==c)
{
count++;
}
}
return count;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.