JAL-2835 spike updated to latest (use specific SO term for feature)
[jalview.git] / src / jalview / ext / htsjdk / HtsContigDb.java
index f3b5098..729f658 100644 (file)
@@ -1,16 +1,32 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ * 
+ * This file is part of Jalview.
+ * 
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
+ * Jalview is distributed in the hope that it will be useful, but 
+ * WITHOUT ANY WARRANTY; without even the implied warranty 
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
+ * PURPOSE.  See the GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
 package jalview.ext.htsjdk;
 
-import htsjdk.samtools.SAMSequenceDictionary;
-import htsjdk.samtools.SAMSequenceRecord;
-import htsjdk.samtools.reference.ReferenceSequence;
-import htsjdk.samtools.reference.ReferenceSequenceFile;
-import htsjdk.samtools.reference.ReferenceSequenceFileFactory;
-import htsjdk.samtools.util.StringUtil;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
 
 import java.io.File;
+import java.io.IOException;
 import java.math.BigInteger;
+import java.nio.file.Path;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.util.ArrayList;
@@ -18,6 +34,15 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import htsjdk.samtools.SAMException;
+import htsjdk.samtools.SAMSequenceDictionary;
+import htsjdk.samtools.SAMSequenceRecord;
+import htsjdk.samtools.reference.FastaSequenceIndexCreator;
+import htsjdk.samtools.reference.ReferenceSequence;
+import htsjdk.samtools.reference.ReferenceSequenceFile;
+import htsjdk.samtools.reference.ReferenceSequenceFileFactory;
+import htsjdk.samtools.util.StringUtil;
+
 /**
  * a source of sequence data accessed via the HTSJDK
  * 
@@ -26,14 +51,25 @@ import java.util.Set;
  */
 public class HtsContigDb
 {
-
   private String name;
 
   private File dbLocation;
 
   private htsjdk.samtools.reference.ReferenceSequenceFile refFile = null;
 
-  public HtsContigDb(String name, File descriptor) throws Exception
+  public static void createFastaSequenceIndex(Path path, boolean overwrite)
+          throws IOException
+  {
+    try
+    {
+      FastaSequenceIndexCreator.create(path, overwrite);
+    } catch (SAMException e)
+    {
+      throw new IOException(e.getMessage());
+    }
+  }
+
+  public HtsContigDb(String name, File descriptor)
   {
     if (descriptor.isFile())
     {
@@ -43,15 +79,29 @@ public class HtsContigDb
     initSource();
   }
 
-  private void initSource() throws Exception
+  public void close()
+  {
+    if (refFile != null)
+    {
+      try
+      {
+        refFile.close();
+      } catch (IOException e)
+      {
+        // ignore
+      }
+    }
+  }
+
+  private void initSource()
   {
     if (refFile != null)
     {
       return;
     }
 
-    refFile = ReferenceSequenceFileFactory.getReferenceSequenceFile(
-            dbLocation, true);
+    refFile = ReferenceSequenceFileFactory
+            .getReferenceSequenceFile(dbLocation, true);
     if (refFile == null || refFile.getSequenceDictionary() == null)
     {
       // refFile = initSequenceDictionaryFor(dbLocation);
@@ -59,18 +109,21 @@ public class HtsContigDb
 
   }
 
-
   SAMSequenceDictionary rrefDict = null;
-  private ReferenceSequenceFile initSequenceDictionaryFor(File dbLocation2) throws Exception
+
+  private ReferenceSequenceFile initSequenceDictionaryFor(File dbLocation2)
+          throws Exception
   {
     rrefDict = getDictionary(dbLocation2, true);
     if (rrefDict != null)
     {
-      ReferenceSequenceFile rrefFile = ReferenceSequenceFileFactory.getReferenceSequenceFile(dbLocation2, true);
+      ReferenceSequenceFile rrefFile = ReferenceSequenceFileFactory
+              .getReferenceSequenceFile(dbLocation2, true);
       return rrefFile;
     }
     return null;
   }
+
   /**
    * code below hacked out from picard ----
    * 
@@ -79,7 +132,6 @@ public class HtsContigDb
    * broadinstitute/picard/commit/270580d3e28123496576f0b91b3433179bb5d876
    */
 
-
   /*
    * The MIT License
    * 
@@ -120,9 +172,10 @@ public class HtsContigDb
     final ReferenceSequenceFile refSeqFile = ReferenceSequenceFileFactory
             .getReferenceSequenceFile(f, truncate);
     ReferenceSequence refSeq;
-    List<SAMSequenceRecord> ret = new ArrayList<SAMSequenceRecord>();
-    Set<String> sequenceNames = new HashSet<String>();
-    for (int numSequences = 0; (refSeq = refSeqFile.nextSequence()) != null; ++numSequences)
+    List<SAMSequenceRecord> ret = new ArrayList<>();
+    Set<String> sequenceNames = new HashSet<>();
+    for (int numSequences = 0; (refSeq = refSeqFile
+            .nextSequence()) != null; ++numSequences)
     {
       if (sequenceNames.contains(refSeq.getName()))
       {
@@ -197,7 +250,7 @@ public class HtsContigDb
 
   // ///// end of hts bits.
 
-  SequenceI getSequenceProxy(String id)
+  public SequenceI getSequenceProxy(String id)
   {
     if (!isValid())
     {
@@ -207,4 +260,10 @@ public class HtsContigDb
     ReferenceSequence sseq = refFile.getSequence(id);
     return new Sequence(sseq.getName(), new String(sseq.getBases()));
   }
+
+  public boolean isIndexed()
+  {
+    return refFile != null && refFile.isIndexed();
+  }
+
 }