Skip to content

Commit

Permalink
Throwing exception when matching input.sv.bam files are required but …
Browse files Browse the repository at this point in the history
…missing. Prevents misconfigured pipelines from generating bad assembly results
  • Loading branch information
d-cameron committed Jun 5, 2018
1 parent 7e89071 commit b8fba59
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>au.edu.wehi</groupId>
<artifactId>gridss</artifactId>
<packaging>jar</packaging>
<version>1.7.2-gridss-SNAPSHOT</version>
<version>1.7.2-gridss</version>
<name>gridss</name>
<url>https://github.com/PapenfussLab/gridss</url>
<properties>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/au/edu/wehi/idsv/AssemblyEvidenceSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public AssemblyEvidenceSource(ProcessingContext processContext, List<SAMEvidence
* @throws IOException
*/
public void assembleBreakends(ExecutorService threadpool) throws IOException {
source.stream().forEach(ses -> ses.assertPreprocessingComplete());
if (threadpool == null) {
threadpool = MoreExecutors.newDirectExecutorService();
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/au/edu/wehi/idsv/FileSystemContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public synchronized File getIntermediateDirectory(File file) {
return workingDir;
}
private static File getSource(File file) {
if (file == null) {
return null;
}
String source = file.getPath();
if (source.contains(INTERMEDIATE_DIR_SUFFIX)) {
source = source.substring(0, source.indexOf(INTERMEDIATE_DIR_SUFFIX));
Expand All @@ -107,6 +110,9 @@ private static File getSource(File file) {
return result;
}
private synchronized File getFile(String path) {
if (path == null) {
return null;
}
File file = new File(path);
file.getParentFile().mkdirs();
return file;
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/au/edu/wehi/idsv/SAMEvidenceSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,22 @@ public CloseableIterator<DirectedEvidence> iterator() {
return new AutoClosingIterator<>(eit, reader, it);
}
private SamReader getReader() {
File svFile = getContext().getFileSystemContext().getSVBam(getFile());
File svFile = getSVFile();
SamReader reader = getProcessContext().getSamReader(svFile.exists() ? svFile : getFile());
return reader;
}
public File getSVFile() {
if (getFile() == null) {
return null;
}
return getContext().getFileSystemContext().getSVBam(getFile());
}
public void assertPreprocessingComplete() {
File svFile = getSVFile();
if (svFile != null && !svFile.exists()) {
throw new IllegalStateException(String.format("Missing required file %s. See GRIDSS pipeline examples and documentation.", svFile));
}
}
private Iterator<DirectedEvidence> asEvidence(Iterator<SAMRecord> it) {
it = new BufferedIterator<>(it, 2); // TODO: remove when https://github.com/samtools/htsjdk/issues/760 is resolved
it = Iterators.transform(it, r -> transform(r));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/au/edu/wehi/idsv/VariantCaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public VariantCaller(ProcessingContext context, List<SAMEvidenceSource> samEvide
this.assemblyEvidence = assemblyEvidence;
}
public void callBreakends(File vcf, ExecutorService threadpool) throws IOException {
samEvidence.stream().forEach(ses -> ses.assertPreprocessingComplete());
assemblyEvidence.assertPreprocessingComplete();
log.info("Identifying Breakpoints");
if (threadpool == null) {
threadpool = MoreExecutors.newDirectExecutorService();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/au/edu/wehi/idsv/util/FileHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ private static void trymovesingle(File from, File to) throws IOException {
* @param to
* @throws IOException
*/
public static void copy(File from, File to, boolean moveIndexes) throws IOException {
public static void copy(File from, File to, boolean copyIndexes) throws IOException {
if (!from.exists()) {
throw new IllegalArgumentException("Cannot copy nonexist file" + from.getAbsolutePath());
}
if (to.exists()) {
FileHelper.delete(to, moveIndexes);
FileHelper.delete(to, copyIndexes);
}
Files.copy(from, to);
copyIndex(from, to, ".bai");
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/gridss/AllocateEvidence.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutorService;

import org.broadinstitute.barclay.argparser.Argument;
Expand Down Expand Up @@ -54,7 +55,9 @@ public CloseableIterator<DirectedEvidence> getReadIterator() {
if (getContext().getVariantCallingParameters().callOnlyAssemblies) {
evidenceIt = SAMEvidenceSource.mergedIterator(ImmutableList.of(), false);
} else {
evidenceIt = SAMEvidenceSource.mergedIterator(ImmutableList.<SAMEvidenceSource>builder().addAll(getSamEvidenceSources()).build(), true);
List<SAMEvidenceSource> sources = getSamEvidenceSources();
sources.stream().forEach(ses -> ses.assertPreprocessingComplete());
evidenceIt = SAMEvidenceSource.mergedIterator(ImmutableList.<SAMEvidenceSource>builder().addAll(sources).build(), true);
}
if (Defaults.SANITY_CHECK_ITERATORS) {
evidenceIt = new AutoClosingIterator<>(
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/au/edu/wehi/idsv/AssemblyEvidenceSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import au.edu.wehi.idsv.picard.InMemoryReferenceSequenceFile;
import au.edu.wehi.idsv.sam.SAMRecordUtil;
import au.edu.wehi.idsv.util.FileHelper;
import htsjdk.samtools.SAMFileHeader;
import htsjdk.samtools.SAMFileHeader.SortOrder;
import htsjdk.samtools.SAMRecord;
Expand All @@ -38,6 +39,7 @@ public void setup() throws IOException {
public void should_write_breakend_bam() throws IOException {
createInput(RP(0, 1, 2, 1));
SAMEvidenceSource ses = new SAMEvidenceSource(getCommandlineContext(), input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(getCommandlineContext(), ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
assertTrue(assemblyFile.exists());
Expand All @@ -46,6 +48,7 @@ public void should_write_breakend_bam() throws IOException {
public void breakend_bam_should_be_coordinate_sorted() throws IOException {
createInput(RP(0, 1, 2, 1));
SAMEvidenceSource ses = new SAMEvidenceSource(getCommandlineContext(), input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(getCommandlineContext(), ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
try (SamReader r = SamReaderFactory.makeDefault().open(assemblyFile)) {
Expand All @@ -61,6 +64,7 @@ public void debruijn_should_generate_bam() throws IOException {
ProcessingContext pc = getCommandlineContext();
pc.getConfig().getAssembly().minReads = 1;
SAMEvidenceSource ses = new SAMEvidenceSource(pc, input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);

Expand All @@ -83,6 +87,7 @@ public void iterator_should_return_in_chr_order() throws IOException {
pc.getConfig().getAssembly().minReads = 1;
//pc.getRealignmentParameters().requireRealignment = false;
SAMEvidenceSource ses = new SAMEvidenceSource(pc, input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
List<DirectedEvidence> list = Lists.newArrayList(aes.iterator());
Expand All @@ -99,6 +104,7 @@ public void should_not_write_filtered_assemblies() throws IOException {
ProcessingContext pc = getCommandlineContext();
pc.getAssemblyParameters().writeFiltered = false;
SAMEvidenceSource ses = new SAMEvidenceSource(pc, input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
List<DirectedEvidence> contigs = Lists.newArrayList(aes.iterator());
Expand Down Expand Up @@ -229,6 +235,7 @@ public void should_adjust_assembly_contig_alignment_to_align_with_reference_homo
Read(0, 5, "5M7S")
);
SAMEvidenceSource ses = new SAMEvidenceSource(getCommandlineContext(), input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(getCommandlineContext(), ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
List<SAMRecord> assemblies = getRecords(assemblyFile);
Expand Down Expand Up @@ -268,6 +275,7 @@ public void chunck_spanning_assemblies_should_not_be_repeated() throws IOExcepti
pc.getConfig().getAssembly().minReads = 1;
pc.getConfig().chunkSize = 100;
SAMEvidenceSource ses = new SAMEvidenceSource(pc, input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
List<DirectedEvidence> list = Lists.newArrayList(aes.iterator());
Expand All @@ -284,6 +292,7 @@ public void parallel_assembly_should_not_affect_assembly_results() throws IOExce
pc.getConfig().getAssembly().minReads = 1;
pc.getConfig().chunkSize = 100;
SAMEvidenceSource ses = new SAMEvidenceSource(pc, input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
ExecutorService threadpool = Executors.newFixedThreadPool(8);
aes.assembleBreakends(threadpool);
Expand All @@ -307,6 +316,7 @@ public void bounds_check_should_apply_to_final_assembly_SAMRecord() throws IOExc
pc.getConfig().getSoftClip().minAnchorIdentity = 0;
pc.getConfig().getSoftClip().minAverageQual = 0;
SAMEvidenceSource ses = new SAMEvidenceSource(pc, input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
getRecords(assemblyFile);
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/au/edu/wehi/idsv/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,9 @@ public CloseableIterator<DirectedEvidence> iterator(QueryInterval[] qi) {
QueryIntervalUtil.overlaps(qi, de.getBreakendSummary()))
);
}
@Override
public void assertPreprocessingComplete() {
}
}
public static class StubAssemblyEvidenceSource extends AssemblyEvidenceSource {
public int assemblyWindowSize = 10;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import au.edu.wehi.idsv.ProcessingContext;
import au.edu.wehi.idsv.SAMEvidenceSource;
import au.edu.wehi.idsv.sam.SAMFileUtil;
import au.edu.wehi.idsv.util.FileHelper;
import gridss.ComputeSamTags;
import htsjdk.samtools.SAMFileHeader.SortOrder;
import picard.sam.BuildBamIndex;
Expand All @@ -36,6 +37,7 @@ public void positional_should_export_dot() throws IOException {
});
ProcessingContext pc = getCommandlineContext();
SAMEvidenceSource ses = new SAMEvidenceSource(pc, input, null, 0);
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), output);
aes.assembleBreakends(null);
File dir = new File(super.testFolder.getRoot(), "visualisation");
Expand Down
6 changes: 6 additions & 0 deletions src/test/java/gridss/AllocateEvidenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import au.edu.wehi.idsv.VariantContextDirectedBreakpoint;
import au.edu.wehi.idsv.VariantContextDirectedEvidence;
import au.edu.wehi.idsv.util.AutoClosingIterator;
import au.edu.wehi.idsv.util.FileHelper;
import htsjdk.samtools.SAMFileHeader.SortOrder;
import htsjdk.samtools.SAMRecord;

Expand All @@ -60,6 +61,7 @@ public void should_annotate_reads() throws IOException {
DP(0, 2, "5M5S", true, 1, 10, "5M", true));
SAMEvidenceSource ses = new SAMEvidenceSource(getContext(), input, null, 0);
ses.ensureMetrics();
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
File assemblyFile = new File(testFolder.getRoot(), "assembly.bam");
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
Expand Down Expand Up @@ -92,6 +94,7 @@ public void should_apply_filters() throws IOException {
DP(0, 1, "5M5S", true, 1, 10, "5M", true));
SAMEvidenceSource ses = new SAMEvidenceSource(getContext(), input, null, 0);
ses.ensureMetrics();
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
File assemblyFile = new File(testFolder.getRoot(), "assembly.bam");
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
Expand Down Expand Up @@ -184,6 +187,7 @@ public void should_filter_if_insufficient_reads() throws IOException {
DP(0, 1, "5M5S", true, 1, 10, "5M", true));
SAMEvidenceSource ses = new SAMEvidenceSource(getContext(), input, null, 0);
ses.ensureMetrics();
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
File assemblyFile = new File(testFolder.getRoot(), "assembly.bam");
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
Expand Down Expand Up @@ -215,6 +219,7 @@ public void insufficient_reads_filter_should_count_fragments() throws IOExceptio
withReadName("read", DP(0, 1, "6M4S", true, 1, 10, "5M", true)));
SAMEvidenceSource ses = new SAMEvidenceSource(getContext(), input, null, 0);
ses.ensureMetrics();
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
File assemblyFile = new File(testFolder.getRoot(), "assembly.bam");
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
Expand Down Expand Up @@ -243,6 +248,7 @@ public void should_filter_if_insufficient_quality() throws IOException {
DP(0, 1, "5M5S", true, 1, 10, "5M", true));
SAMEvidenceSource ses = new SAMEvidenceSource(getContext(), input, null, 0);
ses.ensureMetrics();
FileHelper.copy(ses.getFile(), ses.getSVFile(), true);
File assemblyFile = new File(testFolder.getRoot(), "assembly.bam");
AssemblyEvidenceSource aes = new AssemblyEvidenceSource(pc, ImmutableList.of(ses), assemblyFile);
aes.assembleBreakends(null);
Expand Down

0 comments on commit b8fba59

Please sign in to comment.