Added xReadLines class to utils. It is a iterator<string> and iterable<string> 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
This commit is contained in:
depristo 2009-03-25 15:17:38 +00:00
parent 4c29dca70d
commit d11bb0fc64
3 changed files with 115 additions and 28 deletions

View File

@ -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<String> 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<GenomeLoc> 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 <T> 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));

View File

@ -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.

View File

@ -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<String>, Iterable<String> {
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<String> readLines() {
List<String> lines = new LinkedList<String>();
for ( String line : this ) {
lines.add(line);
}
return lines;
}
public Iterator<String> 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();
}
}