Java Code Examples for htsjdk.samtools.SAMReadGroupRecord#getSample()

The following examples show how to use htsjdk.samtools.SAMReadGroupRecord#getSample() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SamCommandHelper.java    From rtg-tools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * This validates an retrieves from a sam file the read group matching the {@code selectReadGroup} parameter
 * @param rgFile sam file containing the read group
 * @param selectReadGroup the read group ID to locate
 * @return a {@code SAM ReadGroupRecord} that corresponds to the requested id
 * @throws java.io.IOException if IO falls over when reading the SAM file
 */
public static SAMReadGroupRecord validateSelectedSamRG(File rgFile, String selectReadGroup) throws IOException {
  try (BufferedInputStream bis = FileUtils.createInputStream(rgFile, false)) {
    final SamReader sfr = SamUtils.makeSamReader(bis);
    final List<SAMReadGroupRecord> readGroups = sfr.getFileHeader().getReadGroups();
    if (readGroups.isEmpty()) {
      throw new InvalidParamsException("No read group information matching \"" + selectReadGroup + "\" present in the input file \"" + rgFile.getPath() + "\"");
    }
    for (SAMReadGroupRecord r : readGroups) {
      if (selectReadGroup.equals(r.getId())) {
        if (r.getSample() == null) {
          Diagnostic.warning("Sample not specified in read group, it is recommended to set the sample tag.");
        }
        return r;
      }
    }
    throw new InvalidParamsException("No read group information matching \"" + selectReadGroup + "\" present in the input file \"" + rgFile.getPath() + "\"");
  }

}
 
Example 2
Source File: CoverageUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Method takes a ReadGroup record and a partition type and returns the appropriate summary string for displaying in output tables.
 * Since each partition type has its own pattern for separating out readgroups this method is intended to handle all of the
 * processing for regroups so that outputs can be properly partitioned in all cases.
 *
 * @param rg readGroupRecord to be processed.
 * @param type partition type to generate read group string for.
 */
public static String getTypeID(final SAMReadGroupRecord rg, final DoCOutputType.Partition type ) {
    switch (type) {
        case sample:
            return rg.getSample();
        case readgroup:
            return rg.getSample()+"_rg_"+rg.getReadGroupId();
        case library:
            return rg.getLibrary();
        case center:
            return rg.getSequencingCenter();
        case platform:
            return rg.getPlatform();
        case sample_by_center:
            return rg.getSample()+"_cn_"+rg.getSequencingCenter();
        case sample_by_platform:
            return rg.getSample()+"_pl_"+rg.getPlatform();
        case sample_by_platform_by_center:
            return rg.getSample()+"_pl_"+rg.getPlatform()+"_cn_"+rg.getSequencingCenter();
        default:
            throw new GATKException(String.format("Invalid aggregation type %s", type));
    }
}
 
Example 3
Source File: SamUtils.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param header combined sam header
 * @return creates a map of read group to sample id
 */
public static Map<String, String> getReadGroupToSampleId(final SAMFileHeader header) {
  final HashMap<String, String> readGroupToSampleMap = new HashMap<>();
  for (final SAMReadGroupRecord rec : header.getReadGroups()) {
    //System.err.println("k=" + rec.getReadGroupId() + " v=" + rec.getSample());
    if (rec.getSample() == null) {
      throw new NoTalkbackSlimException("Read group with ID \"" + rec.getReadGroupId() + "\" does not contain a sample tag.");
    }
    readGroupToSampleMap.put(rec.getReadGroupId(), rec.getSample());
  }
  return readGroupToSampleMap;
}
 
Example 4
Source File: SamUtils.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Get a list of the sample names mentioned in the header in sorted order.
 * @param header combined sam header
 * @return creates an array of sample names
 */
public static String[] getSampleNames(final SAMFileHeader header) {
  final Set<String> sampleNames = new TreeSet<>();
  for (final SAMReadGroupRecord rec : header.getReadGroups()) {
    if (rec.getSample() != null) {
      sampleNames.add(rec.getSample());
    }
  }
  return sampleNames.toArray(new String[0]);
}
 
Example 5
Source File: SamUtils.java    From rtg-tools with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * creates a header with the contents from the first file except the read group information is merged from all headers. Does some checking that headers are compatible
 *
 * @param reference The reference (required if any input files are CRAM)
 * @param files SAM files
 * @param ignoreHeaderIncompatibility true if should not care about incompatible header
 * @param expectedSamples if non-null, check that headers contain sample information that overlaps the supplied names   @return the combined header
 * @return the combined header
 * @throws IOException if an I/O error occurs
 */
public static SAMFileHeader getUberHeader(SequencesReader reference, Collection<File> files, boolean ignoreHeaderIncompatibility, String[] expectedSamples) throws IOException {
  if (files.size() == 0) {
    throw new IllegalArgumentException("File list is empty!");
  }
  final HashMap<String, SAMReadGroupRecord> readGroups = new HashMap<>();
  final HashMap<String, String> readGroupsSampleMap = new HashMap<>();
  SAMFileHeader first = null;
  File firstFile = null;
  final StringBuilder errorMessage = new StringBuilder();
  SdfId currentGuid = null;
  final HashSet<String> expectedSamplesSet = expectedSamples == null ? null : new HashSet<>(Arrays.asList(expectedSamples));
  boolean guidMismatch = false;
  for (final File file : files) {
    if (!file.isFile()) {
      errorMessage.append("Input file \"").append(file.getPath()).append("\" is not an ordinary file").append(StringUtils.LS);
      continue;
    }
    try (SamReader sfr = SamUtils.makeSamReader(file, reference)) {
      if (first == null) {
        first = sfr.getFileHeader();
        firstFile = file;
        currentGuid = getReferenceGuid(first);
      } else if (!checkHeaderDictionary(first, sfr.getFileHeader())) {
        Diagnostic.warning(WarningType.SAM_INCOMPATIBLE_HEADERS, firstFile.getPath(), file.getPath());
        if (!ignoreHeaderIncompatibility) {
          throw new NoTalkbackSlimException(ErrorType.SAM_INCOMPATIBLE_HEADER_ERROR, "1");
        }
      }
      final SdfId fileGuid = getReferenceGuid(sfr.getFileHeader());
      if (!currentGuid.check(fileGuid)) {
        guidMismatch = true;
      } else if (!currentGuid.available()) {
        currentGuid = fileGuid;
      }
      if (!sfr.getFileHeader().getReadGroups().isEmpty()) {
        for (final SAMReadGroupRecord r : sfr.getFileHeader().getReadGroups()) {
          final String sample = r.getSample();
          if (!readGroups.containsKey(r.getReadGroupId())) {
            readGroups.put(r.getReadGroupId(), r);
            readGroupsSampleMap.put(r.getReadGroupId(), sample);
          } else {
            //check that the sample isn't different for the same read group id
            if (sample != null && !r.getSample().equals(readGroupsSampleMap.get(r.getReadGroupId()))) {
              Diagnostic.warning(file.getPath() + " contained read group with ID \"" + r.getId() + "\" and sample \"" + sample + "\" but this read group has already been associated with sample \"" + readGroupsSampleMap.get(r.getReadGroupId()) + "\"");
              if (!ignoreHeaderIncompatibility) {
                throw new NoTalkbackSlimException(ErrorType.SAM_INCOMPATIBLE_HEADER_ERROR, "1");
              }
            }
          }
          if (expectedSamplesSet != null) {
            if (sample != null) {
              if (!expectedSamplesSet.contains(sample)) {
                errorMessage.append("Unexpected read group sample name: ").append(sample).append('.').append(StringUtils.LS);
              }
            } else {
              errorMessage.append("Input file \"").append(file.getPath()).append("\" contains a read group with no sample tag: ").append(r.getId()).append('.').append(StringUtils.LS);
            }
          }
        }
      } else if (expectedSamples != null) {
        errorMessage.append("Input file \"").append(file.getPath()).append("\" does not contain read group information.").append(StringUtils.LS);
      }
    }
  }
  if (guidMismatch) {
    Diagnostic.warning("Input SAM files contain mismatching template GUIDs");
  }
  if (errorMessage.length() > 0) {
    throw new NoTalkbackSlimException(errorMessage.toString().trim());
  }

  final SAMFileHeader header = first;
  if (readGroups.size() > 0) {
    final List<SAMReadGroupRecord> recList = new ArrayList<>(readGroups.values());
    header.setReadGroups(recList);
  }
  if (currentGuid.available() && !getReferenceGuid(header).available()) {
    header.addComment(TEMPLATE_SDF_ATTRIBUTE + currentGuid);
  }
  return header;
}
 
Example 6
Source File: FingerprintIdDetails.java    From picard with MIT License 4 votes vote down vote up
public FingerprintIdDetails(final SAMReadGroupRecord rg, final String file) {
    this(rg.getPlatformUnit(), file);
    this.sample = rg.getSample();
    this.library = rg.getLibrary();
}
 
Example 7
Source File: MultiLevelCollector.java    From picard with MIT License 4 votes vote down vote up
@Override
protected String getKey(SAMReadGroupRecord rg) {
    return rg.getSample();
}
 
Example 8
Source File: MultiLevelCollector.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected String getKey(SAMReadGroupRecord rg) {
    return rg.getSample();
}
 
Example 9
Source File: MultiLevelReducibleCollector.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
protected String getKey(SAMReadGroupRecord rg) {
    return rg.getSample();
}
 
Example 10
Source File: CoverageUtils.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Takes an AlignmentContext object and extracts all the reads that pass the provided filters, and then returns a
 * count breakdown for each base (plus D and N bases) present at the site.
 *
 * NOTE: this currently doesn't support counts by fragments as was the case in gatk3
 */
private static Map<SAMReadGroupRecord,int[]> getBaseCountsByReadGroup(final AlignmentContext context, final byte minBaseQ, final byte maxBaseQ, final CountPileupType countType, final SAMFileHeader header) {
    Map<SAMReadGroupRecord, int[]> countsByRG = new HashMap<>();

    Map<String, int[]> countsByRGName = new HashMap<>();
    Map<String, SAMReadGroupRecord> RGByName = new HashMap<>();

    List<PileupElement> countPileup = new ArrayList<>(context.getBasePileup().size());

    switch (countType) {

        case COUNT_READS:
            for (PileupElement read : context.getBasePileup()) {
                if (elementWithinQualRange(read, minBaseQ, maxBaseQ)) {
                    countPileup.add(read);
                }
            }
            break;

        // TODO see reconcile FragmentUtils.create() and its various idiosyncrasies to re-enable this feature see https://github.com/broadinstitute/gatk/issues/6491
        case COUNT_FRAGMENTS: // ignore base identities and put in FIRST base that passes filters:
            throw new UnsupportedOperationException("Fragment based counting is currently unsupported");

        case COUNT_FRAGMENTS_REQUIRE_SAME_BASE:
            throw new UnsupportedOperationException("Fragment based counting is currently unsupported");

        default:
            throw new UserException("Must use valid CountPileupType");
    }

    for (PileupElement e : countPileup) {
        SAMReadGroupRecord readGroup = ReadUtils.getSAMReadGroupRecord(e.getRead(), header);
        Utils.nonNull(readGroup, String.format("Read %s was missing read group information", e.getRead()));

        // uniqueReadGroupID is unique across the library, read group ID, and the sample
        String uniqueReadGroupId = readGroup.getSample() + "_" + readGroup.getReadGroupId() + "_" + readGroup.getLibrary() + "_" + readGroup.getPlatformUnit();
        int[] counts = countsByRGName.get(uniqueReadGroupId);
        if (counts == null) {
            counts = new int[6];
            countsByRGName.put(uniqueReadGroupId, counts);
            RGByName.put(uniqueReadGroupId, readGroup);
        }

        updateCounts(counts, e);
    }

    for (String readGroupId : RGByName.keySet()) {
        countsByRG.put(RGByName.get(readGroupId), countsByRGName.get(readGroupId));
    }

    return countsByRG;
}