2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.analysis.AlignSeq;
24 import jalview.api.FeatureSettingsModelI;
25 import jalview.datamodel.Alignment;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.DBRefEntry;
29 import jalview.datamodel.DBRefSource;
30 import jalview.datamodel.PDBEntry;
31 import jalview.datamodel.PDBEntry.Type;
32 import jalview.datamodel.SequenceI;
33 import jalview.structure.StructureImportSettings;
35 import java.awt.Color;
36 import java.io.IOException;
37 import java.lang.reflect.Constructor;
38 import java.util.List;
39 import java.util.Vector;
41 import MCview.PDBChain;
43 public abstract class StructureFile extends AlignFile
47 private PDBEntry.Type dbRefType;
50 * set to true to add derived sequence annotations (temp factor read from
51 * file, or computed secondary structure) to the alignment
53 protected boolean visibleChainAnnotation = false;
56 * Set true to predict secondary structure (using JMol for protein, Annotate3D
59 protected boolean predictSecondaryStructure = false;
62 * Set true (with predictSecondaryStructure=true) to predict secondary
63 * structure using an external service (currently Annotate3D for RNA only)
65 protected boolean externalSecondaryStructure = false;
67 private Vector<PDBChain> chains;
69 private boolean pdbIdAvailable;
71 public StructureFile(String inFile, DataSourceType sourceType)
74 super(inFile, sourceType);
77 public StructureFile(FileParse fp) throws IOException
82 public void addSettings(boolean addAlignmentAnnotations,
83 boolean predictSecondaryStructure, boolean externalSecStr)
85 this.visibleChainAnnotation = addAlignmentAnnotations;
86 this.predictSecondaryStructure = predictSecondaryStructure;
87 this.externalSecondaryStructure = externalSecStr;
90 public void xferSettings()
92 this.visibleChainAnnotation = StructureImportSettings
93 .isVisibleChainAnnotation();
94 this.predictSecondaryStructure = StructureImportSettings
95 .isProcessSecondaryStructure();
96 this.externalSecondaryStructure = StructureImportSettings
97 .isExternalSecondaryStructure();
101 public StructureFile(boolean parseImmediately, String dataObject,
102 DataSourceType sourceType) throws IOException
104 super(parseImmediately, dataObject, sourceType);
107 public StructureFile(boolean a, FileParse fp) throws IOException
112 public StructureFile()
116 protected SequenceI postProcessChain(PDBChain chain)
118 SequenceI pdbSequence = chain.sequence;
119 pdbSequence.setName(getId() + "|" + pdbSequence.getName());
120 PDBEntry entry = new PDBEntry();
121 entry.setId(getId());
122 entry.setType(getStructureFileType());
123 if (chain.id != null)
125 entry.setChainCode(chain.id);
129 entry.setFile(inFile.getAbsolutePath());
133 entry.setFile(getDataName());
136 DBRefEntry sourceDBRef = new DBRefEntry();
137 sourceDBRef.setAccessionId(getId());
138 sourceDBRef.setSource(DBRefSource.PDB);
139 // TODO: specify version for 'PDB' database ref if it is read from a file.
140 // TODO: decide if jalview.io should be creating primary refs!
141 sourceDBRef.setVersion("");
142 pdbSequence.addPDBId(entry);
143 pdbSequence.addDBRef(sourceDBRef);
144 SequenceI chainseq = pdbSequence;
145 seqs.addElement(chainseq);
146 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
148 if (chainannot != null && visibleChainAnnotation)
150 for (int ai = 0; ai < chainannot.length; ai++)
152 chainannot[ai].visible = visibleChainAnnotation;
153 annotations.addElement(chainannot[ai]);
160 * filetype of structure file - default is PDB
162 String structureFileType = PDBEntry.Type.PDB.toString();
164 protected void setStructureFileType(String structureFileType)
166 this.structureFileType = structureFileType;
170 * filetype of last file processed
174 public String getStructureFileType()
176 return structureFileType;
179 @SuppressWarnings({ "unchecked", "rawtypes" })
180 protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
183 // System.out.println("this is a PDB format and RNA sequence");
184 // note: we use reflection here so that the applet can compile and run
185 // without the HTTPClient bits and pieces needed for accessing Annotate3D
189 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
192 // TODO: use the PDB ID of the structure if one is available, to save
193 // bandwidth and avoid uploading the whole structure to the service
194 Object annotate3d = cl.getConstructor(new Class[] {})
195 .newInstance(new Object[] {});
196 AlignmentI al = ((AlignmentI) cl
197 .getMethod("getRNAMLFor", new Class[]
199 .invoke(annotate3d, new Object[]
200 { new FileParse(getDataName(), dataSourceType) }));
201 for (SequenceI sq : al.getSequences())
203 if (sq.getDatasetSequence() != null)
205 if (sq.getDatasetSequence().getAllPDBEntries() != null)
207 sq.getDatasetSequence().getAllPDBEntries().clear();
212 if (sq.getAllPDBEntries() != null)
214 sq.getAllPDBEntries().clear();
218 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
220 } catch (ClassNotFoundException x)
222 // ignore classnotfounds - occurs in applet
226 @SuppressWarnings("unchecked")
227 protected void replaceAndUpdateChains(List<SequenceI> prot, AlignmentI al,
228 String pep, boolean b)
230 List<List<? extends Object>> replaced = AlignSeq
231 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
233 for (PDBChain ch : getChains())
236 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
239 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
248 // set shadow entry for chains
249 ch.shadow = (SequenceI) replaced.get(1).get(p);
250 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
251 .getMappingFromS1(false);
257 * Predict secondary structure for RNA and/or protein sequences and add as
260 * @param rnaSequences
261 * @param proteinSequences
263 protected void addSecondaryStructure(List<SequenceI> rnaSequences,
264 List<SequenceI> proteinSequences)
267 * Currently using Annotate3D for RNA, but only if the 'use external
268 * prediction' flag is set
270 if (externalSecondaryStructure && rnaSequences.size() > 0)
274 processPdbFileWithAnnotate3d(rnaSequences);
275 } catch (Exception x)
277 System.err.println("Exceptions when dealing with RNA in pdb file");
284 * Currently using JMol PDB parser for peptide
286 if (proteinSequences.size() > 0)
290 processWithJmolParser(proteinSequences);
291 } catch (Exception x)
294 "Exceptions from Jmol when processing data in pdb file");
300 @SuppressWarnings({ "unchecked", "rawtypes" })
301 private void processWithJmolParser(List<SequenceI> prot) throws Exception
306 Class cl = Class.forName("jalview.ext.jmol.JmolParser");
309 final Constructor constructor = cl
310 .getConstructor(new Class[]
311 { FileParse.class });
312 final Object[] args = new Object[] {
313 new FileParse(getDataName(), dataSourceType) };
315 StructureImportSettings.setShowSeqFeatures(false);
316 StructureImportSettings.setVisibleChainAnnotation(false);
317 StructureImportSettings
318 .setProcessSecondaryStructure(predictSecondaryStructure);
319 StructureImportSettings
320 .setExternalSecondaryStructure(externalSecondaryStructure);
321 Object jmf = constructor.newInstance(args);
322 AlignmentI al = new Alignment((SequenceI[]) cl
323 .getMethod("getSeqsAsArray", new Class[] {}).invoke(jmf));
324 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
326 for (SequenceI sq : al.getSequences())
328 if (sq.getDatasetSequence() != null)
330 sq.getDatasetSequence().getAllPDBEntries().clear();
334 sq.getAllPDBEntries().clear();
337 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
339 } catch (ClassNotFoundException q)
342 StructureImportSettings.setShowSeqFeatures(true);
345 public PDBChain findChain(String id) throws Exception
347 for (PDBChain chain : getChains())
349 if (chain.id.equals(id))
354 throw new Exception("PDB chain not Found!");
357 public void makeResidueList()
359 for (PDBChain chain : getChains())
361 chain.makeResidueList(visibleChainAnnotation);
365 public void makeCaBondList()
367 for (PDBChain chain : getChains())
369 chain.makeCaBondList();
373 public void setChargeColours()
375 for (PDBChain chain : getChains())
377 chain.setChargeColours();
381 public void setColours(jalview.schemes.ColourSchemeI cs)
383 for (PDBChain chain : getChains())
385 chain.setChainColours(cs);
389 public void setChainColours()
392 for (PDBChain chain : getChains())
394 chain.setChainColours(Color.getHSBColor(1.0f / i++, .4f, 1.0f));
398 public static boolean isRNA(SequenceI seq)
400 int length = seq.getLength();
401 for (int i = 0; i < length; i++)
403 char c = seq.getCharAt(i);
404 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))
413 * make a friendly ID string.
416 * @return truncated dataName to after last '/' and pruned .extension if
419 protected String safeName(String dataName)
422 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
424 dataName = dataName.substring(p + 1);
426 if (dataName.indexOf(".") > -1)
428 dataName = dataName.substring(0, dataName.lastIndexOf("."));
433 public String getId()
438 public void setId(String id)
443 public Vector<PDBChain> getChains()
448 public void setChains(Vector<PDBChain> chains)
450 this.chains = chains;
453 public Type getDbRefType()
458 public void setDbRefType(String dbRefType)
460 this.dbRefType = Type.getType(dbRefType);
463 public void setDbRefType(Type dbRefType)
465 this.dbRefType = dbRefType;
469 * Returns a descriptor for suitable feature display settings with
471 * <li>ResNums or insertions features visible</li>
472 * <li>insertions features coloured red</li>
473 * <li>ResNum features coloured by label</li>
474 * <li>Insertions displayed above (on top of) ResNums</li>
478 public FeatureSettingsModelI getFeatureColourScheme()
480 return new PDBFeatureSettings();
484 * Answers true if the structure file has a PDBId
488 public boolean isPPDBIdAvailable()
490 return pdbIdAvailable;
493 public void setPDBIdAvailable(boolean pdbIdAvailable)
495 this.pdbIdAvailable = pdbIdAvailable;