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

Adding error in LiftoverVcf if reference dictionary does not exist (Issue #1157) #1189

Merged
merged 5 commits into from
Jul 6, 2018
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
5 changes: 5 additions & 0 deletions src/main/java/picard/vcf/LiftoverVcf.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,11 @@ protected int doWork() {
log.info("Loading up the target reference genome.");
final ReferenceSequenceFileWalker walker = new ReferenceSequenceFileWalker(REFERENCE_SEQUENCE);
final Map<String, ReferenceSequence> refSeqs = new HashMap<>();
// check if sequence dictionary exists
if (walker.getSequenceDictionary() == null) {
log.error("Reference " + REFERENCE_SEQUENCE.getAbsolutePath() + " must have an associated Dictionary .dict file in the same directory.");
return 1;
}
for (final SAMSequenceRecord rec : walker.getSequenceDictionary().getSequences()) {
refSeqs.put(rec.getSequenceName(), walker.get(rec.getSequenceIndex()));
}
Expand Down
55 changes: 54 additions & 1 deletion src/test/java/picard/util/LiftoverVcfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import htsjdk.samtools.util.Interval;
import htsjdk.variant.variantcontext.*;
import htsjdk.variant.vcf.VCFFileReader;
import htsjdk.samtools.SAMException;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import picard.PicardException;
import picard.cmdline.CommandLineProgramTest;
import picard.vcf.LiftoverVcf;
import picard.vcf.VcfTestUtils;
Expand All @@ -21,7 +23,9 @@
import java.io.File;
import java.io.IOException;
import java.util.*;

import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
/**
* Test class for LiftoverVcf.
* <p>
Expand Down Expand Up @@ -1169,4 +1173,53 @@ public void testLiftOverNoCallAndSymbolic(final LiftOver liftOver, final Variant
Assert.assertEquals(resultAlleles, result.getAttributeAsStringList(LiftoverVcf.ORIGINAL_ALLELES, null));
}
}

@Test()
public void testNoDictionary() throws IOException {
final Path liftOutput = Files.createTempFile("tmpouput", ".vcf");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with all these deleteOnExit, I wonder if it's cleaner to have a
try{
....
} finally {
TestUtil.recursiveDelete(...);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe not....nevermind.

liftOutput.toFile().deleteOnExit();
final Path rejectOutput = Files.createTempFile("tmpreject", ".vcf");
rejectOutput.toFile().deleteOnExit();
final Path input = TEST_DATA_PATH.toPath().resolve("testLiftoverBiallelicIndels.vcf");
final Path tmpCopyDir = Files.createTempDirectory("copy");
tmpCopyDir.toFile().deleteOnExit();
final Path referenceCopy = tmpCopyDir.resolve("refCopy.fasta");
referenceCopy.toFile().deleteOnExit();
Files.copy(REFERENCE_FILE.toPath(), referenceCopy);
final String[] args = new String[]{
"INPUT=" + input,
"OUTPUT=" + liftOutput,
"REJECT=" + rejectOutput,
"CHAIN=" + CHAIN_FILE,
"REFERENCE_SEQUENCE=" + referenceCopy,
};
Assert.assertEquals(runPicardCommandLine(args), 1);
}

@Test(expectedExceptions = SAMException.class, expectedExceptionsMessageRegExp = "File exists but is not readable:.*")
public void testUnreadableDictionary() throws IOException {
final Path liftOutput = Files.createTempFile("tmpouput", ".vcf");
liftOutput.toFile().deleteOnExit();
final Path rejectOutput = Files.createTempFile("tmpreject", ".vcf");
rejectOutput.toFile().deleteOnExit();
final Path input = TEST_DATA_PATH.toPath().resolve("testLiftoverBiallelicIndels.vcf");
final Path tmpCopyDir = Files.createTempDirectory("copy");
tmpCopyDir.toFile().deleteOnExit();
final Path referenceCopy = tmpCopyDir.resolve("refCopy.fasta");
referenceCopy.toFile().deleteOnExit();
Files.copy(REFERENCE_FILE.toPath(), referenceCopy);
final Path dictCopy = referenceCopy.resolveSibling(referenceCopy.toFile().getName().replaceAll("fasta$", "dict"));
dictCopy.toFile().deleteOnExit();
final Path dictionary = REFERENCE_FILE.toPath().resolveSibling(REFERENCE_FILE.getName().replaceAll("fasta$", "dict"));
Files.copy(dictionary, dictCopy);
dictCopy.toFile().setReadable(false);
final String[] args = new String[]{
"INPUT=" + input,
"OUTPUT=" + liftOutput,
"REJECT=" + rejectOutput,
"CHAIN=" + CHAIN_FILE,
"REFERENCE_SEQUENCE=" + referenceCopy,
};
runPicardCommandLine(args);
}
}