Search in sources :

Example 11 with SAMFileWriterFactory

use of htsjdk.samtools.SAMFileWriterFactory in project ASCIIGenome by dariober.

the class Utils method sortAndIndexSamOrBam.

/**
 * Sort and index input sam or bam.
 * @throws IOException
 */
public static void sortAndIndexSamOrBam(String inSamOrBam, String sortedBam, boolean deleteOnExit) throws IOException {
    /*  ------------------------------------------------------ */
    /* This chunk prepares SamReader from local bam or URL bam */
    UrlValidator urlValidator = new UrlValidator();
    SamReaderFactory srf = SamReaderFactory.make();
    srf.validationStringency(ValidationStringency.SILENT);
    SamReader samReader;
    if (urlValidator.isValid(inSamOrBam)) {
        samReader = SamReaderFactory.makeDefault().open(SamInputResource.of(new URL(inSamOrBam)));
    } else {
        samReader = srf.open(new File(inSamOrBam));
    }
    /*  ------------------------------------------------------ */
    samReader.getFileHeader().setSortOrder(SortOrder.coordinate);
    File out = new File(sortedBam);
    if (deleteOnExit) {
        out.deleteOnExit();
        File idx = new File(out.getAbsolutePath().replaceAll("\\.bam$", "") + ".bai");
        idx.deleteOnExit();
    }
    SAMFileWriter outputSam = new SAMFileWriterFactory().setCreateIndex(true).makeSAMOrBAMWriter(samReader.getFileHeader(), false, out);
    for (final SAMRecord samRecord : samReader) {
        outputSam.addAlignment(samRecord);
    }
    samReader.close();
    outputSam.close();
}
Also used : SamReader(htsjdk.samtools.SamReader) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) UrlValidator(org.apache.commons.validator.routines.UrlValidator) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) File(java.io.File) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile) URL(java.net.URL)

Example 12 with SAMFileWriterFactory

use of htsjdk.samtools.SAMFileWriterFactory in project gridss by PapenfussLab.

the class StubFastqAligner method align.

@Override
public void align(File fastq, File output, File reference, int threads) throws IOException {
    SAMFileHeader header = context.getBasicSamHeader();
    try (FastqReader reader = new FastqReader(fastq)) {
        try (SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(header, true, output)) {
            for (FastqRecord fqr : reader) {
                SAMRecord source = nameLookup.get(fqr.getReadName());
                SAMRecord r = new SAMRecord(header);
                r.setReadName(fqr.getReadName());
                r.setReadBases(fqr.getReadString().getBytes());
                r.setBaseQualities(SAMUtils.fastqToPhred(fqr.getBaseQualityString()));
                if (source == null) {
                    r.setReadUnmappedFlag(true);
                } else {
                    ChimericAlignment aln = map.get(fqr.getReadName());
                    r.setReferenceName(aln.rname);
                    r.setAlignmentStart(aln.pos);
                    r.setCigar(aln.cigar);
                    r.setMappingQuality(aln.mapq);
                    r.setAttribute("NM", aln.nm);
                    if (aln.isNegativeStrand) {
                        SequenceUtil.reverseComplement(r.getReadBases());
                        ArrayUtils.reverse(r.getBaseQualities());
                    }
                }
                writer.addAlignment(r);
            }
        }
    }
}
Also used : ChimericAlignment(au.edu.wehi.idsv.sam.ChimericAlignment) FastqReader(htsjdk.samtools.fastq.FastqReader) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) FastqRecord(htsjdk.samtools.fastq.FastqRecord) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) SAMFileHeader(htsjdk.samtools.SAMFileHeader)

Example 13 with SAMFileWriterFactory

use of htsjdk.samtools.SAMFileWriterFactory in project gridss by PapenfussLab.

the class SmithWatermanFastqAligner method align.

@Override
public void align(File fastq, File output, File reference, int threads) throws IOException {
    try (ReferenceSequenceFile ref = new IndexedFastaSequenceFile(reference)) {
        SAMFileHeader header = new SAMFileHeader();
        header.setSequenceDictionary(ref.getSequenceDictionary());
        byte[] bases = ref.getSequence(ref.getSequenceDictionary().getSequence(referenceIndex).getSequenceName()).getBases();
        try (FastqReader reader = new FastqReader(fastq)) {
            try (SAMFileWriter writer = new SAMFileWriterFactory().makeSAMOrBAMWriter(header, true, output)) {
                for (FastqRecord fqr : reader) {
                    Alignment aln = aligner.align_smith_waterman(fqr.getReadString().getBytes(), bases);
                    SAMRecord r = new SAMRecord(header);
                    r.setReadName(fqr.getReadName());
                    r.setReferenceIndex(referenceIndex);
                    r.setAlignmentStart(aln.getStartPosition() + 1);
                    r.setCigarString(aln.getCigar());
                    r.setReadBases(fqr.getReadString().getBytes());
                    r.setBaseQualities(SAMUtils.fastqToPhred(fqr.getBaseQualityString()));
                    writer.addAlignment(r);
                }
            }
        }
    }
}
Also used : FastqReader(htsjdk.samtools.fastq.FastqReader) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMRecord(htsjdk.samtools.SAMRecord) FastqRecord(htsjdk.samtools.fastq.FastqRecord) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) ReferenceSequenceFile(htsjdk.samtools.reference.ReferenceSequenceFile) SAMFileHeader(htsjdk.samtools.SAMFileHeader) IndexedFastaSequenceFile(htsjdk.samtools.reference.IndexedFastaSequenceFile)

Example 14 with SAMFileWriterFactory

use of htsjdk.samtools.SAMFileWriterFactory in project gridss by PapenfussLab.

the class ComputeSamTags method doWork.

@Override
protected int doWork() {
    log.debug("Setting language-neutral locale");
    java.util.Locale.setDefault(Locale.ROOT);
    validateParameters();
    SamReaderFactory readerFactory = SamReaderFactory.make();
    SAMFileWriterFactory writerFactory = new SAMFileWriterFactory();
    try {
        try (SamReader reader = readerFactory.open(INPUT)) {
            SAMFileHeader header = reader.getFileHeader();
            if (!ASSUME_SORTED) {
                if (header.getSortOrder() != SortOrder.queryname) {
                    log.error("INPUT is not sorted by queryname. " + "ComputeSamTags requires that reads with the same name be sorted together. " + "If the input file satisfies this constraint (the output from many aligners do)," + " this check can be disabled with the ASSUME_SORTED option.");
                    return -1;
                }
            }
            try (SAMRecordIterator it = reader.iterator()) {
                File tmpoutput = gridss.Defaults.OUTPUT_TO_TEMP_FILE ? FileSystemContext.getWorkingFileFor(OUTPUT, "gridss.tmp.ComputeSamTags.") : OUTPUT;
                try (SAMFileWriter writer = writerFactory.makeSAMOrBAMWriter(header, true, tmpoutput)) {
                    compute(it, writer, getReference(), TAGS, SOFTEN_HARD_CLIPS, FIX_MATE_INFORMATION, RECALCULATE_SA_SUPPLEMENTARY, INPUT.getName() + "-");
                }
                if (tmpoutput != OUTPUT) {
                    FileHelper.move(tmpoutput, OUTPUT, true);
                }
            }
        }
    } catch (IOException e) {
        log.error(e);
        return -1;
    }
    return 0;
}
Also used : SamReader(htsjdk.samtools.SamReader) SamReaderFactory(htsjdk.samtools.SamReaderFactory) SAMRecordIterator(htsjdk.samtools.SAMRecordIterator) SAMFileWriter(htsjdk.samtools.SAMFileWriter) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) IOException(java.io.IOException) SAMFileHeader(htsjdk.samtools.SAMFileHeader) File(java.io.File)

Example 15 with SAMFileWriterFactory

use of htsjdk.samtools.SAMFileWriterFactory in project gridss by PapenfussLab.

the class ExtractSVReads method setup.

@Override
protected void setup(SAMFileHeader header, File samFile) {
    if (METRICS_OUTPUT != null) {
        metricsCollector = new CollectStructuralVariantReadMetrics();
        copyInput(metricsCollector);
        metricsCollector.OUTPUT = METRICS_OUTPUT;
        metricsCollector.setup(header, samFile);
    }
    SAMFileWriterFactory writerFactory = new SAMFileWriterFactory();
    if (header.getSortOrder() != SortOrder.queryname) {
        log.info("Not considering multiple read alignments as the input file is not queryname sorted.");
    }
    tmpoutput = gridss.Defaults.OUTPUT_TO_TEMP_FILE ? FileSystemContext.getWorkingFileFor(OUTPUT, "gridss.tmp.ExtractSVReads.") : OUTPUT;
    writer = writerFactory.makeSAMOrBAMWriter(header, true, tmpoutput);
    IndelReadFilter indelFilter = new IndelReadFilter(INDELS ? MIN_INDEL_SIZE : Integer.MAX_VALUE);
    ClippedReadFilter softClipFilter = new ClippedReadFilter(CLIPPED ? MIN_CLIP_LENGTH : Integer.MAX_VALUE);
    SplitReadFilter splitReadFilter = new SplitReadFilter();
    AlignedFilter unmappedFilter = new AlignedFilter(false);
    OneEndAnchoredReadFilter oeaFilter = new OneEndAnchoredReadFilter();
    ReadPairConcordanceFilter dpFilter = getReadPairConcordanceCalculator() != null ? new ReadPairConcordanceFilter(getReadPairConcordanceCalculator(), false, true) : null;
    List<SamRecordFilter> readfilters = new ArrayList<>();
    readfilters.add(indelFilter);
    readfilters.add(softClipFilter);
    if (SPLIT)
        readfilters.add(splitReadFilter);
    if (UNMAPPED_READS)
        readfilters.add(unmappedFilter);
    readfilter = new UnionAggregateFilter(readfilters);
    List<SamRecordFilter> pairfilters = new ArrayList<>();
    if (SINGLE_MAPPED_PAIRED)
        pairfilters.add(oeaFilter);
    if (dpFilter != null && DISCORDANT_READ_PAIRS)
        pairfilters.add(dpFilter);
    pairfilter = new UnionAggregateFilter(pairfilters);
    if (!SINGLE_MAPPED_PAIRED && !DISCORDANT_READ_PAIRS) {
        pairfilter = new FixedFilter(true);
    }
    count = 0;
}
Also used : FixedFilter(gridss.filter.FixedFilter) SamRecordFilter(htsjdk.samtools.filter.SamRecordFilter) IndelReadFilter(gridss.filter.IndelReadFilter) ArrayList(java.util.ArrayList) SAMFileWriterFactory(htsjdk.samtools.SAMFileWriterFactory) UnionAggregateFilter(gridss.filter.UnionAggregateFilter) CollectStructuralVariantReadMetrics(gridss.analysis.CollectStructuralVariantReadMetrics) SplitReadFilter(gridss.filter.SplitReadFilter) OneEndAnchoredReadFilter(gridss.filter.OneEndAnchoredReadFilter) ClippedReadFilter(gridss.filter.ClippedReadFilter) AlignedFilter(htsjdk.samtools.filter.AlignedFilter) ReadPairConcordanceFilter(gridss.filter.ReadPairConcordanceFilter)

Aggregations

SAMFileWriterFactory (htsjdk.samtools.SAMFileWriterFactory)33 SAMFileWriter (htsjdk.samtools.SAMFileWriter)26 File (java.io.File)26 SAMRecord (htsjdk.samtools.SAMRecord)22 SAMFileHeader (htsjdk.samtools.SAMFileHeader)20 SamReader (htsjdk.samtools.SamReader)17 SAMRecordIterator (htsjdk.samtools.SAMRecordIterator)14 IOException (java.io.IOException)14 SamReaderFactory (htsjdk.samtools.SamReaderFactory)12 SAMSequenceDictionary (htsjdk.samtools.SAMSequenceDictionary)8 SAMReadGroupRecord (htsjdk.samtools.SAMReadGroupRecord)7 ArrayList (java.util.ArrayList)7 SAMSequenceRecord (htsjdk.samtools.SAMSequenceRecord)5 Test (org.testng.annotations.Test)5 Interval (htsjdk.samtools.util.Interval)4 BufferedReader (java.io.BufferedReader)4 List (java.util.List)4 BAMIndex (htsjdk.samtools.BAMIndex)3 DefaultSAMRecordFactory (htsjdk.samtools.DefaultSAMRecordFactory)3 SAMProgramRecord (htsjdk.samtools.SAMProgramRecord)3