For my Computer science class I am suppose input a file call
For my Computer science class I am suppose input a file called dna.txt which includes codons. And then create a output file that includes various things about the codons to see if it includes protons. I have gotten it create the output file with all the things needed. However, when I create the output file, it will not print the output file out in the console. What do I need to add to have the program print the output in the console?
import java.util.*;
import java.io.*;
import java.text.*;
public class DNAAA {
public static final int MNC = 5; //the (M)inimun (N)umber of (C)odons a valid protein must have
public static final double CG = 30.0; //the minimum percentage of mass from (C) and (G) nucleotides
public static final int UN = 4; //minimum number of (U)nique (N)ucleotides
public static final int NPC = 3; //the number of (N)ucleotides (P)er (C)odon
//This main method is structured to first call methods which introduce the program and ask
//for file names. Then, it uses a while loop to examine lines of DNA sequences. Finally,
//it calls a test method to determine if the sequence is a valid protein or not, based on
//constant values and a simple set of conditions.
public static void main(String[] args) throws FileNotFoundException {
//introduces program and accepts inputs from the user to specify source and text
//output file names:
String inputText = input();
String outputText = output();
//creates scanner to read input file:
Scanner DNA = new Scanner(new File(inputText));
//creates PrintStream to print to output file:
PrintStream out = new PrintStream(new File(outputText));
//Scans the text file and takes advantage of alternating parity to differentiate name versus
//DNA sequences. Calls methods that calculate data of potential DNA sequences. Calls a test
//method to determine if the sequence is a protein or not:
while(DNA.hasNextLine()) {
name(DNA.nextLine(), out); //prints the name of nucleotide sequence
String sequenceDNA = sequence(DNA.nextLine(), out);
int[] nuc_Count = nuc_Count(sequenceDNA, out);
double[] percentages = masses(nuc_Count, out);
String codonList = groupCodons(sequenceDNA, out);
proteinTest(codonList, percentages, out);
}
}
//Prints a description of the program and invites user to name the input text file.
//Returns the .txt file name as a String to main.
public static String input() throws FileNotFoundException {
System.out.println(\"This program reports information about DNA\");
System.out.println(\"nucleotides sequences that may encode proteins.\");
System.out.print(\"Input file name? \");
Scanner input = new Scanner(System.in);
String text = (input.next());
return text;
}
//Invites the user to name the output text file to print into. Returns the .txt file name
//as a String to main.
public static String output() {
System.out.print(\"Output file name? \");
Scanner output = new Scanner(System.in);
String text = (output.next());
return text;
}
//Accpets next line as parameter and prints the name of the nucleotide region.
public static void name(String name, PrintStream out) {
out.println(\"Region Name: \" + name);
}
//Accepts next line as parameter, prints nucleotide region in upper-case, and returns a new String.
public static String sequence(String sequenceDNA, PrintStream out) {
sequenceDNA = sequenceDNA.toUpperCase();
out.println(\"Nucleotides: \" + sequenceDNA);
return sequenceDNA;
}
//Accepts String of nucleotides and Printstream as parameters, scans for and tallies each
//nucleotide present, and returns an int array of the individual nucleotides.
public static int[] nuc_Count(String sequenceDNA, PrintStream out) throws FileNotFoundException {
int[] counts = new int[UN + 1];
char[] codons = {\'A\', \'C\', \'G\', \'T\', \'-\'};
for(int i = 0; i < sequenceDNA.length(); i++) {
char c = sequenceDNA.charAt(i);
for(int j = 0; j < codons.length; j++) {
if(c == codons[j]) {
counts[j]++;
}
}
}
int[] counts_Short = Arrays.copyOf(counts, 4);
out.println(\"Nuc. Counts: \" + Arrays.toString(counts_Short));
return counts;
}
//Accepts the array of nucleotide counts, calculates the masses of each nucleotide, calculates the
//total mass of the sequence, prints the percentage of mass for nucleotides, returns arry of
//rounded percentages.
public static double[] masses(int[] nuc_Count, PrintStream out) throws FileNotFoundException {
double[] masses_Constant = {135.128, 111.103, 151.128, 125.107, 100.000};
double[] masses_Nuc = new double[5];
double totalMass = 0;
for(int i = 0; i < 5; i++) {
masses_Nuc[i] = nuc_Count[i] * masses_Constant[i];
totalMass += masses_Nuc[i];
}
//calls method to convert array of masses into percentages and prints:
double[] percentages = convert_Percentage(masses_Nuc, totalMass);
double[] percentages2 = Arrays.copyOf(percentages, 4);
out.print(\"Total Mass%: \" + Arrays.toString(percentages2) + \" of \");
out.printf(\"%.1f\", totalMass);
out.println();
return percentages;
}
//Accepts the double array of nucleotide masses and converts it to a rounded percentage using the
//total mass. Returns an array of type double.
public static double[] convert_Percentage(double[] masses_Nuc, double totalMass) throws FileNotFoundException {
double[] percentages = new double[5];
for(int i = 0; i <=4; i++) {
percentages[i] = Math.round((masses_Nuc[i] / totalMass * 100) * 10.0) / 10.0;
}
return percentages;
}
//Accepts DNA sequence and Prinstream as parameters and creates and prints an array of codons by
//grouping nucleotides into chunks of codons using the constant nucleotide count. Returns a
//String of the codons.
public static String groupCodons(String sequenceDNA, PrintStream out) throws FileNotFoundException {
String sequenceDNA2 = sequenceDNA.replace(\"-\",\"\");
int length = sequenceDNA2.length() / NPC;
String[] codons = new String[length];
int j = 1;
for(int i = 0; i <= sequenceDNA2.length() - NPC; i = i + NPC) {
String codon = sequenceDNA2.substring(i, NPC * j);
codons[j - 1] = codon;
j++;
}
String codonList = Arrays.toString(codons);
out.println(\"Codons List: \" + codonList);
return codonList;
}
//Calls four test methods to determine if the sequence is a valid protein and prints result.
//Accepts a string of codons, the percentages array, and a printsream as parameters. Passes
//list of codons and percentages array to test methods.
public static void proteinTest(String codonList, double[] percentages, PrintStream out) {
if (startTest(codonList) && stopTest(codonList) && mNCTest(codonList) && percentageTest(percentages)) {
out.println(\"Is Protein?: YES\");
} else {
out.println(\"Is Protein?: NO\");
}
out.println();
}
//This method returns \"true\" if the first codon is a valid start codon (\"ATG\").
//Accepts string list of codons as parameter.
public static boolean startTest(String codonList) {
return(codonList.substring(1, 4).equals(\"ATG\"));
}
//This method returns \"true\" if the last codon is a valid stop codon. (\"TAA\", \"TAG\", or \"TGA\").
//Accepts string list of codons as parameter.
public static boolean stopTest(String codonList) {
String EndC = codonList.substring(codonList.length() - 4, codonList.length() - 1);
return (EndC.equals(\"TAA\") || EndC.equals(\"TAG\") || EndC.equals(\"TGA\"));
}
//This method returns \"true\" if there are 5 or more codons in the sequence. Does so by analyzing
//the codon string to determine if a 5th data set exists. Accepts Str list of codons as parameter.
public static boolean mNCTest(String codonList) {
return(codonList.charAt(19) == \',\');
}
//This method returns \"true\" if the combined mass percentages of nucleotide \"C\" and \"G\" are equal
//to or greater than 30.0% of total mass. Accepts a double array of nucleotide mass percentages.
public static boolean percentageTest(double[] percentages) {
return(percentages[1] + percentages[2] >= 30.0);
}
}
Solution
import java.util.*;
import java.io.*;
import java.text.*;
public class DNAAA {
public static final int MNC = 5; //the (M)inimun (N)umber of (C)odons a valid protein must have
public static final double CG = 30.0; //the minimum percentage of mass from (C) and (G) nucleotides
public static final int UN = 4; //minimum number of (U)nique (N)ucleotides
public static final int NPC = 3; //the number of (N)ucleotides (P)er (C)odon
//This main method is structured to first call methods which introduce the program and ask
//for file names. Then, it uses a while loop to examine lines of DNA sequences. Finally,
//it calls a test method to determine if the sequence is a valid protein or not, based on
//constant values and a simple set of conditions.
public static void main(String[] args) throws FileNotFoundException {
//introduces program and accepts inputs from the user to specify source and text
//output file names:
String inputText = input();
String outputText = output();
//creates scanner to read input file:
Scanner DNA = new Scanner(new File(inputText));
//creates PrintStream to print to output file:
PrintStream out = new PrintStream(new File(outputText));
//Scans the text file and takes advantage of alternating parity to differentiate name versus
//DNA sequences. Calls methods that calculate data of potential DNA sequences. Calls a test
//method to determine if the sequence is a protein or not:
while(DNA.hasNextLine()) {
name(DNA.nextLine(), out); //prints the name of nucleotide sequence
String sequenceDNA = sequence(DNA.nextLine(), out);
int[] nuc_Count = nuc_Count(sequenceDNA, out);
double[] percentages = masses(nuc_Count, out);
String codonList = groupCodons(sequenceDNA, out);
proteinTest(codonList, percentages, out);
}
}
//Prints a description of the program and invites user to name the input text file.
//Returns the .txt file name as a String to main.
public static String input() throws FileNotFoundException {
System.out.println(\"This program reports information about DNA\");
System.out.println(\"nucleotides sequences that may encode proteins.\");
System.out.print(\"Input file name? \");
Scanner input = new Scanner(System.in);
String text = (input.next());
return text;
}
//Invites the user to name the output text file to print into. Returns the .txt file name
//as a String to main.
public static String output() {
System.out.print(\"Output file name? \");
Scanner output = new Scanner(System.in);
String text = (output.next());
return text;
}
//Accpets next line as parameter and prints the name of the nucleotide region.
public static void name(String name, PrintStream out) {
out.println(\"Region Name: \" + name);
// when print to file, print to the console as well
System.out.println(\"Region Name: \" + name);
}
//Accepts next line as parameter, prints nucleotide region in upper-case, and returns a new String.
public static String sequence(String sequenceDNA, PrintStream out) {
sequenceDNA = sequenceDNA.toUpperCase();
out.println(\"Nucleotides: \" + sequenceDNA);
// when print to file, print to the console as well
System.out.println(\"Nucleotides: \" + sequenceDNA);
return sequenceDNA;
}
//Accepts String of nucleotides and Printstream as parameters, scans for and tallies each
//nucleotide present, and returns an int array of the individual nucleotides.
public static int[] nuc_Count(String sequenceDNA, PrintStream out) throws FileNotFoundException {
int[] counts = new int[UN + 1];
char[] codons = {\'A\', \'C\', \'G\', \'T\', \'-\'};
for(int i = 0; i < sequenceDNA.length(); i++) {
char c = sequenceDNA.charAt(i);
for(int j = 0; j < codons.length; j++) {
if(c == codons[j]) {
counts[j]++;
}
}
}
int[] counts_Short = Arrays.copyOf(counts, 4);
out.println(\"Nuc. Counts: \" + Arrays.toString(counts_Short));
// when print to file, print to the console as well
System.out.println(\"Nuc. Counts: \" + Arrays.toString(counts_Short));
return counts;
}
//Accepts the array of nucleotide counts, calculates the masses of each nucleotide, calculates the
//total mass of the sequence, prints the percentage of mass for nucleotides, returns arry of
//rounded percentages.
public static double[] masses(int[] nuc_Count, PrintStream out) throws FileNotFoundException {
double[] masses_Constant = {135.128, 111.103, 151.128, 125.107, 100.000};
double[] masses_Nuc = new double[5];
double totalMass = 0;
for(int i = 0; i < 5; i++) {
masses_Nuc[i] = nuc_Count[i] * masses_Constant[i];
totalMass += masses_Nuc[i];
}
//calls method to convert array of masses into percentages and prints:
double[] percentages = convert_Percentage(masses_Nuc, totalMass);
double[] percentages2 = Arrays.copyOf(percentages, 4);
out.print(\"Total Mass%: \" + Arrays.toString(percentages2) + \" of \");
out.printf(\"%.1f\", totalMass);
out.println();
// when print to file, print to the console as well
System.out.print(\"Total Mass%: \" + Arrays.toString(percentages2) + \" of \");
System.out.printf(\"%.1f\", totalMass);
System.out.println();
return percentages;
}
//Accepts the double array of nucleotide masses and converts it to a rounded percentage using the
//total mass. Returns an array of type double.
public static double[] convert_Percentage(double[] masses_Nuc, double totalMass) throws FileNotFoundException {
double[] percentages = new double[5];
for(int i = 0; i <=4; i++) {
percentages[i] = Math.round((masses_Nuc[i] / totalMass * 100) * 10.0) / 10.0;
}
return percentages;
}
//Accepts DNA sequence and Prinstream as parameters and creates and prints an array of codons by
//grouping nucleotides into chunks of codons using the constant nucleotide count. Returns a
//String of the codons.
public static String groupCodons(String sequenceDNA, PrintStream out) throws FileNotFoundException {
String sequenceDNA2 = sequenceDNA.replace(\"-\",\"\");
int length = sequenceDNA2.length() / NPC;
String[] codons = new String[length];
int j = 1;
for(int i = 0; i <= sequenceDNA2.length() - NPC; i = i + NPC) {
String codon = sequenceDNA2.substring(i, NPC * j);
codons[j - 1] = codon;
j++;
}
String codonList = Arrays.toString(codons);
out.println(\"Codons List: \" + codonList);
// when print to file, print to the console as well
System.out.println(\"Codons List: \" + codonList);
return codonList;
}
//Calls four test methods to determine if the sequence is a valid protein and prints result.
//Accepts a string of codons, the percentages array, and a printsream as parameters. Passes
//list of codons and percentages array to test methods.
public static void proteinTest(String codonList, double[] percentages, PrintStream out) {
if (startTest(codonList) && stopTest(codonList) && mNCTest(codonList) && percentageTest(percentages)) {
out.println(\"Is Protein?: YES\");
// when print to file, print to the console as well
System.out.println(\"Is Protein?: YES\");
} else {
out.println(\"Is Protein?: NO\");
// when print to file, print to the console as well
System.out.println(\"Is Protein?: NO\");
}
out.println();
// when print to file, print to the console as well
System.out.println();
}
//This method returns \"true\" if the first codon is a valid start codon (\"ATG\").
//Accepts string list of codons as parameter.
public static boolean startTest(String codonList) {
return(codonList.substring(1, 4).equals(\"ATG\"));
}
//This method returns \"true\" if the last codon is a valid stop codon. (\"TAA\", \"TAG\", or \"TGA\").
//Accepts string list of codons as parameter.
public static boolean stopTest(String codonList) {
String EndC = codonList.substring(codonList.length() - 4, codonList.length() - 1);
return (EndC.equals(\"TAA\") || EndC.equals(\"TAG\") || EndC.equals(\"TGA\"));
}
//This method returns \"true\" if there are 5 or more codons in the sequence. Does so by analyzing
//the codon string to determine if a 5th data set exists. Accepts Str list of codons as parameter.
public static boolean mNCTest(String codonList) {
return(codonList.charAt(19) == \',\');
}
//This method returns \"true\" if the combined mass percentages of nucleotide \"C\" and \"G\" are equal
//to or greater than 30.0% of total mass. Accepts a double array of nucleotide mass percentages.
public static boolean percentageTest(double[] percentages) {
return(percentages[1] + percentages[2] >= 30.0);
}
}






