JAL-629 implementation of --tempfac options
[jalview.git] / src / jalview / ws / dbsources / EBIAlfaFold.java
index f98ae6d..5314207 100644 (file)
@@ -23,11 +23,15 @@ package jalview.ws.dbsources;
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+import org.json.simple.parser.ParseException;
+
 import com.stevesoft.pat.Regex;
 
 import jalview.api.FeatureSettingsModelI;
@@ -217,14 +221,14 @@ public class EBIAlfaFold extends EbiFileRetrievedProxy
    * @param retrievalUrl
    *          - URL of .mmcif from EBI-AlphaFold - will be used to generate the
    *          pAE URL automatically
+   * @throws IOException
    * @throws Exception
    */
   public static void retrieve_AlphaFold_pAE(String id,
-          AlignmentI pdbAlignment, String retrievalUrl) throws Exception
+          AlignmentI pdbAlignment, String retrievalUrl) throws IOException
   {
     // import PAE as contact matrix - assume this will work if there was a
     // model
-    File pae = File.createTempFile(id, "pae_json");
     String paeURL = getAlphaFoldPaeDownloadUrl(id, AF_VERSION);
 
     if (retrievalUrl != null)
@@ -233,22 +237,49 @@ public class EBIAlfaFold extends EbiFileRetrievedProxy
       paeURL = retrievalUrl.replace("model", "predicted_aligned_error")
               .replace(".cif", ".json");
     }
+
+    File pae = null;
+    try
+    {
+      pae = File.createTempFile(id == null ? "af_pae" : id, "pae_json");
+    } catch (IOException e)
+    {
+      e.printStackTrace();
+    }
     Console.debug("Downloading pae from " + paeURL + " to " + pae.toString()
             + "");
+    UrlDownloadClient.download(paeURL, pae);
+    addAlphaFoldPAE(pdbAlignment, pae);
+  }
 
+  public static void addAlphaFoldPAE(AlignmentI pdbAlignment, File pae)
+  {
+    FileInputStream pae_input = null;
     try
     {
-      UrlDownloadClient.download(paeURL, pae);
-      FileInputStream pae_input = new FileInputStream(pae);
+      pae_input = new FileInputStream(pae);
+    } catch (FileNotFoundException e)
+    {
+      Console.error(
+              "Could not find pAE file '" + pae.getAbsolutePath() + "'", e);
+    }
 
+    try
+    {
       if (!importPaeJSONAsContactMatrix(pdbAlignment, pae_input))
       {
-        Console.warn("Couln't import contact matrix from " + paeURL
-                + " (stored in " + pae.toString() + ")");
+        Console.warn("Could not import contact matrix from '"
+                + pae.getAbsolutePath() + "'");
       }
-    } catch (Exception pae_ex)
+    } catch (IOException e1)
+    {
+      Console.error("Error when importing pAE file '"
+              + pae.getAbsolutePath() + "'", e1);
+    } catch (ParseException e2)
     {
-      Console.error("Couldn't download PAE", pae_ex);
+      Console.error(
+              "Error when parsing pAE file '" + pae.getAbsolutePath() + "'",
+              e2);
     }
 
   }
@@ -260,10 +291,13 @@ public class EBIAlfaFold extends EbiFileRetrievedProxy
    * @param pdbAlignment
    * @param pae_input
    * @return true if there was a pAE matrix added
+   * @throws ParseException
+   * @throws IOException
    * @throws Exception
    */
   public static boolean importPaeJSONAsContactMatrix(
-          AlignmentI pdbAlignment, InputStream pae_input) throws Exception
+          AlignmentI pdbAlignment, InputStream pae_input)
+          throws IOException, ParseException
   {
 
     List<Object> pae_obj = (List<Object>) Platform.parseJSON(pae_input);