JAL-1479 added support for SIFTs mapping output viewing and implemented exception...
[jalview.git] / src / jalview / ws / sifts / SiftsClient.java
index bde215d..176c511 100644 (file)
@@ -18,7 +18,7 @@
  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
-package jalview.io;
+package jalview.ws.sifts;
 
 import jalview.analysis.AlignSeq;
 import jalview.api.DBRefEntryI;
@@ -34,7 +34,6 @@ import jalview.xml.binding.sifts.Entry.Entity.Segment;
 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListMapRegion.MapRegion;
 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue;
 import jalview.xml.binding.sifts.Entry.Entity.Segment.ListResidue.Residue.CrossRefDb;
-import jalview.xml.binding.sifts.Entry.EntryDetail;
 import jalview.xml.binding.sifts.Entry.ListDB.Db;
 
 import java.io.File;
@@ -46,8 +45,9 @@ import java.io.InputStream;
 import java.io.PrintStream;
 import java.net.URL;
 import java.net.URLConnection;
-import java.util.HashMap;
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.zip.GZIPInputStream;
 
@@ -65,6 +65,10 @@ public class SiftsClient implements SiftsClientI
 
   private String pdbId;
 
+  private String structId;
+
+  private String segStartEnd;
+
   private static final int BUFFER_SIZE = 4096;
 
   private static final String SIFTS_FTP_BASE_URL = "ftp://ftp.ebi.ac.uk/pub/databases/msd/sifts/xml/";
@@ -248,7 +252,8 @@ public class SiftsClient implements SiftsClientI
    * @throws Exception
    *           if no valid source DBRefEntry was found for the given sequences
    */
-  public DBRefEntryI getValidSourceDBRef(SequenceI seq) throws Exception
+  public DBRefEntryI getValidSourceDBRef(SequenceI seq)
+          throws SiftsException
   {
     DBRefEntryI sourceDBRef = null;
     sourceDBRef = seq.getSourceDBRef();
@@ -269,7 +274,7 @@ public class SiftsClient implements SiftsClientI
 
       if (dbRefs == null || dbRefs.length < 1)
       {
-        throw new Exception("Could not get source DB Ref");
+        throw new SiftsException("Could not get source DB Ref");
       }
 
       for (DBRefEntryI dbRef : dbRefs)
@@ -291,7 +296,7 @@ public class SiftsClient implements SiftsClientI
     {
       return sourceDBRef;
     }
-    throw new Exception("Could not get source DB Ref");
+    throw new SiftsException("Could not get source DB Ref");
   }
 
 
@@ -331,44 +336,80 @@ public class SiftsClient implements SiftsClientI
     return accessions;
   }
 
+  @Override
+  public StructureMapping getSiftsStructureMapping(SequenceI seq,
+          String pdbFile, String chain) throws SiftsException
+  {
+    structId = (chain == null) ? pdbId : pdbId + "|" + chain;
+    System.out.println("Getting mapping for: " + pdbId + "|" + chain
+            + " : seq- " + seq.getName());
+
+    final StringBuilder mappingDetails = new StringBuilder(128);
+    PrintStream ps = new PrintStream(System.out)
+    {
+      @Override
+      public void print(String x)
+      {
+        mappingDetails.append(x);
+      }
+
+      @Override
+      public void println()
+      {
+        mappingDetails.append(NEWLINE);
+      }
+    };
+    int[][] mapping = getGreedyMapping(chain, seq, ps);
+
+    String mappingOutput = mappingDetails.toString();
+    return new StructureMapping(seq, pdbFile, pdbId, chain, mapping,
+            mappingOutput);
+  }
 
   @Override
   public int[][] getGreedyMapping(String entityId, SequenceI seq,
           java.io.PrintStream os)
-          throws Exception
+ throws SiftsException
   {
+    int matchedResStart = -1;
+    int matchedResEnd = -1;
+    int counter = 0;
+    int pdbStart = -1;
+    int pdbEnd = -1;
+    int sStart = -1;
+    int sEnd = -1;
+    boolean startDetected = false;
+
     System.out.println("Generating mappings for : " + entityId);
     Entity entity = null;
     entity = getEntityById(entityId);
     String seqStr = AlignSeq.extractGaps(jalview.util.Comparison.GapChars,
             seq.getSequenceAsString());
-    // StringBuilder mappedStrucSeq = new StringBuilder(seqStr.length());
-    String[] mappedStrucSeq = new String[seqStr.length()];
-    int mapping[][] = new int[seqStr.length()][2];
+    int mapping[][] = new int[seqStr.length() + seq.getStart()][2];
     DBRefEntryI sourceDBRef = seq.getSourceDBRef();
     if (sourceDBRef == null)
     {
       sourceDBRef = getValidSourceDBRef(seq);
-      // TODO if sourceDBRef is null at this point then throw an Exception
-
       // TODO update sequence start/end with sourceDBRef start/end
       // seq.setStart(sourceDBRef.getStartRes());
       // seq.setEnd(sourceDBRef.getEndRes());
     }
 
     String crossRefAccessionId = sourceDBRef.getAccessionId();
-    int start = seq.getStart() - 1;
+    int count = 0;
     for (int residue[] : mapping)
     {
-      residue[1] = start++;
+      residue[1] = count++;
+      residue[0] = -1;
     }
     
-    HashMap<Integer, String> resNumMap = new HashMap<Integer, String>();
+    LinkedHashMap<Integer, String> resNumMap = new LinkedHashMap<Integer, String>();
     List<Segment> segments = entity.getSegment();
     for (Segment segment : segments)
     {
+      segStartEnd = segment.getStart() + " - " + segment.getEnd();
       System.out.println("Mappging segments : " + segment.getSegId() + "\\"
-              + segment.getStart() + "-" + segment.getEnd());
+              + segStartEnd);
       List<Residue> residues = segment.getListResidue().getResidue();
       for (Residue residue : residues)
       {
@@ -386,42 +427,84 @@ public class SiftsClient implements SiftsClientI
         {
           continue;
         }
+        int loopCount = 0;
         for (int[] x : mapping)
         {
-          if (x[1] == refDbResNum)
+          if (loopCount > seq.getStart() && x[1] == refDbResNum)
           {
             int resNum = Integer.valueOf(residue.getDbResNum());
             x[0] = resNum;
-            String value = "x";
-            resNumMap.put(resNum, value);
+            char resCharCode = ResidueProperties
+                    .getSingleCharacterCode(residue.getDbResName());
+            resNumMap.put(resNum, String.valueOf(resCharCode));
           }
+          ++loopCount;
         }
       }
     }
-    
-    //Generate visual mapping output
-    // StringBuilder strucSeq = new StringBuilder();
-    // for(int[] x : mapping){
-      // if(mapping[0] == 0){
-      // strucSeq.append(b)
-      // }
-    // }
-    mappedStrucSeq[1] = "x";
+
+    for (int[] x : mapping)
+    {
+      if (!startDetected && x[0] > -1)
+      {
+        matchedResStart = counter;
+        // System.out.println(matchedResStart);
+        startDetected = true;
+      }
+
+      if (startDetected && x[0] == -1)
+      {
+        matchedResEnd = counter;
+      }
+      ++counter;
+    }
+
+    String matchedSeqStr = seqStr;
+    if (matchedResStart != -1)
+    {
+      matchedResEnd = (matchedResEnd == -1) ? counter : matchedResEnd;
+      pdbStart = mapping[matchedResStart][0];
+      pdbEnd = mapping[matchedResEnd - 1][0];
+      sStart = mapping[matchedResStart][1];
+      sEnd = mapping[matchedResEnd - 1][1];
+      int seqStart = seq.getStart();
+      if (seqStart > 1)
+      {
+        matchedResStart = matchedResStart - seqStart;
+        matchedResEnd = matchedResEnd - seqStart;
+      }
+      else
+      {
+        --matchedResStart;
+        --matchedResEnd;
+      }
+      matchedSeqStr = seqStr.substring(matchedResStart, matchedResEnd);
+    }
+
+    StringBuilder targetStrucSeqs = new StringBuilder();
+    for (String res : resNumMap.values())
+    {
+      targetStrucSeqs.append(res);
+    }
+
     try
     {
-      System.out.println(">>>> seq: " + seqStr + "\nlength "
-              + seqStr.length());
-      System.out.println(">>>> pdb: " + mappedStrucSeq.toString()
-              + "\nlength " + mappedStrucSeq.toString().length());
-
-      String printedMapping = getMappingOutput(mappedStrucSeq.toString(),
-              seqStr, "seqAccession", "strucAccession", "pep", 3)
-              .toString();
       if (os != null)
       {
-        os.print(printedMapping);
+        MappingOutputPojo mop = new MappingOutputPojo();
+        mop.setSeqStart(sStart);
+        mop.setSeqEnd(sEnd);
+        mop.setSeqName(seq.getName());
+        mop.setSeqResidue(matchedSeqStr);
+
+        mop.setStrStart(pdbStart);
+        mop.setStrEnd(pdbEnd);
+        mop.setStrName(structId);
+        mop.setStrResidue(targetStrucSeqs.toString());
+
+        mop.setType("pep");
+        os.print(getMappingOutput(mop).toString());
       }
-      System.out.println();
     } catch (Exception ex)
     {
       ex.printStackTrace();
@@ -436,44 +519,9 @@ public class SiftsClient implements SiftsClientI
             && getAllMappingAccession().contains(accessionId);
   }
 
-  @Override
-  public StructureMapping getSiftsStructureMapping(SequenceI seq,
-          String pdbFile, String chain)
-  {
-    System.out.println("Getting mapping for: " + pdbId + "|" + chain
-            + " : seq- " + seq.getName());
-
-    final StringBuilder mappingDetails = new StringBuilder(128);
-    PrintStream ps = new PrintStream(System.out)
-    {
-      @Override
-      public void print(String x)
-      {
-        mappingDetails.append(x);
-      }
-
-      @Override
-      public void println()
-      {
-        mappingDetails.append(NEWLINE);
-      }
-    };
-    int[][] mapping = null;
-    try
-    {
-      mapping = getGreedyMapping(chain, seq, ps);
-    } catch (Exception e)
-    {
-      e.printStackTrace();
-    }
-    // String mappingOutput = mappingDetails.toString();
-    String mappingOutput = null;
-    return new StructureMapping(seq, pdbFile, pdbId, chain, mapping,
-            mappingOutput);
-  }
 
   @Override
-  public Entity getEntityById(String id) throws Exception
+  public Entity getEntityById(String id) throws SiftsException
   {
     List<Entity> entities = siftsEntry.getEntity();
     for (Entity entity : entities)
@@ -484,52 +532,77 @@ public class SiftsClient implements SiftsClientI
       }
       return entity;
     }
-    throw new Exception("Entity " + id + " not found");
+    throw new SiftsException("Entity " + id + " not found");
   }
 
   @Override
   public String[] getEntryDBs()
   {
     System.out.println("\nListing DB entries...");
+    List<String> availDbs = new ArrayList<String>();
     List<Db> dbs = siftsEntry.getListDB().getDb();
     for (Db db : dbs)
     {
+      availDbs.add(db.getDbSource());
       System.out.println(db.getDbSource() + " | " + db.getDbCoordSys());
     }
-    return null;
-  }
-
-  @Override
-  public void getEntryDetails()
-  {
-    List<EntryDetail> eds = siftsEntry.getEntryDetail();
-    for (EntryDetail ed : eds)
-    {
-      System.out.println("Entry Details: " + ed.getContent() + " "
-              + ed.getDbSource() + " " + ed.getProperty() + " "
-              + ed.toString());
-    }
+    return availDbs.toArray(new String[0]);
   }
 
   @Override
-  public StringBuffer getMappingOutput(String astr1, String astr2, String s1id,
-          String s2id, String type, int nochunks)
+  public StringBuffer getMappingOutput(MappingOutputPojo mp)
   {
-    int maxid = s1id.length();
+    String seqRes = mp.getSeqResidue();
+    String seqName = mp.getSeqName();
+    int sStart = mp.getSeqStart();
+    int sEnd = mp.getSeqEnd();
+
+    String strRes = mp.getStrResidue();
+    String strName = mp.getStrName();
+    int pdbStart = mp.getStrStart();
+    int pdbEnd = mp.getStrEnd();
+    
+    String type = mp.getType();
+    
+    int maxid = (seqName.length() >= strName.length()) ? seqName.length()
+            : strName.length();
     int len = 72 - maxid - 1;
-    StringBuffer output = new StringBuffer();
+
+    // int nochunks = 2;// mp.getWrapHeight();
+    int nochunks = ((seqRes.length()) / len)
+            + ((seqRes.length()) % len > 0 ? 1 : 0);
     // output mappings
+    StringBuffer output = new StringBuffer();
+    output.append(NEWLINE);
+    output.append("Sequence ⟷ Structure mapping details:");
+    output.append(NEWLINE).append(NEWLINE);
+
+    output.append(new Format("%" + maxid + "s").form(seqName));
+    output.append(" :  ");
+    output.append(String.valueOf(sStart));
+    output.append(" - ");
+    output.append(String.valueOf(sEnd));
+    output.append(" Maps to ");
+    output.append(NEWLINE);
+    output.append(new Format("%" + maxid + "s").form(structId));
+    output.append(" :  ");
+    output.append(String.valueOf(pdbStart));
+    output.append(" - ");
+    output.append(String.valueOf(pdbEnd));
+    output.append(NEWLINE).append(NEWLINE);
+    
     float pid = 0;
     for (int j = 0; j < nochunks; j++)
     {
       // Print the first aligned sequence
-      output.append(new Format("%" + (maxid) + "s").form(s1id)).append(" ");
+      output.append(new Format("%" + (maxid) + "s").form(seqName)).append(
+              " ");
 
       for (int i = 0; i < len; i++)
       {
-        if ((i + (j * len)) < astr1.length())
+        if ((i + (j * len)) < seqRes.length())
         {
-          output.append(astr1.charAt(i + (j * len)));
+          output.append(seqRes.charAt(i + (j * len)));
         }
       }
 
@@ -539,10 +612,10 @@ public class SiftsClient implements SiftsClientI
       // Print out the matching chars
       for (int i = 0; i < len; i++)
       {
-        if ((i + (j * len)) < astr1.length())
+        if ((i + (j * len)) < seqRes.length())
         {
-          if (astr1.charAt(i + (j * len)) == astr2.charAt(i + (j * len))
-                  && !jalview.util.Comparison.isGap(astr1.charAt(i
+          if (seqRes.charAt(i + (j * len)) == strRes.charAt(i + (j * len))
+                  && !jalview.util.Comparison.isGap(seqRes.charAt(i
                           + (j * len))))
           {
             pid++;
@@ -550,8 +623,8 @@ public class SiftsClient implements SiftsClientI
           }
           else if (type.equals("pep"))
           {
-            if (ResidueProperties.getPAM250(astr1.charAt(i + (j * len)),
-                    astr2.charAt(i + (j * len))) > 0)
+            if (ResidueProperties.getPAM250(seqRes.charAt(i + (j * len)),
+                    strRes.charAt(i + (j * len))) > 0)
             {
               output.append(".");
             }
@@ -568,21 +641,23 @@ public class SiftsClient implements SiftsClientI
       }
       // Now print the second aligned sequence
       output = output.append(NEWLINE);
-      output = output.append(new Format("%" + (maxid) + "s").form(s2id))
+      output = output.append(new Format("%" + (maxid) + "s").form(strName))
               .append(" ");
       for (int i = 0; i < len; i++)
       {
-        if ((i + (j * len)) < astr2.length())
+        if ((i + (j * len)) < strRes.length())
         {
-          output.append(astr2.charAt(i + (j * len)));
+          output.append(strRes.charAt(i + (j * len)));
         }
       }
       output.append(NEWLINE).append(NEWLINE);
     }
-    pid = pid / (astr1.length()) * 100;
-    System.out.println(output);
-    System.out.println(pid);
-    // TODO return output & pid
+    pid = pid / (seqRes.length()) * 100;
+    output.append("Length of alignment = " + seqRes.length())
+            .append(NEWLINE);
+    output.append(new Format("Percentage ID = %2.2f").form(pid));
+    output.append(NEWLINE);
+    output.append("Mapping method: SIFTS").append(NEWLINE);
     return output;
   }