extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/grid/PicardMetrics.java

Code
Comments
Other
Rev Date Author Line
7106 12 Apr 23 nicklas 1 package net.sf.basedb.reggie.grid;
7106 12 Apr 23 nicklas 2
7106 12 Apr 23 nicklas 3 import java.util.Arrays;
7106 12 Apr 23 nicklas 4 import java.util.Collections;
7106 12 Apr 23 nicklas 5 import java.util.List;
7106 12 Apr 23 nicklas 6
7106 12 Apr 23 nicklas 7 import net.sf.basedb.util.Values;
7106 12 Apr 23 nicklas 8
7106 12 Apr 23 nicklas 9 /**
7106 12 Apr 23 nicklas 10   Helper class for parsing metric files created by Picard.
7106 12 Apr 23 nicklas 11   @since 4.46
7106 12 Apr 23 nicklas 12 */
7106 12 Apr 23 nicklas 13 public class PicardMetrics 
7106 12 Apr 23 nicklas 14 {
7106 12 Apr 23 nicklas 15   
7106 12 Apr 23 nicklas 16   private final String[] lines;
7106 12 Apr 23 nicklas 17   private List<String> columnHeaders;
7106 12 Apr 23 nicklas 18   private int currentLine;
7106 12 Apr 23 nicklas 19   private String[] currentData;
7106 12 Apr 23 nicklas 20   
7106 12 Apr 23 nicklas 21   /**
7106 12 Apr 23 nicklas 22     Create a new parser. The file data is given as a string.
7106 12 Apr 23 nicklas 23     The parser will check the file data line by line until a
7106 12 Apr 23 nicklas 24     it finds a header line with all the given headers.
7106 12 Apr 23 nicklas 25     
7106 12 Apr 23 nicklas 26     Use next() to move to the next data line and then one of the
7106 12 Apr 23 nicklas 27     getInteger(), getLong(), etc. methods to get data from the columns.
7106 12 Apr 23 nicklas 28   */
7106 12 Apr 23 nicklas 29   public PicardMetrics(String fileData, String... headers)
7106 12 Apr 23 nicklas 30   {
7106 12 Apr 23 nicklas 31     this.lines = fileData.split("\n");
7106 12 Apr 23 nicklas 32     this.currentLine = 0;
7106 12 Apr 23 nicklas 33     this.columnHeaders = Collections.emptyList();
7106 12 Apr 23 nicklas 34     
7106 12 Apr 23 nicklas 35     while (currentLine < lines.length)
7106 12 Apr 23 nicklas 36     {
7106 12 Apr 23 nicklas 37       String[] cols = lines[currentLine].split("\t");
7106 12 Apr 23 nicklas 38       if (cols.length >= headers.length)
7106 12 Apr 23 nicklas 39       {
7106 12 Apr 23 nicklas 40         List<String> colsA = Arrays.asList(cols);
7106 12 Apr 23 nicklas 41         int matchedHeaders = 0;
7106 12 Apr 23 nicklas 42         for (String h : headers)
7106 12 Apr 23 nicklas 43         {
7106 12 Apr 23 nicklas 44           if (colsA.indexOf(h) == -1) break;
7106 12 Apr 23 nicklas 45           matchedHeaders++;
7106 12 Apr 23 nicklas 46         }
7106 12 Apr 23 nicklas 47         if (matchedHeaders == headers.length) 
7106 12 Apr 23 nicklas 48         {
7106 12 Apr 23 nicklas 49           columnHeaders = colsA;
7106 12 Apr 23 nicklas 50           break;
7106 12 Apr 23 nicklas 51         }
7106 12 Apr 23 nicklas 52       }
7106 12 Apr 23 nicklas 53       currentLine++;
7106 12 Apr 23 nicklas 54     }
7106 12 Apr 23 nicklas 55   }
7106 12 Apr 23 nicklas 56   
7106 12 Apr 23 nicklas 57   /**
7106 12 Apr 23 nicklas 58     Move to the next data line. 
7106 12 Apr 23 nicklas 59     @return TRUE if there is another line, FALSE if not
7106 12 Apr 23 nicklas 60   */
7106 12 Apr 23 nicklas 61   public boolean next()
7106 12 Apr 23 nicklas 62   {
7106 12 Apr 23 nicklas 63     currentLine++;
7106 12 Apr 23 nicklas 64     currentData = currentLine < lines.length ? lines[currentLine].split("\t") : null;
7106 12 Apr 23 nicklas 65     return currentData != null;
7106 12 Apr 23 nicklas 66   }
7106 12 Apr 23 nicklas 67   
7106 12 Apr 23 nicklas 68   /**
7106 12 Apr 23 nicklas 69     Get the line number of the current line. The first line
7106 12 Apr 23 nicklas 70     has number 0.
7106 12 Apr 23 nicklas 71   */
7106 12 Apr 23 nicklas 72   public int getCurrentLine()
7106 12 Apr 23 nicklas 73   {
7106 12 Apr 23 nicklas 74     return currentLine;
7106 12 Apr 23 nicklas 75   }
7106 12 Apr 23 nicklas 76   
7106 12 Apr 23 nicklas 77   /**
7106 12 Apr 23 nicklas 78     Get the value from the given column as an integer.
7106 12 Apr 23 nicklas 79   */
7106 12 Apr 23 nicklas 80   public Integer getInteger(String col, Integer defaultValue)
7106 12 Apr 23 nicklas 81   {
7106 12 Apr 23 nicklas 82     int index = columnHeaders.indexOf(col);
7106 12 Apr 23 nicklas 83     return Values.getInteger(currentData[index], defaultValue);
7106 12 Apr 23 nicklas 84   }
7106 12 Apr 23 nicklas 85
7106 12 Apr 23 nicklas 86   /**
7106 12 Apr 23 nicklas 87     Get the value from the given column as a long.
7106 12 Apr 23 nicklas 88   */
7106 12 Apr 23 nicklas 89   public Long getLong(String col, Long defaultValue)
7106 12 Apr 23 nicklas 90   {
7106 12 Apr 23 nicklas 91     int index = columnHeaders.indexOf(col);
7106 12 Apr 23 nicklas 92     return Values.getLong(currentData[index], defaultValue);
7106 12 Apr 23 nicklas 93   }
7106 12 Apr 23 nicklas 94
7106 12 Apr 23 nicklas 95   /**
7106 12 Apr 23 nicklas 96     Get the value from the given column as a float.
7106 12 Apr 23 nicklas 97   */
7106 12 Apr 23 nicklas 98   public Float getFloat(String col, Float defaultValue)
7106 12 Apr 23 nicklas 99   {
7106 12 Apr 23 nicklas 100     int index = columnHeaders.indexOf(col);
7106 12 Apr 23 nicklas 101     return Values.getFloat(currentData[index], defaultValue);
7106 12 Apr 23 nicklas 102   }
7106 12 Apr 23 nicklas 103   
7106 12 Apr 23 nicklas 104   /**
7106 12 Apr 23 nicklas 105     Get the value from the given column as a string.
7106 12 Apr 23 nicklas 106   */
7106 12 Apr 23 nicklas 107   public String getString(String col, String defaultValue)
7106 12 Apr 23 nicklas 108   {
7106 12 Apr 23 nicklas 109     int index = columnHeaders.indexOf(col);
7106 12 Apr 23 nicklas 110     return Values.getString(currentData[index], defaultValue);
7106 12 Apr 23 nicklas 111   }
7106 12 Apr 23 nicklas 112   
7106 12 Apr 23 nicklas 113 }