Java Code Examples for htsjdk.samtools.SAMRecord#setProperPairFlag()

The following examples show how to use htsjdk.samtools.SAMRecord#setProperPairFlag() . 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: ReadContextCounterTest.java    From hmftools with GNU General Public License v3.0 6 votes vote down vote up
@NotNull
public static SAMRecord buildSamRecord(final int alignmentStart, @NotNull final String cigar, @NotNull final String readString,
        @NotNull final String qualities) {
    final SAMRecord record = new SAMRecord(null);
    record.setAlignmentStart(alignmentStart);
    record.setCigarString(cigar);
    record.setReadString(readString);
    record.setReadNegativeStrandFlag(false);
    record.setBaseQualityString(qualities);
    record.setMappingQuality(20);
    record.setDuplicateReadFlag(false);
    record.setReadUnmappedFlag(false);
    record.setProperPairFlag(true);
    record.setReadPairedFlag(true);
    return record;
}
 
Example 2
Source File: PalindromeArtifactClipReadTransformerUnitTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private static GATKRead makeRead(final SAMFileHeader header, final String contig, final int readStart, final int fragmentLength, final byte[] bases, final byte[] qual, final String cigar) {
    final SAMRecord read = new SAMRecord(header);
    read.setReferenceName(contig);
    read.setAlignmentStart(readStart);
    read.setReadPairedFlag(true);
    read.setReadUnmappedFlag(false);
    read.setMateUnmappedFlag(false);
    read.setMateReferenceName("Mate");
    read.setReadNegativeStrandFlag(false);
    read.setMateNegativeStrandFlag(true);
    read.setReadBases(bases);
    read.setBaseQualities(qual);


    final int mateStart = readStart + fragmentLength - bases.length;
    read.setMateAlignmentStart(mateStart);
    read.setInferredInsertSize(fragmentLength);
    read.setProperPairFlag(true);
    read.setCigarString(cigar);


    return new SAMRecordToGATKReadAdapter(read);
}
 
Example 3
Source File: FilterBamByTagTest.java    From Drop-seq with MIT License 5 votes vote down vote up
@Test
public void filterByReadNumberTest() {
	FilterBamByTag t = new FilterBamByTag();

	// record paired and read is 1st
	List<SAMRecord> recs = getPairedRead ();
	SAMRecord recFirstPaired = recs.get(0);
	SAMRecord recSecondPaired = recs.get(1);

	boolean flag1= t.retainByReadNumber(recFirstPaired, 1);
	boolean flag2= t.retainByReadNumber(recFirstPaired, 2);
	Assert.assertTrue(flag1);
	Assert.assertFalse(flag2);

	// record paired and read is 2st
	recSecondPaired.setProperPairFlag(true);
	recSecondPaired.setSecondOfPairFlag(true);
	flag1= t.retainByReadNumber(recSecondPaired, 1);
	flag2= t.retainByReadNumber(recSecondPaired, 2);
	Assert.assertTrue(flag2);
	Assert.assertFalse(flag1);

	// record unpaired and read is 1st
	SAMRecordSetBuilder builder = new SAMRecordSetBuilder();
	builder.addUnmappedFragment("foo");
	SAMRecord recFirstUnPaired = builder.getRecords().iterator().next();

	flag1= t.retainByReadNumber(recFirstUnPaired, 1);
	flag2= t.retainByReadNumber(recFirstPaired, 2);
	Assert.assertTrue(flag1);
	Assert.assertFalse(flag2);
}
 
Example 4
Source File: SamUtils.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Convert an aligned record to being unmapped
 * @param record the record to adjust
 */
public static void convertToUnmapped(SAMRecord record) {
  record.setReadUnmappedFlag(true);
  record.setProperPairFlag(false);
  record.setReadNegativeStrandFlag(false);
  record.setMappingQuality(0);
  record.setInferredInsertSize(0);
  record.setCigarString("*");
  record.setAttribute(ATTRIBUTE_NUM_MISMATCHES, null);
}
 
Example 5
Source File: DefaultSamFilterTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void testFilterRecordByFlags() {
  final SamFilterParams.SamFilterParamsBuilder builder = SamFilterParams.builder();
  final SAMRecord rec = new SAMRecord(new SAMFileHeader()); // Not unmapped but alignment position == 0
  rec.setReadUnmappedFlag(true);      // Unmapped with alignment position == 0
  assertTrue(new DefaultSamFilter(builder.create()).acceptRecord(rec));
  builder.excludeUnmapped(true);
  assertFalse(new DefaultSamFilter(builder.create()).acceptRecord(rec));

  rec.setReadUnmappedFlag(false);      // Mapped with alignment position == 10
  rec.setAlignmentStart(10);
  assertTrue(new DefaultSamFilter(builder.create()).acceptRecord(rec));

  rec.setDuplicateReadFlag(true);      // Now a duplicate
  assertTrue(new DefaultSamFilter(builder.create()).acceptRecord(rec));
  builder.excludeDuplicates(true);
  assertFalse(new DefaultSamFilter(builder.create()).acceptRecord(rec));
  builder.excludeDuplicates(false);

  rec.setReadPairedFlag(true); // Now paired-end

  builder.excludeUnmated(true);
  assertFalse(new DefaultSamFilter(builder.create()).acceptRecord(rec));
  rec.setProperPairFlag(true); // Now properly paired (i.e. no longer unmated)
  assertTrue(new DefaultSamFilter(builder.create()).acceptRecord(rec));

  builder.excludeMated(true);
  assertFalse(new DefaultSamFilter(builder.create()).acceptRecord(rec));
}
 
Example 6
Source File: SamCompareUtilsTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void test() {
  final SAMFileHeader header = new SAMFileHeader();
  header.setSequenceDictionary(new SAMSequenceDictionary(Arrays.asList(new SAMSequenceRecord("raga", 100), new SAMSequenceRecord("yaga", 100), new SAMSequenceRecord("zaga", 100))));
  final SAMRecord rec1 = new SAMRecord(header);
  rec1.setReferenceIndex(1);
  final SAMRecord rec2 = new SAMRecord(header);
  rec2.setReferenceIndex(2);
  assertEquals(-1, SamCompareUtils.compareSamRecords(rec1, rec2));
  assertEquals(1, SamCompareUtils.compareSamRecords(rec2, rec1));
  rec1.setReferenceIndex(2);
  rec1.setAlignmentStart(50);
  rec2.setAlignmentStart(25);
  assertEquals(1, SamCompareUtils.compareSamRecords(rec1, rec2));
  assertEquals(-1, SamCompareUtils.compareSamRecords(rec2, rec1));
  rec1.setReadPairedFlag(true);
  rec2.setReadPairedFlag(true);
  rec1.setProperPairFlag(true);
  rec2.setProperPairFlag(false);
  rec1.setAlignmentStart(25);
  assertEquals(-1, SamCompareUtils.compareSamRecords(rec1, rec2));
  assertEquals(1, SamCompareUtils.compareSamRecords(rec2, rec1));
  rec2.setProperPairFlag(true);
  rec1.setReadUnmappedFlag(true);
  assertEquals(1, SamCompareUtils.compareSamRecords(rec1, rec2));
  assertEquals(-1, SamCompareUtils.compareSamRecords(rec2, rec1));
  rec2.setReadUnmappedFlag(true);
  assertEquals(0, SamCompareUtils.compareSamRecords(rec1, rec2));
  assertEquals(0, SamCompareUtils.compareSamRecords(rec2, rec1));
  rec1.setReferenceIndex(-1);
  assertEquals(1, SamCompareUtils.compareSamRecords(rec1, rec2));
  assertEquals(-1, SamCompareUtils.compareSamRecords(rec2, rec1));
  rec2.setReferenceIndex(-1);
  assertEquals(0, SamCompareUtils.compareSamRecords(rec2, rec1));
}
 
Example 7
Source File: RevertSam.java    From picard with MIT License 4 votes vote down vote up
/**
 * Takes an individual SAMRecord and applies the set of changes/reversions to it that
 * have been requested by program level options.
 */
public void revertSamRecord(final SAMRecord rec) {
    if (RESTORE_ORIGINAL_QUALITIES) {
        final byte[] oq = rec.getOriginalBaseQualities();
        if (oq != null) {
            rec.setBaseQualities(oq);
            rec.setOriginalBaseQualities(null);
        }
    }

    if (REMOVE_DUPLICATE_INFORMATION) {
        rec.setDuplicateReadFlag(false);
    }

    if (REMOVE_ALIGNMENT_INFORMATION) {
        if (rec.getReadNegativeStrandFlag()) {
            rec.reverseComplement(true);
            rec.setReadNegativeStrandFlag(false);
        }

        // Remove all alignment based information about the read itself
        rec.setReferenceIndex(SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX);
        rec.setAlignmentStart(SAMRecord.NO_ALIGNMENT_START);
        rec.setCigarString(SAMRecord.NO_ALIGNMENT_CIGAR);
        rec.setMappingQuality(SAMRecord.NO_MAPPING_QUALITY);

        rec.setInferredInsertSize(0);
        rec.setNotPrimaryAlignmentFlag(false);
        rec.setProperPairFlag(false);
        rec.setReadUnmappedFlag(true);

        // Then remove any mate flags and info related to alignment
        rec.setMateAlignmentStart(SAMRecord.NO_ALIGNMENT_START);
        rec.setMateNegativeStrandFlag(false);
        rec.setMateReferenceIndex(SAMRecord.NO_ALIGNMENT_REFERENCE_INDEX);
        rec.setMateUnmappedFlag(rec.getReadPairedFlag());

        if (RESTORE_HARDCLIPS) {
            String hardClippedBases = rec.getStringAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASES_TAG);
            String hardClippedQualities = rec.getStringAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASE_QUALITIES_TAG);
            if (hardClippedBases != null && hardClippedQualities != null) {
                // Record has already been reverse complemented if this was on the negative strand
                rec.setReadString(rec.getReadString() + hardClippedBases);
                rec.setBaseQualities(SAMUtils.fastqToPhred(SAMUtils.phredToFastq(rec.getBaseQualities()) + hardClippedQualities));

                // Remove hard clipping storage tags
                rec.setAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASES_TAG, null);
                rec.setAttribute(AbstractAlignmentMerger.HARD_CLIPPED_BASE_QUALITIES_TAG, null);
            }
        }

        // And then remove any tags that are calculated from the alignment
        ATTRIBUTE_TO_CLEAR.forEach(tag -> rec.setAttribute(tag, null));
    }
}
 
Example 8
Source File: Read.java    From cramtools with Apache License 2.0 3 votes vote down vote up
SAMRecord[] toSAMRecord(SAMFileHeader header) {

		SAMRecord first = firstSAMRecord(header);
		SAMRecord second = secondSAMRecord(header);

		SamPairUtil.setMateInfo(first, second, header);

		first.setProperPairFlag(true);
		second.setProperPairFlag(true);

		return new SAMRecord[] { first, second };
	}