use of htsjdk.samtools.util.StopWatch in project pgs-calc by lukfor.
the class ApplyScoreCommand method call.
public Integer call() throws Exception {
if (noAnsi) {
TaskService.setAnimated(false);
TaskService.setAnsiColors(false);
}
if (vcfs == null || vcfs.isEmpty()) {
System.out.println();
System.out.println("Please provide at least one VCF file.");
System.out.println();
return 1;
}
System.out.println();
System.out.println("Input:");
System.out.println(" ref: " + ref);
System.out.println(" out: " + out);
System.out.println(" genotypes: " + genotypeFormat);
System.out.println(" minR2: " + minR2);
if (chunk != null) {
System.out.println(" Chunk: " + chunk.getStart() + " - " + chunk.getEnd());
}
System.out.println(" vcfs (" + vcfs.size() + "):");
for (String vcf : vcfs) {
System.out.println(" - " + vcf);
}
System.out.println();
StopWatch watch = new StopWatch();
watch.start();
String outParent = new File(out).getParent();
String tempFolder = FileUtil.path(outParent, "temp");
File tempFolderFile = new File(tempFolder);
tempFolderFile.mkdirs();
List<ApplyScoreTask> tasks = new Vector<ApplyScoreTask>();
for (String vcf : vcfs) {
ApplyScoreTask task = new ApplyScoreTask();
String[] refs = parseRef(ref);
task.setRiskScoreFilenames(refs);
for (String file : refs) {
String autoFormat = file + ".format";
if (new File(autoFormat).exists()) {
task.setRiskScoreFormat(file, RiskScoreFormat.MAPPING_FILE);
}
}
if (chunk != null) {
task.setChunk(chunk);
}
String taskPrefix = FileUtil.path(tempFolder, "task_" + tasks.size());
if (dbsnp != null) {
task.setDbSnp(dbsnp);
}
task.setVcfFilename(vcf);
task.setMinR2(minR2);
task.setGenotypeFormat(genotypeFormat);
task.setOutputVariantFilename(outputVariantFilename);
if (outputEffectsFilename != null) {
task.setOutputEffectsFilename(taskPrefix + ".effects.txt");
}
task.setIncludeVariantFilename(includeVariantFilename);
task.setIncludeSamplesFilename(includeSamplesFilename);
task.setOutput(taskPrefix + ".scores.txt");
tasks.add(task);
}
TaskService.setThreads(threads);
List<Task> results = TaskService.monitor(App.STYLE_LONG_TASK).run(tasks);
if (isFailed(results)) {
cleanUp();
return 1;
}
System.out.println();
MergeScoreTask mergeScore = new MergeScoreTask();
mergeScore.setInputs(tasks);
mergeScore.setOutput(out);
if (isFailed(TaskService.monitor(App.STYLE_SHORT_TASK).run(mergeScore))) {
cleanUp();
return 1;
}
if (outputEffectsFilename != null) {
MergeEffectsTask mergeEffectsTask = new MergeEffectsTask();
mergeEffectsTask.setInputs(tasks);
mergeEffectsTask.setOutput(outputEffectsFilename);
if (isFailed(TaskService.monitor(App.STYLE_SHORT_TASK).run(mergeEffectsTask))) {
cleanUp();
return 1;
}
}
MergeReportTask mergeReport = new MergeReportTask();
mergeReport.setInputs(tasks);
mergeReport.setOutput(reportJson);
if (isFailed(TaskService.monitor(App.STYLE_SHORT_TASK).run(mergeReport))) {
cleanUp();
return 1;
}
ReportFile report = mergeReport.getResult();
if (reportHtml != null) {
if (meta != null) {
MetaFile metaFile = MetaFile.load(meta);
report.mergeWithMeta(metaFile);
}
OutputFile data = new OutputFile(out);
CreateHtmlReportTask htmlReportTask = new CreateHtmlReportTask();
htmlReportTask.setReport(report);
htmlReportTask.setData(data);
htmlReportTask.setOutput(reportHtml);
if (isFailed(TaskService.monitor(App.STYLE_SHORT_TASK).run(htmlReportTask))) {
cleanUp();
return 1;
}
}
System.out.println();
System.out.println("Execution Time: " + formatTime(watch.getElapsedTimeSecs()));
System.out.println();
watch.stop();
cleanUp();
return 0;
}
use of htsjdk.samtools.util.StopWatch in project jvarkit by lindenb.
the class MakeMiniBam method doWork.
@Override
public int doWork(final List<String> args) {
ArchiveFactory archive = null;
int id_generator = 0;
final Set<String> outputFileNames = new HashSet<>();
try {
if (StringUtils.isBlank(this.filePrefix) || this.filePrefix.equals("now")) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
this.filePrefix = simpleDateFormat.format(new Date()) + ".";
}
if (!this.filePrefix.endsWith(".")) {
this.filePrefix += ".";
}
IOUtil.assertDirectoryIsWritable(tmpDir);
final List<Path> bamFiles = IOUtils.unrollPaths(args);
if (bamFiles.isEmpty()) {
LOG.error("no bam file defined");
return -1;
}
final List<Locatable> locatables = this.intervalListProvider.enableBreakEndInterval(!disable_sv_bnd).enableSinglePoint().stream().collect(Collectors.toList());
if (locatables.isEmpty()) {
LOG.error("no position defined");
return -1;
}
final SAMFileWriterFactory swf = new SAMFileWriterFactory();
swf.setCompressionLevel(9);
swf.setCreateIndex(true);
final SamReaderFactory srf = super.createSamReaderFactory();
if (this.referencePath != null)
srf.referenceSequence(this.referencePath);
archive = ArchiveFactory.open(this.outputFile);
archive.setCompressionLevel(Deflater.NO_COMPRESSION);
for (final Path bamFile : bamFiles) {
LOG.info(bamFile.toString());
final StopWatch stopWatch = new StopWatch();
stopWatch.start();
final SamReader sr = srf.open(bamFile);
if (!sr.hasIndex()) {
sr.close();
LOG.error("file " + bamFile + " is not indexed.");
return -1;
}
final SAMFileHeader header = sr.getFileHeader();
if (!header.getSortOrder().equals(SortOrder.coordinate)) {
sr.close();
LOG.error("file " + bamFile + " is not sorted on coordinate.");
return -1;
}
final SAMSequenceDictionary dict = SequenceDictionaryUtils.extractRequired(header);
final Optional<String> dictLabel = SequenceDictionaryUtils.getBuildName(dict);
final String labelSuffix = (dictLabel.isPresent() ? "." + dictLabel.get() : "") + (locatables.size() == 1 ? "." + locatables.get(0).getContig() + "_" + locatables.get(0).getStart() + (locatables.get(0).getStart() == locatables.get(0).getEnd() ? "" : "_" + locatables.get(0).getEnd()) : "");
final ContigNameConverter ctgConvert = ContigNameConverter.fromOneDictionary(dict);
final QueryInterval[] array = locatables.stream().flatMap(loc -> {
if (this.bound_edge < 1 || loc.getLengthOnReference() <= this.bound_edge) {
return Collections.singletonList(loc).stream();
}
return Arrays.asList((Locatable) new SimpleInterval(loc.getContig(), loc.getStart(), loc.getStart()), (Locatable) new SimpleInterval(loc.getContig(), loc.getEnd(), loc.getEnd())).stream();
}).map(LOC -> {
final String contig = ctgConvert.apply(LOC.getContig());
if (StringUtils.isBlank(contig)) {
LOG.warn("Cannot find " + LOC.getContig() + " in " + bamFile);
return null;
}
final SAMSequenceRecord ssr = dict.getSequence(contig);
if (LOC.getStart() > ssr.getSequenceLength()) {
LOG.warn("pos " + LOC + " is greater than chromosome size " + ssr.getSequenceLength() + " in " + bamFile);
return null;
}
return new QueryInterval(ssr.getSequenceIndex(), Math.max(1, LOC.getStart() - extend), Math.min(ssr.getSequenceLength(), LOC.getEnd() + extend));
}).filter(Q -> Q != null).collect(HtsCollectors.optimizedQueryIntervals());
final Path tmpBam = Files.createTempFile(this.tmpDir, "tmp.", ".bam");
final SAMFileHeader header2 = header.clone();
final SAMProgramRecord prg = header2.createProgramRecord();
prg.setProgramName(this.getProgramName());
prg.setProgramVersion(this.getGitHash());
prg.setCommandLine(this.getProgramCommandLine());
JVarkitVersion.getInstance().addMetaData(this, header2);
header2.addComment("MiniBam : Bam was " + bamFile);
try (SAMFileWriter sfw = swf.makeBAMWriter(header2, true, tmpBam)) {
if (array.length > 0) {
try (CloseableIterator<SAMRecord> ssr = sr.query(array, false)) {
while (ssr.hasNext()) {
final SAMRecord rec = ssr.next();
if (this.samRecordFilter.filterOut(rec))
continue;
rec.setAttribute(SAMTag.PG.name(), prg.getId());
sfw.addAlignment(rec);
}
}
}
}
final Path tmpBai = SamFiles.findIndex(tmpBam);
if (!Files.exists(tmpBai)) {
LOG.error("Cannot find tmp bai Index for " + bamFile + " " + tmpBam);
return -1;
}
final String sampleName1 = header.getReadGroups().stream().map(RG -> RG.getSample()).filter(S -> !StringUtils.isBlank(S)).findFirst().orElse(null);
final String sampleName;
if (!StringUtils.isBlank(sampleName1)) {
sampleName = sampleName1;
} else if (this.allow_no_sample) {
sampleName = IOUtils.getFilenameWithoutSuffix(bamFile, 1);
LOG.warn("No Read group in " + bamFile + " using filename : " + sampleName);
} else {
throw new IllegalArgumentException("No Sample found in " + bamFile + ". Use --no-samples option ?");
}
String filename = this.filePrefix + sampleName + labelSuffix;
while (outputFileNames.contains(filename)) {
filename = this.filePrefix + sampleName + "." + (id_generator++) + labelSuffix;
}
outputFileNames.add(filename);
archive.copyTo(tmpBam, filename + FileExtensions.BAM);
archive.copyTo(tmpBai, filename + IOUtils.getFileSuffix(tmpBai));
stopWatch.stop();
LOG.info("Added " + StringUtils.niceFileSize(Files.size(tmpBam)) + "(bam) and " + StringUtils.niceFileSize(Files.size(tmpBai)) + " (Bai). " + StringUtils.niceDuration(stopWatch.getElapsedTime()));
Files.deleteIfExists(tmpBam);
Files.deleteIfExists(tmpBai);
}
if (!StringUtils.isBlank(this.userComment)) {
try (final PrintWriter pw = archive.openWriter(this.filePrefix + (this.filePrefix.endsWith(".") ? "" : ".") + "README.md")) {
pw.append(this.userComment);
pw.println();
pw.println("## BAMS");
pw.println();
for (final Path bamFile : bamFiles) pw.println(" * " + bamFile);
pw.println();
pw.println("## Date");
pw.println();
pw.println(new SimpleDateFormat("yyyyMMdd").format(new Date()));
pw.flush();
}
}
archive.close();
archive = null;
return 0;
} catch (final Throwable err) {
LOG.error(err);
return -1;
} finally {
CloserUtil.close(archive);
}
}
Aggregations