htsjdk.samtools.SAMRecord Java Examples

The following examples show how to use htsjdk.samtools.SAMRecord. 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: TagValueFilteringIteratorTest.java    From Drop-seq with MIT License 6 votes vote down vote up
@Test
public void testCellBarcodeFiltering () {
	String cellBarcodeTag = "XC";

	SamReader inputSam = SamReaderFactory.makeDefault().open(IN_FILE);
	String [] desiredBarcodes = {"ATCAGGGACAGA", "TTGCCTTACGCG", "TACAATTAAGGC"};

	TagValueFilteringIterator<String> iter = new TagValueFilteringIterator<String>(inputSam.iterator(), cellBarcodeTag, Arrays.asList(desiredBarcodes));
	Set<String> barcodesFound = new HashSet<String>();
	while (iter.hasNext()) {
		SAMRecord r = iter.next();
		String v = r.getStringAttribute(cellBarcodeTag);
		barcodesFound.add(v);
	}

	// should be the same size.
	Assert.assertEquals(barcodesFound.size(), desiredBarcodes.length);
	// should contain the same answers.
	for (String s: desiredBarcodes)
		Assert.assertTrue(barcodesFound.contains(s));

}
 
Example #2
Source File: SortSam.java    From picard with MIT License 6 votes vote down vote up
protected int doWork() {
    IOUtil.assertFileIsReadable(INPUT);
    IOUtil.assertFileIsWritable(OUTPUT);
    final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
    ;
    reader.getFileHeader().setSortOrder(SORT_ORDER.getSortOrder());
    final SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(reader.getFileHeader(), false, OUTPUT);
    writer.setProgressLogger(
            new ProgressLogger(log, (int) 1e7, "Wrote", "records from a sorting collection"));

    final ProgressLogger progress = new ProgressLogger(log, (int) 1e7, "Read");
    for (final SAMRecord rec : reader) {
        writer.addAlignment(rec);
        progress.record(rec);
    }

    log.info("Finished reading inputs, merging and writing to output now.");

    CloserUtil.close(reader);
    writer.close();
    return 0;
}
 
Example #3
Source File: SAMRecordUtils.java    From abra2 with MIT License 6 votes vote down vote up
public static String getTrailingClips(SAMRecord read) {
	List<CigarElement> elems = read.getCigar().getCigarElements();
	
	List<CigarElement> trailing = new ArrayList<CigarElement>();
	boolean isNonClippedReached = false;
	
	for (CigarElement elem : elems) {
		if (isClip(elem)) {
			if (isNonClippedReached) {
				trailing.add(elem);
			}
		} else {
			isNonClippedReached = true;
		}
	}

	String ret = "";
	if (trailing.size() > 0) {
		Cigar cigar = new Cigar(trailing);
		ret =  TextCigarCodec.encode(cigar);
	}
	
	return ret;
}
 
Example #4
Source File: QuerySortedReadPairIteratorUtilTest.java    From picard with MIT License 6 votes vote down vote up
@Test
public void testBasicPairedRead() {
    SAMRecordSetBuilder builder = new SAMRecordSetBuilder(false, SAMFileHeader.SortOrder.queryname);
    builder.setReadLength(READ_LENGTH);
    builder.addPair("mapped_paired", 1, 1, 31);
    PeekableIterator<SAMRecord> iterator = new PeekableIterator<SAMRecord>(builder.iterator());

    QuerySortedReadPairIteratorUtil.ReadPair pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNotNull(pair);
    Assert.assertNotNull(pair.read1);
    Assert.assertNotNull(pair.read2);
    Assert.assertEquals("mapped_paired", pair.read1.getReadName());
    Assert.assertEquals("mapped_paired", pair.read2.getReadName());

    pair = QuerySortedReadPairIteratorUtil.getNextReadPair(iterator);
    Assert.assertNull(pair);
}
 
Example #5
Source File: CollectDuplicateMetricsTester.java    From picard with MIT License 6 votes vote down vote up
@Override
public File getOutput() {
    final File output;
    try {
        output = File.createTempFile("CollectDuplicateMetrics", ".sam");
    } catch (IOException e) {
        throw new PicardException("problems creating output file", e);
    }
    output.deleteOnExit();

    try(SAMFileWriter writer = new SAMFileWriterFactory().makeWriter(samRecordSetBuilder.getHeader(), true, output.toPath(), this.fastaFiles.get(samRecordSetBuilder.getHeader()))) {
        samRecordSetBuilder.getRecords().forEach(a -> {
            final SAMRecord b = a.deepCopy();
            b.setDuplicateReadFlag(duplicateFlags.get(samRecordToDuplicatesFlagsKey(a)));
            writer.addAlignment(b);
        });
    }

    return output;
}
 
Example #6
Source File: DetectBeadSubstitutionErrorsTest.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
  * Very lame test -- just confirms that program doesn't crash and return 0 exit status.
  */
 @Test
 public void testBasic() throws IOException {
     final DetectBeadSubstitutionErrors clp = new DetectBeadSubstitutionErrors();
     clp.INPUT = Arrays.asList(TEST_FILE);
     clp.OUTPUT_REPORT = File.createTempFile("DetectBeadSubstitutionErrorsTest.", ".substitution_report.txt");
     clp.OUTPUT_REPORT.deleteOnExit();
     clp.OUTPUT = File.createTempFile("DetectBeadSubstitutionErrorsTest.", ".sam");
     clp.OUTPUT.deleteOnExit();
     clp.OUTPUT_SUMMARY = File.createTempFile("DetectBeadSubstitutionErrorsTest.", "substitution_summary.txt");
     clp.OUTPUT_SUMMARY.deleteOnExit();
     Assert.assertEquals(clp.doWork(), 0);
     final TabbedTextFileWithHeaderParser parser = new TabbedTextFileWithHeaderParser(clp.OUTPUT_REPORT);
     int numRows = 0;
     for (final TabbedTextFileWithHeaderParser.Row row : parser) {
         ++numRows;
         Assert.assertEquals(row.getField("intended_barcode"), BIG_BARCODE);
         final String neighbor_barcode = row.getField("neighbor_barcode");
         Assert.assertTrue(SMALL_BARCODES.contains(neighbor_barcode));
         Assert.assertTrue(Boolean.parseBoolean(row.getField("repaired")));
     }
     final SamReader reader = SamReaderFactory.makeDefault().open(clp.OUTPUT);
     for (final SAMRecord rec : reader)
Assert.assertEquals(rec.getStringAttribute("XC"), BIG_BARCODE);
     CloserUtil.close(reader);
 }
 
Example #7
Source File: AltContigGenerator.java    From abra2 with MIT License 6 votes vote down vote up
private boolean hasHighQualitySoftClipping(SAMRecord read, int start, int length) {
	
	int numHighQualBases = 0;
	int requiredHighQualBases = (int) (softClipFraction * length);
	
	for (int bq = start; bq < start+length; bq++) {
		if (read.getBaseQualities()[bq] >= minBaseQual) {
			numHighQualBases += 1;
			
			if (numHighQualBases >= requiredHighQualBases) {
				return true;
			}
		}
	}
	
	return false;
}
 
Example #8
Source File: MergeBamAlignmentTest.java    From picard with MIT License 6 votes vote down vote up
private void addAlignmentsForBestFragmentMapqStrategy(
        final SAMFileWriter writer, final SAMRecord unmappedRecord, final String sequence, final int[] mapqs) {
    boolean reverse = false;
    int alignmentStart = 1;
    for (final int mapq : mapqs) {
        final SAMRecord alignedRecord = new SAMRecord(writer.getFileHeader());
        alignedRecord.setReadName(unmappedRecord.getReadName());
        alignedRecord.setReadBases(unmappedRecord.getReadBases());
        alignedRecord.setBaseQualities(unmappedRecord.getBaseQualities());
        alignedRecord.setReferenceName(sequence);
        alignedRecord.setAlignmentStart(alignmentStart);
        alignmentStart += 10; // Any old position will do
        alignedRecord.setReadNegativeStrandFlag(reverse);
        reverse = !reverse;
        alignedRecord.setCigarString(unmappedRecord.getReadBases().length + "M");
        alignedRecord.setMappingQuality(mapq);
        alignedRecord.setReadPairedFlag(unmappedRecord.getReadPairedFlag());
        alignedRecord.setFirstOfPairFlag(unmappedRecord.getFirstOfPairFlag());
        alignedRecord.setSecondOfPairFlag(unmappedRecord.getSecondOfPairFlag());
        alignedRecord.setMateUnmappedFlag(true);
        writer.addAlignment(alignedRecord);
    }
}
 
Example #9
Source File: ReadContextCounter.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
@NotNull
private RealignedContext realignmentContext(boolean realign, int readIndex, SAMRecord record) {
    if (!realign) {
        return new RealignedContext(RealignedType.NONE, 0);
    }

    int index = readContext.readBasesPositionIndex();
    int leftIndex = readContext.readBasesLeftCentreIndex();
    int rightIndex = readContext.readBasesRightCentreIndex();

    int leftOffset = index - leftIndex;
    int rightOffset = rightIndex - index;

    int indelLength = indelLength(record);
    return Realigned.realignedAroundIndex(readContext,
            readIndex,
            record.getReadBases(),
            Math.max(indelLength + Math.max(leftOffset, rightOffset), Realigned.MAX_REPEAT_SIZE));
}
 
Example #10
Source File: SamRecordSortingIteratorFactory.java    From Drop-seq with MIT License 6 votes vote down vote up
/**
   * @param progressLogger pass null if not interested in progress.
   * @return An iterator with all the records from underlyingIterator, in order defined by comparator.
   */
  public static CloseableIterator<SAMRecord> create(final SAMFileHeader header,
                                         final Iterator<SAMRecord> underlyingIterator,
                                         final Comparator<SAMRecord> comparator,
                                         final ProgressLogger progressLogger) {
      final SortingIteratorFactory.ProgressCallback<SAMRecord> progressCallback;
      if (progressLogger != null)
	progressCallback = new SortingIteratorFactory.ProgressCallback<SAMRecord>() {
              @Override
              public void logProgress(final SAMRecord record) {
                  progressLogger.record(record);
              }
          };
else
	progressCallback = null;
      return SortingIteratorFactory.create(SAMRecord.class,
              underlyingIterator, comparator, new BAMRecordCodec(header),
              SAMFileWriterImpl.getDefaultMaxRecordsInRam(),
              progressCallback);
  }
 
Example #11
Source File: CellBarcodeFilteringIteratorTest.java    From Drop-seq with MIT License 6 votes vote down vote up
@Test
public void filterOut2() {
	Iterator<SAMRecord> underlyingIterator = Collections.emptyIterator();
	Collection<String> cellBarcodesToKeep = null;
	String cellBCTag ="XC";

	CellBarcodeFilteringIterator f = new CellBarcodeFilteringIterator(underlyingIterator, cellBCTag, cellBarcodesToKeep);
	SAMRecordSetBuilder b = new SAMRecordSetBuilder();
	// second argument is the contig index which is 0 based.  So contig index=0 -> chr1.  index=2 -> chr3, etc.
	b.addFrag("1", 0, 1, false);
	SAMRecord r1 = b.getRecords().iterator().next();

	r1.setAttribute(cellBCTag, "CELL1");
	Assert.assertFalse(f.filterOut(r1));

	r1.setAttribute(cellBCTag, "CELL2");
	Assert.assertFalse(f.filterOut(r1));

}
 
Example #12
Source File: TestBAMInputFormat.java    From Hadoop-BAM with MIT License 6 votes vote down vote up
@Test
public void testMultipleSplits() throws Exception {
  input = BAMTestUtil.writeBamFile(1000, SAMFileHeader.SortOrder.queryname)
      .getAbsolutePath();
  completeSetup();
  jobContext.getConfiguration().setInt(FileInputFormat.SPLIT_MAXSIZE, 40000);
  BAMInputFormat inputFormat = new BAMInputFormat();
  List<InputSplit> splits = inputFormat.getSplits(jobContext);
  assertEquals(2, splits.size());
  List<SAMRecord> split0Records = getSAMRecordsFromSplit(inputFormat, splits.get(0));
  List<SAMRecord> split1Records = getSAMRecordsFromSplit(inputFormat, splits.get(1));
  assertEquals(1577, split0Records.size());
  assertEquals(425, split1Records.size());
  SAMRecord lastRecordOfSplit0 = split0Records.get(split0Records.size() - 1);
  SAMRecord firstRecordOfSplit1 = split1Records.get(0);
  assertEquals(lastRecordOfSplit0.getReadName(), firstRecordOfSplit1.getReadName());
  assertTrue(lastRecordOfSplit0.getFirstOfPairFlag());
  assertTrue(firstRecordOfSplit1.getSecondOfPairFlag());
}
 
Example #13
Source File: NativeAssembler.java    From abra2 with MIT License 6 votes vote down vote up
private int maxSoftClipLength(SAMRecord read, Feature region) {
	int len = 0;
	if (read.getCigarLength() > 1) {
		
		CigarElement elem = read.getCigar().getCigarElement(0);
		if (elem.getOperator() == CigarOperator.S && read.getAlignmentStart() >= region.getStart()-readLength) {
			len = elem.getLength();
		}
		
		elem = read.getCigar().getCigarElement(read.getCigarLength()-1);
		if (elem.getOperator() == CigarOperator.S && elem.getLength() > len && read.getAlignmentEnd() <= region.getEnd()+readLength) {
			len = elem.getLength();
		}			
	}
	
	return len;
}
 
Example #14
Source File: SamRecordComparision.java    From cramtools with Apache License 2.0 6 votes vote down vote up
/**
 * This is supposed to check if the mates have valid pairing flags.
 * 
 * @param r1
 * @param r2
 * @return
 */
private boolean checkMateFlags(SAMRecord r1, SAMRecord r2) {
	if (!r1.getReadPairedFlag() || !r2.getReadPairedFlag())
		return false;

	if (r1.getReadUnmappedFlag() != r2.getMateUnmappedFlag())
		return false;
	if (r1.getReadNegativeStrandFlag() != r2.getMateNegativeStrandFlag())
		return false;
	if (r1.getProperPairFlag() != r2.getProperPairFlag())
		return false;
	if (r1.getFirstOfPairFlag() && r2.getFirstOfPairFlag())
		return false;
	if (r1.getSecondOfPairFlag() && r2.getSecondOfPairFlag())
		return false;

	if (r2.getReadUnmappedFlag() != r1.getMateUnmappedFlag())
		return false;
	if (r2.getReadNegativeStrandFlag() != r1.getMateNegativeStrandFlag())
		return false;

	return true;
}
 
Example #15
Source File: BestEndMapqPrimaryAlignmentStrategy.java    From picard with MIT License 5 votes vote down vote up
public int compare(final SAMRecord rec1, final SAMRecord rec2) {
    if (rec1.getReadUnmappedFlag()) {
        if (rec2.getReadUnmappedFlag()) return 0;
        else return 1;
    } else if (rec2.getReadUnmappedFlag()) {
        return -1;
    }
    return -SAMUtils.compareMapqs(rec1.getMappingQuality(), rec2.getMappingQuality());
}
 
Example #16
Source File: AggregatedTagOrderIteratorTest.java    From Drop-seq with MIT License 5 votes vote down vote up
@Test(enabled=true)
public void testGeneCellSorting() {
       final Iterator<List<SAMRecord>> groupingIterator = filterSortAndGroupByTags(IN_FILE, "GE", "ZC");
	int counter=0;
	  
	
	String [] geneOrder={"CHUK", "CHUK", "NKTR", "NKTR", "SNRPA1", "SNRPA1"};
	String [] cellOrder={"ATCAGGGACAGA", "TGGCGAAGAGAT", "ATCAGGGACAGA", "TGGCGAAGAGAT", "ATCAGGGACAGA", "TGGCGAAGAGAT"};
	int [] expectedSize = {2,2,2,2,2,2};
	
	while (groupingIterator.hasNext()) {
		Collection<SAMRecord> r = groupingIterator.next();
		int size = r.size();
		SAMRecord rec = r.iterator().next();
		
		String readName = rec.getReadName();
		String cellName = rec.getStringAttribute("ZC");
		String geneName = rec.getStringAttribute("GE");
		
		System.out.println("Cell [" + cellName +"] geneName [" + geneName +"] size [" + size +"]");

		Assert.assertEquals(cellName, cellOrder[counter]);
		Assert.assertEquals(geneName, geneOrder[counter]);
		Assert.assertEquals(size, expectedSize[counter]);
		counter++;
	}
}
 
Example #17
Source File: TestSAMInputFormat.java    From Hadoop-BAM with MIT License 5 votes vote down vote up
@Test
public void testMapReduceJob() throws Exception {
  Configuration conf = new Configuration();

  FileSystem fileSystem = FileSystem.get(conf);
  Path inputPath = new Path(input);
  Path outputPath = fileSystem.makeQualified(new Path("target/out"));
  fileSystem.delete(outputPath, true);

  Job job = Job.getInstance(conf);
  FileInputFormat.setInputPaths(job, inputPath);
  job.setInputFormatClass(SAMInputFormat.class);
  job.setOutputKeyClass(LongWritable.class);
  job.setOutputValueClass(SAMRecordWritable.class);
  job.setNumReduceTasks(0);
  FileOutputFormat.setOutputPath(job, outputPath);

  boolean success = job.waitForCompletion(true);
  assertTrue(success);

  List<String> samStrings = new ArrayList<String>();
  SamReader samReader = SamReaderFactory.makeDefault().open(new File(input));
  for (SAMRecord r : samReader) {
    samStrings.add(r.getSAMString().trim());
  }
  samReader.close();

  File outputFile = new File(new File(outputPath.toUri()), "part-m-00000");
  BufferedReader br = new BufferedReader(new FileReader(outputFile));
  String line;
  int index = 0;
  while ((line = br.readLine()) != null) {
    String value = line.substring(line.indexOf("\t") + 1); // ignore key
    assertEquals(samStrings.get(index++), value);
  }
  br.close();
}
 
Example #18
Source File: BaseErrorAggregationTest.java    From picard with MIT License 5 votes vote down vote up
@Test
public void testBaseErrorAggregation() {
    final SAMSequenceRecord samSequenceRecord = new SAMSequenceRecord("chr1", 200);
    final SAMFileHeader samFileHeader = new SAMFileHeader();
    samFileHeader.addSequence(samSequenceRecord);

    final SAMRecordSetBuilder builder = new SAMRecordSetBuilder();

    final List<SAMRecord> samRecords = builder.addPair("Read1234", 0, 1, 1,
            false, false, "16M", "16M", true, false, 20);
    final SAMRecord samRecord1 = samRecords.get(0);
    final SAMRecord samRecord2 = samRecords.get(1);

    samRecord1.setReadBases("CgTGtGGAcAAAgAAA".getBytes());
    samRecord2.setReadBases("CcTGGtGAcAAAgAAA".getBytes());
    final byte[] refBases = "CATGGGGAAAAAAAAA".getBytes();

    BaseErrorAggregation<?> baseErrorAggregation = new BaseErrorAggregation<>(OverlappingReadsErrorCalculator::new, ReadBaseStratification.readOrdinalityStratifier);

    final int length = getLengthAndAddBases(samSequenceRecord, samRecord1, samRecord2, refBases, baseErrorAggregation);

    final ErrorMetric[] metrics = baseErrorAggregation.getMetrics();
    final OverlappingErrorMetric metric1 = (OverlappingErrorMetric) metrics[0];
    metric1.calculateDerivedFields();
    Assert.assertEquals(metric1.COVARIATE, ReadBaseStratification.ReadOrdinality.FIRST.name());
    Assert.assertEquals(metric1.TOTAL_BASES, length);
    Assert.assertEquals(metric1.NUM_BASES_WITH_OVERLAPPING_READS, length);
    Assert.assertEquals(metric1.NUM_DISAGREES_WITH_REFERENCE_ONLY, 2L);
    Assert.assertEquals(metric1.NUM_DISAGREES_WITH_REF_AND_MATE, 1L);
    Assert.assertEquals(metric1.NUM_THREE_WAYS_DISAGREEMENT, 1L);

    final OverlappingErrorMetric metric2 = (OverlappingErrorMetric) metrics[1];
    metric2.calculateDerivedFields();
    Assert.assertEquals(metric2.COVARIATE, ReadBaseStratification.ReadOrdinality.SECOND.name());
    Assert.assertEquals(metric2.TOTAL_BASES, length);
    Assert.assertEquals(metric2.NUM_BASES_WITH_OVERLAPPING_READS, length);
    Assert.assertEquals(metric2.NUM_DISAGREES_WITH_REFERENCE_ONLY, 2L);
    Assert.assertEquals(metric2.NUM_DISAGREES_WITH_REF_AND_MATE, 1L);
    Assert.assertEquals(metric2.NUM_THREE_WAYS_DISAGREEMENT, 1L);
}
 
Example #19
Source File: ScoringDeletionsTest.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void doesNotDetectDELinRef() {
    final SAMRecord reference = TestUtils.buildSamRecord(1, "11M", "GATCCCCGATC");
    final VariantScore score = SamRecordScoring.getVariantScore(reference, DELETION);
    assertEquals(ReadType.REF, score.type());
    assertTrue(score.score() > 0);
}
 
Example #20
Source File: ReadContextCounter.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
private int indelLength(@NotNull final SAMRecord record) {
    int result = 0;
    for (CigarElement cigarElement : record.getCigar()) {
        switch (cigarElement.getOperator()) {
            case I:
            case D:
                result += cigarElement.getLength();
        }

    }

    return result;
}
 
Example #21
Source File: OrientationTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static SAMRecord makeTestRecord(SAMFileHeader sfh, int flags, String sequence, String mateSequence, int pos, int matePos, String readGroup) {
  final SAMRecord sr = new SAMRecord(sfh);
  sr.setFlags(flags);
  sr.setReferenceName(sequence);
  sr.setMateReferenceName(mateSequence);
  sr.setAlignmentStart(pos);
  sr.setMateAlignmentStart(matePos);
  sr.setAttribute("RG", readGroup);
  if (pos < matePos) {
    sr.setInferredInsertSize(matePos - pos + 5);
  } else {
    sr.setInferredInsertSize(matePos - pos  - 5); //-1 * (pos - matePos + 5));
  }
  return sr;
}
 
Example #22
Source File: SamRecordScoring.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
public static Map<VariantContext, VariantScore> scoresPerVariant(@NotNull final SAMRecord record,
        @NotNull final Collection<VariantContext> variants) {
    return variants.stream()
            .map(variant -> ImmutablePair.of(variant, getVariantScore(record, variant)))
            .collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
}
 
Example #23
Source File: ScoringDeletionsTest.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void detectsDELinTumorWithDELAfter() {
    final SAMRecord tumor = TestUtils.buildSamRecord(1, "3M2D2M3D1M", "GATCCC");
    final VariantScore score = SamRecordScoring.getVariantScore(tumor, DELETION);
    assertEquals(ReadType.ALT, score.type());
    assertTrue(score.score() > 0);
}
 
Example #24
Source File: FastqSAMFileWriter.java    From cramtools with Apache License 2.0 5 votes vote down vote up
@Override
public void addAlignment(SAMRecord alignment) {
	PrintStream ps = s1;
	if (s2 != null && alignment.getReadPairedFlag() && alignment.getFirstOfPairFlag())
		ps = s2;

	printFastq(ps, alignment);
}
 
Example #25
Source File: ScoringDeletionsTest.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void detectsDELinTumorWithINSandDEL() {
    final SAMRecord tumor = TestUtils.buildSamRecord(1, "2M2I1M2D4M", "GATCTCCGA");
    final VariantScore score = SamRecordScoring.getVariantScore(tumor, DELETION);
    assertEquals(ReadType.ALT, score.type());
    assertTrue(score.score() > 0);
}
 
Example #26
Source File: SamUtils.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Use MAPQ and IH/NH attributes to determine if the read was uniquely mapped.
 * @param record the sam record
 * @return true if the record was uniquely mapped
 */
public static boolean uniquelyMapped(SAMRecord record) {
  assert !record.getReadUnmappedFlag();
  if (record.getMappingQuality() == 0 || record.isSecondaryAlignment()) {
    return false;
  }
  final Integer nh = SamUtils.getNHOrIH(record);
  return (nh == null) || nh == 1;
}
 
Example #27
Source File: HeaderInfo.java    From dataflow-java with Apache License 2.0 5 votes vote down vote up
public static HeaderInfo getHeaderFromBAMFile(Storage.Objects storage, String BAMPath, Iterable<Contig> explicitlyRequestedContigs) throws IOException {
  HeaderInfo result = null;

  // Open and read start of BAM
  LOG.info("Reading header from " + BAMPath);
  final SamReader samReader = BAMIO
      .openBAM(storage, BAMPath, ValidationStringency.DEFAULT_STRINGENCY);
  final SAMFileHeader header = samReader.getFileHeader();
  Contig firstContig = getFirstExplicitContigOrNull(header, explicitlyRequestedContigs);
  if (firstContig == null) {
    final SAMSequenceRecord seqRecord = header.getSequence(0);
    firstContig = new Contig(seqRecord.getSequenceName(), -1, -1);
  }

  LOG.info("Reading first chunk of reads from " + BAMPath);
  final SAMRecordIterator recordIterator = samReader.query(
      firstContig.referenceName, (int)firstContig.start + 1, (int)firstContig.end + 1, false);

  Contig firstShard = null;
  while (recordIterator.hasNext() && result == null) {
    SAMRecord record = recordIterator.next();
    final int alignmentStart = record.getAlignmentStart();
    if (firstShard == null && alignmentStart > firstContig.start &&
        (alignmentStart < firstContig.end || firstContig.end == -1)) {
      firstShard = new Contig(firstContig.referenceName, alignmentStart, alignmentStart);
      LOG.info("Determined first shard to be " + firstShard);
      result = new HeaderInfo(header, firstShard);
    }
  }
  recordIterator.close();
  samReader.close();

  if (result == null) {
    throw new IOException("Did not find reads for the first contig " + firstContig.toString());
  }
  LOG.info("Finished header reading from " + BAMPath);
  return result;
}
 
Example #28
Source File: GeneFunctionIteratorWrapperTest.java    From Drop-seq with MIT License 5 votes vote down vote up
@Test
// don't need to test FunctionalDataProcessorTest functions, but do need to test assignReadsToAllGenes and decoding LocusFunction from string attributes.
public void testAssignReadsToAllGenesFalse() {
	String geneTag="gn";
	String strandTag="gs";
	String functionTag="gf";
	boolean assignReadsToAllGenes=false;
	StrandStrategy strandFilterStrategy = StrandStrategy.SENSE;
	LocusFunction [] alf = {LocusFunction.CODING, LocusFunction.UTR, LocusFunction.INTRONIC};
	Collection<LocusFunction> acceptedLociFunctions = Arrays.asList(alf);

	// set up test data.
	String recName="H575CBGXY:4:23606:1714:16102";
	SAMRecord r1 = getRecord(recName);
	r1.setAttribute(geneTag, "A,B,C");
	r1.setAttribute(strandTag, "+,+,+");
	r1.setAttribute(functionTag, "CODING,INTRONIC,CODING");

	List<SAMRecord> list = new ArrayList<SAMRecord>();
	list.add(r1);

	GeneFunctionIteratorWrapper gfiw = new GeneFunctionIteratorWrapper(list.iterator(), geneTag, strandTag, functionTag, assignReadsToAllGenes, strandFilterStrategy, acceptedLociFunctions);
	int counter=0;
	while (gfiw.hasNext()) {
		SAMRecord result = gfiw.next();
		if (result.getStringAttribute(geneTag).equals("A")) {
			Assert.assertEquals(result.getStringAttribute(geneTag), "A");
			Assert.assertEquals(result.getStringAttribute(strandTag), "+");
			Assert.assertEquals(result.getStringAttribute(functionTag), "CODING");
		}
		if (result.getStringAttribute(geneTag).equals("C")) {
			Assert.assertEquals(result.getStringAttribute(geneTag), "B");
			Assert.assertEquals(result.getStringAttribute(strandTag), "+");
			Assert.assertEquals(result.getStringAttribute(functionTag), "CODING");
		}
		counter++;
	}
	// No reads emerge.
	Assert.assertEquals(0, counter);
}
 
Example #29
Source File: SetNmMdAndUqTagsTest.java    From picard with MIT License 5 votes vote down vote up
private void validateUq(final File input, final File reference) {
    final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(reference).open(input);
    final SAMRecordIterator iterator = reader.iterator();
    while (iterator.hasNext()){
        SAMRecord rec = iterator.next();
        if (!rec.getReadUnmappedFlag())
            Assert.assertNotNull(rec.getAttribute("UQ"));
    }
}
 
Example #30
Source File: RevertSam.java    From picard with MIT License 5 votes vote down vote up
void addAlignment(final SAMRecord rec) {
    final SAMFileWriter writer;
    if (outputByReadGroup) {
        writer = writerMap.get(rec.getReadGroup().getId());
    } else {
        writer = singleWriter;
    }
    writer.addAlignment(rec);
}