use of htsjdk.samtools.SAMFileWriter in project jvarkit by lindenb.
the class SortSamRefName method doWork.
@Override
public int doWork(List<String> args) {
SamReader in = null;
SAMFileWriter out = null;
SAMRecordIterator iter = null;
CloseableIterator<SAMRecord> iter2 = null;
SortingCollection<SAMRecord> sorter = null;
try {
in = openSamReader(oneFileOrNull(args));
final SAMFileHeader header = in.getFileHeader();
final BAMRecordCodec bamRecordCodec = new BAMRecordCodec(header);
final RefNameComparator refNameComparator = new RefNameComparator();
sorter = SortingCollection.newInstance(SAMRecord.class, bamRecordCodec, refNameComparator, this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
sorter.setDestructiveIteration(true);
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header);
iter = in.iterator();
while (iter.hasNext()) {
sorter.add(progress.watch(iter.next()));
}
in.close();
in = null;
sorter.doneAdding();
final SAMFileHeader header2 = header.clone();
header2.addComment(getProgramName() + " " + getVersion() + " " + getProgramCommandLine());
header2.setSortOrder(SortOrder.unsorted);
out = this.writingBamArgs.openSAMFileWriter(outputFile, header2, true);
iter2 = sorter.iterator();
while (iter2.hasNext()) {
out.addAlignment(iter2.next());
}
out.close();
out = null;
sorter.cleanup();
progress.finish();
LOG.info("done");
return RETURN_OK;
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(iter2);
CloserUtil.close(iter);
CloserUtil.close(out);
CloserUtil.close(in);
}
}
use of htsjdk.samtools.SAMFileWriter in project jvarkit by lindenb.
the class SamGrep method doWork.
@Override
public int doWork(List<String> args) {
readNames.clear();
if (namefile != null) {
BufferedReader in = null;
try {
in = IOUtils.openFileForBufferedReading(this.namefile);
String line;
while ((line = in.readLine()) != null) {
line = line.trim();
if (line.isEmpty())
continue;
readNames.put(line, 0);
}
} catch (Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(in);
}
}
for (final String line : this.nameStrings) {
readNames.put(line, 0);
}
if (readNames.isEmpty()) {
LOG.warn("no read found.");
}
SAMFileWriter sfw = null;
SAMFileWriter samStdout = null;
SamReader sfr = null;
try {
sfr = super.openSamReader(oneFileOrNull(args));
final SAMFileHeader header = sfr.getFileHeader().clone();
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header);
if (this.outputFile == null) {
sfw = this.writingBamArgs.openSAMFileWriter(null, header, true);
} else {
samStdout = this.writingBamArgs.openSAMFileWriter(null, header, true);
sfw = this.writingBamArgs.openSAMFileWriter(outputFile, header, true);
}
SAMRecordIterator iter = sfr.iterator();
while (iter.hasNext()) {
boolean keep = false;
final SAMRecord rec = progress.watch(iter.next());
if (samStdout != null)
samStdout.addAlignment(rec);
Integer count = readNames.get(rec.getReadName());
if (count != null) {
keep = true;
}
if (this.inverse)
keep = !keep;
if (keep) {
sfw.addAlignment(rec);
}
if (n_before_remove != -1 && !inverse && keep) {
count++;
if (count >= n_before_remove) {
readNames.remove(rec.getReadName());
if (samStdout == null && readNames.isEmpty())
break;
} else {
readNames.put(rec.getReadName(), count);
}
}
}
progress.finish();
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(samStdout);
CloserUtil.close(sfw);
CloserUtil.close(sfr);
}
}
use of htsjdk.samtools.SAMFileWriter in project jvarkit by lindenb.
the class SamCustomSortJdk method doWork.
@Override
public int doWork(final List<String> args) {
SAMRecordIterator iter = null;
SamReader samFileReader = null;
SAMFileWriter sw = null;
SortingCollection<SAMRecord> sorter = null;
CloseableIterator<SAMRecord> iter2 = null;
try {
final String code;
if (this.scriptFile != null) {
code = IOUtil.slurp(this.scriptFile);
} else if (!StringUtil.isBlank(this.scriptExpr)) {
code = this.scriptExpr;
} else {
LOG.error("Option -e or -f are required. The content of those empty mut be not empty");
return -1;
}
final Random rand = new Random(System.currentTimeMillis());
final String javaClassName = SamCustomSortJdk.class.getSimpleName() + "Custom" + Math.abs(rand.nextInt());
final StringWriter codeWriter = new StringWriter();
final PrintWriter pw = new PrintWriter(codeWriter);
pw.println("import java.util.*;");
pw.println("import java.util.stream.*;");
pw.println("import java.util.function.*;");
pw.println("import htsjdk.samtools.*;");
pw.println("import htsjdk.samtools.util.*;");
pw.println("import com.github.lindenb.jvarkit.tools.misc.IlluminaReadName;");
pw.println("import javax.annotation.Generated;");
pw.println("@Generated(value=\"" + SamCustomSortJdk.class.getSimpleName() + "\",date=\"" + new Iso8601Date(new Date()) + "\")");
pw.println("public class " + javaClassName + " extends " + AbstractSamComparator.class.getName().replace('$', '.') + " {");
pw.println(" public " + javaClassName + "(final SAMFileHeader header) {");
pw.println(" super(header);");
pw.println(" }");
if (user_code_is_body) {
pw.println(" //user's code starts here");
pw.println(code);
pw.println(" // user's code ends here");
} else {
pw.println(" @Override");
pw.println(" public int compare(final SAMRecord R1,final SAMRecord R2) {");
pw.println(" /** user's code starts here */");
pw.println(code);
pw.println("/** user's code ends here */");
pw.println(" }");
}
pw.println("}");
pw.flush();
if (!hideGeneratedCode) {
LOG.debug(" Compiling :\n" + InMemoryCompiler.beautifyCode(codeWriter.toString()));
}
if (this.saveCodeInDir != null) {
PrintWriter cw = null;
try {
IOUtil.assertDirectoryIsWritable(this.saveCodeInDir);
cw = new PrintWriter(new File(this.saveCodeInDir, javaClassName + ".java"));
cw.write(codeWriter.toString());
cw.flush();
cw.close();
cw = null;
LOG.info("saved " + javaClassName + ".java in " + this.saveCodeInDir);
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(cw);
}
}
final InMemoryCompiler inMemoryCompiler = new InMemoryCompiler();
final Class<?> compiledClass = inMemoryCompiler.compileClass(javaClassName, codeWriter.toString());
final Constructor<?> ctor = compiledClass.getDeclaredConstructor(SAMFileHeader.class);
samFileReader = openSamReader(oneFileOrNull(args));
final SAMFileHeader headerIn = samFileReader.getFileHeader();
final StableSort customComparator = new StableSort((Comparator<SAMRecord>) ctor.newInstance(headerIn));
final BAMRecordCodec bamRecordCodec = new BAMRecordCodec(headerIn);
sorter = SortingCollection.newInstance(SAMRecord.class, bamRecordCodec, customComparator, this.writingSortingCollection.getMaxRecordsInRam(), this.writingSortingCollection.getTmpPaths());
sorter.setDestructiveIteration(true);
SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(headerIn).logger(LOG);
iter = samFileReader.iterator();
while (iter.hasNext()) {
sorter.add(progress.watch(iter.next()));
}
samFileReader.close();
samFileReader = null;
sorter.doneAdding();
final SAMFileHeader headerOut = headerIn.clone();
headerOut.setSortOrder(SAMFileHeader.SortOrder.unsorted);
headerOut.addComment(getProgramName() + " " + getVersion() + " " + getProgramCommandLine());
sw = this.writingBamArgs.openSAMFileWriter(this.outputFile, headerOut, false);
progress = new SAMSequenceDictionaryProgress(headerIn).logger(LOG);
iter2 = sorter.iterator();
while (iter2.hasNext()) {
sw.addAlignment(progress.watch(iter2.next()));
}
iter2.close();
iter2 = null;
sw.close();
sw = null;
progress.finish();
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
try {
if (sorter != null)
sorter.cleanup();
} catch (Exception e) {
}
CloserUtil.close(iter);
CloserUtil.close(iter2);
CloserUtil.close(samFileReader);
CloserUtil.close(sw);
}
}
use of htsjdk.samtools.SAMFileWriter in project jvarkit by lindenb.
the class SamJavascript method doWork.
@Override
public int doWork(final List<String> args) {
SAMRecordIterator iter = null;
SamReader samFileReader = null;
SAMFileWriter sw = null;
try {
this.script = super.compileJavascript(this.jsExpression, this.jsFile);
samFileReader = openSamReader(oneFileOrNull(args));
final SAMFileHeader header = samFileReader.getFileHeader();
sw = writingBamArgs.openSAMFileWriter(outputFile, header, true);
long count = 0L;
final Bindings bindings = this.script.getEngine().createBindings();
bindings.put("header", samFileReader.getFileHeader());
final SAMSequenceDictionaryProgress progress = new SAMSequenceDictionaryProgress(header).logger(LOG);
iter = samFileReader.iterator();
while (iter.hasNext()) {
final SAMRecord record = iter.next();
progress.watch(record);
bindings.put("record", record);
if (super.evalJavaScriptBoolean(this.script, bindings)) {
++count;
sw.addAlignment(record);
if (this.LIMIT > 0L && count >= this.LIMIT)
break;
} else {
failing(record, header);
}
}
sw.close();
/* create empty if never called */
openFailing(header);
return RETURN_OK;
} catch (final Exception err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(iter);
CloserUtil.close(samFileReader);
CloserUtil.close(sw);
CloserUtil.close(failingReadsWriter);
}
}
use of htsjdk.samtools.SAMFileWriter in project gatk by broadinstitute.
the class SamFormatConverter method doWork.
@Override
protected Object doWork() {
IOUtil.assertFileIsReadable(INPUT);
IOUtil.assertFileIsWritable(OUTPUT);
final SamReader reader = SamReaderFactory.makeDefault().referenceSequence(REFERENCE_SEQUENCE).open(INPUT);
try (final SAMFileWriter writer = createSAMWriter(OUTPUT, REFERENCE_SEQUENCE, reader.getFileHeader(), true)) {
final ProgressLogger progress = new ProgressLogger(logger);
for (final SAMRecord rec : reader) {
writer.addAlignment(rec);
progress.record(rec);
}
}
CloserUtil.close(reader);
return null;
}
Aggregations