Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrintFileDiagnostics for cram, crai and bai. #8577

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package org.broadinstitute.hellbender.tools;

import org.broadinstitute.barclay.argparser.*;
import org.broadinstitute.hellbender.cmdline.CommandLineProgram;
import org.broadinstitute.hellbender.cmdline.StandardArgumentDefinitions;
import org.broadinstitute.hellbender.engine.GATKPath;
import org.broadinstitute.hellbender.tools.filediagnostics.HTSAnalyzer;
import org.broadinstitute.hellbender.tools.filediagnostics.HTSAnalyzerFactory;
import picard.cmdline.programgroups.OtherProgramGroup;

import java.io.File;

/**
* A diagnostic tool that prints meta information about a GATK input file.
*
* Works on files ending in .cram, .crai, and .bai.
*
* Sample Usage:
*
* gatk PrintFileDiagnostics \
* -I input.cram \
* -count-limit 10
*/
@ExperimentalFeature
@WorkflowProperties
@CommandLineProgramProperties(
summary = "Print diagnostic information about a genomics file to stdout",
oneLineSummary = "Print diagnostic information about a genomics file to stdout",
programGroup = OtherProgramGroup.class
)
public class PrintFileDiagnostics extends CommandLineProgram {

@Argument(fullName = StandardArgumentDefinitions.INPUT_LONG_NAME,
shortName = StandardArgumentDefinitions.INPUT_SHORT_NAME,
doc = "Input path for diagnostics",
optional = false,
common = true)
@WorkflowInput
public GATKPath inputPath;

@Argument(fullName = StandardArgumentDefinitions.OUTPUT_LONG_NAME,
shortName = StandardArgumentDefinitions.OUTPUT_SHORT_NAME,
doc = "Outut file for diagnostics (must be a local file)",
optional = false,
common = true)
@WorkflowInput
public File outputFile;

@Argument(shortName="count-limit",
fullName="count-limit",
doc="Limit on how much output to emit (.cram only)")
private long countLimit = 1000;

private HTSAnalyzer htsAnalyzer;

@Override
protected void onStartup() {
super.onStartup();
htsAnalyzer = HTSAnalyzerFactory.getFileAnalyzer(inputPath, outputFile, countLimit);
}

@Override
protected Object doWork() {
htsAnalyzer.analyze();
return 0;
}

@Override
protected void onShutdown() {
if ( htsAnalyzer != null ) {
try {
htsAnalyzer.close();
} catch (Exception e) {
throw new RuntimeException(e);

Check warning on line 74 in src/main/java/org/broadinstitute/hellbender/tools/PrintFileDiagnostics.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/broadinstitute/hellbender/tools/PrintFileDiagnostics.java#L73-L74

Added lines #L73 - L74 were not covered by tests
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.broadinstitute.hellbender.tools.filediagnostics;

import htsjdk.samtools.BAMIndexer;
import org.broadinstitute.hellbender.engine.GATKPath;

import java.io.File;
import java.io.IOException;

/**
* Analyzer for BAI files.
*/
public class BAIAnalyzer extends HTSAnalyzer {

public BAIAnalyzer(final GATKPath inputPath, final File outputFile) {
super(inputPath, outputFile);
}

/**
* Run the analyzer for the file.
*/
protected void doAnalysis() {
System.out.println(String.format("\nOutput written to %s\n", outputFile));
BAMIndexer.createAndWriteIndex(inputPath.toPath().toFile(), outputFile, true);
}

@Override
public void close() throws IOException {
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.broadinstitute.hellbender.tools.filediagnostics;

import htsjdk.samtools.CRAMCRAIIndexer;
import htsjdk.samtools.cram.CRAIIndex;
import htsjdk.samtools.util.RuntimeIOException;
import org.broadinstitute.hellbender.engine.GATKPath;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* Analyzer for CRAM (.crai) index files.
*/
public class CRAIAnalyzer extends HTSAnalyzer {

final FileOutputStream fos;

public CRAIAnalyzer(final GATKPath inputPath, final File outputFile) {
super(inputPath, outputFile);
try {
fos = new FileOutputStream(outputFile);
} catch (final IOException e) {
throw new RuntimeIOException(e);

Check warning on line 25 in src/main/java/org/broadinstitute/hellbender/tools/filediagnostics/CRAIAnalyzer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/broadinstitute/hellbender/tools/filediagnostics/CRAIAnalyzer.java#L24-L25

Added lines #L24 - L25 were not covered by tests
}
}

protected void emitln(final String s) {
try {
fos.write(s.getBytes());
fos.write('\n');
} catch (IOException e) {
throw new RuntimeException(e);

Check warning on line 34 in src/main/java/org/broadinstitute/hellbender/tools/filediagnostics/CRAIAnalyzer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/broadinstitute/hellbender/tools/filediagnostics/CRAIAnalyzer.java#L33-L34

Added lines #L33 - L34 were not covered by tests
}
}

/**
* Run the analyzer for the file.
*/
protected void doAnalysis() {
try (final InputStream is = inputPath.getInputStream()) {
final CRAIIndex craiIndex = CRAMCRAIIndexer.readIndex(is);
emitln("\nSeqId AlignmentStart AlignmentSpan ContainerOffset SliceOffset SliceSize\n");
craiIndex.getCRAIEntries().stream().forEach(e -> emitln(e.toString()));
} catch (IOException e) {
throw new RuntimeException(e);

Check warning on line 47 in src/main/java/org/broadinstitute/hellbender/tools/filediagnostics/CRAIAnalyzer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/broadinstitute/hellbender/tools/filediagnostics/CRAIAnalyzer.java#L46-L47

Added lines #L46 - L47 were not covered by tests
}
}

@Override
public void close() throws IOException {
if (fos != null) {
fos.close();
}
}

}

Loading