JAL-1919 initial support for mmCIF using JMol API. Created an Abstract class - Struct...
[jalview.git] / src / jalview / io / StructureFile.java
1 package jalview.io;
2
3 import jalview.analysis.AlignSeq;
4 import jalview.datamodel.AlignmentAnnotation;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.DBRefEntry;
7 import jalview.datamodel.DBRefSource;
8 import jalview.datamodel.PDBEntry;
9 import jalview.datamodel.SequenceI;
10
11 import java.awt.Color;
12 import java.io.IOException;
13 import java.util.Hashtable;
14 import java.util.List;
15 import java.util.Vector;
16
17 import MCview.PDBChain;
18
19 public abstract class StructureFile extends AlignFile
20 {
21
22   private String id;
23
24   /**
25    * set to true to add derived sequence annotations (temp factor read from
26    * file, or computed secondary structure) to the alignment
27    */
28   protected boolean visibleChainAnnotation = false;
29
30   /*
31    * Set true to predict secondary structure (using JMol for protein, Annotate3D
32    * for RNA)
33    */
34   protected boolean predictSecondaryStructure = true;
35
36   /*
37    * Set true (with predictSecondaryStructure=true) to predict secondary
38    * structure using an external service (currently Annotate3D for RNA only)
39    */
40   protected boolean externalSecondaryStructure = false;
41
42   private Vector<PDBChain> chains;
43
44   public StructureFile(String inFile, String type) throws IOException
45   {
46     super(inFile, type);
47   }
48
49   public StructureFile(FileParse fp) throws IOException
50   {
51     super(fp);
52   }
53
54   public StructureFile(boolean parseImmediately, String inFile, String type)
55           throws IOException
56   {
57     super(parseImmediately, inFile, type);
58   }
59
60   public StructureFile(boolean a, FileParse fp) throws IOException
61   {
62     super(a, fp);
63   }
64
65   public StructureFile()
66   {
67   }
68
69   protected SequenceI postProcessChain(PDBChain chain)
70   {
71     SequenceI pdbSequence = chain.sequence;
72     pdbSequence.setName(getId() + "|" + pdbSequence.getName());
73     PDBEntry entry = new PDBEntry();
74     entry.setId(getId());
75     entry.setType(PDBEntry.Type.PDB);
76     entry.setProperty(new Hashtable());
77     if (chain.id != null)
78     {
79       entry.setChainCode(String.valueOf(chain.id));
80     }
81     if (inFile != null)
82     {
83       entry.setFile(inFile.getAbsolutePath());
84     }
85     else
86     {
87       entry.setFile(getDataName());
88     }
89
90     DBRefEntry sourceDBRef = new DBRefEntry();
91     sourceDBRef.setAccessionId(getId());
92     sourceDBRef.setSource(DBRefSource.PDB);
93     sourceDBRef.setStartRes(pdbSequence.getStart());
94     sourceDBRef.setEndRes(pdbSequence.getEnd());
95
96     // PDBChain objects maintain reference to dataset
97     SequenceI chainseq = pdbSequence.deriveSequence();
98     chainseq.setSourceDBRef(sourceDBRef);
99     chainseq.addPDBId(entry);
100     chainseq.addDBRef(sourceDBRef);
101
102     seqs.addElement(chainseq);
103
104     AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
105
106     if (chainannot != null && visibleChainAnnotation)
107     {
108       for (int ai = 0; ai < chainannot.length; ai++)
109       {
110         chainannot[ai].visible = visibleChainAnnotation;
111         annotations.addElement(chainannot[ai]);
112       }
113     }
114     return chainseq;
115   }
116
117   protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
118           throws Exception
119   {
120     // System.out.println("this is a PDB format and RNA sequence");
121     // note: we use reflection here so that the applet can compile and run
122     // without the HTTPClient bits and pieces needed for accessing Annotate3D
123     // web service
124     try
125     {
126       Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
127       if (cl != null)
128       {
129         // TODO: use the PDB ID of the structure if one is available, to save
130         // bandwidth and avoid uploading the whole structure to the service
131         Object annotate3d = cl.getConstructor(new Class[] {}).newInstance(
132                 new Object[] {});
133         AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
134                 new Class[] { FileParse.class }).invoke(annotate3d,
135                 new Object[] { new FileParse(getDataName(), type) }));
136         for (SequenceI sq : al.getSequences())
137         {
138           if (sq.getDatasetSequence() != null)
139           {
140             if (sq.getDatasetSequence().getAllPDBEntries() != null)
141             {
142               sq.getDatasetSequence().getAllPDBEntries().clear();
143             }
144           }
145           else
146           {
147             if (sq.getAllPDBEntries() != null)
148             {
149               sq.getAllPDBEntries().clear();
150             }
151           }
152         }
153         replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
154       }
155     } catch (ClassNotFoundException x)
156     {
157       // ignore classnotfounds - occurs in applet
158     }
159   }
160
161   protected void replaceAndUpdateChains(List<SequenceI> prot,
162           AlignmentI al,
163           String pep, boolean b)
164   {
165     List<List<? extends Object>> replaced = AlignSeq
166             .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
167                     false);
168     for (PDBChain ch : getChains())
169     {
170       int p = 0;
171       for (SequenceI sq : (List<SequenceI>) replaced.get(0))
172       {
173         p++;
174         if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
175         {
176           p = -p;
177           break;
178         }
179       }
180       if (p < 0)
181       {
182         p = -p - 1;
183         // set shadow entry for chains
184         ch.shadow = (SequenceI) replaced.get(1).get(p);
185         ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
186                 .getMappingFromS1(false);
187       }
188     }
189   }
190
191   public PDBChain findChain(String id) throws Exception
192   {
193     for (PDBChain chain : getChains())
194     {
195       if (chain.id.equalsIgnoreCase(id))
196       {
197         return chain;
198       }
199     }
200     throw new Exception("PDB chain not Found!");
201   }
202
203   public void makeResidueList()
204   {
205     for (PDBChain chain : getChains())
206     {
207       chain.makeResidueList(visibleChainAnnotation);
208     }
209   }
210
211   public void makeCaBondList()
212   {
213     for (PDBChain chain : getChains())
214     {
215       chain.makeCaBondList();
216     }
217   }
218
219   public void setChargeColours()
220   {
221     for (PDBChain chain : getChains())
222     {
223       chain.setChargeColours();
224     }
225   }
226
227   public void setColours(jalview.schemes.ColourSchemeI cs)
228   {
229     for (PDBChain chain : getChains())
230     {
231       chain.setChainColours(cs);
232     }
233   }
234
235   public void setChainColours()
236   {
237     int i = 0;
238     for (PDBChain chain : getChains())
239     {
240       chain.setChainColours(Color.getHSBColor(1.0f / i++, .4f, 1.0f));
241     }
242   }
243
244   public static boolean isRNA(SequenceI seq)
245   {
246     for (char c : seq.getSequence())
247     {
248       if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))
249       {
250         return false;
251       }
252     }
253     return true;
254   }
255
256   /**
257    * make a friendly ID string.
258    * 
259    * @param dataName
260    * @return truncated dataName to after last '/'
261    */
262   protected String safeName(String dataName)
263   {
264     int p = 0;
265     while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
266     {
267       dataName = dataName.substring(p + 1);
268     }
269     return dataName;
270   }
271
272   public String getId()
273   {
274     return id;
275   }
276
277   public void setId(String id)
278   {
279     this.id = id;
280   }
281
282   public Vector<PDBChain> getChains()
283   {
284     return chains;
285   }
286
287   public void setChains(Vector<PDBChain> chains)
288   {
289     this.chains = chains;
290   }
291 }