JAL-629 add AF examples for FER1_SPIOL. Link and display PAE matrix annotation
[jalview.git] / src / jalview / ws / dbsources / EBIAlfaFold.java
index e5f1243..672f0ac 100644 (file)
@@ -30,6 +30,8 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
 import org.json.simple.parser.ParseException;
 
 import com.stevesoft.pat.Regex;
@@ -290,14 +292,8 @@ public class EBIAlfaFold extends EbiFileRetrievedProxy
       if (ssm != null)
       {
         String structFile = isStructId ? ssm.findFileForPDBId(id) : id;
-        Console.debug("##### AHA! structFile = " + structFile);
-        Console.debug("##### structFile "
-                + (ssm.isPDBFileRegistered(structFile) ? "IS " : "is NOT ")
-                + "registered.");
 
         StructureMapping[] smArray = ssm.getMapping(structFile);
-        Console.debug("##### AHA! smArray obtained with " + smArray.length
-                + " elements");
 
         try
         {
@@ -362,34 +358,25 @@ public class EBIAlfaFold extends EbiFileRetrievedProxy
   }
 
   public static boolean importPaeJSONAsContactMatrixToSequence(
+          AlignmentI pdbAlignment, File paeFile, int index, String seqId)
+          throws FileNotFoundException, IOException, ParseException
+  {
+    return importPaeJSONAsContactMatrixToSequence(pdbAlignment,
+            new FileInputStream(paeFile), index, seqId);
+  }
+
+  public static boolean importPaeJSONAsContactMatrixToSequence(
           AlignmentI pdbAlignment, InputStream pae_input, int index,
           String seqId) throws IOException, ParseException
   {
     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)
       {
@@ -403,43 +390,67 @@ public class EBIAlfaFold extends EbiFileRetrievedProxy
       }
     }
 
-    List<Object> pae_obj = (List<Object>) Platform.parseJSON(pae_input);
-    if (pae_obj == null)
+    JSONObject paeDict = parseJSONtoPAEContactMatrix(pae_input);
+    if (paeDict == null)
     {
       Console.debug("JSON file did not parse properly.");
       return false;
     }
     ContactMatrixI matrix = new PAEContactMatrix(sequence,
-            (Map<String, Object>) pae_obj.get(0));
+            (Map<String, Object>) paeDict);
 
     AlignmentAnnotation cmannot = sequence.addContactList(matrix);
     pdbAlignment.addAnnotation(cmannot);
+
     return true;
   }
 
+  public static JSONObject parseJSONtoPAEContactMatrix(
+          InputStream pae_input) throws IOException, ParseException
+  {
+    Object paeJson = Platform.parseJSON(pae_input);
+    JSONObject paeDict = null;
+    if (paeJson instanceof JSONObject)
+    {
+      paeDict = (JSONObject) paeJson;
+    }
+    else if (paeJson instanceof JSONArray)
+    {
+      JSONArray jsonArray = (JSONArray) paeJson;
+      if (jsonArray.size() > 0)
+        paeDict = (JSONObject) jsonArray.get(0);
+    }
+
+    return paeDict;
+  }
+
   public static boolean importPaeJSONAsContactMatrixToStructure(
           StructureMapping[] smArray, InputStream paeInput)
           throws IOException, ParseException
   {
     boolean someDone = false;
-    Console.debug("##### smArray.length=" + smArray.length);
     for (StructureMapping sm : smArray)
     {
-      Console.debug("##### sm[n]=" + sm.getPdbId());
       boolean thisDone = importPaeJSONAsContactMatrixToStructure(sm,
               paeInput);
-      Console.debug("##### thisDone = " + thisDone);
       someDone |= thisDone;
     }
     return someDone;
   }
 
   public static boolean importPaeJSONAsContactMatrixToStructure(
+          StructureMapping sm, File paeFile)
+          throws FileNotFoundException, IOException, ParseException
+  {
+    return importPaeJSONAsContactMatrixToStructure(sm,
+            new FileInputStream(paeFile));
+  }
+
+  public static boolean importPaeJSONAsContactMatrixToStructure(
           StructureMapping sm, InputStream paeInput)
           throws IOException, ParseException
   {
-
-    List<Object> pae_obj = (List<Object>) Platform.parseJSON(paeInput);
+    JSONObject pae_obj = parseJSONtoPAEContactMatrix(paeInput);
     if (pae_obj == null)
     {
       Console.debug("JSON file did not parse properly.");
@@ -447,7 +458,7 @@ public class EBIAlfaFold extends EbiFileRetrievedProxy
     }
 
     ContactMatrixI matrix = new PAEContactMatrix(sm.getSequence(),
-            (Map<String, Object>) pae_obj.get(0));
+            (Map<String, Object>) pae_obj);
 
     AlignmentAnnotation cmannot = sm.getSequence().addContactList(matrix);
     // sm.getSequence().addAlignmentAnnotation(cmannot);