JAL-2909 recognise .bam extension in URL and allow location spec as suffix - e.g...
authorJim Procter <jprocter@issues.jalview.org>
Tue, 20 Mar 2018 16:29:25 +0000 (16:29 +0000)
committerJim Procter <jprocter@issues.jalview.org>
Tue, 20 Mar 2018 16:29:25 +0000 (16:29 +0000)
src/jalview/io/BamFile.java
src/jalview/io/FileLoader.java
src/jalview/io/FileParse.java
src/jalview/io/IdentifyFile.java

index c8bac62..8a04867 100644 (file)
 package jalview.io;
 
 import jalview.datamodel.CigarParser;
+import jalview.datamodel.Range;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 import java.util.PrimitiveIterator.OfInt;
 import java.util.SortedMap;
 
 import htsjdk.samtools.SAMRecord;
 import htsjdk.samtools.SAMRecordIterator;
 import htsjdk.samtools.SAMSequenceRecord;
+import htsjdk.samtools.SamInputResource;
 import htsjdk.samtools.SamReader;
 import htsjdk.samtools.SamReaderFactory;
 import htsjdk.samtools.ValidationStringency;
@@ -94,13 +98,24 @@ public class BamFile extends AlignFile
   public BamFile(FileParse source) throws IOException
   {
     super(true, source);
+    parseSuffix();
     final SamReaderFactory factory = SamReaderFactory.makeDefault()
             .enable(SamReaderFactory.Option.INCLUDE_SOURCE_IN_RECORDS,
                     SamReaderFactory.Option.VALIDATE_CRC_CHECKSUMS)
             .validationStringency(ValidationStringency.SILENT);
 
     // File-based bam
-    fileReader = factory.open(source.inFile);
+    if (source.getDataSourceType() == DataSourceType.FILE)
+    {
+      fileReader = factory.open(source.inFile);
+    }
+    else
+    {
+      // locate index ?
+      String index = source.getDataName() + ".bai";
+      fileReader = factory.open(SamInputResource.of(source.getDataName())
+              .index(new URL(index)));
+    }
   }
   
   @Override
@@ -187,5 +202,39 @@ public class BamFile extends AlignFile
     chromosome = chr;
     start = s;
     end = e;
+    suffix = chromosome + ":" + start + "-" + end;
+  }
+
+  public boolean parseSuffix()
+  {
+    if (suffix == null)
+    {
+      return false;
+    }
+    int csep = suffix.indexOf(":");
+    int rsep = suffix.indexOf("-", csep);
+    if (csep < 0 || rsep < 0 || suffix.length() - rsep <= 1)
+    {
+      return false;
+    }
+    String chr, p1, p2;
+    chr = suffix.substring(0, csep);
+    p1 = suffix.substring(csep + 1, rsep);
+    p2 = suffix.substring(rsep + 1);
+    int cstart, cend;
+    try
+    {
+      cstart = Integer.parseInt(p1);
+      cend = Integer.parseInt(p2);
+    } catch (Exception e)
+    {
+      warningMessage = (warningMessage == null ? "" : warningMessage + "\n")
+              + "Couldn't parse range from " + suffix;
+      return false;
+    }
+    chromosome = chr;
+    start = cstart;
+    end = cend;
+    return true;
   }
 }
index 96ab8bb..8eb60e5 100755 (executable)
@@ -317,28 +317,33 @@ public class FileLoader implements Runnable
           fa.prepareFileReader(file, protocol, format);
           source = fa.getAlignFile();
         }
-
-        BamFileOptionsChooser bamoptions = new BamFileOptionsChooser(
-                source);
-
-        // ask the user which bit of the bam they want to load
-        int confirm = JvOptionPane.showConfirmDialog(null,
-                bamoptions,
-                MessageManager.getString("label.bam_file_options"),
-                JvOptionPane.OK_CANCEL_OPTION,
-                JvOptionPane.PLAIN_MESSAGE);
-        
-        if (confirm == JvOptionPane.CANCEL_OPTION
-                || confirm == JvOptionPane.CLOSED_OPTION)
-        {
-          Desktop.instance.stopLoading();
-          return;
-        }
-        else
+        if (!((BamFile) source).parseSuffix())
         {
-          bamoptions.update(source);
-          al = fa.readFile(file, protocol, format);
+          // configure a window
+          BamFileOptionsChooser bamoptions = new BamFileOptionsChooser(
+                  source);
+          // ask the user which bit of the bam they want to load
+          int confirm = JvOptionPane.showConfirmDialog(null, bamoptions,
+                  MessageManager.getString("label.bam_file_options"),
+                  JvOptionPane.OK_CANCEL_OPTION,
+                  JvOptionPane.PLAIN_MESSAGE);
+
+          if (confirm == JvOptionPane.CANCEL_OPTION
+                  || confirm == JvOptionPane.CLOSED_OPTION)
+          {
+            Desktop.instance.stopLoading();
+            return;
+          }
+          else
+          {
+            bamoptions.update(source);
+            if (file.indexOf("#") == -1)
+            {
+              file = file + "#" + ((BamFile) source).suffix;
+            }
+          }
         }
+        al = fa.readFile(file, protocol, format);
       }
 
       if (FileFormat.Jalview.equals(format))
index c0328d5..f077e21 100755 (executable)
@@ -344,7 +344,8 @@ public class FileParse
           checkURLSource(fileStr);
           if (suffixSeparator == '#')
           {
-            extractSuffix(fileStr); // URL lref is stored for later reference.
+            dataName = extractSuffix(fileStr); // URL lref is stored for later
+                                               // reference.
           }
         } catch (IOException e)
         {
index 38414e3..e65101d 100755 (executable)
@@ -127,9 +127,10 @@ public class IdentifyFile
         {
           // jar files are special - since they contain all sorts of random
           // characters.
-          if (source.inFile != null)
+          if (source.inFile != null || source.getDataName() != null)
           {
-            String fileStr = source.inFile.getName();
+            String fileStr = source.inFile == null ? source.getDataName()
+                    : source.inFile.getName();
             // possibly a Jalview archive.
             if (fileStr.lastIndexOf(".jar") > -1
                     || fileStr.lastIndexOf(".zip") > -1)