From bc12055fcf7abc00ec312ce9b0a38c4f0a12b089 Mon Sep 17 00:00:00 2001 From: bthomas Date: Fri, 17 Sep 2010 14:05:26 +0000 Subject: [PATCH] Quick patch to fix the sample code. It wasn't actually initializing the sample data source, so I added a call to initializeSampleDataSource() in GenomeAnalysisEngine. I think there was just an error resolving the versions of GenomeAnalysisEngine Also added a new error message that I thought would be helpful... git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@4301 348d0f76-0448-11de-a6fe-93d51630548a --- .../sting/gatk/GenomeAnalysisEngine.java | 2 ++ .../datasources/sample/SampleDataSource.java | 15 ++++++++++--- .../sample/CountLociByPopulationWalker.java | 22 ++++++++++++------- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisEngine.java b/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisEngine.java index 755b58c36..640f71d95 100755 --- a/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisEngine.java +++ b/java/src/org/broadinstitute/sting/gatk/GenomeAnalysisEngine.java @@ -381,6 +381,8 @@ public class GenomeAnalysisEngine { validateSuppliedReferenceAgainstWalker(my_walker, argCollection); referenceDataSource = openReferenceSequenceFile(argCollection.referenceFile); + initializeSampleDataSource(); + if (argCollection.DBSNPFile != null) bindConvenienceRods(DbSNPHelper.STANDARD_DBSNP_TRACK_NAME, "dbsnp", argCollection.DBSNPFile); RMDTrackBuilder manager = new RMDTrackBuilder(); diff --git a/java/src/org/broadinstitute/sting/gatk/datasources/sample/SampleDataSource.java b/java/src/org/broadinstitute/sting/gatk/datasources/sample/SampleDataSource.java index 960664832..f8e923eb1 100644 --- a/java/src/org/broadinstitute/sting/gatk/datasources/sample/SampleDataSource.java +++ b/java/src/org/broadinstitute/sting/gatk/datasources/sample/SampleDataSource.java @@ -121,7 +121,7 @@ public class SampleDataSource { try { parser = (SampleFileParser) yaml.load(reader); } - catch (Exception e) { // TODO: should we have more granular exception here? + catch (Exception e) { throw new StingException("There was a syntactic error with the YAML in sample file " + sampleFile.getAbsolutePath(), e); } @@ -149,6 +149,7 @@ public class SampleDataSource { // loop through each sample in the file - a SampleParser stores an object that will become a Sample for (SampleParser sampleParser : parser.getSamples()) { + try { // step 1: add the sample if it doesn't already exist Sample sample = getSampleById(sampleParser.getId()); if (sample == null) { @@ -219,6 +220,10 @@ public class SampleDataSource { saveRelationship(sample, relationship, relativeId); } } + } catch (Exception e) { + throw new StingException("An error occurred while loading this sample from the sample file: " + + sampleParser.getId(), e); + } } } @@ -326,9 +331,13 @@ public class SampleDataSource { else if (value != null) { throw new StingException("'gender' property must be male, female, or unknown."); } - value = null; } - sample.setProperty(key, value); + try { + sample.setProperty(key, value); + } + catch (Exception e) { + throw new StingException("Could not save property " + key, e); + } } /** diff --git a/java/src/org/broadinstitute/sting/playground/sample/CountLociByPopulationWalker.java b/java/src/org/broadinstitute/sting/playground/sample/CountLociByPopulationWalker.java index 905bb9bf9..f21903acf 100644 --- a/java/src/org/broadinstitute/sting/playground/sample/CountLociByPopulationWalker.java +++ b/java/src/org/broadinstitute/sting/playground/sample/CountLociByPopulationWalker.java @@ -3,6 +3,7 @@ package org.broadinstitute.sting.playground.sample; import net.sf.samtools.SAMRecord; import org.broadinstitute.sting.gatk.contexts.AlignmentContext; import org.broadinstitute.sting.gatk.contexts.ReferenceContext; +import org.broadinstitute.sting.gatk.datasources.sample.Sample; import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker; import org.broadinstitute.sting.gatk.walkers.LocusWalker; import org.broadinstitute.sting.gatk.walkers.TreeReducible; @@ -11,27 +12,32 @@ import java.util.ArrayList; import java.util.HashMap; /** - * Walks over the input data set, calculating the total number of covered loci for diagnostic purposes. - * Simplest example of a locus walker. + * Extends locus walker to print how many reads there are at each locus, by population */ public class CountLociByPopulationWalker extends LocusWalker implements TreeReducible { public Integer map(RefMetaDataTracker tracker, ReferenceContext ref, AlignmentContext context) { + // in this HashMap, we'll keep count of how many HashMap count = new HashMap(); ArrayList reads = (ArrayList) context.getBasePileup().getReads(); for (SAMRecord read : reads) { - String population = getToolkit().getSampleByRead(read).getPopulation(); - if (!count.containsKey(population)) { - count.put(population, 1); + + // get the sample + Sample sample = getToolkit().getSampleByRead(read); + if (sample == null) + return 1; + + if (!count.containsKey(sample.getPopulation())) { + count.put(sample.getPopulation(), 1); } - count.put(population, count.get(population) + 1); + count.put(sample.getPopulation(), count.get(sample.getPopulation()) + 1); } - System.out.println("\n\n\n***** LOCUS: " + ref.toString() + " *****"); + System.out.println("\n\n\n***** LOCUS: " + ref.getLocus().toString() + " *****"); for (String population : count.keySet()) { - System.out.println(String.format("%s | %d\n", population, count.get(population))); + System.out.println(String.format("%s | %d", population, count.get(population))); } return 1;