JAL-674 map on StructureSelectionManager to record PDB IDs extracted/assigned to...
[jalview.git] / src / jalview / structure / StructureSelectionManager.java
index c0c559a..b38fa7c 100644 (file)
  */
 package jalview.structure;
 
-import java.io.*;
-import java.util.*;
-
-import MCview.*;
-import jalview.analysis.*;
-import jalview.api.AlignmentViewPanel;
+import jalview.analysis.AlignSeq;
 import jalview.api.StructureSelectionManagerProvider;
-import jalview.datamodel.*;
+import jalview.datamodel.AlignedCodonFrame;
+import jalview.datamodel.AlignmentAnnotation;
+import jalview.datamodel.Annotation;
+import jalview.datamodel.PDBEntry;
+import jalview.datamodel.SearchResults;
+import jalview.datamodel.SequenceI;
+import jalview.io.FormatAdapter;
+import jalview.util.MessageManager;
+
+import java.io.PrintStream;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.IdentityHashMap;
+import java.util.Vector;
+
+import MCview.Atom;
+import MCview.PDBChain;
 
 public class StructureSelectionManager
 {
@@ -55,7 +66,36 @@ public class StructureSelectionManager
     }
   }
 
-  Hashtable mappingData = new Hashtable();
+  /**
+   * map between the PDB IDs (or structure identifiers) used by Jalview and the
+   * absolute filenames for PDB data that corresponds to it
+   */
+  HashMap<String, String> pdbIdFileName = new HashMap<String, String>(),
+          pdbFileNameId = new HashMap<String, String>();
+
+  public void registerPDBFile(String idForFile, String absoluteFile)
+  {
+    pdbIdFileName.put(idForFile, absoluteFile);
+    pdbFileNameId.put(absoluteFile, idForFile);
+  }
+
+  public String findIdForPDBFile(String idOrFile)
+  {
+    String id = pdbFileNameId.get(idOrFile);
+    return id;
+  }
+
+  public String findFileForPDBId(String idOrFile)
+  {
+    String id = pdbIdFileName.get(idOrFile);
+    return id;
+  }
+
+  public boolean isPDBFileRegistered(String idOrFile)
+  {
+    return pdbFileNameId.containsKey(idOrFile)
+            || pdbIdFileName.containsKey(idOrFile);
+  }
 
   private static StructureSelectionManager nullProvider = null;
 
@@ -68,9 +108,8 @@ public class StructureSelectionManager
       {
         if (instances != null)
         {
-          throw new Error(
-                  "Implementation error. Structure selection manager's context is 'null'",
-                  new NullPointerException("SSM context is null"));
+          throw new Error(MessageManager.getString("error.implementation_error_structure_selection_manager_null"),
+                  new NullPointerException(MessageManager.getString("exception.ssm_context_is_null")));
         }
         else
         {
@@ -181,9 +220,26 @@ public class StructureSelectionManager
      * the tried and tested MCview pdb mapping
      */
     MCview.PDBfile pdb = null;
+    boolean parseSecStr=true;
+    for (SequenceI sq:sequence)
+    {
+      SequenceI ds = sq;while (ds.getDatasetSequence()!=null) { ds = ds.getDatasetSequence();};
+      if (ds.getAnnotation()!=null)
+      {
+        for (AlignmentAnnotation ala:ds.getAnnotation())
+        {
+          // false if any annotation present from this structure
+          if (MCview.PDBfile.isCalcIdForFile(ala.getCalcId(), pdbFile))
+          {
+            parseSecStr = false;
+          }
+        }
+      }
+    }
     try
     {
-      pdb = new MCview.PDBfile(pdbFile, protocol);
+      pdb = new MCview.PDBfile(true, parseSecStr, pdbFile, protocol);
+      
     } catch (Exception ex)
     {
       ex.printStackTrace();
@@ -217,7 +273,9 @@ public class StructureSelectionManager
         }
       }
       else
+      {
         targetChain = "";
+      }
 
       int max = -10;
       AlignSeq maxAlignseq = null;
@@ -226,7 +284,7 @@ public class StructureSelectionManager
       boolean first = true;
       for (int i = 0; i < pdb.chains.size(); i++)
       {
-        PDBChain chain = ((PDBChain) pdb.chains.elementAt(i));
+        PDBChain chain = (pdb.chains.elementAt(i));
         if (targetChain.length() > 0 && !targetChain.equals(chain.id)
                 && !infChain)
         {
@@ -235,8 +293,8 @@ public class StructureSelectionManager
         // TODO: correctly determine sequence type for mixed na/peptide
         // structures
         AlignSeq as = new AlignSeq(sequence[s],
-                ((PDBChain) pdb.chains.elementAt(i)).sequence,
-                ((PDBChain) pdb.chains.elementAt(i)).isNa ? AlignSeq.DNA
+                pdb.chains.elementAt(i).sequence,
+                pdb.chains.elementAt(i).isNa ? AlignSeq.DNA
                         : AlignSeq.PEP);
         as.calcScoreMatrix();
         as.traceAlignment();
@@ -317,7 +375,9 @@ public class StructureSelectionManager
       }
 
       if (protocol.equals(jalview.io.AppletFormatAdapter.PASTE))
+      {
         pdbFile = "INLINE" + pdb.id;
+      }
 
       mappings[mappings.length - 1] = new StructureMapping(sequence[s],
               pdbFile, pdb.id, maxChainId, mapping,
@@ -352,7 +412,9 @@ public class StructureSelectionManager
     String[] handlepdbs;
     Vector pdbs = new Vector();
     for (int i = 0; i < pdbfiles.length; pdbs.addElement(pdbfiles[i++]))
+    {
       ;
+    }
     StructureListener sl;
     for (int i = 0; i < listeners.size(); i++)
     {
@@ -444,7 +506,9 @@ public class StructureSelectionManager
       {
         Object li = listeners.elementAt(i);
         if (li instanceof SequenceListener)
+        {
           ((SequenceListener) li).highlightSequence(results);
+        }
       }
     }
   }
@@ -468,7 +532,9 @@ public class StructureSelectionManager
     boolean hasSequenceListeners = handlingVamsasMo || seqmappings != null;
     SearchResults results = null;
     if (index == -1)
+    {
       index = seq.findPosition(indexpos);
+    }
     StructureListener sl;
     int atomNo = 0;
     for (int i = 0; i < listeners.size(); i++)
@@ -686,9 +752,13 @@ public class StructureSelectionManager
           AlignedCodonFrame[] codonFrames)
   {
     if (!add && (seqmappings == null || seqmappings.size() == 0))
+    {
       return;
+    }
     if (seqmappings == null)
+    {
       seqmappings = new Vector();
+    }
     if (codonFrames != null && codonFrames.length > 0)
     {
       for (int cf = 0; cf < codonFrames.length; cf++)
@@ -726,8 +796,10 @@ public class StructureSelectionManager
             int[] nsr = new int[(seqmappingrefs == null) ? 1
                     : seqmappingrefs.length + 1];
             if (seqmappingrefs != null && seqmappingrefs.length > 0)
+            {
               System.arraycopy(seqmappingrefs, 0, nsr, 0,
                       seqmappingrefs.length);
+            }
             nsr[(seqmappingrefs == null) ? 0 : seqmappingrefs.length] = 1;
             seqmappingrefs = nsr;
           }
@@ -814,10 +886,10 @@ public class StructureSelectionManager
       listeners.clear();
       listeners = null;
     }
-    if (mappingData != null)
+    if (pdbIdFileName != null)
     {
-      mappingData.clear();
-      mappingData = null;
+      pdbIdFileName.clear();
+      pdbIdFileName = null;
     }
     if (sel_listeners != null)
     {
@@ -861,4 +933,13 @@ public class StructureSelectionManager
     }
   }
 
+  public void registerPDBEntry(PDBEntry pdbentry)
+  {
+    if (pdbentry.getFile() != null
+            && pdbentry.getFile().trim().length() > 0)
+    {
+      registerPDBFile(pdbentry.getId(), pdbentry.getFile());
+    }
+  }
+
 }