From eeddf0d08ef2007834be50eb10e063632bb15709 Mon Sep 17 00:00:00 2001 From: ebanks Date: Fri, 18 Dec 2009 18:51:21 +0000 Subject: [PATCH] Adding sample utils for convenience methods to pull out samples from e.g. SAMFileHeader or Genotype objects git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@2405 348d0f76-0448-11de-a6fe-93d51630548a --- .../sting/utils/SampleUtils.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 java/src/org/broadinstitute/sting/utils/SampleUtils.java diff --git a/java/src/org/broadinstitute/sting/utils/SampleUtils.java b/java/src/org/broadinstitute/sting/utils/SampleUtils.java new file mode 100755 index 000000000..8f68f209b --- /dev/null +++ b/java/src/org/broadinstitute/sting/utils/SampleUtils.java @@ -0,0 +1,54 @@ +package org.broadinstitute.sting.utils; + +import net.sf.samtools.SAMReadGroupRecord; +import net.sf.samtools.SAMFileHeader; + +import java.util.*; + +import org.broadinstitute.sting.utils.genotype.Genotype; +import org.broadinstitute.sting.utils.genotype.SampleBacked; + + +/** + * SampleUtils is a static class (no instantiation allowed!) with some utility methods for getting samples + * quality scores. + * + * @author ebanks + */ +public class SampleUtils { + /** + * Private constructor. No instantiating this class! + */ + private SampleUtils() {} + + /** + * Pull out the samples from a SAMFileHeader; + * note that we use a TreeSet so that they are sorted + * + * @param header the sam file header + * @return list of strings representing the sample names + */ + public static Set getSAMFileSamples(SAMFileHeader header) { + // get all of the unique sample names + Set samples = new TreeSet(); + List readGroups = header.getReadGroups(); + for ( SAMReadGroupRecord readGroup : readGroups ) + samples.add(readGroup.getSample()); + return samples; + } + + /** + * get the samples names from genotype objects if they are backed by samples + * + * @param genotypes the genotype list + * @return list of strings representing the sample names + */ + public static List getGenotypeSamples(List genotypes) { + List samples = new ArrayList(); + for ( Genotype genotype : genotypes ) { + if ( genotype instanceof SampleBacked ) + samples.add(((SampleBacked)genotype).getSampleName()); + } + return samples; + } +} \ No newline at end of file