JAL-2136 JAL-2137 Modified STRUCTMODEL annotation format and added implementation...
[jalview.git] / src / jalview / io / AnnotationFile.java
index d40ee1b..c1ddeed 100755 (executable)
@@ -32,6 +32,7 @@ import jalview.datamodel.PDBEntry;
 import jalview.datamodel.PDBEntry.Type;
 import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
+import jalview.gui.Desktop;
 import jalview.schemes.ColourSchemeI;
 import jalview.schemes.ColourSchemeProperty;
 import jalview.schemes.UserColourScheme;
@@ -40,7 +41,6 @@ import jalview.structure.StructureSelectionManager;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileReader;
-import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.StringReader;
 import java.net.URL;
@@ -1017,8 +1017,8 @@ public class AnnotationFile
         {
           boolean failedtoadd = true;
           // expect
-          // STRUCTMODEL <QUERYID> <TemplateID> <URL to model> <URL to
-          // alignment>
+          // STRUCTMODEL <TemplateSeqId> <TemplateFileName> <Confidence> <%.I.D>
+          // <MatchStart> <MatchEnd> <Coverage> [<Other Information>]
           if (st.hasMoreTokens()) {
             refSeq = al.findName(refSeqId = st.nextToken());
             if (refSeq == null)
@@ -1030,11 +1030,13 @@ public class AnnotationFile
             else
             {
               String tempId = st.nextToken();
-              String urlToModel = st.nextToken();
-              String urlToPairwise = st.hasMoreTokens() ? st.nextToken()
-                      : "";
-              if (add_structmodel(al, refSeq, tempId, urlToModel,
-                      urlToPairwise))
+              String confidence = 100 * Double.valueOf(st.nextToken()) + "";
+              String pid = st.nextToken();
+              String alignRange = st.nextToken() + "-" + st.nextToken();
+              String otherInfo = st.hasMoreTokens() ? st.nextToken() : "";
+              String coverage = "";
+              if (add_structmodel(al, refSeq, tempId, alignRange, coverage,
+                      confidence, pid, otherInfo))
               {
                 failedtoadd = false;
               }
@@ -1043,8 +1045,9 @@ public class AnnotationFile
           if (failedtoadd)
           {
             System.err
-                    .println("Need <QueryId> <TemplateId> <URL to Model> [<URL to pairwise alignment>] as tab separated fields after "
-                            + STRUCTMODEL);
+                    .println("Need <TemplateSeqId> <TemplateFileName> <Confidence> <%.I.D> <MatchStart> <MatchEnd> <Coverage> [<Other Information>] as tab separated fields after"
+                            + STRUCTMODEL
+                            + ".\nNote: other information could be provided in html format ");
           } else {
             modified = true;
           }
@@ -1259,77 +1262,26 @@ public class AnnotationFile
    * @param urlToPairwise
    * @return true if model and sequence was added
    */
-  private boolean add_structmodel(AlignmentI al, SequenceI refSeq2, String tempId,
-          String urlToModel, String urlToPairwise)
+  private boolean add_structmodel(AlignmentI al, SequenceI refSeq,
+          String tempId, String aRange, String coverage, String confidence,
+          String pid, String otherInfo)
   {
-    String warningMessage = null, modelPath = null, aliPath = null;
-    DataSourceType modelProt = null, aliProt = null;
+    String warningMessage = null;
     boolean added = false;
     try {
-      // locate tempId. if it exists, will need to merge, otherwise:
-      SequenceI templateSeq = al.findName(tempId);
-      // 1. load urlToModel
-      modelPath = resolveAbsolute(urlToModel);
-      modelProt = AppletFormatAdapter.checkProtocol(modelPath);
-      // need to transfer to local temp file ?
-      PDBEntry modelpe = new PDBEntry(tempId, null, Type.FILE, modelPath);
-      PDBEntry templpe = new PDBEntry(tempId, null, Type.FILE, modelPath);
-      refSeq2.addPDBId(modelpe);
-      aliPath = resolveAbsolute(urlToPairwise);
-      aliProt = AppletFormatAdapter.checkProtocol(aliPath);
-      // 2. load urlToPairwise
-      AlignmentI pwa = new AppletFormatAdapter().readFile(aliPath, aliProt,
-              FileFormat.Fasta);
-      SequenceI qPw = null, tPw = null;
-      if (pwa != null)
-      {
-        // resolve query/template sequences in provided alignment
-        qPw = pwa.findName(refSeqId);
-        tPw = pwa.findName(tempId);
-      }
-      if (false)
-      // (qPw != null && tPw != null)
-      {
-        // not yet complete
-        // refalQ vvva--addrvvvtttddd
-        // refalT ---aaaa---sss---ddd
-        // profalQ ---v-v-v-a.-.-a---dd--r--vvvtt--td--dd
-        // profalT ---.-.-.-aa-a-a---..--.--sss..--.d--dd
-        // Pragmatic solution here:
-        // Map templpe onto refalT only where refalT and refalQ are both
-        // non-gaps
-
-        // columns for start..end in refSeq2
-        int[] gapMap = refSeq2.gapMap();
-        // insert gaps in tPw
-        int curi = 0, width = refSeq2.getLength();
-        // TBC
-      }
-      else
-      {
-        // assume 1:1 - so synthesise sequences to use to construct mapping
-        StructureFile pdbf = StructureSelectionManager
-                .getStructureSelectionManager().setMapping(false,
-                        new SequenceI[] { refSeq2.getDatasetSequence() },
-                        null, modelPath, modelProt);
-        refSeq2.getDatasetSequence().addPDBId(modelpe);
-        if (templateSeq == null && tPw != null)
-        {
-          tPw.createDatasetSequence();
-          tPw.getDatasetSequence().addPDBId(templpe); // needs to set mapping based on model yet...
-          al.addSequence(tPw);
-          added = true;
-        }
-      }
-    // 3. pad/insert gaps in urlToPairwise according to gaps already present in
-    // refSeq2
-    // 4. add padded tempId sequence to alignment
-    // 4. associate urlToModel with refSeq2 based on position map provided by
-    // urlToPairwise
-    // 5. associate urlToModel with tempId based on position map provided by
-    // urlToPairwise
-    // start a thread to load urlToModel and process/annotate sequences.
-    } catch (IOException x)
+      String pdbFile = baseUri + tempId;
+      PDBEntry phyre2PDBEntry = new PDBEntry(tempId, null, Type.FILE,
+              pdbFile);
+      refSeq.getDatasetSequence().addPDBId(phyre2PDBEntry);
+      String phyre2ModelDesc = generatePhyre2InfoHTMLTable(aRange,
+              coverage, confidence, pid, otherInfo);
+      refSeq.setDescription(phyre2ModelDesc);
+      StructureSelectionManager ssm = StructureSelectionManager
+              .getStructureSelectionManager(Desktop.instance);
+      ssm.registerPhyre2Template(pdbFile);
+      added = true;
+
+    } catch (Exception x)
     {
       warningMessage = x.toString();
     } finally {
@@ -1337,8 +1289,44 @@ public class AnnotationFile
       {
         System.err.println("Warnings whilst processing STRUCTMODEL: "+warningMessage);
       }
-      return added;
     }
+    return added;
+  }
+
+  private String generatePhyre2InfoHTMLTable(String aRange,
+          String coverage, String confidence, String pid, String otherInfo)
+  {
+    StringBuilder phyre2InfoBuilder = new StringBuilder();
+    phyre2InfoBuilder.append("<html><table border=\"1\" width=100%>");
+    phyre2InfoBuilder
+            .append("<tr><td colspan=\"2\"><strong>Phyre2 Template Info</strong></td></tr>");
+    if (aRange != null && !aRange.isEmpty())
+    {
+      phyre2InfoBuilder.append("<tr><td>").append("Aligned range")
+              .append("</td><td>").append(aRange).append("</td></tr>");
+    }
+    if (coverage != null && !coverage.isEmpty())
+    {
+      phyre2InfoBuilder.append("<tr><td>").append("Coverage")
+              .append("</td><td>").append(coverage).append("</td></tr>");
+    }
+    if (confidence != null && !confidence.isEmpty())
+    {
+      phyre2InfoBuilder.append("<tr><td>").append("Confidence")
+              .append("</td><td>").append(confidence).append("</td></tr>");
+    }
+    if (pid != null && !pid.isEmpty())
+    {
+      phyre2InfoBuilder.append("<tr><td>").append("%.i.d")
+              .append("</td><td>").append(pid).append("</td></tr>");
+    }
+    if (otherInfo != null && !otherInfo.isEmpty())
+    {
+      phyre2InfoBuilder.append("<tr><td>").append("Other information")
+              .append("</td><td>").append(otherInfo).append("</td></tr>");
+    }
+    phyre2InfoBuilder.append("</table></html>");
+    return phyre2InfoBuilder.toString();
   }
 
   private String resolveAbsolute(String relURI)