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.StringUtil;
import net.sf.samtools.util.RuntimeIOException;
import org.apache.log4j.Logger;
/**
* Implementation of ReferenceSequenceFile for reading from FASTA files.
@ -30,6 +31,11 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
private SAMSequenceDictionary sequenceDictionary = 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
*/
@ -129,9 +135,9 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
assert contig1Rec != null : "Contig2 record is null: " + contig2;
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(),
contig2, contig2Rec.getSequenceIndex());
contig2, contig2Rec.getSequenceIndex()));
int startIndex = Math.min(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);
bytesToTraverse += rec.getSequenceLength();
if ( DEBUG )
System.out.printf(" -> 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);
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));
}
if ( contig1Rec.getSequenceIndex() > contig2Rec.getSequenceIndex() )
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;
}
@ -180,13 +186,13 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
* @return true on success
*/
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 nextContig = null;
if ( curContig == null ) {
System.out.printf("CurrentContig is null");
logger.info(String.format("CurrentContig is null"));
if ( this.sequenceDictionary == null )
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
}
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
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
*/
public ReferenceSequence nextSequence() {
if ( DEBUG ) System.out.printf("Calling nextSequence()%n");
if ( DEBUG ) logger.debug(String.format("Calling nextSequence()%n"));
// Read the header line
currentContigName = getNextContigName();
@ -309,8 +315,8 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
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 ) System.out.printf(" => nextSequence() next is %s%n", this.getNextContigName());
if ( DEBUG ) logger.debug(String.format(" => nextSequence() is returning %s, known length = %d%n", this.currentContigName, knownLength));
if ( DEBUG ) logger.debug(String.format(" => nextSequence() next is %s%n", this.getNextContigName()));
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
*/
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 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();
}
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;
}
@ -400,7 +406,7 @@ public class FastaSequenceFile2 implements ReferenceSequenceFile {
if ( foundIndex == ourIndex ) {
// we found our target!
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;
}
else if ( foundIndex <= ourIndex )

View File

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

View File

@ -1,6 +1,7 @@
package org.broadinstitute.sting.utils;
import org.broadinstitute.sting.utils.GenomeLoc;
import org.apache.log4j.Logger;
import java.util.List;
import java.util.ArrayList;
@ -8,9 +9,9 @@ import java.util.Arrays;
/**
* Hanging data off the reference sequence
*
* <p/>
* Supports in effect the following data structure
*
* <p/>
* <-- reference bases: A T G C -->
* d d d d
* d d d d
@ -18,7 +19,7 @@ import java.util.Arrays;
* d d d
* d d
* d
*
* <p/>
* 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
* randomly accessing data anywhere within window.
@ -32,6 +33,11 @@ public class RefHanger<T> {
// -----------------------------------------------------------------------------------------------------------------
ArrayList<Hanger> hangers;
/**
* our log, which we want to capture anything from this class
*/
private static Logger logger = Logger.getLogger(RefHanger.class);
// -----------------------------------------------------------------------------------------------------------------
//
// Info structure
@ -46,9 +52,17 @@ public class RefHanger<T> {
this.data = data;
}
public final ArrayList<T> getData() { return data; }
public final int size() { return this.data.size(); }
public final T get(int i) { return this.data.get(i); }
public final ArrayList<T> getData() {
return data;
}
public final int size() {
return this.data.size();
}
public final T get(int i) {
return this.data.get(i);
}
}
@ -66,15 +80,26 @@ public class RefHanger<T> {
//System.out.printf("leftLoc is %s%n", getLeftLoc());
}
protected int getLeftOffset() { return 0; }
protected int getRightOffset() { return hangers.size() - 1; }
protected int getLeftOffset() {
return 0;
}
protected int getRightOffset() {
return hangers.size() - 1;
}
protected int getOffset(GenomeLoc loc) {
//System.out.printf("Loc: %s vs %s%n", loc, getLeftLoc());
return loc.minus(getLeftLoc());
}
public GenomeLoc getLeftLoc() { return hangers.get(getLeftOffset()).loc; }
public GenomeLoc getRightLoc() { return hangers.get(getRightOffset()).loc; }
public GenomeLoc getLeftLoc() {
return hangers.get(getLeftOffset()).loc;
}
public GenomeLoc getRightLoc() {
return hangers.get(getRightOffset()).loc;
}
public boolean hasLocation(GenomeLoc loc) {
return !isEmpty() && loc.isBetween(getLeftLoc(), getRightLoc());
@ -83,6 +108,7 @@ public class RefHanger<T> {
public boolean isEmpty() {
return hangers.isEmpty();
}
public boolean hasHangers() {
return !isEmpty();
}
@ -193,15 +219,15 @@ public class RefHanger<T> {
}
public void printState() {
System.out.printf("Hanger:%n");
logger.info("Hanger: ");
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.
*
* <p/>
* 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
*

View File

@ -11,6 +11,8 @@ import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.BufferedReader;
import org.apache.log4j.Logger;
/**
* Created by IntelliJ IDEA.
* User: depristo
@ -19,25 +21,32 @@ import java.io.BufferedReader;
* To change this template use File | Settings | File Templates.
*/
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) {
System.out.printf("********************************************************************************%n");
System.out.printf("* WARNING:%n");
System.out.printf("*%n");
System.out.printf("* %s%n", msg);
System.out.printf("********************************************************************************%n");
logger.warn(String.format("********************************************************************************%n"));
logger.warn(String.format("* WARNING:%n"));
logger.warn(String.format("*%n"));
logger.warn(String.format("* %s%n", msg));
logger.warn(String.format("********************************************************************************%n"));
}
public static void scareUser(final String msg) {
System.out.printf("********************************************************************************%n");
System.out.printf("* ERROR:%n");
System.out.printf("*%n");
System.out.printf("* %s%n", msg);
System.out.printf("********************************************************************************%n");
logger.fatal(String.format("********************************************************************************%n"));
logger.fatal(String.format("* ERROR:%n"));
logger.fatal(String.format("*%n"));
logger.fatal(String.format("* %s%n", msg));
logger.fatal(String.format("********************************************************************************%n"));
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 ).
*
* @param pred filtering condition ( objects, for which pred.apply() is true pass the filter )
* @param c collection to filter (will not be modified)
* @return new list built from elements of <c> passing the filter
@ -56,7 +65,8 @@ public class Utils {
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
* 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
@ -67,6 +77,7 @@ public class Utils {
* 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,
* 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 c collection to filter (will be modified - should be mutable and should implement remove() )
* @return reference to the same (modified) collection <c>
@ -213,8 +224,13 @@ public class Utils {
return (1.0 * sum) / i;
}
public static double average(List<Long> vals) { return average(vals, vals.size()); }
public static double averageDouble(List<Double> vals) { return averageDouble(vals, vals.size()); }
public static double average(List<Long> vals) {
return average(vals, vals.size());
}
public static double averageDouble(List<Double> vals) {
return averageDouble(vals, vals.size());
}
public static boolean setupRefContigOrdering(final ReferenceSequenceFile refFile) {
final SAMSequenceDictionary seqDict = refFile.getSequenceDictionary();
@ -227,13 +243,13 @@ public class Utils {
if (refContigs != null) {
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) {
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);
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);
@ -242,62 +258,62 @@ public class Utils {
// Java Generics can't do primitive types, so I had to do this the simplistic way
public static Integer[] SortPermutation(final int[] A)
{
class comparator implements Comparator
{
public int compare(Object a, Object b)
{
if (A[(Integer)a] < A[(Integer)b]) { return -1; }
if (A[(Integer)a] == A[(Integer)b]) { return 0; }
if (A[(Integer)a] > A[(Integer)b]) { return 1; }
public static Integer[] SortPermutation(final int[] A) {
class comparator implements Comparator {
public int compare(Object a, Object b) {
if (A[(Integer) a] < A[(Integer) b]) {
return -1;
}
if (A[(Integer) a] == A[(Integer) b]) {
return 0;
}
if (A[(Integer) a] > A[(Integer) b]) {
return 1;
}
return 0;
}
}
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;
}
Arrays.sort(permutation, new comparator());
return permutation;
}
public static Integer[] SortPermutation(final double[] A)
{
class comparator implements Comparator
{
public int compare(Object a, Object b)
{
if (A[(Integer)a] < A[(Integer)b]) { return -1; }
if (A[(Integer)a] == A[(Integer)b]) { return 0; }
if (A[(Integer)a] > A[(Integer)b]) { return 1; }
public static Integer[] SortPermutation(final double[] A) {
class comparator implements Comparator {
public int compare(Object a, Object b) {
if (A[(Integer) a] < A[(Integer) b]) {
return -1;
}
if (A[(Integer) a] == A[(Integer) b]) {
return 0;
}
if (A[(Integer) a] > A[(Integer) b]) {
return 1;
}
return 0;
}
}
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;
}
Arrays.sort(permutation, new comparator());
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();
class comparator implements Comparator<Integer>
{
public int compare(Integer a, Integer b)
{
class comparator implements Comparator<Integer> {
public int compare(Integer a, Integer b) {
return ((T) data[a]).compareTo(data[b]);
}
}
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;
}
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];
for (int i = 0; i < output.length; i++)
{
for (int i = 0; i < output.length; i++) {
output[i] = array[permutation[i]];
}
return output;
}
public static double[] PermuteArray(double[] array, Integer[] permutation)
{
public static double[] PermuteArray(double[] array, Integer[] permutation) {
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]];
}
return output;
}
public static Object[] PermuteArray(Object[] array, Integer[] permutation)
{
public static Object[] PermuteArray(Object[] array, Integer[] permutation) {
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]];
}
return output;
}
public static String[] PermuteArray(String[] array, Integer[] permutation)
{
public static String[] PermuteArray(String[] array, Integer[] permutation) {
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]];
}
return output;