Package htsjdk.samtools

Examples of htsjdk.samtools.SAMFileReader


                                                final Integer expectedPrimaryHitIndex, final int expectedNumFirst,
                                                final int expectedNumSecond, final int expectedPrimaryMapq) throws Exception {

        // Create the aligned file by copying bases, quals, readname from the unmapped read, and conforming to each HitSpec.
        final File unmappedSam = new File(TEST_DATA_DIR, "multihit.filter.unmapped.sam");
        final SAMRecordIterator unmappedSamFileIterator = new SAMFileReader(unmappedSam).iterator();
        final SAMRecord firstUnmappedRec = unmappedSamFileIterator.next();
        final SAMRecord secondUnmappedRec = unmappedSamFileIterator.next();
        unmappedSamFileIterator.close();
        final File alignedSam = File.createTempFile("aligned.", ".sam");
        alignedSam.deleteOnExit();
        final SAMFileHeader alignedHeader = new SAMFileHeader();
        alignedHeader.setSortOrder(SAMFileHeader.SortOrder.queryname);
        alignedHeader.setSequenceDictionary(new SAMFileReader(sequenceDict).getFileHeader().getSequenceDictionary());
        final SAMFileWriter alignedWriter = new SAMFileWriterFactory().makeSAMWriter(alignedHeader, true, alignedSam);
        for (int i = 0; i < Math.max(firstOfPair.size(), secondOfPair.size()); ++i) {
            final HitSpec firstHitSpec = firstOfPair.isEmpty()? null: firstOfPair.get(i);
            final HitSpec secondHitSpec = secondOfPair.isEmpty()? null: secondOfPair.get(i);
            final SAMRecord first = makeRead(alignedHeader, firstUnmappedRec, firstHitSpec, true, i);
            final SAMRecord second = makeRead(alignedHeader, secondUnmappedRec, secondHitSpec, false, i);
            if (first != null && second != null) SamPairUtil.setMateInfo(first, second, alignedHeader);
            if (first != null) {
                if (second == null) first.setMateUnmappedFlag(true);
                alignedWriter.addAlignment(first);
            }
            if (second != null) {
                if (first == null) second.setMateUnmappedFlag(true);
                alignedWriter.addAlignment(second);
            }
        }
        alignedWriter.close();

        // Merge aligned file with original unmapped file.
        final File mergedSam = File.createTempFile("merged.", ".sam");
        mergedSam.deleteOnExit();

        doMergeAlignment(unmappedSam, Collections.singletonList(alignedSam),
                null, null, null, null,
                false, true, false, 1,
                "0", "1.0", "align!", "myAligner",
                true, fasta, mergedSam,
                null, null, null, null
        );

        assertSamValid(mergedSam);

        // Tally metrics and check for agreement with expected.
        final SAMFileReader mergedReader = new SAMFileReader(mergedSam);
        int numFirst = 0;
        int numSecond = 0;
        Integer primaryHitIndex = null;
        int primaryMapq = 0;
        for (final SAMRecord rec : mergedReader) {
View Full Code Here


                                                final Integer expectedPrimaryHitIndex, final int expectedNumReads,
                                                final int expectedPrimaryMapq) throws Exception {

        // Create the aligned file by copying bases, quals, readname from the unmapped read, and conforming to each HitSpec.
        final File unmappedSam = new File(TEST_DATA_DIR, "multihit.filter.fragment.unmapped.sam");
        final SAMRecordIterator unmappedSamFileIterator = new SAMFileReader(unmappedSam).iterator();
        final SAMRecord unmappedRec = unmappedSamFileIterator.next();
        unmappedSamFileIterator.close();
        final File alignedSam = File.createTempFile("aligned.", ".sam");
        alignedSam.deleteOnExit();
        final SAMFileHeader alignedHeader = new SAMFileHeader();
        alignedHeader.setSortOrder(SAMFileHeader.SortOrder.queryname);
        alignedHeader.setSequenceDictionary(new SAMFileReader(sequenceDict).getFileHeader().getSequenceDictionary());
        final SAMFileWriter alignedWriter = new SAMFileWriterFactory().makeSAMWriter(alignedHeader, true, alignedSam);
        for (int i = 0; i < hitSpecs.size(); ++i) {
            final HitSpec hitSpec = hitSpecs.get(i);
            final SAMRecord mappedRec = makeRead(alignedHeader, unmappedRec, hitSpec, i);
            if (mappedRec != null) {
                alignedWriter.addAlignment(mappedRec);
            }
        }
        alignedWriter.close();

        // Merge aligned file with original unmapped file.
        final File mergedSam = File.createTempFile("merged.", ".sam");
        mergedSam.deleteOnExit();

        doMergeAlignment(unmappedSam, Collections.singletonList(alignedSam),
                null, null, null, null,
                false, true, false, 1,
                "0", "1.0", "align!", "myAligner",
                false, fasta, mergedSam,
                null, null, null, null);

        assertSamValid(mergedSam);

        // Tally metrics and check for agreement with expected.
        final SAMFileReader mergedReader = new SAMFileReader(mergedSam);
        int numReads = 0;
        Integer primaryHitIndex = null;
        int primaryMapq = 0;
        for (final SAMRecord rec : mergedReader) {
            ++numReads;
View Full Code Here

                ONE_OF_THE_BEST_TAG,
                null
        );


        final SAMFileReader mergedReader = new SAMFileReader(output);
        boolean seenPrimary = false;
        for (final SAMRecord rec : mergedReader) {
            if (!rec.getNotPrimaryAlignmentFlag()) {
                seenPrimary = true;
                final Integer oneOfTheBest = rec.getIntegerAttribute(ONE_OF_THE_BEST_TAG);
View Full Code Here

                true,
                new File(TEST_DATA_DIR, "cliptest.fasta"), output,
                SamPairUtil.PairOrientation.FR, null,
                null, null);

        final SAMFileReader result = new SAMFileReader(output);
        final Map<String, SAMRecord> firstReadEncountered = new HashMap<String, SAMRecord>();

        for (final SAMRecord rec : result) {
            final SAMRecord otherEnd = firstReadEncountered.get(rec.getReadName());
            if (otherEnd == null) {
                firstReadEncountered.put(rec.getReadName(), rec);
            } else {
                final int fragmentStart = Math.min(rec.getAlignmentStart(), otherEnd.getAlignmentStart());
                final int fragmentEnd = Math.max(rec.getAlignmentEnd(), otherEnd.getAlignmentEnd());
                final String[] readNameFields = rec.getReadName().split(":");
                // Read name of each pair includes the expected fragment start and fragment end positions.
                final int expectedFragmentStart = Integer.parseInt(readNameFields[1]);
                final int expectedFragmentEnd = Integer.parseInt(readNameFields[2]);
                Assert.assertEquals(fragmentStart, expectedFragmentStart, rec.getReadName());
                Assert.assertEquals(fragmentEnd, expectedFragmentEnd, rec.getReadName());
            }
        }
        result.close();
    }
View Full Code Here

                new File(TEST_DATA_DIR, "cliptest.fasta"), output,
                SamPairUtil.PairOrientation.FR,
                MergeBamAlignment.PrimaryAlignmentStrategy.BestEndMapq,
                null, includeSecondary
        );
        final SAMFileReader reader = new SAMFileReader(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);
View Full Code Here

        unmappedWriter.close();

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

        final SAMFileReader dictReader = new SAMFileReader(sequenceDict);
        header.setSequenceDictionary(dictReader.getFileHeader().getSequenceDictionary());
        dictReader.close();

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

        String expectedFirstPrimarySequence = null;
        int expectedFirstPrimaryAlignmentStart = -1;
        String expectedSecondPrimarySequence = null;
        int expectedSecondPrimaryAlignmentStart = -1;

        // Semi-randomly make the reads align to forward or reverse strand.
        boolean reverse = false;
        for (final MostDistantStrategyAlignmentSpec spec: firstEndSpecs) {
            addAlignmentForMostStrategy(alignedWriter, firstUnmappedRead, spec, reverse);
            reverse = !reverse;
            if (spec.expectedPrimary) {
                expectedFirstPrimarySequence = spec.sequence;
                expectedFirstPrimaryAlignmentStart = spec.alignmentStart;
            }
        }
        for (final MostDistantStrategyAlignmentSpec spec: secondEndSpecs) {
            addAlignmentForMostStrategy(alignedWriter, secondUnmappedRead, spec, reverse);
            reverse = !reverse;
            if (spec.expectedPrimary) {
                expectedSecondPrimarySequence = spec.sequence;
                expectedSecondPrimaryAlignmentStart = spec.alignmentStart;
            }
        }
        alignedWriter.close();


        final File output = File.createTempFile("testMostDistantStrategy." + 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.MostDistant,
                null, includeSecondary);

        final SAMFileReader reader = new SAMFileReader(output);
        int numFirstRecords = 0;
        int numSecondRecords = 0;
        String firstPrimarySequence = null;
        int firstPrimaryAlignmentStart = -1;
        String secondPrimarySequence = null;
        int secondPrimaryAlignmentStart = -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(firstPrimaryAlignmentStart, -1);
                    firstPrimarySequence = rec.getReferenceName();
                    firstPrimaryAlignmentStart = rec.getAlignmentStart();
                } else {
                    Assert.assertEquals(secondPrimaryAlignmentStart, -1);
                    secondPrimarySequence = rec.getReferenceName();
                    secondPrimaryAlignmentStart = rec.getAlignmentStart();
                }
            } else if (rec.getNotPrimaryAlignmentFlag()) {
                Assert.assertTrue(rec.getMateUnmappedFlag());
            }
        }
        reader.close();
        Assert.assertEquals(firstPrimarySequence, expectedFirstPrimarySequence);
        Assert.assertEquals(firstPrimaryAlignmentStart, expectedFirstPrimaryAlignmentStart);
        Assert.assertEquals(secondPrimarySequence, expectedSecondPrimarySequence);
        Assert.assertEquals(secondPrimaryAlignmentStart, expectedSecondPrimaryAlignmentStart);
        if (!includeSecondary) {
View Full Code Here

        Assert.assertEquals(runPicardCommandLine(illuminaArgv), 0);

        System.out.println ("Ouput Sam file is in " + samFile.getAbsolutePath());

        // Read the file and confirm it contains what is expected
        final SAMFileReader samReader = new SAMFileReader(samFile);

        // look for clipped adaptor attribute in lane 3 PE (2) and in lane 6 (1) non-PE
        int count = 0;
        for (final SAMRecord record : samReader) {
            if (record.getIntegerAttribute(ReservedTagConstants.XT) != null) {
                count++;
                if ((count == 1 || count == 2) && LANE.equals("2")){
                    Assert.assertEquals (114, (int)record.getIntegerAttribute(ReservedTagConstants.XT));
                } else if (count == 1 || count == 2 && LANE.equals("1")) {
                    Assert.assertEquals(68, (int) record.getIntegerAttribute(ReservedTagConstants.XT));
                }
            }
        }
        samReader.close();
    }
View Full Code Here

        for (final String attr : attributesToClear) {
            args[index++] = "ATTRIBUTE_TO_CLEAR=" + attr;
        }
        runPicardCommandLine(args);

        final SAMFileReader reader = new SAMFileReader(output);
        final SAMFileHeader header = reader.getFileHeader();
        Assert.assertEquals(header.getSortOrder(), SAMFileHeader.SortOrder.queryname);
        Assert.assertEquals(header.getProgramRecords().size(), removeAlignmentInfo ? 0 : 1);
        for (final SAMReadGroupRecord rg : header.getReadGroups()) {
            if (sample != null) {
                Assert.assertEquals(rg.getSample(), sample);
View Full Code Here

        return set ;
    }

    private Map<String,MatePair> createSamMatePairsMap(final File samFile) throws IOException {
        IOUtil.assertFileIsReadable(samFile);
        final SAMFileReader reader = new SAMFileReader(IOUtil.openFileForReading(samFile));

        final Map<String,MatePair> map = new LinkedHashMap<String,MatePair>();
        for (final SAMRecord record : reader ) {
            MatePair mpair = map.get(record.getReadName());
            if (mpair == null) {
                 mpair = new MatePair();
                 map.put(record.getReadName(), mpair);
            }
            mpair.add(record);
        }
        reader.close();
        return map;
    }
View Full Code Here

    }


    private Map<String, Map<String, MatePair>> createPUPairsMap(final File samFile) throws IOException {
        IOUtil.assertFileIsReadable(samFile);
        final SAMFileReader reader = new SAMFileReader(IOUtil.openFileForReading(samFile));
        final Map<String, Map<String, MatePair>> map = new LinkedHashMap<String, Map<String,MatePair>>();

        Map<String,MatePair> curFileMap;
        for (final SAMRecord record : reader ) {
            final String platformUnit = record.getReadGroup().getPlatformUnit();
            curFileMap = map.get(platformUnit);
            if(curFileMap == null)
            {
                curFileMap = new LinkedHashMap<String, MatePair>();
                map.put(platformUnit, curFileMap);
            }

            MatePair mpair = curFileMap.get(record.getReadName());
            if (mpair == null) {
                 mpair = new MatePair();
                 curFileMap.put(record.getReadName(), mpair);
            }
            mpair.add(record);
        }
        reader.close();
        return map;
    }
View Full Code Here

TOP

Related Classes of htsjdk.samtools.SAMFileReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.