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

The following examples show how to use htsjdk.samtools.SAMRecord#setReadString() . 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: ClippingUtilityTest.java    From picard with MIT License 6 votes vote down vote up
@Test(dataProvider="clipPairedTestData")
public void testPairedEndClip(final String testName, final String read1, final String read2, final AdapterPair expected) {

    final SAMRecord rec1 = new SAMRecord(new SAMFileHeader());
    rec1.setReadString(read1);
    rec1.setFirstOfPairFlag(true);
    final SAMRecord rec2 = new SAMRecord(new SAMFileHeader());
    rec2.setReadString(read2);
    rec2.setSecondOfPairFlag(true);

    final AdapterPair result = ClippingUtility.adapterTrimIlluminaPairedReads(rec1, rec2,
            IlluminaAdapterPair.INDEXED, IlluminaAdapterPair.PAIRED_END);
    if (result != null) {
        Assert.assertEquals(result.getName(), expected.getName(), testName);
    }
    else {
        Assert.assertNull(expected, testName);
    }
}
 
Example 2
Source File: FastqToSam.java    From picard with MIT License 6 votes vote down vote up
private SAMRecord createSamRecord(final SAMFileHeader header, final String baseName, final FastqRecord frec, final boolean paired) {
    final SAMRecord srec = new SAMRecord(header);
    srec.setReadName(baseName);
    srec.setReadString(frec.getReadString());
    srec.setReadUnmappedFlag(true);
    srec.setAttribute(ReservedTagConstants.READ_GROUP_ID, READ_GROUP_NAME);
    final byte[] quals = StringUtil.stringToBytes(frec.getBaseQualityString());
    convertQuality(quals, QUALITY_FORMAT);
    for (final byte qual : quals) {
        final int uQual = qual & 0xff;
        if (uQual < MIN_Q || uQual > MAX_Q) {
            throw new PicardException("Base quality " + uQual + " is not in the range " + MIN_Q + ".." +
            MAX_Q + " for read " + frec.getReadHeader());
        }
    }
    srec.setBaseQualities(quals);

    if (paired) {
        srec.setReadPairedFlag(true);
        srec.setMateUnmappedFlag(true);
    }
    return srec ;
}
 
Example 3
Source File: SmartSamWriterTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static SAMRecord createRecord(int position, String ref) {
  final SAMFileHeader header = new SAMFileHeader();
  header.getSequenceDictionary().addSequence(new SAMSequenceRecord("A", 100000));
  header.getSequenceDictionary().addSequence(new SAMSequenceRecord("B", 100000));
  final SAMRecord rec = new SAMRecord(header);
  rec.setReferenceName(ref);
  rec.setReadName("read");
  rec.setReadString("ACGT");
  rec.setBaseQualityString("####");
  if (ref.equals(SAMRecord.NO_ALIGNMENT_REFERENCE_NAME)) {
    rec.setReadUnmappedFlag(true);
  } else {
    rec.setCigarString("4=");
    rec.setAlignmentStart(position);
  }
  return rec;
}
 
Example 4
Source File: ClippingUtilityTest.java    From picard with MIT License 5 votes vote down vote up
private SAMRecord createSamRecordWithAdapterSequence(final int readLength, final IlluminaAdapterPair adapterPair, final int adapterPosition) {
    final String adapterString = adapterPair.get3PrimeAdapterInReadOrder();
    final int replacementLength = Math.min(adapterString.length(), readLength - adapterPosition);
    final String adapterSubstring = adapterString.substring(0, replacementLength);
    final String readBases = replaceSubstring(makeBogusReadString(readLength), adapterSubstring, adapterPosition, adapterSubstring.length());
    final SAMRecord rec = new SAMRecord(null);
    rec.setReadString(readBases);
    return rec;
}
 
Example 5
Source File: TestUtils.java    From hmftools with GNU General Public License v3.0 5 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 boolean negativeStrand) {
    final SAMRecord record = new SAMRecord(null);
    record.setAlignmentStart(alignmentStart);
    record.setCigarString(cigar);
    record.setReadString(readString);
    record.setReadNegativeStrandFlag(negativeStrand);
    record.setBaseQualityString(qualities);
    record.setMappingQuality(20);
    record.setDuplicateReadFlag(false);
    record.setReadUnmappedFlag(false);
    return record;
}
 
Example 6
Source File: SortedSAMWriter.java    From abra2 with MIT License 5 votes vote down vote up
public void addAlignment(int sampleIdx, SAMRecordWrapper samRecord, int chromosomeChunkIdx) {
	Feature chunk = this.chromosomeChunker.getChunks().get(chromosomeChunkIdx);
	
	SAMRecord read = samRecord.getSamRecord();
	
	// Only output reads with original start pos within specified chromosomeChunk
	// Avoids reads being written in 2 different chunks
	
	int origAlignmentStart = read.getAlignmentStart();
	String yo = read.getStringAttribute("YO");
	if (yo != null) {
		String[] fields = yo.split(":");
		origAlignmentStart = Integer.parseInt(fields[1]);
	}
	
	if (origAlignmentStart >= chunk.getStart() && origAlignmentStart <= chunk.getEnd()) {
		
		if (samRecord.isUnalignedRc() && read.getReadUnmappedFlag()) {
			// This read was reverse complemented, but not updated.
			// Change it back to its original state.
			read.setReadString(rc.reverseComplement(read.getReadString()));
			read.setBaseQualityString(rc.reverse(read.getBaseQualityString()));
			read.setReadNegativeStrandFlag(!read.getReadNegativeStrandFlag());
		}
		
		writers[sampleIdx][chromosomeChunkIdx].addAlignment(read);
	}
}
 
Example 7
Source File: SAMRecordUtils.java    From abra2 with MIT License 5 votes vote down vote up
/**
 * Replace hard clips with soft clips.
 */
public static void replaceHardClips(SAMRecord read) {
	Cigar cigar = read.getCigar();
	
	if (cigar.getCigarElements().size() > 0) {
		CigarElement firstElement = cigar.getCigarElement(0);
		CigarElement lastElement  = cigar.getCigarElement(cigar.numCigarElements()-1);
		
		if ((firstElement.getOperator() == CigarOperator.H) ||
			(lastElement.getOperator() == CigarOperator.H)) {
			
			Cigar newCigar = new Cigar();
			
			boolean isFirst = true;
			
			for (CigarElement element : cigar.getCigarElements()) {
				if (element.getOperator() != CigarOperator.H) {
					newCigar.add(element);
				} else {
					CigarElement softClip = new CigarElement(element.getLength(), CigarOperator.S);
					newCigar.add(softClip);
					
					if (isFirst) {
						read.setReadString(padBases(element.getLength()) + read.getReadString());
					} else {
						read.setReadString(read.getReadString() + padBases(element.getLength()));							
					}
				}
				
				isFirst = false;
			}
			
			read.setCigar(newCigar);
		}
	}
}
 
Example 8
Source File: SAMRecordUtils.java    From abra2 with MIT License 5 votes vote down vote up
/**
 * Remove leading or trailing soft clips from the input read.
 * Does not modify a read entirely comprised of soft clips.
 */
public static void removeSoftClips(SAMRecord read) {
	
	Cigar cigar = read.getCigar();
	
	CigarElement firstElement = cigar.getCigarElement(0);
	CigarElement lastElement  = cigar.getCigarElement(cigar.numCigarElements()-1);
	
	if ((firstElement.getOperator() == CigarOperator.S) ||
		(lastElement.getOperator() == CigarOperator.S)) {
	
		Cigar newCigar = new Cigar();
		
		String bases = read.getReadString();
		//String qualities = read.getBaseQualityString();
				
		if (firstElement.getOperator() == CigarOperator.S) {
			bases = bases.substring(firstElement.getLength(), bases.length());
			//qualities = qualities.substring(firstElement.getLength(), qualities.length()-1);
		} else {
			newCigar.add(firstElement);
		}
		
		for (int i=1; i<cigar.numCigarElements()-1; i++) {
			newCigar.add(cigar.getCigarElement(i));
		}
		
		if (lastElement.getOperator() == CigarOperator.S) {
			bases = bases.substring(0, bases.length() - lastElement.getLength());
			//qualities = qualities.substring(0, qualities.length() - lastElement.getLength() - 1);
		} else {
			newCigar.add(lastElement);
		}
		
		read.setCigar(newCigar);
		read.setReadString(bases);
		//read.setBaseQualityString(qualities);
	}
}
 
Example 9
Source File: SAMRecordUtilsTest.java    From abra2 with MIT License 5 votes vote down vote up
@Test (groups = "unit" )
public void testRemoveSoftClips_withDeletionAndSoftClipAtEnd() {
	SAMRecord read = new SAMRecord(null);
	
	read.setReadName("TEST1");
	read.setCigarString("5M2D3M2S");
	read.setReadString("CCCCCCAGCC");
	
	SAMRecordUtils.removeSoftClips(read);
	
	Assert.assertEquals(read.getCigarString(), "5M2D3M");
	Assert.assertEquals(read.getReadString(), "CCCCCCAG");
}
 
Example 10
Source File: SAMRecordUtilsTest.java    From abra2 with MIT License 5 votes vote down vote up
@Test (groups = "unit" )
public void testRemoveSoftClips_withDeletionAndSoftClipAtStart() {
	SAMRecord read = new SAMRecord(null);
	
	read.setReadName("TEST1");
	read.setCigarString("2S5M2D3M");
	read.setReadString("CCCCCCAGCC");
	
	SAMRecordUtils.removeSoftClips(read);
	
	Assert.assertEquals(read.getCigarString(), "5M2D3M");
	Assert.assertEquals(read.getReadString(), "CCCCAGCC");
}
 
Example 11
Source File: SAMRecordsTest.java    From hmftools with GNU General Public License v3.0 5 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);
    return record;
}
 
Example 12
Source File: SamUtilsTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void testCheckNhPicard() {
  final SAMRecord rec = new SAMRecord(null);
  rec.setReadString("TATTAGGATTGAGACTGGTAAAATGGNCCACCAAG");
  assertEquals(null, SamUtils.getNHOrIH(rec));
  rec.setAttribute("IH", 1);
  assertEquals(Integer.valueOf(1), SamUtils.getNHOrIH(rec));
  rec.setAttribute("NH", 2);
  assertEquals(Integer.valueOf(2), SamUtils.getNHOrIH(rec));
}
 
Example 13
Source File: SamOutputTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void test() throws IOException {
  final SAMFileHeader header = new SAMFileHeader();
  header.setSortOrder(SAMFileHeader.SortOrder.coordinate);
  header.getSequenceDictionary().addSequence(new SAMSequenceRecord("seq", 1000000));
  final SAMRecord rec = new SAMRecord(header);
  rec.setAlignmentStart(500000);
  rec.setReferenceName("seq");
  rec.setReadName("read");
  rec.setReadString("acgt");
  rec.setBaseQualityString("####");
  rec.setCigarString("4=");
  try (TestDirectory dir = new TestDirectory()) {
    final MemoryPrintStream mps = new MemoryPrintStream();
    try (SamOutput out = SamOutput.getSamOutput(new File(dir, "foo"), mps.outputStream(), header, true, true, null)) {
      out.getWriter().addAlignment(rec);
    }
    mNano.check("samoutput_expected_1.sam", MainResult.run(new ExtractCli(), "--header", new File(dir, "foo.bam").getPath(), "seq:500000+1").out());
    assertTrue(new File(dir, "foo.bam.bai").exists());
    assertEquals("", mps.toString());

    try (SamOutput out = SamOutput.getSamOutput(new File(dir, "foo.sam"), mps.outputStream(), header, true, true, null)) {
      out.getWriter().addAlignment(rec);
    }
    mNano.check("samoutput_expected_1.sam", MainResult.run(new ExtractCli(), "--header", new File(dir, "foo.sam.gz").getPath(), "seq:500000+1").out());
    assertTrue(new File(dir, "foo.sam.gz.tbi").exists());
    assertEquals("", mps.toString());

    try (SamOutput out = SamOutput.getSamOutput(new File(dir, "foo.sam"), mps.outputStream(), header, false, true, null)) {
      out.getWriter().addAlignment(rec);
    }
    mNano.check("samoutput_expected_1.sam", FileHelper.fileToString(new File(dir, "foo.sam")));
    assertFalse(new File(dir, "foo.sam.tbi").exists());
    assertEquals("", mps.toString());

    try (SamOutput out = SamOutput.getSamOutput(new File("-"), mps.outputStream(), header, true, true, null)) {
      out.getWriter().addAlignment(rec);
    }
    mNano.check("samoutput_expected_1.sam", mps.toString());
  }
}
 
Example 14
Source File: DefaultSamFilterTest.java    From rtg-tools with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void testFilterReadLength() {
  final SamFilterParams.SamFilterParamsBuilder builder = SamFilterParams.builder().minLength(10);
  final DefaultSamFilter f = new DefaultSamFilter(builder.create());
  final DefaultSamFilter notf = new DefaultSamFilter(builder.invertFilters(true).create());
  final SAMRecord rec = new SAMRecord(new SAMFileHeader());
  assertFalse(f.acceptRecord(rec));
  assertTrue(notf.acceptRecord(rec));
  rec.setReadString("atgcatgcatgcatgcatgcatgcatgc");
  assertTrue(f.acceptRecord(rec));
  assertFalse(notf.acceptRecord(rec));
}
 
Example 15
Source File: ReadContextFactoryTest.java    From hmftools with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
static SAMRecord buildSamRecord(@NotNull final String cigar, @NotNull final String readString, @NotNull final String qualities) {
    final SAMRecord record = new SAMRecord(null);
    record.setAlignmentStart(1000);
    record.setCigarString(cigar);
    record.setReadString(readString);
    record.setReadNegativeStrandFlag(false);
    record.setBaseQualityString(qualities);
    record.setMappingQuality(20);
    record.setDuplicateReadFlag(false);
    record.setReadUnmappedFlag(false);
    return record;
}
 
Example 16
Source File: MergeBamAlignmentTest.java    From picard with MIT License 4 votes vote down vote up
/**
 * @return a 2-element array in which the first element is the unmapped SAM, and the second the mapped SAM.
 */
private File[] createSamFilesToBeMerged(final MultipleAlignmentSpec[] specs) {
    try {
        final File unmappedSam = File.createTempFile("unmapped.", ".sam");
        unmappedSam.deleteOnExit();
        final SAMFileWriterFactory factory = new SAMFileWriterFactory();
        final SAMFileHeader header = new SAMFileHeader();
        header.setSortOrder(SAMFileHeader.SortOrder.queryname);
        final SAMRecord unmappedRecord = new SAMRecord(header);

        unmappedRecord.setReadName("theRead");
        unmappedRecord.setReadString("ACGTACGTACGTACGT");
        unmappedRecord.setBaseQualityString("5555555555555555");
        unmappedRecord.setReadUnmappedFlag(true);

        final SAMFileWriter unmappedWriter = factory.makeSAMWriter(header, false, unmappedSam);
        unmappedWriter.addAlignment(unmappedRecord);
        unmappedWriter.close();

        final File alignedSam = File.createTempFile("aligned.", ".sam");
        alignedSam.deleteOnExit();

        final String sequence = "chr1";
        // Populate the header with SAMSequenceRecords
        header.setSequenceDictionary(SAMSequenceDictionaryExtractor.extractDictionary(sequenceDict2.toPath()));

        final SAMFileWriter alignedWriter = factory.makeSAMWriter(header, false, alignedSam);
        for (final MultipleAlignmentSpec spec : specs) {
            final SAMRecord alignedRecord = new SAMRecord(header);
            alignedRecord.setReadName(unmappedRecord.getReadName());
            alignedRecord.setReadBases(unmappedRecord.getReadBases());
            alignedRecord.setBaseQualities(unmappedRecord.getBaseQualities());
            alignedRecord.setReferenceName(sequence);
            alignedRecord.setAlignmentStart(1);
            alignedRecord.setReadNegativeStrandFlag(spec.reverseStrand);
            alignedRecord.setCigarString(spec.cigar);
            alignedRecord.setMappingQuality(spec.mapQ);
            if (spec.oneOfTheBest) {
                alignedRecord.setAttribute(ONE_OF_THE_BEST_TAG, 1);
            }
            alignedWriter.addAlignment(alignedRecord);
        }
        alignedWriter.close();

        return new File[]{unmappedSam, alignedSam};
    } catch (IOException e) {
        throw new PicardException(e.getMessage(), e);
    }
}
 
Example 17
Source File: MergeBamAlignmentTest.java    From picard with MIT License 4 votes vote down vote up
private void testBestFragmentMapqStrategy(final String testName, final int[] firstMapQs, final int[] secondMapQs,
                                          final boolean includeSecondary, final int expectedFirstMapq,
                                          final int expectedSecondMapq) throws Exception {
    final File unmappedSam = File.createTempFile("unmapped.", ".sam");
    unmappedSam.deleteOnExit();
    final SAMFileWriterFactory factory = new SAMFileWriterFactory();
    final SAMFileHeader header = new SAMFileHeader();
    header.setSortOrder(SAMFileHeader.SortOrder.queryname);

    final String readName = "theRead";
    final SAMRecord firstUnmappedRead = new SAMRecord(header);
    firstUnmappedRead.setReadName(readName);
    firstUnmappedRead.setReadString("ACGTACGTACGTACGT");
    firstUnmappedRead.setBaseQualityString("5555555555555555");
    firstUnmappedRead.setReadUnmappedFlag(true);
    firstUnmappedRead.setMateUnmappedFlag(true);
    firstUnmappedRead.setReadPairedFlag(true);
    firstUnmappedRead.setFirstOfPairFlag(true);

    final SAMRecord secondUnmappedRead = new SAMRecord(header);
    secondUnmappedRead.setReadName(readName);
    secondUnmappedRead.setReadString("TCGAACGTTCGAACTG");
    secondUnmappedRead.setBaseQualityString("6666666666666666");
    secondUnmappedRead.setReadUnmappedFlag(true);
    secondUnmappedRead.setMateUnmappedFlag(true);
    secondUnmappedRead.setReadPairedFlag(true);
    secondUnmappedRead.setSecondOfPairFlag(true);

    final SAMFileWriter unmappedWriter = factory.makeSAMWriter(header, false, unmappedSam);
    unmappedWriter.addAlignment(firstUnmappedRead);
    unmappedWriter.addAlignment(secondUnmappedRead);
    unmappedWriter.close();

    final File alignedSam = File.createTempFile("aligned.", ".sam");
    alignedSam.deleteOnExit();

    final String sequence = "chr1";
    // Populate the header with SAMSequenceRecords
    header.setSequenceDictionary(SAMSequenceDictionaryExtractor.extractDictionary(sequenceDict2.toPath()));

    final SAMFileWriter alignedWriter = factory.makeSAMWriter(header, false, alignedSam);

    addAlignmentsForBestFragmentMapqStrategy(alignedWriter, firstUnmappedRead, sequence, firstMapQs);
    addAlignmentsForBestFragmentMapqStrategy(alignedWriter, secondUnmappedRead, sequence, secondMapQs);
    alignedWriter.close();

    final File output = File.createTempFile("testBestFragmentMapqStrategy." + testName, ".sam");
    output.deleteOnExit();
    doMergeAlignment(unmappedSam, Collections.singletonList(alignedSam),
            null, null, null, null,
            false, true, false, 1,
            "0", "1.0", "align!", "myAligner",
            true,
            fasta, output,
            SamPairUtil.PairOrientation.FR,
            MergeBamAlignment.PrimaryAlignmentStrategy.BestEndMapq,
            null, includeSecondary, null, null);
    final SamReader reader = SamReaderFactory.makeDefault().open(output);

    int numFirstRecords = 0;
    int numSecondRecords = 0;
    int firstPrimaryMapq = -1;
    int secondPrimaryMapq = -1;
    for (final SAMRecord rec: reader) {
        Assert.assertTrue(rec.getReadPairedFlag());
        if (rec.getFirstOfPairFlag()) ++numFirstRecords;
        else if (rec.getSecondOfPairFlag()) ++ numSecondRecords;
        else Assert.fail("unpossible!");
        if (!rec.getReadUnmappedFlag() && !rec.getNotPrimaryAlignmentFlag()) {
            if (rec.getFirstOfPairFlag()) {
                Assert.assertEquals(firstPrimaryMapq, -1);
                firstPrimaryMapq = rec.getMappingQuality();
            } else {
                Assert.assertEquals(secondPrimaryMapq, -1);
                secondPrimaryMapq = rec.getMappingQuality();
            }
        } else if (rec.getNotPrimaryAlignmentFlag()) {
            Assert.assertTrue(rec.getMateUnmappedFlag());
        }
    }
    reader.close();
    Assert.assertEquals(firstPrimaryMapq, expectedFirstMapq);
    Assert.assertEquals(secondPrimaryMapq, expectedSecondMapq);
    if (!includeSecondary) {
        Assert.assertEquals(numFirstRecords, 1);
        Assert.assertEquals(numSecondRecords, 1);
    } else {
        // If no alignments for an end, there will be a single unmapped record
        Assert.assertEquals(numFirstRecords, Math.max(1, firstMapQs.length));
        Assert.assertEquals(numSecondRecords, Math.max(1, secondMapQs.length));
    }
}
 
Example 18
Source File: MergeBamAlignmentTest.java    From picard with MIT License 4 votes vote down vote up
/**
 * Confirm that paired reads are rejected by PrimaryAlignmentStrategy.EarliestFragment.
 */
@Test(expectedExceptions = UnsupportedOperationException.class)
public void testEarliestFragmentStrategyPaired() throws Exception {
    final File output = File.createTempFile("mergeTest", ".sam");
    output.deleteOnExit();

    final File unmappedSam = File.createTempFile("unmapped.", ".sam");
    unmappedSam.deleteOnExit();
    final SAMFileWriterFactory factory = new SAMFileWriterFactory();
    final SAMFileHeader header = new SAMFileHeader();
    header.setSortOrder(SAMFileHeader.SortOrder.queryname);
    final String cigar = "16M";

    final SAMRecord firstOfPair = new SAMRecord(header);
    firstOfPair.setReadName("theRead");
    firstOfPair.setReadString("ACGTACGTACGTACGT");
    firstOfPair.setBaseQualityString("5555555555555555");
    firstOfPair.setReadUnmappedFlag(true);
    firstOfPair.setReadPairedFlag(true);
    firstOfPair.setFirstOfPairFlag(true);

    final SAMRecord secondOfPair = new SAMRecord(header);
    secondOfPair.setReadName("theRead");
    secondOfPair.setReadString("ACGTACGTACGTACGT");
    secondOfPair.setBaseQualityString("5555555555555555");
    secondOfPair.setReadUnmappedFlag(true);
    secondOfPair.setReadPairedFlag(true);
    secondOfPair.setSecondOfPairFlag(true);
    SamPairUtil.setMateInfo(firstOfPair, secondOfPair);

    final SAMFileWriter unmappedWriter = factory.makeSAMWriter(header, false, unmappedSam);
    unmappedWriter.addAlignment(firstOfPair);
    unmappedWriter.addAlignment(secondOfPair);
    unmappedWriter.close();

    final File alignedSam = File.createTempFile("aligned.", ".sam");
    alignedSam.deleteOnExit();

    // Populate the header with SAMSequenceRecords
    header.setSequenceDictionary(SAMSequenceDictionaryExtractor.extractDictionary(sequenceDict2.toPath()));

    // Create 2 alignments for each end of pair
    final SAMFileWriter alignedWriter = factory.makeSAMWriter(header, false, alignedSam);
    for (int i = 1; i <= 2; ++i) {
        final SAMRecord firstOfPairAligned = new SAMRecord(header);
        firstOfPairAligned.setReadName(firstOfPair.getReadName());
        firstOfPairAligned.setReadBases(firstOfPair.getReadBases());
        firstOfPairAligned.setBaseQualities(firstOfPair.getBaseQualities());
        firstOfPairAligned.setReferenceName("chr1");
        firstOfPairAligned.setAlignmentStart(i);
        firstOfPairAligned.setCigarString(cigar);
        firstOfPairAligned.setMappingQuality(100);
        firstOfPairAligned.setReadPairedFlag(true);
        firstOfPairAligned.setFirstOfPairFlag(true);
        firstOfPairAligned.setAttribute(SAMTag.HI.name(), i);

        final SAMRecord secondOfPairAligned = new SAMRecord(header);
        secondOfPairAligned.setReadName(secondOfPair.getReadName());
        secondOfPairAligned.setReadBases(secondOfPair.getReadBases());
        secondOfPairAligned.setBaseQualities(secondOfPair.getBaseQualities());
        secondOfPairAligned.setReferenceName("chr1");
        secondOfPairAligned.setAlignmentStart(i + 10);
        secondOfPairAligned.setCigarString(cigar);
        secondOfPairAligned.setMappingQuality(100);
        secondOfPairAligned.setReadPairedFlag(true);
        secondOfPairAligned.setSecondOfPairFlag(true);
        secondOfPairAligned.setAttribute(SAMTag.HI.name(), i);

        SamPairUtil.setMateInfo(firstOfPairAligned, secondOfPairAligned);

        alignedWriter.addAlignment(firstOfPairAligned);
        alignedWriter.addAlignment(secondOfPairAligned);
    }
    alignedWriter.close();

    doMergeAlignment(unmappedSam, Collections.singletonList(alignedSam),
            null, null, null, null,
            false, true, false, 1,
            "0", "1.0", "align!", "myAligner",
            true,
            fasta, output,
            SamPairUtil.PairOrientation.FR,
            MergeBamAlignment.PrimaryAlignmentStrategy.EarliestFragment,
            null, null, null, null);

    Assert.fail("Exception was not thrown");
}
 
Example 19
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 20
Source File: ClippingUtilityTest.java    From picard with MIT License 4 votes vote down vote up
/**
 * Confirm that after the requisite number of sightings of adapter, the list is trimmed
 */
@Test(dataProvider = "testAdapterListTruncationDataProvider")
public void testAdapterListTruncation(final IlluminaAdapterPair adapterPair) {
    final int thresholdForTruncatingAdapterList = 20;
    final int readLength = 100;

    // Throw all the adapter pairs into the marker.  There is some danger in doing this because
    // IlluminaAdapterPair.ALTERNATIVE_SINGLE_END has 3' that is a substring of some of the others.  In the enum it appears
    // last so it is tested last.
    final AdapterMarker marker =
            new AdapterMarker(IlluminaUtil.IlluminaAdapterPair.values()).
                    setThresholdForSelectingAdaptersToKeep(thresholdForTruncatingAdapterList);
    int adapterPosition = 1;

    Assert.assertTrue(adapterPosition + thresholdForTruncatingAdapterList < readLength - ClippingUtility.MIN_MATCH_BASES,
            "Test is configured improperly -- eventually will not be enough adapter bases in read.");

    int originalNumberOfAdapters = marker.getAdapters().length;
    for (int i = 0; i < thresholdForTruncatingAdapterList; ++i) {

        // First, a read with no adapter in it.
        SAMRecord rec = new SAMRecord(null);
        rec.setReadString(makeBogusReadString(readLength));
        AdapterPair matchedPair = marker.adapterTrimIlluminaSingleRead(rec);
        Assert.assertNull(matchedPair);
        Assert.assertNull(rec.getAttribute(ReservedTagConstants.XT));

        // Adapter list should not be truncated yet
        Assert.assertEquals(marker.getAdapters().length, originalNumberOfAdapters);

        // Then, a record with some adapter sequence in it.
        rec = createSamRecordWithAdapterSequence(readLength, adapterPair, adapterPosition);
        matchedPair = marker.adapterTrimIlluminaSingleRead(rec);
        Assert.assertNotNull(matchedPair);
        Assert.assertEquals(rec.getIntegerAttribute(ReservedTagConstants.XT).intValue(), adapterPosition + 1,
                rec.getReadString() + " matched " + matchedPair);

        // Put adapter in different place next time.
        adapterPosition++;
    }

    Assert.assertEquals(marker.getAdapters().length, 1, "Did not truncate adapter list to 1 element");
    Assert.assertTrue(marker.getAdapters()[0].getName().contains(adapterPair.getName()),
            String.format("Expected '%s' to contain '%s'", marker.getAdapters()[0].getName(), adapterPair.getName()));
}