Skip to content

Instantly share code, notes, and snippets.

@stephaniekwoods
Forked from cwake/Assign3.java
Last active June 26, 2018 03:18
Show Gist options
  • Save stephaniekwoods/172ee78cc90386cce6c6 to your computer and use it in GitHub Desktop.
Save stephaniekwoods/172ee78cc90386cce6c6 to your computer and use it in GitHub Desktop.
latest working version
import java.io.*;
import java.util.*;
public class stephsFork1 {
public static void intro() {//figure out what we want to say
System.out.println("This program reports information about DNA");
System.out.println("nucleotide sequences that may encode proteins.");
System.out.println("(such as Captain Picards hair growth protein)");
System.out.println("");
}
public static final int HOW_MANY_OF_EACH = 4;//AGCT-
public static final int NUCS_IN_CODON = 3;//groups of 3
public static final int PERCENT_MASS = 30;//min percent
public static final int ARE_THERE_ENOUGH_CODONS = 5;//min num of codons
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
intro();
processFile();
}
private static void processFile() throws FileNotFoundException {
// whatFile(console);
Scanner console = new Scanner(System.in);
File userFileAnswer = whatFile(console);
Scanner read1 = new Scanner(userFileAnswer);
String nLine = read1.nextLine();
String nName = (""); //insert name into quotes
PrintStream output = new PrintStream(output(console));
while (read1.hasNextLine()) {
nName = nLine;
String nucsInARow = read1.nextLine().toUpperCase();//here's Chloe's toUpperCase statement
int[] numberOfNucs = howManyNucs(nucsInARow);
double[] massP = massPercentage(numberOfNucs);
String [] codon = codon(nucsInARow);
boolean IsItAProtein = isItAProtein(codon, massP);
outputToFile(nName, nucsInARow, numberOfNucs, massP, codon,
output, IsItAProtein);
if (read1.hasNextLine()) {
nLine = read1.nextLine();
}
}
}
public static File whatFile(Scanner console) {//ask for file
System.out.print("Input file name: ");
File answer = new File(console.nextLine());
return answer;
}
public static File output(Scanner console) {//where to send output
System.out.print("Output file name: ");
File fileName = new File (console.nextLine());
return fileName;
}
public static int[] howManyNucs(String lineNucs) {//counting all the nucleotides at the same time
int[] nucCount = new int[HOW_MANY_OF_EACH];
for (int i = 0; i < lineNucs.length(); i++) {
int index = "ACGT".indexOf(lineNucs.charAt(i));
if (index >= 0) {
nucCount[index]++;
}
}
return nucCount;
}
public static double[] massPercentage (int[] nucCount) {
double[] mass = {135.128, 111.103, 151.128, 125.107};//total mass of string**not sure where to put the 'junk'
double[] massOfEach = new double[HOW_MANY_OF_EACH];//mass of each nuc
double[] massPercent = new double[HOW_MANY_OF_EACH];
double massSum = 0;
for (int m = 0; m < HOW_MANY_OF_EACH; m++) {
massOfEach[m] = ((nucCount[m]) * (mass[m]));
massSum = massOfEach[m]+ massSum;
}
for (int p = 0; p < HOW_MANY_OF_EACH; p++) {
massPercent[p] = Math.round((massOfEach[p] / massSum) * 1000.0)/10.0;//percent mass of each nuc
}
return massPercent;
}
public static String[] codon(String lineOfNucs) {//puts into groups of 3 (codons)
String[] codon = new String [(lineOfNucs.length() / NUCS_IN_CODON)];
int indexStart = 0;
for (int c = 0; c < codon.length; c++) {
codon[c] = lineOfNucs.substring(indexStart, (indexStart + NUCS_IN_CODON));
indexStart = indexStart + NUCS_IN_CODON;
}
return codon;
}
public static boolean isItAProtein(String[] codon, double[] massPercent) {
//is it a protein?
if (!codon[0].equals("ATG")) {//start with ATG
return false;
}
if ((!codon[codon.length-1].equals("TAA")) && (!codon[codon.length-1].equals("TAG")) &&
(!codon[codon.length-1].equals("TGA"))) {
return false;//end requirements
}
if (codon.length < ARE_THERE_ENOUGH_CODONS) {//is it long enough?
return false;
}
if ((massPercent[1] + massPercent[2]) < PERCENT_MASS) {
return false;//percentage mass check
}
return true;
} //output
public static void outputToFile (String nName, String nNucs, int[] nucCounts,
double[] massPercent,String[] codons, PrintStream output, boolean protein) {
output.println("Name: " + nName);
output.println("Nucleotides: " + nNucs);
output.println("Nucleotide counts: " + Arrays.toString(nucCounts));
output.println("Mass percentages: " + Arrays.toString(massPercent));
output.println("Codons: " + Arrays.toString(codons));
output.print("Encodes a protein: ");
if (protein == true) {
output.println("yes");
} else {
output.println("no");
}
output.println("");
}
}
@cwake
Copy link

cwake commented Jun 2, 2015

Only thing with this new part you added is that it doesn't have the processFile method that Stuart wrote in order for the file to be read.

Copy link

ghost commented Jun 2, 2015

I am updating it now with the processFile method, I noticed the model we're basing our code off has a lot going on in their main method so I'm going to streamline ours down and keep less work in main

@stephaniekwoods
Copy link
Author

As of now these two method calls:
intro();
processFile();
will run the program and save the output to a txt file. We still haven't figured out how to not calculate dashes - "junk" in the mass percentages

Copy link

ghost commented Jun 2, 2015

Ok that was weird, the last comment was by me but apparently Stephanie was still logged into my laptop. I'm back to being Stuart now

@cwake
Copy link

cwake commented Jun 3, 2015

It's only asking for the input file name and output file name for me and then doesn't do anything. :/

@cwake
Copy link

cwake commented Jun 3, 2015

I was a dummy and didn't see it was creating the file in my documents! P.S. I fixed the junk output and updated the code on my original!

Copy link

ghost commented Jun 3, 2015

That's cool, I didn't know it would create the file for you I thought we had to have a .txt file already saved before hand

@jsanders89
Copy link

Hello,

My name is Jonathan and I work with the introductory computer science courses at the University of Washington. This post contains solution code to a homework assignment that was adapted from one of our own.

This post's code is indexed by Google, enabling current students to easily find it and cheat on their assignments.

Would you please remove this post or make it secret in order to help us enforce our academic policy?

By removing this post, you would be removing a tool that our students could use in academic misconduct.

Thank you,
Jonathan Sanders
Intro Support, Computer Science & Engineering
University of Washington
jsanders@cs.washington.edu

More info on our course: http://courses.cs.washington.edu/courses/cse142/15au/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment