Package htsjdk.samtools

Examples of htsjdk.samtools.SAMFileReader


        int writtenCount = SAMWriter.writeAlignmentFilePicard(inlocator, outPath, sequence, start, end);

        assertEquals("Index file existence unexpected: " + indexFile.getAbsolutePath(), createIndex, indexFile.exists());

        SAMFileReader writtenReader = new SAMFileReader(new File(outPath));
        writtenReader.setValidationStringency(ValidationStringency.SILENT);
        SAMRecordIterator iter = null;
        if(createIndex){
            iter = writtenReader.queryOverlapping(sequence, start + 1, end);
        }else{
            iter = writtenReader.iterator();
        }

        int readCount = 0;
        while (iter.hasNext()) {
            readCount++;
View Full Code Here


*/
public class AlignmentDecoder implements FeatureDecoder<PicardAlignment> {

    @Override
    public Iterator<PicardAlignment> decodeAll(InputStream is, boolean strictParsing) throws IOException {
        SAMFileReader reader = new SAMFileReader(is);
        ValidationStringency stringency =
                strictParsing ? ValidationStringency.STRICT : ValidationStringency.SILENT;
        reader.setValidationStringency(stringency);
        return new WrappedIterator(reader.iterator());
    }
View Full Code Here

        if( samFile == null )
            throw new ReviewedGATKException( "Filename for output sam file must be supplied.");
        if( validationStringency == null )
            throw new ReviewedGATKException( "Header for output sam file must be supplied.");

        SAMFileReader reader = new SAMFileReader( samFile );
        reader.setValidationStringency( validationStringency );

        return reader;
    }
View Full Code Here

    public int execute() {
        long readsWritten = 0;
        long readsAltered = 0;

        SAMFileReader reader = new SAMFileReader(input);
        SAMFileWriter writer = new SAMFileWriterFactory().makeBAMWriter(reader.getFileHeader(),true,output,compressionLevel);

        for(SAMRecord read: reader) {
            Object value = read.getAttribute(sourceTagName);
            if(value != null) {
                read.setAttribute(sourceTagName,null);
View Full Code Here

            exitSystemWithError(e);
        }
    }

    private void showIndexBins(File bamFile,String contigName) {
        SAMFileReader reader;
        BAMIndex index;

        reader = new SAMFileReader(bamFile);
        reader.setValidationStringency(ValidationStringency.SILENT);
        reader.enableIndexCaching(true);
        index = reader.getIndex();

        reader.queryOverlapping(contigName,1,reader.getFileHeader().getSequence(contigName).getSequenceLength()).close();

        int numBins = 0;
        int numChunks = 0;
        int numLinearIndexEntries = 0;

        try {
            Field[] fields = index.getClass().getDeclaredFields();
            for(Field field: fields) {
                if(field.getName().equals("mLastReferenceRetrieved")) {
                    field.setAccessible(true);
                    Integer lastReferenceRetrieved = (Integer)field.get(index);
                    System.out.printf("Last reference retrieved: %d%n", lastReferenceRetrieved);
                }

                if(field.getName().equals("mQueriesByReference")) {
                    field.setAccessible(true);
                    Map<Integer,Object> cachedQueries = (Map<Integer,Object>)field.get(index);

                    for(Object bamIndexContent: cachedQueries.values()) {
                        List<Object> bins = null;
                        Map<Object,Object> binToChunkMap = null;
                        Object linearIndex = null;

                        Field[] indexContentFields = bamIndexContent.getClass().getDeclaredFields();
                        for(Field indexContentField: indexContentFields) {
                            if(indexContentField.getName().equals("mReferenceSequence")) {
                                indexContentField.setAccessible(true);
                                System.out.printf("Reference sequence: %d%n", indexContentField.getInt(bamIndexContent));
                            }

                            if(indexContentField.getName().equals("mBins")) {
                                indexContentField.setAccessible(true);
                                bins = (List<Object>)indexContentField.get(bamIndexContent);
                            }

                            if(indexContentField.getName().equals("mBinToChunks")) {
                                indexContentField.setAccessible(true);
                                binToChunkMap = (Map<Object,Object>)indexContentField.get(bamIndexContent);
                            }

                            if(indexContentField.getName().equals("mLinearIndex")) {
                                indexContentField.setAccessible(true);
                                linearIndex = indexContentField.get(bamIndexContent);
                            }
                        }

                        numBins = bins.size();
                        for(Object bin: bins) {
                            int binNumber;

                            Field[] binFields = bin.getClass().getDeclaredFields();
                            for(Field binField: binFields) {
                                if(binField.getName().equals("binNumber")) {
                                    binField.setAccessible(true);
                                    binNumber = binField.getInt(bin);
                                    List<Object> chunks = (List<Object>)binToChunkMap.get(bin);
                                    System.out.printf("\tBin: %d, number of chunks: %d%n",binNumber,chunks.size());
                                    for(Object chunk: chunks)
                                        System.out.printf("\t\tChunk: %s%n",chunk);
                                    numChunks += chunks.size();
                                }
                            }
                        }

                        Field[] linearIndexFields = linearIndex.getClass().getDeclaredFields();
                        for(Field linearIndexField: linearIndexFields) {
                            if(linearIndexField.getName().equals("mIndexEntries")) {
                                linearIndexField.setAccessible(true);
                                long[] linearIndexEntries = (long[])linearIndexField.get(linearIndex);
                                System.out.printf("\t\tIndex entries: %d", linearIndexEntries.length);
                                for(long indexEntry: linearIndexEntries)
                                    System.out.printf("%d,",indexEntry);
                                System.out.printf("%n");
                                numLinearIndexEntries = linearIndexEntries.length;
                            }
                        }
                    }
                }
            }
        }
        catch(IllegalAccessException ex) {
            throw new ReviewedGATKException("Unable to examine cached index",ex);
        }

        System.out.printf("%nOverall: %d bins, %d chunks, %d linear index entries",numBins,numChunks,numLinearIndexEntries);
        if(Sizeof.isEnabled())
            System.out.printf(", total index size in bytes: %d",Sizeof.getObjectGraphSize(index));
        System.out.println();

        reader.close();
    }
View Full Code Here

    @Override
    public String getName() { return "BAM"; }

    @Override
    public DiffElement readFromFile(File file, int maxElementsToRead) {
        final SAMFileReader reader = new SAMFileReader(file, null); // null because we don't want it to look for the index
        reader.setValidationStringency(ValidationStringency.SILENT);

        DiffNode root = DiffNode.rooted(file.getName());
        SAMRecordIterator iterator = reader.iterator();

        int count = 0;
        while ( iterator.hasNext() ) {
            final SAMRecord record = iterator.next();

            // name is the read name + first of pair
            String name = record.getReadName().replace('.', '_');
            if ( record.getReadPairedFlag() ) {
                name += record.getFirstOfPairFlag() ? "_1" : "_2";
            }

            DiffNode readRoot = DiffNode.empty(name, root);

            // add fields
            readRoot.add("NAME", record.getReadName());
            readRoot.add("FLAGS", record.getFlags());
            readRoot.add("RNAME", record.getReferenceName());
            readRoot.add("POS", record.getAlignmentStart());
            readRoot.add("MAPQ", record.getMappingQuality());
            readRoot.add("CIGAR", record.getCigarString());
            readRoot.add("RNEXT", record.getMateReferenceName());
            readRoot.add("PNEXT", record.getMateAlignmentStart());
            readRoot.add("TLEN", record.getInferredInsertSize());
            readRoot.add("SEQ", record.getReadString());
            readRoot.add("QUAL", record.getBaseQualityString());

            for ( SAMRecord.SAMTagAndValue xt : record.getAttributes() ) {
                readRoot.add(xt.tag, xt.value);
            }

            // add record to root
            if ( ! root.hasElement(name) )
                // protect ourselves from malformed files
                root.add(readRoot);
            count += readRoot.size();
            if ( count > maxElementsToRead && maxElementsToRead != -1)
                break;
        }

        reader.close();

        return root.getBinding();
    }
View Full Code Here

    protected File inputFile;

    @Override
    public void setUp() {
        SAMFileReader fullInputFile = new SAMFileReader(new File(getBAMFile()));

        File tempFile = null;
        try {
            tempFile = File.createTempFile("testfile_"+getMaxReads(),".bam");
        }
        catch(IOException ex) {
            throw new ReviewedGATKException("Unable to create temporary BAM",ex);
        }
        SAMFileWriterFactory factory = new SAMFileWriterFactory();
        factory.setCreateIndex(true);
        SAMFileWriter writer = factory.makeBAMWriter(fullInputFile.getFileHeader(),true,tempFile);

        long numReads = 0;
        for(SAMRecord read: fullInputFile) {
            if(numReads++ >= getMaxReads())
                break;
View Full Code Here

    @Test(enabled = true)
    public void testSamTextFileError1() {
        final File samFile = new File(publicTestDir + "testfile.sam");
        final File indexFile = new File(publicTestDir + "HiSeq.1mb.1RG.bai");
        try {
            final SAMFileReader reader = new SAMFileReader(samFile, indexFile, false);

            // we shouldn't get here
            Assert.fail("We should have exceptioned out when trying to create a reader with an index for a textual SAM file");
        } catch (RuntimeException e) {
            Assert.assertTrue(e.getMessage().indexOf(CommandLineGATK.PICARD_TEXT_SAM_FILE_ERROR_1) != -1);
View Full Code Here

    @Test(enabled = true)
    public void testSamTextFileError2() {
        File samFile = new File(publicTestDir + "testfile.sam");
        try {
            final SAMFileReader reader = new SAMFileReader(samFile);
            reader.getFilePointerSpanningReads();

            // we shouldn't get here
            Assert.fail("We should have exceptioned out when trying to call getFilePointerSpanningReads() for a textual SAM file");
        } catch (RuntimeException e) {
            Assert.assertTrue(e.getMessage().indexOf(CommandLineGATK.PICARD_TEXT_SAM_FILE_ERROR_2) != -1);
View Full Code Here

        for ( final GATKSAMRecord read : reads ) {
            assertGoodRead(read, bamBuilder);
        }

        final File bam = bamBuilder.makeTemporarilyBAMFile();
        final SAMFileReader reader = new SAMFileReader(bam);
        Assert.assertTrue(reader.hasIndex());
        final Iterator<SAMRecord> bamIt = reader.iterator();
        int nReadsFromBam = 0;
        int lastStart = -1;
        while ( bamIt.hasNext() ) {
            final SAMRecord read = bamIt.next();
            assertGoodRead(read, bamBuilder);
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.