From d11bb0fc64cd3f11159d6df4cb6cda89c454b903 Mon Sep 17 00:00:00 2001 From: depristo Date: Wed, 25 Mar 2009 15:17:38 +0000 Subject: [PATCH] Added xReadLines class to utils. It is a iterator and iterable so you can easily read all lines from a file. It's been used to simplify the code to process intervals, and will be used to add merging data support to the system... git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@187 348d0f76-0448-11de-a6fe-93d51630548a --- .../sting/gatk/TraversalEngine.java | 39 ++----- .../org/broadinstitute/sting/utils/Utils.java | 4 + .../sting/utils/xReadLines.java | 100 ++++++++++++++++++ 3 files changed, 115 insertions(+), 28 deletions(-) create mode 100755 java/src/org/broadinstitute/sting/utils/xReadLines.java diff --git a/java/src/org/broadinstitute/sting/gatk/TraversalEngine.java b/java/src/org/broadinstitute/sting/gatk/TraversalEngine.java index 2f90eef0d..c835dd1ed 100755 --- a/java/src/org/broadinstitute/sting/gatk/TraversalEngine.java +++ b/java/src/org/broadinstitute/sting/gatk/TraversalEngine.java @@ -21,10 +21,7 @@ import org.broadinstitute.sting.gatk.refdata.ReferenceOrderedData; import org.broadinstitute.sting.gatk.refdata.ReferenceOrderedDatum; import org.broadinstitute.sting.gatk.walkers.LocusWalker; import org.broadinstitute.sting.gatk.walkers.ReadWalker; -import org.broadinstitute.sting.utils.FastaSequenceFile2; -import org.broadinstitute.sting.utils.FileProgressTracker; -import org.broadinstitute.sting.utils.GenomeLoc; -import org.broadinstitute.sting.utils.Utils; +import org.broadinstitute.sting.utils.*; import java.io.*; import java.util.*; @@ -187,32 +184,17 @@ public class TraversalEngine { * @param file_name */ public void setLocationFromFile(final String file_name) { - StringBuilder locStr = new StringBuilder(); - - Scanner scanner = null; try { - scanner = new Scanner(new File(file_name)); - while (scanner.hasNextLine()) { - String line = scanner.nextLine(); - line.replaceAll("\n", ""); - locStr.append(line); - if (scanner.hasNextLine()) { - locStr.append(";"); - } - } - } - catch (Exception e) { + xReadLines reader = new xReadLines(new File(file_name)); + List lines = reader.readLines(); + reader.close(); + String locStr = Utils.join(";", lines); + logger.debug("locStr: " + locStr); + this.locs = parseGenomeLocs(locStr); + } catch (Exception e) { e.printStackTrace(); System.exit(-1); } - finally { - //ensure the underlying stream is always closed - scanner.close(); - } - - logger.debug("DEBUG: locStr: " + locStr.toString()); - - this.locs = parseGenomeLocs(locStr.toString()); } /** @@ -233,7 +215,8 @@ public class TraversalEngine { Collection result = Functions.map(f1, Arrays.asList(str.split(";"))); GenomeLoc[] locs = (GenomeLoc[]) result.toArray(new GenomeLoc[0]); Arrays.sort(locs); - System.out.println(" Locations are: " + Utils.join("\n", Functions.map(Operators.toString, Arrays.asList(locs)))); + logger.info(String.format("Going to process %d locations", locs.length)); + //System.out.println(" Locations are: " + Utils.join("\n", Functions.map(Operators.toString, Arrays.asList(locs)))); return locs; } catch (Exception e) { logger.fatal(String.format("Invalid locations string: %s, format is loc1;loc2; where each locN can be 'chr2', 'chr2:1000000' or 'chr2:1,000,000-2,000,000'", str)); @@ -339,7 +322,7 @@ public class TraversalEngine { */ protected void printOnTraversalDone(final String type, T sum) { printProgress(true, type, null); - System.out.printf(String.format("Traversal reduce result is %d%n", sum)); // TODO: fixme -- how do we use this logger? + System.out.println("Traversal reduce result is " + sum); // TODO: fixme -- how do we use this logger? logger.info(String.format("Traversal skipped %d reads out of %d total (%.2f%%)", nSkippedReads, nReads, (nSkippedReads * 100.0) / nReads)); logger.info(String.format(" -> %d unmapped reads", nUnmappedReads)); logger.info(String.format(" -> %d non-primary reads", nNotPrimary)); diff --git a/java/src/org/broadinstitute/sting/utils/Utils.java b/java/src/org/broadinstitute/sting/utils/Utils.java index fe8aefe84..3551b6d88 100755 --- a/java/src/org/broadinstitute/sting/utils/Utils.java +++ b/java/src/org/broadinstitute/sting/utils/Utils.java @@ -6,6 +6,10 @@ import net.sf.samtools.SAMSequenceDictionary; import edu.mit.broad.picard.reference.ReferenceSequenceFile; import java.util.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.BufferedReader; /** * Created by IntelliJ IDEA. diff --git a/java/src/org/broadinstitute/sting/utils/xReadLines.java b/java/src/org/broadinstitute/sting/utils/xReadLines.java new file mode 100755 index 000000000..8ec51e4e6 --- /dev/null +++ b/java/src/org/broadinstitute/sting/utils/xReadLines.java @@ -0,0 +1,100 @@ +package org.broadinstitute.sting.utils; + +import java.util.Iterator; +import java.util.List; +import java.util.LinkedList; +import java.io.*; + +/** + * Created by IntelliJ IDEA. + * User: depristo + * Date: Mar 25, 2009 + * Time: 10:46:07 AM + * To change this template use File | Settings | File Templates. + */ +public class xReadLines implements Iterator, Iterable { + BufferedReader in; // The stream we're reading from + String nextline = null; // Return value of next call to next() + + public xReadLines(final File filename) throws FileNotFoundException { + // Open the file and read and remember the first line. + // We peek ahead like this for the benefit of hasNext(). + this(new FileReader(filename)); + } + + public xReadLines(final FileReader fileReader) throws FileNotFoundException { + // Open the file and read and remember the first line. + // We peek ahead like this for the benefit of hasNext(). + this(new BufferedReader(fileReader)); + } + + public xReadLines(final InputStream inputStream) throws FileNotFoundException { + // Open the file and read and remember the first line. + // We peek ahead like this for the benefit of hasNext(). + this(new BufferedReader(new InputStreamReader(inputStream))); + } + + public xReadLines(final BufferedReader in) throws FileNotFoundException { + // Open the file and read and remember the first line. + // We peek ahead like this for the benefit of hasNext(). + try { + this.in = in; + nextline = readNextLine(); + } catch(IOException e) { + throw new IllegalArgumentException(e); + } + } + + public List readLines() { + List lines = new LinkedList(); + for ( String line : this ) { + lines.add(line); + } + return lines; + } + + public Iterator iterator() { + return this; + } + + // If the next line is non-null, then we have a next line + public boolean hasNext() { + return nextline != null; + } + + private String readNextLine() throws IOException { + String nextline = in.readLine(); // Read another line + if (nextline == null) + return null; + else + nextline = nextline.trim(); + return nextline; + } + + // Return the next line, but first read the line that follows it. + public String next() { + try { + String result = nextline; + nextline = readNextLine(); + + // If we haven't reached EOF yet + if (nextline == null) { + in.close(); // And close on EOF + } + + // Return the line we read last time through. + return result; + } catch(IOException e) { + throw new IllegalArgumentException(e); + } + } + + // The file is read-only; we don't allow lines to be removed. + public void remove() { + throw new UnsupportedOperationException(); + } + + public void close() throws IOException { + this.in.close(); + } +}