JAL-1919 basic support for importing and viewing mmCIF files - mapping support not...
[jalview.git] / src / jalview / ext / jmol / JmolParser.java
similarity index 85%
rename from src/jalview/ext/jmol/PDBFileWithJmol.java
rename to src/jalview/ext/jmol/JmolParser.java
index d32a10d..dcbd6ad 100644 (file)
@@ -22,6 +22,8 @@ package jalview.ext.jmol;
 
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.Annotation;
+import jalview.datamodel.DBRefEntry;
+import jalview.datamodel.DBRefSource;
 import jalview.datamodel.PDBEntry;
 import jalview.datamodel.Sequence;
 import jalview.datamodel.SequenceI;
@@ -33,6 +35,7 @@ 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;
@@ -51,28 +54,38 @@ import org.jmol.modelsetbio.BioPolymer;
 import org.jmol.modelsetbio.Monomer;
 import org.jmol.viewer.Viewer;
 
+import MCview.PDBChain;
+
 /**
- * Import and process PDB files with Jmol
+ * Import and process files with Jmol for file like PDB, mmCIF 
  * 
  * @author jprocter
  * 
  */
-public class PDBFileWithJmol extends AlignFile implements
+public class JmolParser extends AlignFile implements
         JmolStatusListener
 {
   Viewer viewer = null;
 
-  public PDBFileWithJmol(String inFile, String type) throws IOException
+  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
   {
     super(inFile, type);
   }
 
-  public PDBFileWithJmol(FileParse fp) throws IOException
+  public JmolParser(FileParse fp) throws IOException
   {
     super(fp);
   }
 
-  public PDBFileWithJmol()
+  public JmolParser()
   {
   }
 
@@ -361,6 +374,8 @@ public class PDBFileWithJmol extends AlignFile implements
   @Override
   public void parse() throws IOException
   {
+
+    chains = new ArrayList<PDBChain>();
     Viewer jmolModel = getJmolData();
     jmolModel.openReader(getDataName(), getDataName(), getReader());
     waitForScript(jmolModel);
@@ -453,21 +468,91 @@ public class PDBFileWithJmol extends AlignFile implements
     /*
      * construct and add the Jalview sequence
      */
-    String seqName = "" + getDataName() + "|" + modelTitle + "|"
+    String seqName = "" + modelTitle + "|"
             + chainId;
-    SequenceI sq = new Sequence(seqName, seq, firstResNum, firstResNum + length - 1);
+    int start = firstResNum;
+    int end = firstResNum + length - 1;
+
+    SequenceI sq = new Sequence(seqName, seq, start, end);
+
+    addPdbid(sq, modelTitle, chainId);
+
+    addSourceDBref(sq, modelTitle, start, end);
+
     seqs.add(sq);
 
+    addChainMetaData(sq, monomers, chainId);
+
     /*
      * add secondary structure predictions (if any)
      */
-    addSecondaryStructureAnnotation(modelTitle, sq, secstr, secstrcode,
+    if (isPredictSecondaryStructure())
+    {
+      addSecondaryStructureAnnotation(modelTitle, sq, secstr, secstrcode,
             chainId, firstResNum);
+    }
 
-    /*
-     * record the PDB id for the sequence
-     */
-    addPdbid(sq, chainId);
+  }
+
+  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.
+   * 
+   * @param sq
+   * @param accessionId
+   * @param start
+   * @param end
+   */
+  protected void addSourceDBref(SequenceI sq, String accessionId,
+          int start, int end)
+  {
+    DBRefEntry sourceDBRef = new DBRefEntry();
+    sourceDBRef.setAccessionId(accessionId);
+    sourceDBRef.setSource(DBRefSource.MMCIF);
+    sourceDBRef.setStartRes(start);
+    sourceDBRef.setEndRes(end);
+    sq.setSourceDBRef(sourceDBRef);
+    sq.addDBRef(sourceDBRef);
+  }
+
+
+  /**
+   * Add a PDBEntry giving the source of PDB data to the sequence
+   * 
+   * @param sq
+   * @param id
+   * @param chainId
+   */
+  protected void addPdbid(SequenceI sq, String id, String chainId)
+  {
+    PDBEntry entry = new PDBEntry();
+    entry.setId(id);
+    entry.setType(PDBEntry.Type.MMCIF);
+    entry.setProperty(new Hashtable());
+    if (chainId != null)
+    {
+      // entry.getProperty().put("CHAIN", chains.elementAt(i).id);
+      entry.setChainCode(String.valueOf(chainId));
+    }
+    if (inFile != null)
+    {
+      entry.setFile(inFile.getAbsolutePath());
+    }
+    else
+    {
+      // TODO: decide if we should dump the datasource to disk
+      entry.setFile(getDataName());
+    }
+
+    sq.addPDBId(entry);
   }
 
   /**
@@ -575,21 +660,6 @@ public class PDBFileWithJmol extends AlignFile implements
     }
   }
 
-  /**
-   * Add a PDBEntry giving the source of PDB data to the sequence
-   * 
-   * @param sq
-   * @param chainId
-   */
-  protected void addPdbid(SequenceI sq, String chainId)
-  {
-    PDBEntry pdbe = new PDBEntry();
-    pdbe.setFile(getDataName());
-    pdbe.setId(getDataName());
-    pdbe.setProperty(new Hashtable());
-    pdbe.setChainCode(chainId);
-    sq.addPDBId(pdbe);
-  }
 
   /**
    * Scans the list of Monomers (residue models), inspecting the chain id for
@@ -668,4 +738,24 @@ public class PDBFileWithJmol extends AlignFile implements
     return result;
   }
 
+  public boolean isPredictSecondaryStructure()
+  {
+    return predictSecondaryStructure;
+  }
+
+  public void setPredictSecondaryStructure(boolean predictSecondaryStructure)
+  {
+    this.predictSecondaryStructure = predictSecondaryStructure;
+  }
+
+  public Collection<PDBChain> getChains()
+  {
+    return chains;
+  }
+
+  public void setChains(Collection<PDBChain> chains)
+  {
+    this.chains = chains;
+  }
+
 }