moved a bunch of files over to the logging system. In some cases I ballparked the severity level of an error, so if you see something wrong feel free to make changes.

git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@211 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
aaron 2009-03-27 14:02:55 +00:00
parent 826781a760
commit 230c1ad161
4 changed files with 220 additions and 172 deletions

View File

@ -14,6 +14,7 @@ import net.sf.samtools.SAMSequenceRecord;
import net.sf.samtools.util.AsciiLineReader; import net.sf.samtools.util.AsciiLineReader;
import net.sf.samtools.util.StringUtil; import net.sf.samtools.util.StringUtil;
import net.sf.samtools.util.RuntimeIOException; import net.sf.samtools.util.RuntimeIOException;
import org.apache.log4j.Logger;
/** /**
* Implementation of ReferenceSequenceFile for reading from FASTA files. * Implementation of ReferenceSequenceFile for reading from FASTA files.
@ -30,6 +31,11 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
private SAMSequenceDictionary sequenceDictionary = null; private SAMSequenceDictionary sequenceDictionary = null;
private String currentContigName = null; private String currentContigName = null;
/**
* our log, which we want to capture anything from this class
*/
private static Logger logger = Logger.getLogger(FastaSequenceFile2.class);
/** /**
* Set to true to see lots of debugging output during operation * Set to true to see lots of debugging output during operation
*/ */
@ -129,9 +135,9 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
assert contig1Rec != null : "Contig2 record is null: " + contig2; assert contig1Rec != null : "Contig2 record is null: " + contig2;
if ( DEBUG ) if ( DEBUG )
System.out.printf("Contig1=(%s, %d), contig2=(%s, %d)%n", logger.debug(String.format("Contig1=(%s, %d), contig2=(%s, %d)%n",
contig1, contig1Rec.getSequenceIndex(), contig1, contig1Rec.getSequenceIndex(),
contig2, contig2Rec.getSequenceIndex()); contig2, contig2Rec.getSequenceIndex()));
int startIndex = Math.min(contig1Rec.getSequenceIndex(), contig2Rec.getSequenceIndex()); int startIndex = Math.min(contig1Rec.getSequenceIndex(), contig2Rec.getSequenceIndex());
int lastIndex = Math.max(contig1Rec.getSequenceIndex(), contig2Rec.getSequenceIndex()); int lastIndex = Math.max(contig1Rec.getSequenceIndex(), contig2Rec.getSequenceIndex());
@ -141,14 +147,14 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
SAMSequenceRecord rec = seqDict.getSequence(i); SAMSequenceRecord rec = seqDict.getSequence(i);
bytesToTraverse += rec.getSequenceLength(); bytesToTraverse += rec.getSequenceLength();
if ( DEBUG ) if ( DEBUG )
System.out.printf(" -> Traversing from %15s to %15s requires reading at least %10d bytes to pass contig %15s, total bytes %10d%n", logger.debug(String.format(" -> Traversing from %15s to %15s requires reading at least %10d bytes to pass contig %15s, total bytes %10d%n",
contig1, contig2, rec.getSequenceLength(), rec.getSequenceName(), bytesToTraverse); contig1, contig2, rec.getSequenceLength(), rec.getSequenceName(), bytesToTraverse));
} }
if ( contig1Rec.getSequenceIndex() > contig2Rec.getSequenceIndex() ) if ( contig1Rec.getSequenceIndex() > contig2Rec.getSequenceIndex() )
bytesToTraverse *= -1; // we are going backward! bytesToTraverse *= -1; // we are going backward!
if ( DEBUG ) System.out.printf(" -> total distance is %d%n", bytesToTraverse); if ( DEBUG ) logger.debug(String.format(" -> total distance is %d%n", bytesToTraverse));
return bytesToTraverse; return bytesToTraverse;
} }
@ -180,13 +186,13 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
* @return true on success * @return true on success
*/ */
public boolean seekToContig(final String seekContig, boolean enableBacktracking ) { public boolean seekToContig(final String seekContig, boolean enableBacktracking ) {
if ( DEBUG ) System.out.printf("seekToContig( %s, %b )%n", seekContig, enableBacktracking); if ( DEBUG ) logger.debug(String.format("seekToContig( %s, %b )%n", seekContig, enableBacktracking));
String curContig = getContigName(); String curContig = getContigName();
String nextContig = null; String nextContig = null;
if ( curContig == null ) { if ( curContig == null ) {
System.out.printf("CurrentContig is null"); logger.info(String.format("CurrentContig is null"));
if ( this.sequenceDictionary == null ) if ( this.sequenceDictionary == null )
throw new PicardException( String.format("Seeking within contigs requires FASTA dictionary, but none was available for %s", this.file )); throw new PicardException( String.format("Seeking within contigs requires FASTA dictionary, but none was available for %s", this.file ));
@ -217,7 +223,7 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
return false; // we're not going backwards just yet return false; // we're not going backwards just yet
} }
else { else {
if ( DEBUG ) System.out.printf("Going to seek to contig %s with skip %d%n", seekContig, dist); if ( DEBUG ) logger.debug(String.format("Going to seek to contig %s with skip %d%n", seekContig, dist));
// we're actually going to jump somewhere, so prepare the state // we're actually going to jump somewhere, so prepare the state
this.nextContigName = null; // reset the contig info this.nextContigName = null; // reset the contig info
@ -256,7 +262,7 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
* @return null if there are no more sequences in the fasta stream * @return null if there are no more sequences in the fasta stream
*/ */
public ReferenceSequence nextSequence() { public ReferenceSequence nextSequence() {
if ( DEBUG ) System.out.printf("Calling nextSequence()%n"); if ( DEBUG ) logger.debug(String.format("Calling nextSequence()%n"));
// Read the header line // Read the header line
currentContigName = getNextContigName(); currentContigName = getNextContigName();
@ -309,8 +315,8 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
this.nextContigName = null; // we no longer know what the next contig name is this.nextContigName = null; // we no longer know what the next contig name is
if ( DEBUG ) System.out.printf(" => nextSequence() is returning %s, known length = %d%n", this.currentContigName, knownLength); if ( DEBUG ) logger.debug(String.format(" => nextSequence() is returning %s, known length = %d%n", this.currentContigName, knownLength));
if ( DEBUG ) System.out.printf(" => nextSequence() next is %s%n", this.getNextContigName()); if ( DEBUG ) logger.debug(String.format(" => nextSequence() next is %s%n", this.getNextContigName()));
return new ReferenceSequence(currentContigName, index, bases); return new ReferenceSequence(currentContigName, index, bases);
} }
@ -326,7 +332,7 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
* @return the name of the next contig, or null if there is no next contig * @return the name of the next contig, or null if there is no next contig
*/ */
public String getNextContigName() { public String getNextContigName() {
if ( DEBUG ) System.out.printf("getNextContigName() => %s%n", this.nextContigName); if ( DEBUG ) logger.debug(String.format("getNextContigName() => %s%n", this.nextContigName));
if ( this.nextContigName == null ) { if ( this.nextContigName == null ) {
// If it's not null, we've already looked up the next contig name, just return it and happily continue // If it's not null, we've already looked up the next contig name, just return it and happily continue
@ -334,7 +340,7 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
this.nextContigName = readNextContigName(); this.nextContigName = readNextContigName();
} }
if ( DEBUG ) System.out.printf("nextContigName is now %s%n", nextContigName); if ( DEBUG ) logger.debug(String.format("nextContigName is now %s%n", nextContigName));
return this.nextContigName; return this.nextContigName;
} }
@ -400,7 +406,7 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
if ( foundIndex == ourIndex ) { if ( foundIndex == ourIndex ) {
// we found our target! // we found our target!
this.nextContigName = foundContig; // store the right answer this.nextContigName = foundContig; // store the right answer
if ( DEBUG ) System.out.printf("seekForNextContig found %s%n", foundContig); if ( DEBUG ) logger.debug(String.format("seekForNextContig found %s%n", foundContig));
return foundContig; return foundContig;
} }
else if ( foundIndex <= ourIndex ) else if ( foundIndex <= ourIndex )

View File

@ -1,5 +1,7 @@
package org.broadinstitute.sting.utils; package org.broadinstitute.sting.utils;
import org.apache.log4j.Logger;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -43,6 +45,12 @@ public class FileProgressTracker<T> implements Iterator<T> {
this(file, it, channel, DEFAULT_HISTORY_SIZE); this(file, it, channel, DEFAULT_HISTORY_SIZE);
} }
/**
* our log, which we want to capture anything from this class
*/
private static Logger logger = Logger.getLogger(FileProgressTracker.class);
// ----------------------------------------------------------------- // -----------------------------------------------------------------
// //
// iterator support // iterator support
@ -126,18 +134,18 @@ public class FileProgressTracker<T> implements Iterator<T> {
} }
public void printStatus() { public void printStatus() {
System.out.printf("FileProgressTracker:%n"); logger.debug(String.format("FileProgressTracker:%n"));
System.out.printf(" -> File size is: %d%n", getFileSize()); logger.debug(String.format(" -> File size is: %d%n", getFileSize()));
System.out.printf(" -> Sampling depth: %d%n", historyI); logger.debug(String.format(" -> Sampling depth: %d%n", historyI));
System.out.printf(" -> File position: %d%n", getPosition()); logger.debug(String.format(" -> File position: %d%n", getPosition()));
System.out.printf(" -> Number of records processed: %d%n", nRecordsProcessed()); logger.debug(String.format(" -> Number of records processed: %d%n", nRecordsProcessed()));
System.out.printf(" -> Average record size is %d%n", averageRecordSize()); logger.debug(String.format(" -> Average record size is %d%n", averageRecordSize()));
System.out.printf(" -> Elapsed time in secs is %.2f%n", elapsedTimeInSecs()); logger.debug(String.format(" -> Elapsed time in secs is %.2f%n", elapsedTimeInSecs()));
System.out.printf(" -> Processing rate (records per second) %.2f%n", processingRate()); logger.debug(String.format(" -> Processing rate (records per second) %.2f%n", processingRate()));
System.out.printf(" -> Estimated number of records in file %d%n", estRecordsInFile()); logger.debug(String.format(" -> Estimated number of records in file %d%n", estRecordsInFile()));
System.out.printf(" -> Estimated percent progress through file %.2f%n", estFractionProgressThroughFile() * 100.0); logger.debug(String.format(" -> Estimated percent progress through file %.2f%n", estFractionProgressThroughFile() * 100.0));
System.out.printf(" -> Estimated time for entire processing %.2f hrs / %.2f min / %.2f sec%n", estTimeTotal() / (60*60), estTimeTotal() / (60), estTimeTotal()); logger.debug(String.format(" -> Estimated time for entire processing %.2f hrs / %.2f min / %.2f sec%n", estTimeTotal() / (60*60), estTimeTotal() / (60), estTimeTotal()));
System.out.printf(" -> Estimated time remaining %.2f hrs / %.2f min / %.2f sec%n", estTimeRemaining() / (60*60), estTimeRemaining() / 60, estTimeRemaining()); logger.debug(String.format(" -> Estimated time remaining %.2f hrs / %.2f min / %.2f sec%n", estTimeRemaining() / (60*60), estTimeRemaining() / 60, estTimeRemaining()));
} }
public String progressMeter() { public String progressMeter() {

View File

@ -1,6 +1,7 @@
package org.broadinstitute.sting.utils; package org.broadinstitute.sting.utils;
import org.broadinstitute.sting.utils.GenomeLoc; import org.broadinstitute.sting.utils.GenomeLoc;
import org.apache.log4j.Logger;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
@ -8,20 +9,20 @@ import java.util.Arrays;
/** /**
* Hanging data off the reference sequence * Hanging data off the reference sequence
* * <p/>
* Supports in effect the following data structure * Supports in effect the following data structure
* * <p/>
* <-- reference bases: A T G C --> * <-- reference bases: A T G C -->
* d d d d * d d d d
* d d d d * d d d d
* d d d d * d d d d
* d d d * d d d
* d d * d d
* d * d
* * <p/>
* Where the little d's are data associated with each position in the reference. * Where the little d's are data associated with each position in the reference.
* Supports adding and removing data to either side of the data structure, as well as * Supports adding and removing data to either side of the data structure, as well as
* randomly accessing data anywhere within window. * randomly accessing data anywhere within window.
*/ */
public class RefHanger<T> { public class RefHanger<T> {
@ -32,6 +33,11 @@ public class RefHanger<T> {
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
ArrayList<Hanger> hangers; ArrayList<Hanger> hangers;
/**
* our log, which we want to capture anything from this class
*/
private static Logger logger = Logger.getLogger(RefHanger.class);
// ----------------------------------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------------------------------
// //
// Info structure // Info structure
@ -40,15 +46,23 @@ public class RefHanger<T> {
public class Hanger { public class Hanger {
public GenomeLoc loc = null; public GenomeLoc loc = null;
public ArrayList<T> data = null; public ArrayList<T> data = null;
public Hanger(GenomeLoc loc, ArrayList<T> data) { public Hanger(GenomeLoc loc, ArrayList<T> data) {
this.loc = loc; this.loc = loc;
this.data = data; this.data = data;
} }
public final ArrayList<T> getData() { return data; } public final ArrayList<T> getData() {
public final int size() { return this.data.size(); } return data;
public final T get(int i) { return this.data.get(i); } }
public final int size() {
return this.data.size();
}
public final T get(int i) {
return this.data.get(i);
}
} }
@ -66,25 +80,37 @@ public class RefHanger<T> {
//System.out.printf("leftLoc is %s%n", getLeftLoc()); //System.out.printf("leftLoc is %s%n", getLeftLoc());
} }
protected int getLeftOffset() { return 0; } protected int getLeftOffset() {
protected int getRightOffset() { return hangers.size() - 1; } return 0;
}
protected int getRightOffset() {
return hangers.size() - 1;
}
protected int getOffset(GenomeLoc loc) { protected int getOffset(GenomeLoc loc) {
//System.out.printf("Loc: %s vs %s%n", loc, getLeftLoc()); //System.out.printf("Loc: %s vs %s%n", loc, getLeftLoc());
return loc.minus(getLeftLoc()); return loc.minus(getLeftLoc());
} }
public GenomeLoc getLeftLoc() { return hangers.get(getLeftOffset()).loc; } public GenomeLoc getLeftLoc() {
public GenomeLoc getRightLoc() { return hangers.get(getRightOffset()).loc; } return hangers.get(getLeftOffset()).loc;
}
public GenomeLoc getRightLoc() {
return hangers.get(getRightOffset()).loc;
}
public boolean hasLocation(GenomeLoc loc) { public boolean hasLocation(GenomeLoc loc) {
return ! isEmpty() && loc.isBetween(getLeftLoc(), getRightLoc()); return !isEmpty() && loc.isBetween(getLeftLoc(), getRightLoc());
} }
public boolean isEmpty() { public boolean isEmpty() {
return hangers.isEmpty(); return hangers.isEmpty();
} }
public boolean hasHangers() { public boolean hasHangers() {
return ! isEmpty(); return !isEmpty();
} }
/** /**
@ -151,7 +177,7 @@ public class RefHanger<T> {
public void pushLeft(GenomeLoc pos, ArrayList<T> data) { public void pushLeft(GenomeLoc pos, ArrayList<T> data) {
hangers.add(0, new Hanger(pos, data)); hangers.add(0, new Hanger(pos, data));
} }
public void pushRight(GenomeLoc pos) { public void pushRight(GenomeLoc pos) {
pushRight(pos, new ArrayList<T>()); pushRight(pos, new ArrayList<T>());
} }
@ -165,7 +191,7 @@ public class RefHanger<T> {
} }
public boolean ensurePos(GenomeLoc pos) { public boolean ensurePos(GenomeLoc pos) {
if ( hasLocation(pos) ) if (hasLocation(pos))
return true; return true;
else { else {
pushRight(pos); pushRight(pos);
@ -178,9 +204,9 @@ public class RefHanger<T> {
} }
public void addData(List<GenomeLoc> positions, List<T> dataByPos) { public void addData(List<GenomeLoc> positions, List<T> dataByPos) {
assert( positions.size() == dataByPos.size() ); assert (positions.size() == dataByPos.size());
for ( int i = 0; i < positions.size(); i++ ) { for (int i = 0; i < positions.size(); i++) {
GenomeLoc pos = positions.get(i); GenomeLoc pos = positions.get(i);
T datum = dataByPos.get(i); T datum = dataByPos.get(i);
expandingPut1(pos, datum); expandingPut1(pos, datum);
@ -193,15 +219,15 @@ public class RefHanger<T> {
} }
public void printState() { public void printState() {
System.out.printf("Hanger:%n"); logger.info("Hanger: ");
for ( Hanger hanger : hangers ) { for (Hanger hanger : hangers) {
System.out.printf(" -> %s => %s:%n", hanger.loc, Utils.join("/", hanger.data) ); logger.info(String.format(" -> %s => %s:%n", hanger.loc, Utils.join("/", hanger.data)));
} }
} }
/** /**
* Pushes locations on the right until we reach the expected position for pos. * Pushes locations on the right until we reach the expected position for pos.
* * <p/>
* For example, if we have chr1:1 and 2 in the hanger, and we push 4 into the hangers * For example, if we have chr1:1 and 2 in the hanger, and we push 4 into the hangers
* this function will add 3 -> {} to the hanger too * this function will add 3 -> {} to the hanger too
* *
@ -211,14 +237,14 @@ public class RefHanger<T> {
public void expandingPut(GenomeLoc pos, T datum) { public void expandingPut(GenomeLoc pos, T datum) {
//System.out.printf("expandingPut(%s, %s)%n", pos, datum); //System.out.printf("expandingPut(%s, %s)%n", pos, datum);
//printState(); //printState();
if ( isEmpty() ) if (isEmpty())
// we have nothing, just push right // we have nothing, just push right
pushRight(pos, datum); pushRight(pos, datum);
else { else {
//assert pos.compareTo(getRightLoc()) == 1 : pos + " " + getRightLoc() + " => " + pos.compareTo(getRightLoc()); //assert pos.compareTo(getRightLoc()) == 1 : pos + " " + getRightLoc() + " => " + pos.compareTo(getRightLoc());
GenomeLoc nextRight = getRightLoc().nextLoc(); GenomeLoc nextRight = getRightLoc().nextLoc();
while ( pos.compareTo(nextRight) == 1 ) { while (pos.compareTo(nextRight) == 1) {
//printState(); //printState();
//System.out.printf(" *** Extending %s, heading for %s%n", nextRight, pos); //System.out.printf(" *** Extending %s, heading for %s%n", nextRight, pos);
ensurePos(nextRight); ensurePos(nextRight);

View File

@ -11,6 +11,8 @@ import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.BufferedReader; import java.io.BufferedReader;
import org.apache.log4j.Logger;
/** /**
* Created by IntelliJ IDEA. * Created by IntelliJ IDEA.
* User: depristo * User: depristo
@ -19,28 +21,35 @@ import java.io.BufferedReader;
* To change this template use File | Settings | File Templates. * To change this template use File | Settings | File Templates.
*/ */
public class Utils { public class Utils {
/**
* our log, which we want to capture anything from this class
*/
private static Logger logger = Logger.getLogger(FileProgressTracker.class);
public static void warnUser(final String msg) { public static void warnUser(final String msg) {
System.out.printf("********************************************************************************%n"); logger.warn(String.format("********************************************************************************%n"));
System.out.printf("* WARNING:%n"); logger.warn(String.format("* WARNING:%n"));
System.out.printf("*%n"); logger.warn(String.format("*%n"));
System.out.printf("* %s%n", msg); logger.warn(String.format("* %s%n", msg));
System.out.printf("********************************************************************************%n"); logger.warn(String.format("********************************************************************************%n"));
} }
public static void scareUser(final String msg) { public static void scareUser(final String msg) {
System.out.printf("********************************************************************************%n"); logger.fatal(String.format("********************************************************************************%n"));
System.out.printf("* ERROR:%n"); logger.fatal(String.format("* ERROR:%n"));
System.out.printf("*%n"); logger.fatal(String.format("*%n"));
System.out.printf("* %s%n", msg); logger.fatal(String.format("* %s%n", msg));
System.out.printf("********************************************************************************%n"); logger.fatal(String.format("********************************************************************************%n"));
throw new RuntimeException(msg); throw new RuntimeException(msg);
} }
/** Returns a new list built from those objects found in collection <c> that satisfy the /**
* Returns a new list built from those objects found in collection <c> that satisfy the
* predicate ( i.e. pred.apply() is true for the objects in th eresulting list ). * predicate ( i.e. pred.apply() is true for the objects in th eresulting list ).
* @param pred filtering condition ( objects, for which pred.apply() is true pass the filter ) *
* @param c collection to filter (will not be modified) * @param pred filtering condition ( objects, for which pred.apply() is true pass the filter )
* @return new list built from elements of <c> passing the filter * @param c collection to filter (will not be modified)
* @return new list built from elements of <c> passing the filter
* @see #filterInPlace(Predicate pred, Collection c) * @see #filterInPlace(Predicate pred, Collection c)
*/ */
public static <T> List<T> filter(Predicate pred, Collection<T> c) { public static <T> List<T> filter(Predicate pred, Collection<T> c) {
@ -56,7 +65,8 @@ public class Utils {
return filtered; return filtered;
} }
/** Removes from the collection <c> all the elements that do not pass the filter (i.e. those elements, /**
* Removes from the collection <c> all the elements that do not pass the filter (i.e. those elements,
* for which pred.apply() is false ). This is an in-place method - the argument is modified, and no new * for which pred.apply() is false ). This is an in-place method - the argument is modified, and no new
* objects are created/copied. Collection's iterator (as returned by iterator()) must implement * objects are created/copied. Collection's iterator (as returned by iterator()) must implement
* optional remove() interface method that allows multiple subsequent removals of elements from the * optional remove() interface method that allows multiple subsequent removals of elements from the
@ -67,27 +77,28 @@ public class Utils {
* with other, custom lists that 1) do not inherit (are not instanceof) from ArrayList and 2) do not implement * with other, custom lists that 1) do not inherit (are not instanceof) from ArrayList and 2) do not implement
* fast (constant time) remove() operation, the performance can degrade significantly (linear traversal times, * fast (constant time) remove() operation, the performance can degrade significantly (linear traversal times,
* e.g., linear removal ~ N^2). * e.g., linear removal ~ N^2).
*
* @param pred filtering condition (only elements, for which pred.apply() is true will be kept in the collection) * @param pred filtering condition (only elements, for which pred.apply() is true will be kept in the collection)
* @param c collection to filter (will be modified - should be mutable and should implement remove() ) * @param c collection to filter (will be modified - should be mutable and should implement remove() )
* @return reference to the same (modified) collection <c> * @return reference to the same (modified) collection <c>
* @see #filter(Predicate pred, Collection c) * @see #filter(Predicate pred, Collection c)
*/ */
public static <T> Collection<T> filterInPlace(Predicate pred, Collection<T> c) { public static <T> Collection<T> filterInPlace(Predicate pred, Collection<T> c) {
if ( c instanceof ArrayList ) { if (c instanceof ArrayList) {
// arraylists are a special case that we know how to process efficiently // arraylists are a special case that we know how to process efficiently
// (generic implementation below removes one element at a time and is not well suited // (generic implementation below removes one element at a time and is not well suited
// for ArrayLists // for ArrayLists
List<T> list = (List<T>)c; List<T> list = (List<T>) c;
int j = 0; // copy-to location int j = 0; // copy-to location
// perform one linear pass copying forward all elements that pass the filter, // perform one linear pass copying forward all elements that pass the filter,
// so that the head of the list is continuous sequence of such elements: // so that the head of the list is continuous sequence of such elements:
for ( int i = 0 ; i < list.size() ; i++ ) { for (int i = 0; i < list.size(); i++) {
// if object passes, copy it forward and increment j (=copy-to location); // if object passes, copy it forward and increment j (=copy-to location);
// otherwise keep the same copy-to location and move on to the next element // otherwise keep the same copy-to location and move on to the next element
if ( pred.apply(list.get(i)) ) list.set(j++,list.get(i)); if (pred.apply(list.get(i))) list.set(j++, list.get(i));
} }
// j now points to first unused copy-to location; elements 0...j-1 pass the filter // j now points to first unused copy-to location; elements 0...j-1 pass the filter
list.subList(j,list.size()).clear(); // remove tail of the list list.subList(j, list.size()).clear(); // remove tail of the list
} }
/* /*
// loop through all the elements in c // loop through all the elements in c
@ -100,8 +111,8 @@ public class Utils {
} }
*/ */
Iterator<T> it = c.iterator(); Iterator<T> it = c.iterator();
while ( it.hasNext() ) { while (it.hasNext()) {
if ( pred.apply(it.next() )) continue; if (pred.apply(it.next())) continue;
it.remove(); it.remove();
} }
return c; return c;
@ -110,10 +121,10 @@ public class Utils {
public static ArrayList<Byte> subseq(byte[] fullArray) { public static ArrayList<Byte> subseq(byte[] fullArray) {
return subseq(fullArray, 0, fullArray.length); return subseq(fullArray, 0, fullArray.length);
} }
public static ArrayList<Byte> subseq(byte[] fullArray, int start, int end) { public static ArrayList<Byte> subseq(byte[] fullArray, int start, int end) {
ArrayList<Byte> dest = new ArrayList<Byte>(end-start+1); ArrayList<Byte> dest = new ArrayList<Byte>(end - start + 1);
for ( int i = start; i < end; i++ ) { for (int i = start; i < end; i++) {
dest.add(fullArray[i]); dest.add(fullArray[i]);
} }
return dest; return dest;
@ -122,38 +133,38 @@ public class Utils {
public static String baseList2string(List<Byte> bases) { public static String baseList2string(List<Byte> bases) {
byte[] basesAsbytes = new byte[bases.size()]; byte[] basesAsbytes = new byte[bases.size()];
int i = 0; int i = 0;
for ( Byte b : bases ) { for (Byte b : bases) {
basesAsbytes[i] = b; basesAsbytes[i] = b;
i++; i++;
} }
return new String(basesAsbytes); return new String(basesAsbytes);
} }
public static GenomeLoc genomicLocationOf( final SAMRecord read ) { public static GenomeLoc genomicLocationOf(final SAMRecord read) {
return new GenomeLoc( read.getReferenceName(), read.getAlignmentStart() ); return new GenomeLoc(read.getReferenceName(), read.getAlignmentStart());
} }
private static final Map<Integer,String> readFlagNames private static final Map<Integer, String> readFlagNames
= new HashMap<Integer,String>(); = new HashMap<Integer, String>();
static { static {
readFlagNames.put(0x1, "Paired"); readFlagNames.put(0x1, "Paired");
readFlagNames.put(0x2, "Proper"); readFlagNames.put(0x2, "Proper");
readFlagNames.put(0x4, "Unmapped"); readFlagNames.put(0x4, "Unmapped");
readFlagNames.put(0x8, "MateUnmapped"); readFlagNames.put(0x8, "MateUnmapped");
readFlagNames.put(0x10, "Forward"); readFlagNames.put(0x10, "Forward");
//readFlagNames.put(0x20, "MateForward"); //readFlagNames.put(0x20, "MateForward");
readFlagNames.put(0x4, "FirstOfPair"); readFlagNames.put(0x4, "FirstOfPair");
readFlagNames.put(0x8, "SecondOfPair"); readFlagNames.put(0x8, "SecondOfPair");
readFlagNames.put(0x100, "NotPrimary"); readFlagNames.put(0x100, "NotPrimary");
readFlagNames.put(0x200, "NON-PF"); readFlagNames.put(0x200, "NON-PF");
readFlagNames.put(0x400, "Duplicate"); readFlagNames.put(0x400, "Duplicate");
} }
public static String readFlagsAsString(SAMRecord rec) { public static String readFlagsAsString(SAMRecord rec) {
String flags = ""; String flags = "";
for ( int flag : readFlagNames.keySet() ) { for (int flag : readFlagNames.keySet()) {
if ( ( rec.getFlags() & flag ) != 0 ) { if ((rec.getFlags() & flag) != 0) {
flags += readFlagNames.get(flag) + " "; flags += readFlagNames.get(flag) + " ";
} }
} }
@ -178,17 +189,17 @@ public class Utils {
public static <T> String join(String separator, Collection<T> objects) { public static <T> String join(String separator, Collection<T> objects) {
ArrayList<String> strs = new ArrayList<String>(); ArrayList<String> strs = new ArrayList<String>();
for ( Object x : objects ) for (Object x : objects)
strs.add(x.toString()); strs.add(x.toString());
return join( separator, strs.toArray(new String[0]) ); return join(separator, strs.toArray(new String[0]));
} }
public static double average(List<Long> vals, int maxI) { public static double average(List<Long> vals, int maxI) {
long sum = 0L; long sum = 0L;
int i = 0; int i = 0;
for ( long x : vals ) { for (long x : vals) {
if ( i > maxI ) if (i > maxI)
break; break;
sum += x; sum += x;
i++; i++;
@ -204,100 +215,105 @@ public class Utils {
double sum = 0.0; double sum = 0.0;
int i = 0; int i = 0;
for ( double x : vals ) { for (double x : vals) {
if ( i > maxI ) if (i > maxI)
break; break;
sum += x; sum += x;
i++; i++;
} }
return (1.0 * sum) / i; return (1.0 * sum) / i;
} }
public static double average(List<Long> vals) { return average(vals, vals.size()); } public static double average(List<Long> vals) {
public static double averageDouble(List<Double> vals) { return averageDouble(vals, vals.size()); } return average(vals, vals.size());
}
public static double averageDouble(List<Double> vals) {
return averageDouble(vals, vals.size());
}
public static boolean setupRefContigOrdering(final ReferenceSequenceFile refFile) { public static boolean setupRefContigOrdering(final ReferenceSequenceFile refFile) {
final SAMSequenceDictionary seqDict = refFile.getSequenceDictionary(); final SAMSequenceDictionary seqDict = refFile.getSequenceDictionary();
if ( seqDict == null ) // we couldn't load the reference dictionary if (seqDict == null) // we couldn't load the reference dictionary
return false; return false;
List<SAMSequenceRecord> refContigs = seqDict.getSequences(); List<SAMSequenceRecord> refContigs = seqDict.getSequences();
HashMap<String, Integer> refContigOrdering = new HashMap<String, Integer>(); HashMap<String, Integer> refContigOrdering = new HashMap<String, Integer>();
if ( refContigs != null ) { if (refContigs != null) {
int i = 0; int i = 0;
System.out.printf("Prepared reference sequence contig dictionary%n order ->"); logger.info(String.format("Prepared reference sequence contig dictionary%n order ->"));
for ( SAMSequenceRecord contig : refContigs ) { for (SAMSequenceRecord contig : refContigs) {
System.out.printf(" %s (%d bp)", contig.getSequenceName(), contig.getSequenceLength()); logger.info(String.format(" %s (%d bp)", contig.getSequenceName(), contig.getSequenceLength()));
refContigOrdering.put(contig.getSequenceName(), i); refContigOrdering.put(contig.getSequenceName(), i);
i++; i++;
} }
System.out.printf("%n Total elements -> %d%n", refContigOrdering.size()); logger.info(String.format("%n Total elements -> %d%n", refContigOrdering.size()));
} }
GenomeLoc.setContigOrdering(refContigOrdering); GenomeLoc.setContigOrdering(refContigOrdering);
return refContigs != null; return refContigs != null;
} }
// Java Generics can't do primitive types, so I had to do this the simplistic way // Java Generics can't do primitive types, so I had to do this the simplistic way
public static Integer[] SortPermutation(final int[] A) public static Integer[] SortPermutation(final int[] A) {
{ class comparator implements Comparator {
class comparator implements Comparator public int compare(Object a, Object b) {
{ if (A[(Integer) a] < A[(Integer) b]) {
public int compare(Object a, Object b) return -1;
{ }
if (A[(Integer)a] < A[(Integer)b]) { return -1; } if (A[(Integer) a] == A[(Integer) b]) {
if (A[(Integer)a] == A[(Integer)b]) { return 0; } return 0;
if (A[(Integer)a] > A[(Integer)b]) { return 1; } }
if (A[(Integer) a] > A[(Integer) b]) {
return 1;
}
return 0; return 0;
} }
} }
Integer[] permutation = new Integer[A.length]; Integer[] permutation = new Integer[A.length];
for (int i = 0; i < A.length; i++) for (int i = 0; i < A.length; i++) {
{
permutation[i] = i; permutation[i] = i;
} }
Arrays.sort(permutation, new comparator()); Arrays.sort(permutation, new comparator());
return permutation; return permutation;
} }
public static Integer[] SortPermutation(final double[] A) public static Integer[] SortPermutation(final double[] A) {
{ class comparator implements Comparator {
class comparator implements Comparator public int compare(Object a, Object b) {
{ if (A[(Integer) a] < A[(Integer) b]) {
public int compare(Object a, Object b) return -1;
{ }
if (A[(Integer)a] < A[(Integer)b]) { return -1; } if (A[(Integer) a] == A[(Integer) b]) {
if (A[(Integer)a] == A[(Integer)b]) { return 0; } return 0;
if (A[(Integer)a] > A[(Integer)b]) { return 1; } }
if (A[(Integer) a] > A[(Integer) b]) {
return 1;
}
return 0; return 0;
} }
} }
Integer[] permutation = new Integer[A.length]; Integer[] permutation = new Integer[A.length];
for (int i = 0; i < A.length; i++) for (int i = 0; i < A.length; i++) {
{
permutation[i] = i; permutation[i] = i;
} }
Arrays.sort(permutation, new comparator()); Arrays.sort(permutation, new comparator());
return permutation; return permutation;
} }
public static <T extends Comparable> Integer[] SortPermutation( List<T> A ) public static <T extends Comparable> Integer[] SortPermutation(List<T> A) {
{
final Object[] data = A.toArray(); final Object[] data = A.toArray();
class comparator implements Comparator<Integer> class comparator implements Comparator<Integer> {
{ public int compare(Integer a, Integer b) {
public int compare(Integer a, Integer b) return ((T) data[a]).compareTo(data[b]);
{
return ((T)data[a]).compareTo(data[b]);
} }
} }
Integer[] permutation = new Integer[A.size()]; Integer[] permutation = new Integer[A.size()];
for (int i = 0; i < A.size(); i++) for (int i = 0; i < A.size(); i++) {
{
permutation[i] = i; permutation[i] = i;
} }
Arrays.sort(permutation, new comparator()); Arrays.sort(permutation, new comparator());
@ -305,41 +321,33 @@ public class Utils {
} }
public static int[] PermuteArray(int[] array, Integer[] permutation) public static int[] PermuteArray(int[] array, Integer[] permutation) {
{
int[] output = new int[array.length]; int[] output = new int[array.length];
for (int i = 0; i < output.length; i++) for (int i = 0; i < output.length; i++) {
{
output[i] = array[permutation[i]]; output[i] = array[permutation[i]];
} }
return output; return output;
} }
public static double[] PermuteArray(double[] array, Integer[] permutation) public static double[] PermuteArray(double[] array, Integer[] permutation) {
{
double[] output = new double[array.length]; double[] output = new double[array.length];
for (int i = 0; i < output.length; i++) for (int i = 0; i < output.length; i++) {
{
output[i] = array[permutation[i]]; output[i] = array[permutation[i]];
} }
return output; return output;
} }
public static Object[] PermuteArray(Object[] array, Integer[] permutation) public static Object[] PermuteArray(Object[] array, Integer[] permutation) {
{
Object[] output = new Object[array.length]; Object[] output = new Object[array.length];
for (int i = 0; i < output.length; i++) for (int i = 0; i < output.length; i++) {
{
output[i] = array[permutation[i]]; output[i] = array[permutation[i]];
} }
return output; return output;
} }
public static String[] PermuteArray(String[] array, Integer[] permutation) public static String[] PermuteArray(String[] array, Integer[] permutation) {
{
String[] output = new String[array.length]; String[] output = new String[array.length];
for (int i = 0; i < output.length; i++) for (int i = 0; i < output.length; i++) {
{
output[i] = array[permutation[i]]; output[i] = array[permutation[i]];
} }
return output; return output;
@ -379,7 +387,7 @@ public class Utils {
} }
*/ */
} }