From 60a86fb34a8f756008ac7e6bb4ee86cae22e795e Mon Sep 17 00:00:00 2001 From: hanna Date: Thu, 9 Jul 2009 18:18:48 +0000 Subject: [PATCH] Better handling of fasta files with non-standard extensions.x git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@1206 348d0f76-0448-11de-a6fe-93d51630548a --- .../utils/fasta/IndexedFastaSequenceFile.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/java/src/org/broadinstitute/sting/utils/fasta/IndexedFastaSequenceFile.java b/java/src/org/broadinstitute/sting/utils/fasta/IndexedFastaSequenceFile.java index d1a426e3f..e79223084 100755 --- a/java/src/org/broadinstitute/sting/utils/fasta/IndexedFastaSequenceFile.java +++ b/java/src/org/broadinstitute/sting/utils/fasta/IndexedFastaSequenceFile.java @@ -22,6 +22,7 @@ import net.sf.samtools.SAMTextHeaderCodec; import net.sf.samtools.SAMFileHeader; import net.sf.samtools.SAMSequenceRecord; import net.sf.samtools.util.AsciiLineReader; +import org.broadinstitute.sting.utils.StingException; /** * Created by IntelliJ IDEA. @@ -64,7 +65,7 @@ public class IndexedFastaSequenceFile implements ReferenceSequenceFile { private void loadDictionary( File fastaFile ) { // Try and locate the dictionary String dictionaryName = fastaFile.getAbsolutePath(); - dictionaryName = dictionaryName.substring(0, dictionaryName.lastIndexOf(".fasta")); + dictionaryName = dictionaryName.substring(0, getFastaFileExtensionStart(dictionaryName)); dictionaryName += ".dict"; final File dictionary = new File(dictionaryName); if (!dictionary.exists()) @@ -85,6 +86,21 @@ public class IndexedFastaSequenceFile implements ReferenceSequenceFile { } + /** + * Gets the index of the first character in the fasta file's extension. + * @param filename The filename of the fasta. Must not be null, and must end with either '.fasta' or '.fa'. + * @return The index of the start of the extension within the filename. If neither '.fasta' nor '.fa' are + * present in the filename, a StingException will be thrown. + */ + private int getFastaFileExtensionStart( String filename ) { + if( filename.endsWith(".fasta") ) + return filename.lastIndexOf(".fasta"); + else if( filename.endsWith(".fa") ) + return filename.lastIndexOf(".fa"); + else + throw new StingException("Invalid fasta filename; fasta filename must end with '.fasta' or '.fa'."); + } + /** * Loads the index for the fasta, if present. Throws an exception if now present. */