JAL-629 refactoring TFType. Remove i18n identification of annotation.
[jalview.git] / src / jalview / ws / dbsources / EBIAlfaFold.java
index ba78199..859ac05 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;
@@ -39,11 +43,13 @@ import jalview.datamodel.DBRefEntry;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.SequenceFeature;
 import jalview.datamodel.SequenceI;
+import jalview.gui.Desktop;
 import jalview.io.DataSourceType;
 import jalview.io.FileFormat;
 import jalview.io.FileFormatI;
 import jalview.io.FormatAdapter;
 import jalview.io.PDBFeatureSettings;
+import jalview.structure.StructureSelectionManager;
 import jalview.util.MessageManager;
 import jalview.util.Platform;
 import jalview.ws.datamodel.alphafold.PAEContactMatrix;
@@ -61,7 +67,7 @@ public class EBIAlfaFold extends EbiFileRetrievedProxy
 
   private static final int PDB_ID_LENGTH = 4;
 
-  private static String AF_VERSION = "2";
+  private static String AF_VERSION = "3";
 
   public EBIAlfaFold()
   {
@@ -217,14 +223,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 +239,66 @@ 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);
+    addAlphaFoldPAEToSequence(pdbAlignment, pae, 0, null);
+  }
 
+  public static void addAlphaFoldPAEToSequence(AlignmentI pdbAlignment,
+          File pae, int index, String seqId)
+  {
+    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);
+    }
 
-      if (!importPaeJSONAsContactMatrix(pdbAlignment, pae_input))
+    try
+    {
+      if (!importPaeJSONAsContactMatrix(pdbAlignment, pae_input, index,
+              seqId))
       {
-        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(
+              "Error when parsing pAE file '" + pae.getAbsolutePath() + "'",
+              e2);
+    }
+
+  }
+
+  public static void addAlphaFoldPAEToStructure(AlignmentI pdbAlignment,
+          File pae, int index, String structId)
+  {
+    StructureSelectionManager ssm = StructureSelectionManager
+            .getStructureSelectionManager(Desktop.instance);
+    if (ssm != null)
     {
-      Console.error("Couldn't download PAE", pae_ex);
+      /*
+      ssm.setAddTempFacAnnot(showTemperatureFactor);
+      ssm.setProcessSecondaryStructure(showSecondaryStructure);
+      */
     }
 
   }
@@ -260,23 +310,69 @@ 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
+  {
+    return importPaeJSONAsContactMatrix(pdbAlignment, pae_input, 0, null);
+  }
+
+  public static boolean importPaeJSONAsContactMatrix(
+          AlignmentI pdbAlignment, InputStream pae_input, int index,
+          String seqId) throws IOException, ParseException
   {
 
     List<Object> pae_obj = (List<Object>) Platform.parseJSON(pae_input);
     if (pae_obj == null)
     {
+      Console.debug("JSON file did not parse properly.");
       return false;
     }
-    ContactMatrixI matrix = new PAEContactMatrix(
-            pdbAlignment.getSequenceAt(0),
+    SequenceI sequence = null;
+    /* debugging */
+    SequenceI[] seqs = pdbAlignment.getSequencesArray();
+    if (seqs == null)
+      Console.debug("******* sequences is null");
+    else
+    {
+      for (int i = 0; i < seqs.length; i++)
+      {
+        SequenceI s = seqs[i];
+        Console.debug("******* sequences[" + i + "]='" + s.getName() + "'");
+      }
+    }
+    /* end debug */
+    if (seqId == null)
+    {
+      int seqToGet = index > 0 ? index : 0;
+      sequence = pdbAlignment.getSequenceAt(seqToGet);
+      Console.debug("***** Got sequence at index " + seqToGet + ": "
+              + (sequence == null ? null : sequence.getName()));
+    }
+    if (sequence == null)
+    {
+      Console.debug("***** Looking for sequence with id '" + seqId + "'");
+
+      SequenceI[] sequences = pdbAlignment.findSequenceMatch(seqId);
+      if (sequences == null || sequences.length < 1)
+      {
+        Console.warn("Could not find sequence with id '" + seqId
+                + "' to attach pAE matrix to. Ignoring matrix.");
+        return false;
+      }
+      else
+      {
+        sequence = sequences[0]; // just use the first sequence with this seqId
+      }
+    }
+    ContactMatrixI matrix = new PAEContactMatrix(sequence,
             (Map<String, Object>) pae_obj.get(0));
 
-    AlignmentAnnotation cmannot = pdbAlignment.getSequenceAt(0)
-            .addContactList(matrix);
+    AlignmentAnnotation cmannot = sequence.addContactList(matrix);
     pdbAlignment.addAnnotation(cmannot);
     return true;
   }