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 java.awt.Color;
24 import java.io.IOException;
25 import java.lang.reflect.Constructor;
26 import java.util.List;
27 import java.util.Vector;
29 import jalview.analysis.AlignSeq;
30 import jalview.api.FeatureSettingsModelI;
31 import jalview.datamodel.Alignment;
32 import jalview.datamodel.AlignmentAnnotation;
33 import jalview.datamodel.AlignmentI;
34 import jalview.datamodel.DBRefEntry;
35 import jalview.datamodel.DBRefSource;
36 import jalview.datamodel.PDBEntry;
37 import jalview.datamodel.PDBEntry.Type;
38 import jalview.datamodel.SequenceI;
39 import jalview.structure.StructureImportSettings;
40 import jalview.structure.StructureImportSettings.TFType;
41 import mc_view.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 private StructureImportSettings.TFType temperatureFactorType = TFType.DEFAULT;
73 public void setTemperatureFactorType(StructureImportSettings.TFType t)
75 this.temperatureFactorType = t;
78 public StructureImportSettings.TFType getTemperatureFactorType()
80 return temperatureFactorType;
83 public StructureFile(Object inFile, DataSourceType sourceType)
86 this(inFile, sourceType, null);
89 public StructureFile(Object inFile, DataSourceType sourceType,
90 StructureImportSettings.TFType tempfacType) throws IOException
92 super(inFile, sourceType);
93 this.setTemperatureFactorType(tempfacType);
96 public StructureFile(FileParse fp) throws IOException
101 public void addSettings(boolean addAlignmentAnnotations,
102 boolean predictSecondaryStructure, boolean externalSecStr)
104 this.visibleChainAnnotation = addAlignmentAnnotations;
105 this.predictSecondaryStructure = predictSecondaryStructure;
106 this.externalSecondaryStructure = externalSecStr;
109 public void xferSettings()
111 this.visibleChainAnnotation = StructureImportSettings
112 .isVisibleChainAnnotation();
113 this.predictSecondaryStructure = StructureImportSettings
114 .isProcessSecondaryStructure();
115 this.externalSecondaryStructure = StructureImportSettings
116 .isExternalSecondaryStructure();
117 this.temperatureFactorType = StructureImportSettings
118 .getTemperatureFactorType();
121 public StructureFile(boolean parseImmediately, Object dataObject,
122 DataSourceType sourceType) throws IOException
124 super(parseImmediately, dataObject, sourceType);
127 public StructureFile(boolean a, FileParse fp) throws IOException
132 public StructureFile()
136 protected SequenceI postProcessChain(PDBChain chain)
138 SequenceI pdbSequence = chain.sequence;
139 pdbSequence.setName(getId() + "|" + pdbSequence.getName());
140 PDBEntry entry = new PDBEntry();
141 entry.setId(getId());
142 entry.setFakedPDBId(!isPPDBIdAvailable());
143 entry.setType(getStructureFileType());
144 if (chain.id != null)
146 entry.setChainCode(chain.id);
150 entry.setFile(inFile.getAbsolutePath());
154 entry.setFile(getDataName());
157 DBRefEntry sourceDBRef = new DBRefEntry();
158 sourceDBRef.setAccessionId(getId());
159 sourceDBRef.setSource(DBRefSource.PDB);
160 // TODO: specify version for 'PDB' database ref if it is read from a file.
161 // TODO: decide if jalview.io should be creating primary refs!
162 sourceDBRef.setVersion("");
163 pdbSequence.addPDBId(entry);
164 pdbSequence.addDBRef(sourceDBRef);
165 SequenceI chainseq = pdbSequence;
166 seqs.addElement(chainseq);
167 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
169 if (chainannot != null && visibleChainAnnotation)
171 for (int ai = 0; ai < chainannot.length; ai++)
173 chainannot[ai].visible = visibleChainAnnotation;
174 annotations.addElement(chainannot[ai]);
181 * filetype of structure file - default is PDB
183 String structureFileType = PDBEntry.Type.PDB.toString();
185 protected void setStructureFileType(String structureFileType)
187 this.structureFileType = structureFileType;
191 * filetype of last file processed
195 public String getStructureFileType()
197 return structureFileType;
200 @SuppressWarnings({ "unchecked", "rawtypes" })
201 protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
204 // System.out.println("this is a PDB format and RNA sequence");
205 // note: we use reflection here so that the applet can compile and run
206 // without the HTTPClient bits and pieces needed for accessing Annotate3D
210 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
213 // TODO: use the PDB ID of the structure if one is available, to save
214 // bandwidth and avoid uploading the whole structure to the service
215 Object annotate3d = cl.getConstructor(new Class[] {})
216 .newInstance(new Object[] {});
217 AlignmentI al = ((AlignmentI) cl
218 .getMethod("getRNAMLFor", new Class[]
220 .invoke(annotate3d, new Object[]
221 { new FileParse(getDataName(), dataSourceType) }));
222 for (SequenceI sq : al.getSequences())
224 if (sq.getDatasetSequence() != null)
226 if (sq.getDatasetSequence().getAllPDBEntries() != null)
228 sq.getDatasetSequence().getAllPDBEntries().clear();
233 if (sq.getAllPDBEntries() != null)
235 sq.getAllPDBEntries().clear();
239 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
241 } catch (ClassNotFoundException x)
243 // ignore classnotfounds - occurs in applet
247 @SuppressWarnings("unchecked")
248 protected void replaceAndUpdateChains(List<SequenceI> prot, AlignmentI al,
249 String pep, boolean b)
251 List<List<? extends Object>> replaced = AlignSeq
252 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
254 for (PDBChain ch : getChains())
257 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
260 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
269 // set shadow entry for chains
270 ch.shadow = (SequenceI) replaced.get(1).get(p);
271 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
272 .getMappingFromS1(false);
278 * Predict secondary structure for RNA and/or protein sequences and add as
281 * @param rnaSequences
282 * @param proteinSequences
284 protected void addSecondaryStructure(List<SequenceI> rnaSequences,
285 List<SequenceI> proteinSequences)
288 * Currently using Annotate3D for RNA, but only if the 'use external
289 * prediction' flag is set
291 if (externalSecondaryStructure && rnaSequences.size() > 0)
295 processPdbFileWithAnnotate3d(rnaSequences);
296 } catch (Exception x)
298 System.err.println("Exceptions when dealing with RNA in pdb file");
305 * Currently using JMol PDB parser for peptide
307 if (proteinSequences.size() > 0)
311 processWithJmolParser(proteinSequences);
312 } catch (Exception x)
315 "Exceptions from Jmol when processing data in pdb file");
321 @SuppressWarnings({ "unchecked", "rawtypes" })
322 private void processWithJmolParser(List<SequenceI> prot) throws Exception
327 Class cl = Class.forName("jalview.ext.jmol.JmolParser");
330 final Constructor constructor = cl
331 .getConstructor(new Class[]
332 { FileParse.class });
333 final Object[] args = new Object[] {
334 new FileParse(getDataName(), dataSourceType) };
336 StructureImportSettings.setShowSeqFeatures(false);
337 StructureImportSettings.setVisibleChainAnnotation(false);
338 StructureImportSettings
339 .setProcessSecondaryStructure(predictSecondaryStructure);
340 StructureImportSettings
341 .setExternalSecondaryStructure(externalSecondaryStructure);
342 Object jmf = constructor.newInstance(args);
343 AlignmentI al = new Alignment((SequenceI[]) cl
344 .getMethod("getSeqsAsArray", new Class[] {}).invoke(jmf));
345 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
347 for (SequenceI sq : al.getSequences())
349 if (sq.getDatasetSequence() != null)
351 sq.getDatasetSequence().getAllPDBEntries().clear();
355 sq.getAllPDBEntries().clear();
358 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
360 } catch (ClassNotFoundException q)
363 StructureImportSettings.setShowSeqFeatures(true);
367 * Answers the first PDBChain found matching the given id, or null if none is
373 public PDBChain findChain(String id)
375 for (PDBChain chain : getChains())
377 if (chain.id.equals(id))
385 public void makeResidueList()
387 for (PDBChain chain : getChains())
389 chain.makeResidueList(visibleChainAnnotation);
393 public void makeCaBondList()
395 for (PDBChain chain : getChains())
397 chain.makeCaBondList();
401 public void setChargeColours()
403 for (PDBChain chain : getChains())
405 chain.setChargeColours();
409 public void setColours(jalview.schemes.ColourSchemeI cs)
411 for (PDBChain chain : getChains())
413 chain.setChainColours(cs);
417 public void setChainColours()
420 for (PDBChain chain : getChains())
422 chain.setChainColours(Color.getHSBColor(1.0f / i++, .4f, 1.0f));
426 public static boolean isRNA(SequenceI seq)
428 int length = seq.getLength();
429 for (int i = 0; i < length; i++)
431 char c = seq.getCharAt(i);
432 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))
441 * make a friendly ID string.
444 * @return truncated dataName to after last '/' and pruned .extension if
447 protected String safeName(String dataName)
450 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
452 dataName = dataName.substring(p + 1);
454 if (dataName.indexOf(".") > -1)
456 dataName = dataName.substring(0, dataName.lastIndexOf("."));
461 public String getId()
466 public void setId(String id)
471 public Vector<PDBChain> getChains()
476 public void setChains(Vector<PDBChain> chains)
478 this.chains = chains;
481 public Type getDbRefType()
486 public void setDbRefType(String dbRefType)
488 this.dbRefType = Type.getType(dbRefType);
491 public void setDbRefType(Type dbRefType)
493 this.dbRefType = dbRefType;
497 * Returns a descriptor for suitable feature display settings with
499 * <li>ResNums or insertions features visible</li>
500 * <li>insertions features coloured red</li>
501 * <li>ResNum features coloured by label</li>
502 * <li>Insertions displayed above (on top of) ResNums</li>
506 public FeatureSettingsModelI getFeatureColourScheme()
508 return new PDBFeatureSettings();
512 * Answers true if the structure file has a PDBId
516 public boolean isPPDBIdAvailable()
518 return pdbIdAvailable;
521 public void setPDBIdAvailable(boolean pdbIdAvailable)
523 this.pdbIdAvailable = pdbIdAvailable;