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();
}
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);
}
}
}
}
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);
}
}
}
}
}
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;
}
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;
}
Aggregations