Advertisement
UrNotSorry

Computing GC Content - Without TreeMap

Jul 21st, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class ComputeGCContent {
  5.    
  6.     private static double getGCContent(String dna) {
  7.         int gc = 0;
  8.         for (char symbol : dna.toCharArray()) {
  9.             if (symbol == 'G' || symbol == 'C') {
  10.                 gc++;
  11.             }
  12.         }
  13.         return (double) gc / dna.length() * 100;
  14.     }
  15.  
  16.     public static void main(String[] args) throws IOException {
  17.        
  18.         Scanner in = new Scanner(new File("input.txt"));
  19.         PrintWriter out = new PrintWriter("output.txt");
  20.        
  21.         String maxID = "";
  22.         double maxGC = 0.0;
  23.        
  24.         while (in.hasNextLine()) {
  25.             String currentID = in.nextLine();
  26.             String currentDNA = "";
  27.             while (in.hasNextLine() && !in.hasNext(">.*")) {
  28.                 currentDNA += in.nextLine();
  29.             }
  30.             double currentGC = getGCContent(currentDNA);
  31.             if (currentGC > maxGC) {
  32.                 maxID = currentID;
  33.                 maxGC = currentGC;
  34.             }
  35.         }
  36.        
  37.         out.println(maxID + " " + maxGC);
  38.        
  39.         in.close();
  40.         out.close();
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement