JAL-1919 initial support for mmCIF using JMol API. Created an Abstract class - Struct...
[jalview.git] / src / jalview / ext / jmol / JmolParser.java
index dcbd6ad..702c0b1 100644 (file)
@@ -27,18 +27,18 @@ import jalview.datamodel.DBRefSource;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
-import jalview.io.AlignFile;
 import jalview.io.FileParse;
+import jalview.io.StructureFile;
 import jalview.schemes.ResidueProperties;
 import jalview.util.Comparison;
 import jalview.util.MessageManager;
 
 import java.io.IOException;
 import java.util.ArrayList;
-import java.util.Collection;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
+import java.util.Vector;
 
 import javajs.awt.Dimension;
 
@@ -54,30 +54,37 @@ import org.jmol.modelsetbio.BioPolymer;
 import org.jmol.modelsetbio.Monomer;
 import org.jmol.viewer.Viewer;
 
+import MCview.Atom;
 import MCview.PDBChain;
 
 /**
- * Import and process files with Jmol for file like PDB, mmCIF 
+ * Import and process files with Jmol for file like PDB, mmCIF
  * 
  * @author jprocter
  * 
  */
-public class JmolParser extends AlignFile implements
-        JmolStatusListener
+public class JmolParser extends StructureFile implements JmolStatusListener
 {
   Viewer viewer = null;
 
-  private Collection<PDBChain> chains;
-
-  /*
-   * Set true to predict secondary structure (using JMol for protein, Annotate3D
-   * for RNA)
-   */
-  private boolean predictSecondaryStructure = true;
-
-  public JmolParser(String inFile, String type) throws IOException
+  public JmolParser(boolean addAlignmentAnnotations,
+          boolean predictSecondaryStructure, boolean externalSecStr,
+          String inFile, String type) throws IOException
   {
     super(inFile, type);
+    this.visibleChainAnnotation = addAlignmentAnnotations;
+    this.predictSecondaryStructure = predictSecondaryStructure;
+    this.externalSecondaryStructure = externalSecStr;
+  }
+
+  public JmolParser(boolean addAlignmentAnnotations,
+          boolean predictSecondaryStructure, boolean externalSecStr,
+          FileParse fp) throws IOException
+  {
+    super(fp);
+    this.visibleChainAnnotation = addAlignmentAnnotations;
+    this.predictSecondaryStructure = predictSecondaryStructure;
+    this.externalSecondaryStructure = externalSecStr;
   }
 
   public JmolParser(FileParse fp) throws IOException
@@ -85,6 +92,11 @@ public class JmolParser extends AlignFile implements
     super(fp);
   }
 
+  public JmolParser(String inFile, String type) throws IOException
+  {
+    super(inFile, type);
+  }
+
   public JmolParser()
   {
   }
@@ -375,7 +387,7 @@ public class JmolParser extends AlignFile implements
   public void parse() throws IOException
   {
 
-    chains = new ArrayList<PDBChain>();
+    setChains(new Vector<PDBChain>());
     Viewer jmolModel = getJmolData();
     jmolModel.openReader(getDataName(), getDataName(), getReader());
     waitForScript(jmolModel);
@@ -385,7 +397,8 @@ public class JmolParser extends AlignFile implements
      */
     if (jmolModel.ms.mc > 0)
     {
-      parseBiopolymers(jmolModel.ms);
+      // parseBiopolymer(jmolModel.ms);
+      transformJmolModelToJalview(jmolModel.ms);
     }
   }
 
@@ -396,17 +409,16 @@ public class JmolParser extends AlignFile implements
    * @param ms
    * @throws IOException
    */
-  public void parseBiopolymers(ModelSet ms) throws IOException
+  public void parseBiopolymer(ModelSet ms) throws IOException
   {
     int modelIndex = -1;
     for (Model model : ms.am)
     {
       modelIndex++;
       String modelTitle = (String) ms.getInfo(modelIndex, "title");
-
       /*
-       * Chains can span BioPolymers, so first make a flattened list, 
-       * and then work out the lengths of chains present
+       * Chains can span BioPolymers, so first make a flattened list, and then
+       * work out the lengths of chains present
        */
       List<Monomer> monomers = getMonomers(ms, (BioModel) model);
       List<Integer> chainLengths = getChainLengths(monomers);
@@ -417,12 +429,99 @@ public class JmolParser extends AlignFile implements
       int from = 0;
       for (int length : chainLengths)
       {
-        buildSequenceFromChain(monomers.subList(from, from + length), modelTitle);
+        buildSequenceFromChain(monomers.subList(from, from + length),
+                modelTitle);
         from += length;
       }
     }
   }
 
+  public void transformJmolModelToJalview(ModelSet ms)
+  {
+    try
+    {
+      String lastID = "";
+      List<SequenceI> rna = new ArrayList<SequenceI>();
+      List<SequenceI> prot = new ArrayList<SequenceI>();
+      PDBChain tmpchain;
+      String pdbId = (String) ms.getInfo(0, "title");
+      setId(pdbId);
+      List<Atom> significantAtoms = convertSignificantAtoms(ms);
+      for (Atom tmpatom : significantAtoms)
+      {
+        try
+        {
+          tmpchain = findChain(tmpatom.chain);
+          if (tmpatom.resNumIns.trim().equals(lastID))
+          {
+            // phosphorylated protein - seen both CA and P..
+            continue;
+          }
+          tmpchain.atoms.addElement(tmpatom);
+        } catch (Exception e)
+        {
+          tmpchain = new PDBChain(pdbId, tmpatom.chain);
+          getChains().add(tmpchain);
+          tmpchain.atoms.addElement(tmpatom);
+        }
+        lastID = tmpatom.resNumIns.trim();
+      }
+      makeResidueList();
+      makeCaBondList();
+
+      if (getId() == null)
+      {
+        setId(inFile.getName());
+      }
+      for (PDBChain chain : getChains())
+      {
+        SequenceI chainseq = postProcessChain(chain);
+        if (isRNA(chainseq))
+        {
+          rna.add(chainseq);
+        }
+        else
+        {
+          prot.add(chainseq);
+        }
+      }
+    } catch (OutOfMemoryError er)
+    {
+      System.out
+              .println("OUT OF MEMORY LOADING TRANSFORMING JMOL MODEL TO JALVIEW MODEL");
+      // throw new IOException(
+      // MessageManager
+      // .getString("exception.outofmemory_loading_pdb_file"));
+    }
+  }
+
+  private List<Atom> convertSignificantAtoms(ModelSet ms)
+  {
+    List<Atom> significantAtoms = new ArrayList<Atom>();
+    for (org.jmol.modelset.Atom atom : ms.at)
+    {
+      if (atom.getAtomName().equalsIgnoreCase("CA")
+              || atom.getAtomName().equalsIgnoreCase("P"))
+      {
+        Atom curAtom = new Atom(atom.x, atom.y, atom.z);
+        curAtom.atomIndex = atom.getIndex();
+        curAtom.chain = atom.getChainIDStr();
+        curAtom.insCode = atom.group.getInsertionCode();
+        curAtom.name = atom.getAtomName();
+        curAtom.number = atom.getAtomNumber();
+        curAtom.resName = atom.getGroup3(true);
+        curAtom.resNumber = atom.getResno();
+        curAtom.occupancy = ms.occupancies != null ? ms.occupancies[atom
+                .getIndex()] : Float.valueOf(atom.getOccupancy100());
+        curAtom.resNumIns = "" + curAtom.resNumber + curAtom.insCode;
+        curAtom.tfactor = 0;
+        curAtom.type = 0;
+        significantAtoms.add(curAtom);
+      }
+    }
+    return significantAtoms;
+  }
+
   /**
    * Helper method to construct a sequence for one chain and add it to the seqs
    * list
@@ -431,7 +530,8 @@ public class JmolParser extends AlignFile implements
    *          a list of all monomers in the chain
    * @param modelTitle
    */
-  protected void buildSequenceFromChain(List<Monomer> monomers, String modelTitle)
+  protected void buildSequenceFromChain(List<Monomer> monomers,
+          String modelTitle)
   {
     final int length = monomers.size();
 
@@ -468,8 +568,7 @@ public class JmolParser extends AlignFile implements
     /*
      * construct and add the Jalview sequence
      */
-    String seqName = "" + modelTitle + "|"
-            + chainId;
+    String seqName = "" + modelTitle + "|" + chainId;
     int start = firstResNum;
     int end = firstResNum + length - 1;
 
@@ -481,28 +580,17 @@ public class JmolParser extends AlignFile implements
 
     seqs.add(sq);
 
-    addChainMetaData(sq, monomers, chainId);
-
     /*
      * add secondary structure predictions (if any)
      */
     if (isPredictSecondaryStructure())
     {
       addSecondaryStructureAnnotation(modelTitle, sq, secstr, secstrcode,
-            chainId, firstResNum);
+              chainId, firstResNum);
     }
 
   }
 
-  public void addChainMetaData(SequenceI sq, List<Monomer> monomers,
-          String chainId)
-  {
-    for (char res : sq.getSequence())
-    {
-
-    }
-  }
-
   /**
    * Add a source db ref entry for the given sequence.
    * 
@@ -523,7 +611,6 @@ public class JmolParser extends AlignFile implements
     sq.addDBRef(sourceDBRef);
   }
 
-
   /**
    * Add a PDBEntry giving the source of PDB data to the sequence
    * 
@@ -574,14 +661,14 @@ public class JmolParser extends AlignFile implements
       seq[pos] = monomer.getGroup1();
 
       /*
-       * JAL-1828 replace a modified amino acid with its standard
-       * equivalent (e.g. MSE with MET->M) to maximise sequence matching
+       * JAL-1828 replace a modified amino acid with its standard equivalent
+       * (e.g. MSE with MET->M) to maximise sequence matching
        */
       replaceNonCanonicalResidue(monomer.getGroup3(), seq, pos);
 
       /*
-       * if Jmol has derived a secondary structure prediction for
-       * this position, convert it to Jalview equivalent and save it
+       * if Jmol has derived a secondary structure prediction for this position,
+       * convert it to Jalview equivalent and save it
        */
       setSecondaryStructure(monomer.getProteinStructureSubType(), pos,
               secstr, secstrcode);
@@ -603,8 +690,8 @@ public class JmolParser extends AlignFile implements
    * @return
    */
   protected void addSecondaryStructureAnnotation(String modelTitle,
-          SequenceI sq, char[] secstr, char[] secstrcode,
-          String chainId, int firstResNum)
+          SequenceI sq, char[] secstr, char[] secstrcode, String chainId,
+          int firstResNum)
   {
     char[] seq = sq.getSequence();
     boolean ssFound = false;
@@ -660,7 +747,6 @@ public class JmolParser extends AlignFile implements
     }
   }
 
-
   /**
    * Scans the list of Monomers (residue models), inspecting the chain id for
    * each, and returns an array whose length is the number of chains, and values
@@ -723,8 +809,8 @@ public class JmolParser extends AlignFile implements
         if (group instanceof Monomer)
         {
           /*
-           * ignore alternate residue at same position
-           * example: 1ejg has residues A:LEU, B:ILE at RESNUM=25
+           * ignore alternate residue at same position example: 1ejg has
+           * residues A:LEU, B:ILE at RESNUM=25
            */
           int resNo = group.getResno();
           if (lastResNo != resNo)
@@ -748,14 +834,4 @@ public class JmolParser extends AlignFile implements
     this.predictSecondaryStructure = predictSecondaryStructure;
   }
 
-  public Collection<PDBChain> getChains()
-  {
-    return chains;
-  }
-
-  public void setChains(Collection<PDBChain> chains)
-  {
-    this.chains = chains;
-  }
-
 }