JAL-2694 add maximal set of chain mappings between a PDBEntry and a sequence
authorJim Procter <jprocter@issues.jalview.org>
Wed, 30 Aug 2017 16:48:35 +0000 (17:48 +0100)
committerJim Procter <jprocter@issues.jalview.org>
Wed, 30 Aug 2017 16:48:35 +0000 (17:48 +0100)
src/jalview/structure/StructureSelectionManager.java

index b973f45..83a055e 100644 (file)
@@ -463,6 +463,9 @@ public class StructureSelectionManager
       String maxChainId = " ";
       PDBChain maxChain = null;
       boolean first = true;
+      List<PDBChain> maximalChain = new ArrayList<>();
+      List<AlignSeq> maximalAlignseqs = new ArrayList<>();
+      List<String> maximalChainIds = new ArrayList<>();
       for (PDBChain chain : pdb.getChains())
       {
         if (targetChainId.length() > 0 && !targetChainId.equals(chain.id)
@@ -480,14 +483,39 @@ public class StructureSelectionManager
         // as.calcScoreMatrix();
         // as.traceAlignment();
 
-        if (first || as.maxscore > max
-                || (as.maxscore == max && chain.id.equals(targetChainId)))
+        if (targetChainId.length() > 0 && chain.id.equals(targetChainId))
         {
-          first = false;
+          // Don't care - just pick this chain as the mapping
           maxChain = chain;
           max = as.maxscore;
           maxAlignseq = as;
           maxChainId = chain.id;
+          // if targetChainId is specified then it is expected to be unique, so
+          // precisely one maximal chain will be added
+          maximalChainIds.add(chain.id);
+          maximalChain.add(chain);
+          maximalAlignseqs.add(as);
+        }
+        else
+        {
+          // select chains with maximal mappings to this sequence
+          if (first || as.maxscore > max)
+          {
+            // clear out old maximal mappings (if any)
+            max = as.maxscore;
+            maximalChain.clear();
+            maxChain = chain;
+            max = as.maxscore;
+            maxAlignseq = as;
+            maxChainId = chain.id;
+            first = false;
+          }
+          if (as.maxscore == max)
+          {
+            maximalChainIds.add(chain.id);
+            maximalChain.add(chain);
+            maximalAlignseqs.add(as);
+          }
         }
       }
       if (maxChain == null)
@@ -500,7 +528,7 @@ public class StructureSelectionManager
         pdbFile = "INLINE" + pdb.getId();
       }
 
-      List<StructureMapping> seqToStrucMapping = new ArrayList<StructureMapping>();
+      List<StructureMapping> seqToStrucMapping = new ArrayList<>();
       if (isMapUsingSIFTs && seq.isProtein())
       {
         setProgressBar(null);
@@ -527,7 +555,8 @@ public class StructureSelectionManager
             // fall back to NW alignment
             System.err.println(e.getMessage());
             StructureMapping nwMapping = getNWMappings(seq, pdbFile,
-                    targetChainId, maxChain, pdb, maxAlignseq);
+                    maximalChainIds, maximalChain, pdb, maximalAlignseqs)
+                            .get(0);
             seqToStrucMapping.add(nwMapping);
             maxChain.makeExactMapping(maxAlignseq, seq);
             maxChain.transferRESNUMFeatures(seq, null); // FIXME: is this
@@ -538,7 +567,7 @@ public class StructureSelectionManager
         }
         else
         {
-          List<StructureMapping> foundSiftsMappings = new ArrayList<StructureMapping>();
+          List<StructureMapping> foundSiftsMappings = new ArrayList<>();
           for (PDBChain chain : pdb.getChains())
           {
             try
@@ -564,7 +593,8 @@ public class StructureSelectionManager
           else
           {
             StructureMapping nwMapping = getNWMappings(seq, pdbFile,
-                    maxChainId, maxChain, pdb, maxAlignseq);
+                    maximalChainIds, maximalChain, pdb, maximalAlignseqs)
+                            .get(0);
             seqToStrucMapping.add(nwMapping);
             maxChain.transferRESNUMFeatures(seq, null); // FIXME: is this
                                                         // "IEA:Jalview" ?
@@ -578,10 +608,14 @@ public class StructureSelectionManager
         setProgressBar(null);
         setProgressBar(MessageManager
                 .getString("status.obtaining_mapping_with_nw_alignment"));
-        StructureMapping nwMapping = getNWMappings(seq, pdbFile, maxChainId,
-                maxChain, pdb, maxAlignseq);
-        seqToStrucMapping.add(nwMapping);
-        ds.addPDBId(maxChain.sequence.getAllPDBEntries().get(0));
+        List<StructureMapping> nwMapping = getNWMappings(seq, pdbFile,
+                maximalChainIds, maximalChain, pdb, maximalAlignseqs);
+        seqToStrucMapping.addAll(nwMapping);
+        for (PDBChain mc : maximalChain)
+        {
+          ds.addPDBId(mc.sequence.getAllPDBEntries().get(0));
+        }
+        ;
 
       }
 
@@ -633,10 +667,17 @@ public class StructureSelectionManager
     return curChainMapping;
   }
 
-  private StructureMapping getNWMappings(SequenceI seq, String pdbFile,
-          String maxChainId, PDBChain maxChain, StructureFile pdb,
-          AlignSeq maxAlignseq)
+  private List<StructureMapping> getNWMappings(SequenceI seq,
+          String pdbFile,
+          List<String> maximalChainId, List<PDBChain> maximalChain,
+          StructureFile pdb, List<AlignSeq> maximalAlignseq)
   {
+    List<StructureMapping> nwMappings = new ArrayList();
+    for (int ch = 0; ch < maximalChain.size(); ch++)
+    {
+      String maxChainId = maximalChainId.get(ch);
+      PDBChain maxChain = maximalChain.get(ch);
+      AlignSeq maxAlignseq = maximalAlignseq.get(ch);
     final StringBuilder mappingDetails = new StringBuilder(128);
     mappingDetails.append(NEWLINE)
             .append("Sequence \u27f7 Structure mapping details");
@@ -710,7 +751,9 @@ public class StructureSelectionManager
     StructureMapping nwMapping = new StructureMapping(seq, pdbFile,
             pdb.getId(), maxChainId, mapping, mappingDetails.toString());
     maxChain.transferResidueAnnotation(nwMapping, sqmpping);
-    return nwMapping;
+      nwMappings.add(nwMapping);
+    }
+    return nwMappings;
   }
 
   public void removeStructureViewerListener(Object svl, String[] pdbfiles)