use of htsjdk.samtools.SAMFileWriter in project hmftools by hartwigmedical.
the class BamSlicerApplication method sliceFromURLs.
private static void sliceFromURLs(@NotNull final URL indexUrl, @NotNull final URL bamUrl, @NotNull final CommandLine cmd) throws IOException {
final File indexFile = downloadIndex(indexUrl);
indexFile.deleteOnExit();
final SamReader reader = SamReaderFactory.makeDefault().open(SamInputResource.of(bamUrl).index(indexFile));
final SAMFileWriter writer = new SAMFileWriterFactory().setCreateIndex(true).makeBAMWriter(reader.getFileHeader(), true, new File(cmd.getOptionValue(OUTPUT)));
final BAMIndex bamIndex = new DiskBasedBAMFileIndex(indexFile, reader.getFileHeader().getSequenceDictionary(), false);
final Optional<Pair<QueryInterval[], BAMFileSpan>> queryIntervalsAndSpan = queryIntervalsAndSpan(reader, bamIndex, cmd);
final Optional<Chunk> unmappedChunk = getUnmappedChunk(bamIndex, HttpUtils.getHeaderField(bamUrl, "Content-Length"), cmd);
final List<Chunk> sliceChunks = sliceChunks(queryIntervalsAndSpan, unmappedChunk);
final SamReader cachingReader = createCachingReader(indexFile, bamUrl, cmd, sliceChunks);
queryIntervalsAndSpan.ifPresent(pair -> {
LOGGER.info("Slicing bam on bed regions...");
final CloseableIterator<SAMRecord> bedIterator = getIterator(cachingReader, pair.getKey(), pair.getValue().toCoordinateArray());
writeToSlice(writer, bedIterator);
LOGGER.info("Done writing bed slices.");
});
unmappedChunk.ifPresent(chunk -> {
LOGGER.info("Slicing unmapped reads...");
final CloseableIterator<SAMRecord> unmappedIterator = cachingReader.queryUnmapped();
writeToSlice(writer, unmappedIterator);
LOGGER.info("Done writing unmapped reads.");
});
reader.close();
writer.close();
cachingReader.close();
}
use of htsjdk.samtools.SAMFileWriter in project polyGembler by c-zhou.
the class SamFileExtract method run.
@Override
public void run() {
// TODO Auto-generated method stub
final SamReaderFactory factory = SamReaderFactory.makeDefault().enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS).validationStringency(ValidationStringency.SILENT);
final SamReader inputSam = factory.open(new File(bam_in));
final SAMFileHeader header = inputSam.getFileHeader();
final SAMSequenceDictionary seqdic = header.getSequenceDictionary();
final SAMFileHeader header_out = new SAMFileHeader();
final SAMSequenceDictionary seqdic_out = new SAMSequenceDictionary();
SAMRecordIterator iter = inputSam.iterator();
File bed_file = new File(bed_in);
final Set<String> extract = new HashSet<String>();
try (BufferedReader br = new BufferedReader(new FileReader(bed_file))) {
String line;
while ((line = br.readLine()) != null) extract.add(line.split("\\s+")[0]);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
header_out.setAttribute("VN", header.getAttribute("VN"));
header_out.setAttribute("SO", header.getAttribute("SO"));
List<SAMSequenceRecord> seqs = seqdic.getSequences();
for (SAMSequenceRecord seq : seqs) if (extract.contains(seq.getSequenceName()))
seqdic_out.addSequence(seq);
header_out.setSequenceDictionary(seqdic_out);
for (SAMReadGroupRecord rg : header.getReadGroups()) header_out.addReadGroup(rg);
for (SAMProgramRecord pg : header.getProgramRecords()) header_out.addProgramRecord(pg);
final SAMFileWriter outputSam = new SAMFileWriterFactory().makeSAMOrBAMWriter(header_out, true, new File(bam_out));
while (iter.hasNext()) {
SAMRecord rec = iter.next();
if (extract.contains(rec.getReferenceName()))
outputSam.addAlignment(rec);
}
iter.close();
try {
inputSam.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
outputSam.close();
System.err.println(bam_in + " return true");
}
use of htsjdk.samtools.SAMFileWriter in project polyGembler by c-zhou.
the class SamFileFilter method run.
@Override
public void run() {
// TODO Auto-generated method stub
File folder = new File(input_dir);
File[] listOfFiles = folder.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".bam");
}
});
int bam_n = listOfFiles.length;
this.initial_thread_pool();
for (int i = 0; i < bam_n; i++) {
executor.submit(new Runnable() {
private String bam_file;
@Override
public void run() {
// TODO Auto-generated method stub
try {
final SamReaderFactory factory = SamReaderFactory.makeDefault().enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS, SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS).validationStringency(ValidationStringency.SILENT);
final SamReader inputSam = factory.open(new File(input_dir + "/" + bam_file));
final SAMFileWriter outputSam = new SAMFileWriterFactory().makeSAMOrBAMWriter(inputSam.getFileHeader(), true, new File(output_dir + "/" + bam_file));
final BufferedReader filter_br = Utils.getBufferedReader(filter_file);
SAMRecordIterator iter = inputSam.iterator();
String line = filter_br.readLine();
if (line == null) {
filter_br.close();
iter.close();
inputSam.close();
outputSam.close();
return;
}
String[] s = line.split("\\s+");
String chr = s[0];
long end_pos = Long.parseLong(s[1]);
SAMRecord samr;
while (iter.hasNext()) {
samr = iter.next();
if (samr.getReferenceName().equals(chr) && samr.getAlignmentEnd() < end_pos) {
outputSam.addAlignment(samr);
continue;
}
if (!samr.getReferenceName().equals(chr) || samr.getAlignmentStart() > end_pos) {
line = null;
while ((line = filter_br.readLine()) != null && end_pos < samr.getAlignmentStart()) {
s = line.split("\\s");
chr = s[0];
end_pos = Long.parseLong(s[1]);
}
if (line != null) {
if (samr.getReferenceName().equals(chr) && samr.getAlignmentEnd() < end_pos)
outputSam.addAlignment(samr);
} else {
outputSam.addAlignment(samr);
while (iter.hasNext()) outputSam.addAlignment(iter.next());
break;
}
}
}
filter_br.close();
iter.close();
inputSam.close();
outputSam.close();
} catch (Exception e) {
Thread t = Thread.currentThread();
t.getUncaughtExceptionHandler().uncaughtException(t, e);
e.printStackTrace();
executor.shutdown();
System.exit(1);
}
}
public Runnable init(String bam_file) {
// TODO Auto-generated method stub
this.bam_file = bam_file;
return this;
}
}.init(listOfFiles[i].getName()));
throw new RuntimeException("incorrect tools!!!!");
}
this.waitFor();
}
use of htsjdk.samtools.SAMFileWriter 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.SAMFileWriter 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);
}
}
}
}
Aggregations