JAL-1919 PDBfile and JmolParser refactor
[jalview.git] / src / jalview / io / StructureFile.java
index d4c2d7f..e2b73d1 100644 (file)
@@ -1,6 +1,7 @@
 package jalview.io;
 
 import jalview.analysis.AlignSeq;
+import jalview.datamodel.Alignment;
 import jalview.datamodel.AlignmentAnnotation;
 import jalview.datamodel.AlignmentI;
 import jalview.datamodel.DBRefEntry;
@@ -10,6 +11,7 @@ import jalview.datamodel.SequenceI;
 
 import java.awt.Color;
 import java.io.IOException;
+import java.lang.reflect.Constructor;
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Vector;
@@ -66,6 +68,7 @@ public abstract class StructureFile extends AlignFile
   {
   }
 
+  @SuppressWarnings("rawtypes")
   protected SequenceI postProcessChain(PDBChain chain)
   {
     SequenceI pdbSequence = chain.sequence;
@@ -94,11 +97,15 @@ public abstract class StructureFile extends AlignFile
     sourceDBRef.setEndRes(pdbSequence.getEnd());
 
     // PDBChain objects maintain reference to dataset
-    SequenceI chainseq = pdbSequence.deriveSequence();
-    chainseq.setSourceDBRef(sourceDBRef);
+
+    // SequenceI chainseq = pdbSequence.deriveSequence();
+    // chainseq.setSourceDBRef(sourceDBRef);
+    // chainseq.addPDBId(entry);
+    // chainseq.addDBRef(sourceDBRef);
+    SequenceI chainseq = chain.sequence;
     chainseq.addPDBId(entry);
+    chainseq.setSourceDBRef(sourceDBRef);
     chainseq.addDBRef(sourceDBRef);
-
     seqs.addElement(chainseq);
 
     AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
@@ -114,6 +121,7 @@ public abstract class StructureFile extends AlignFile
     return chainseq;
   }
 
+  @SuppressWarnings({ "unchecked", "rawtypes" })
   protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
           throws Exception
   {
@@ -158,6 +166,7 @@ public abstract class StructureFile extends AlignFile
     }
   }
 
+  @SuppressWarnings("unchecked")
   protected void replaceAndUpdateChains(List<SequenceI> prot,
           AlignmentI al,
           String pep, boolean b)
@@ -188,6 +197,87 @@ public abstract class StructureFile extends AlignFile
     }
   }
 
+  /**
+   * Predict secondary structure for RNA and/or protein sequences and add as
+   * annotations
+   * 
+   * @param rnaSequences
+   * @param proteinSequences
+   */
+  protected void addSecondaryStructure(List<SequenceI> rnaSequences,
+          List<SequenceI> proteinSequences)
+  {
+    /*
+     * Currently using Annotate3D for RNA, but only if the 'use external
+     * prediction' flag is set
+     */
+    if (externalSecondaryStructure && rnaSequences.size() > 0)
+    {
+      try
+      {
+        processPdbFileWithAnnotate3d(rnaSequences);
+      } catch (Exception x)
+      {
+        System.err.println("Exceptions when dealing with RNA in pdb file");
+        x.printStackTrace();
+
+      }
+    }
+
+    /*
+     * Currently using JMol PDB parser for peptide
+     */
+    if (proteinSequences.size() > 0)
+    {
+      try
+      {
+        processWithJmolParser(proteinSequences);
+      } catch (Exception x)
+      {
+        System.err
+                .println("Exceptions from Jmol when processing data in pdb file");
+        x.printStackTrace();
+      }
+    }
+  }
+
+  @SuppressWarnings({ "unchecked", "rawtypes" })
+  private void processWithJmolParser(List<SequenceI> prot)
+          throws Exception
+  {
+    try
+    {
+
+      Class cl = Class.forName("jalview.ext.jmol.JmolParser");
+      if (cl != null)
+      {
+        final Constructor constructor = cl
+                .getConstructor(new Class[] { FileParse.class });
+        final Object[] args = new Object[] { new FileParse(getDataName(),
+                type) };
+        Object jmf = constructor.newInstance(args);
+        AlignmentI al = new Alignment((SequenceI[]) cl.getMethod(
+                "getSeqsAsArray", new Class[] {}).invoke(jmf));
+        cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
+                .invoke(jmf, al);
+        for (SequenceI sq : al.getSequences())
+        {
+          if (sq.getDatasetSequence() != null)
+          {
+            sq.getDatasetSequence().getAllPDBEntries().clear();
+          }
+          else
+          {
+            sq.getAllPDBEntries().clear();
+          }
+        }
+        replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
+      }
+    } catch (ClassNotFoundException q)
+    {
+    }
+  }
+
   public PDBChain findChain(String id) throws Exception
   {
     for (PDBChain chain : getChains())