Merge branch 'feature/JAL-2909bamfiles_features' into features/JAL-2909_bamImport2_11
[jalview.git] / src / jalview / io / FileLoader.java
index f26d6da..8eb60e5 100755 (executable)
@@ -32,6 +32,7 @@ import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceI;
 import jalview.gui.AlignFrame;
 import jalview.gui.AlignViewport;
+import jalview.gui.BamFileOptionsChooser;
 import jalview.gui.Desktop;
 import jalview.gui.Jalview2XML;
 import jalview.gui.JvOptionPane;
@@ -308,6 +309,43 @@ public class FileLoader implements Runnable
       loadtime = -System.currentTimeMillis();
       AlignmentI al = null;
 
+      if (FileFormat.Bam.equals(format))
+      {
+        FormatAdapter fa = new FormatAdapter();
+        if (source == null)
+        {
+          fa.prepareFileReader(file, protocol, format);
+          source = fa.getAlignFile();
+        }
+        if (!((BamFile) source).parseSuffix())
+        {
+          // 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))
       {
         if (source != null)
@@ -324,37 +362,40 @@ public class FileLoader implements Runnable
         String error = AppletFormatAdapter.getSupportedFormats();
         try
         {
-          if (source != null)
-          {
-            // read from the provided source
-            al = new FormatAdapter().readFromFile(source, format);
-          }
-          else
+          if (al == null)
           {
-
-            // open a new source and read from it
-            FormatAdapter fa = new FormatAdapter();
-            boolean downloadStructureFile = format.isStructureFile()
-                    && protocol.equals(DataSourceType.URL);
-            if (downloadStructureFile)
+            if (source != null)
             {
-              String structExt = format.getExtensions().split(",")[0];
-              String urlLeafName = file.substring(
-                      file.lastIndexOf(
-                              System.getProperty("file.separator")),
-                      file.lastIndexOf("."));
-              String tempStructureFileStr = createNamedJvTempFile(
-                      urlLeafName, structExt);
-              UrlDownloadClient.download(file, tempStructureFileStr);
-              al = fa.readFile(tempStructureFileStr, DataSourceType.FILE,
-                      format);
-              source = fa.getAlignFile();
+              // read from the provided source
+              al = new FormatAdapter().readFromFile(source, format);
             }
             else
             {
-              al = fa.readFile(file, protocol, format);
-              source = fa.getAlignFile(); // keep reference for later if
-                                          // necessary.
+
+              // open a new source and read from it
+              FormatAdapter fa = new FormatAdapter();
+              boolean downloadStructureFile = format.isStructureFile()
+                      && protocol.equals(DataSourceType.URL);
+              if (downloadStructureFile)
+              {
+                String structExt = format.getExtensions().split(",")[0];
+                String urlLeafName = file.substring(
+                        file.lastIndexOf(
+                                System.getProperty("file.separator")),
+                        file.lastIndexOf("."));
+                String tempStructureFileStr = createNamedJvTempFile(
+                        urlLeafName, structExt);
+                UrlDownloadClient.download(file, tempStructureFileStr);
+                al = fa.readFile(tempStructureFileStr, DataSourceType.FILE,
+                        format);
+                source = fa.getAlignFile();
+              }
+              else
+              {
+                al = fa.readFile(file, protocol, format);
+                source = fa.getAlignFile(); // keep reference for later if
+                                            // necessary.
+              }
             }
           }
         } catch (java.io.IOException ex)