From 83dcca7e82d610d1b1d231eea0120578008dad8f Mon Sep 17 00:00:00 2001 From: kiran Date: Mon, 31 Jan 2011 05:31:49 +0000 Subject: [PATCH] Added ability to load a GATKReport from disk. git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@5134 348d0f76-0448-11de-a6fe-93d51630548a --- .../sting/gatk/report/GATKReport.java | 82 ++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/java/src/org/broadinstitute/sting/gatk/report/GATKReport.java b/java/src/org/broadinstitute/sting/gatk/report/GATKReport.java index d9e6782e5..7e69edde3 100755 --- a/java/src/org/broadinstitute/sting/gatk/report/GATKReport.java +++ b/java/src/org/broadinstitute/sting/gatk/report/GATKReport.java @@ -1,6 +1,8 @@ package org.broadinstitute.sting.gatk.report; -import java.io.PrintStream; +import org.broadinstitute.sting.utils.exceptions.StingException; + +import java.io.*; import java.util.TreeMap; /** @@ -9,10 +11,88 @@ import java.util.TreeMap; public class GATKReport { private TreeMap tables; + /** + * Create a new, empty GATKReport. + */ public GATKReport() { tables = new TreeMap(); } + /** + * Create a new GATKReport with the contents of a GATKReport on disk. + * @param filename the path to the file to load + */ + public GATKReport(String filename) { + loadReport(new File(filename)); + } + + /** + * Create a new GATKReport with the contents of a GATKReport on disk. + * @param file the file to load + */ + public GATKReport(File file) { + tables = new TreeMap(); + loadReport(file); + } + + /** + * Load a GATKReport file from disk + * @param file the file to load + */ + private void loadReport(File file) { + try { + BufferedReader reader = new BufferedReader(new FileReader(file)); + + GATKReportTable table = null; + String[] header = null; + int id = 0; + + String line; + while ( (line = reader.readLine()) != null ) { + if (line.startsWith("##:GATKReport.v0.1 ")) { + line = line.replaceFirst("##:GATKReport.v0.1 ", ""); + String[] pieces = line.split(" : "); + + String tableName = pieces[0]; + String tableDesc = pieces[1]; + + addTable(tableName, tableDesc); + table = getTable(tableName); + + header = null; + } else if ( line.isEmpty() ) { + // do nothing + } else { + if (table != null) { + if (header == null) { + header = line.split("\\s+"); + + table.addPrimaryKey("id", false); + + for ( String columnName : header ) { + table.addColumn(columnName, ""); + } + + id = 0; + } else { + String[] entries = line.split("\\s+"); + + for (int columnIndex = 0; columnIndex < header.length; columnIndex++) { + table.set(id, header[columnIndex], entries[columnIndex]); + } + + id++; + } + } + } + } + } catch (FileNotFoundException e) { + throw new StingException("Cannot read GATKReport: " + e); + } catch (IOException e) { + throw new StingException("Cannot read GATKReport: " + e); + } + } + /** * Add a new table to the collection *