Java Code Examples for htsjdk.samtools.SamReader#Type

The following examples show how to use htsjdk.samtools.SamReader#Type . 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: SamUtils.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Guess the supported SAM file type based on extension.
 * @param file the file to check
 * @return the type, or null if the type is not supported
 */
public static SamReader.Type getSamType(final File file) {
  for (SamReader.Type t : SUPPORTED_TYPES) {
    if (file.getName().endsWith(t.fileExtension())) {
      return t;
    }
  }
  // We also handle tabixed block-compressed SAM files.
  if (file.getName().endsWith(SamReader.Type.SAM_TYPE.fileExtension() + FileUtils.GZ_SUFFIX)) {
    return SamReader.Type.SAM_TYPE;
  }
  return null;
}
 
Example 2
Source File: SamUtils.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Test whether an index file exists
 * @param file supplying alignments
 * @return true if the file has an index
 */
public static boolean isIndexed(File file) {
  final SamReader.Type t = getSamType(file);
  if (t == null) {
    return false;
  } else if (t == SamReader.Type.SAM_TYPE) { // We support tabixed block-compressed SAM
    return TabixIndexer.indexFileName(file).exists();
  } else {
    return SamFiles.findIndex(file) != null;
  }
}
 
Example 3
Source File: SamUtils.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Entry point for specifically creating a SamReader given a pre-positioned stream, header, and known type
 * @param stream the stream to read from. Must already be performing decompression if required.
 * @param reference the SequencesReader to be used as the reference (required for CRAM files).
 * @param headerOverride the pre-determined SAM header
 * @param assumeType the type of input to assume.
 * @return the SamReader
 * @throws IOException if an I/O problem occurs opening the file
 */
public static SamReader makeSamReader(InputStream stream, SequencesReader reference, SAMFileHeader headerOverride, SamReader.Type assumeType) throws IOException {
  if (assumeType == null) {
    throw new NullPointerException();
  }
  try {
    return getSamReaderFactory(reference)
      .open(SamInputResource.of(stream).header(headerOverride).assumeType(assumeType));
  } catch (final RuntimeIOException e) {
    throw (IOException) e.getCause();
  }
}
 
Example 4
Source File: SamClosedFileReader.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * @param file SAM or BAM file, if region is specified an index must also be present with the appropriate relative file path
 * @param regions regions the file should select, null for whole file
 * @param reference the SequencesReader to be used as the reference (only required for CRAM files).
 * @param header header that should be used for SAM records may not be null  @throws IOException if an IO error occurs
 * @throws IOException if an IO error occurs
 */
public SamClosedFileReader(File file, ReferenceRanges<String> regions, SequencesReader reference, SAMFileHeader header) throws IOException {
  super(header);
  SamUtils.logRunId(header);
  final SamReader.Type t = SamUtils.getSamType(file);
  mType = t == null ? SamReader.Type.SAM_TYPE : t; // Treat unknown (e.g. piped or /dev/fd63) as SAM
  mFile = file;
  mStream = new ClosedFileInputStream(mFile);
  mReference = reference;
  mRegions = regions;
  mIterator = obtainIteratorInternal();
}
 
Example 5
Source File: SamMultiRestrictingIterator.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
SamMultiRestrictingIterator(BlockCompressedInputStream stream, VirtualOffsets offsets, SequencesReader reference, SAMFileHeader header, SamReader.Type type, String label) throws IOException {
  mStream = stream;
  mOffsets = offsets;
  mHeader = header;
  mReference = reference;
  mType = type;
  mLabel = label;

  mCurrentIt = null;
  mCurrentOffset = 0;
  mCurrentTemplate = -1;

  // Set up for first region and if it has no data, skip ahead to find one that does
  populateNext(true);
}
 
Example 6
Source File: SplitReadsIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test(dataProvider = "splitReadsData")
public void testSplitReadsByReadGroup(final SamReader.Type type,
                                      final String baseName,
                                      final List<String> splitArgs,
                                      final Map<String, Integer> splitCounts) throws Exception {
    final String fileExtension = "." + type.fileExtension();
    final List<String> args = new ArrayList<>();

    Path outputDir = Files.createTempDirectory(
            splitArgs.stream().reduce(baseName, (acc, arg) -> acc + "." + arg) + fileExtension + "."
    );
    outputDir.toFile().deleteOnExit();

    args.add("-"+ StandardArgumentDefinitions.INPUT_SHORT_NAME);
    args.add(getTestDataDir() + "/" + baseName + fileExtension);

    args.add("-"+ StandardArgumentDefinitions.OUTPUT_SHORT_NAME );
    args.add(outputDir.toString());

    if (isReferenceRequired(type)) {
        args.add("-" + StandardArgumentDefinitions.REFERENCE_SHORT_NAME );
        args.add(getTestDataDir()+ "/" + getReferenceSequenceName(baseName));
    }

    splitArgs.forEach(arg -> {
        args.add("-" + arg);
    });

    Assert.assertNull(runCommandLine(args));

    for (final Map.Entry<String, Integer> splitCount: splitCounts.entrySet()) {
        final String outputFileName = baseName + splitCount.getKey() + fileExtension;
        Assert.assertEquals(
                getReadCounts(outputDir, baseName, outputFileName),
                (int)splitCount.getValue(),
                "unexpected read count for " + outputFileName);
    }
}
 
Example 7
Source File: SplitReadsIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Stream<SamReader.Type> getSamReaderTypes() {
    return Stream
            .of(SamReader.Type.class.getFields())
            .filter(f -> Modifier.isStatic(f.getModifiers()))
            .filter(f -> f.getType().isAssignableFrom(SamReader.Type.class))
            .map(f -> orNull(() -> f.get(null)))
            .filter(v -> v instanceof SamReader.Type)
            .map(v -> (SamReader.Type) v)
            .filter(v -> !v.fileExtension().equals("sra")); // exclude SRA file types until we have tests
}
 
Example 8
Source File: SplitReadsIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private boolean isReferenceRequired(final SamReader.Type type) {
    return type == SamReader.Type.CRAM_TYPE;
}
 
Example 9
Source File: SplitReadsIntegrationTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@DataProvider(name = "splitReadsData")
public Object[][] getSplitReadsData() {
    final Map<String, Integer> byNone = new TreeMap<>();
    byNone.put("", 19);

    final Map<String, Integer> bySample = new TreeMap<>();
    bySample.put(".Momma", 17);
    bySample.put(".Poppa", 2);

    final Map<String, Integer> byRG = new TreeMap<>();
    byRG.put(".0", 17);
    byRG.put(".1", 2);

    final Map<String, Integer> byLibrary = new TreeMap<>();
    byLibrary.put(".whatever", 19);

    final Map<String, Integer> bySampleAndRG = new TreeMap<>();
    bySampleAndRG.put(".Momma.0", 17);
    bySampleAndRG.put(".Poppa.1", 2);

    final Map<String, Integer> bySampleAndRGAndLibrary = new TreeMap<>();
    bySampleAndRGAndLibrary.put(".Momma.0.whatever", 17);

    // test that reads from RGs with no library attribute are output to "unknown"
    final Map<String, Integer> byUnknown = new TreeMap<>();
    byUnknown.put(".whatever", 2);
    byUnknown.put("."  + SplitReads.UNKNOWN_OUT_PREFIX, 17);

    final Function<SamReader.Type, Stream<Object[]>> argTests = t -> Stream.of(
            new Object[]{t, TEST_DATA_GOOD_READS_PREFIX, Collections.<String>emptyList(), byNone},
            new Object[]{t, TEST_DATA_GOOD_READS_PREFIX, Collections.singletonList(SplitReads.SAMPLE_SHORT_NAME), bySample},
            new Object[]{t, TEST_DATA_GOOD_READS_PREFIX, Collections.singletonList(SplitReads.READ_GROUP_SHORT_NAME), byRG},
            new Object[]{t, TEST_DATA_GOOD_READS_PREFIX, Collections.singletonList(SplitReads.LIBRARY_NAME_SHORT_NAME), byLibrary},
            new Object[]{t, TEST_DATA_GOOD_READS_PREFIX, Arrays.asList(SplitReads.SAMPLE_SHORT_NAME, SplitReads.READ_GROUP_SHORT_NAME), bySampleAndRG},
            new Object[]{t, TEST_DATA_GOOD_READS_PREFIX, Arrays.asList(
                    SplitReads.SAMPLE_SHORT_NAME,
                    SplitReads.READ_GROUP_SHORT_NAME,
                    SplitReads.LIBRARY_NAME_SHORT_NAME),
                    bySampleAndRGAndLibrary
            },
            new Object[]{t, TEST_DATA_MISSING_LIB__PREFIX, Collections.singletonList(SplitReads.LIBRARY_NAME_SHORT_NAME), byUnknown}
    );

    return getSamReaderTypes()
            .map(argTests)
            .flatMap(Function.identity())
            .toArray(Object[][]::new);
}