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(false, inFile, sourceType);
93 this.setTemperatureFactorType(tempfacType);
97 public StructureFile(FileParse fp) throws IOException
102 public void addSettings(boolean addAlignmentAnnotations,
103 boolean predictSecondaryStructure, boolean externalSecStr)
105 this.visibleChainAnnotation = addAlignmentAnnotations;
106 this.predictSecondaryStructure = predictSecondaryStructure;
107 this.externalSecondaryStructure = externalSecStr;
110 public void xferSettings()
112 this.visibleChainAnnotation = StructureImportSettings
113 .isVisibleChainAnnotation();
114 this.predictSecondaryStructure = StructureImportSettings
115 .isProcessSecondaryStructure();
116 this.externalSecondaryStructure = StructureImportSettings
117 .isExternalSecondaryStructure();
118 this.temperatureFactorType = StructureImportSettings
119 .getTemperatureFactorType();
122 public StructureFile(boolean parseImmediately, Object dataObject,
123 DataSourceType sourceType) throws IOException
125 super(parseImmediately, dataObject, sourceType);
128 public StructureFile(boolean a, FileParse fp) throws IOException
133 public StructureFile()
137 protected SequenceI postProcessChain(PDBChain chain)
139 SequenceI pdbSequence = chain.sequence;
140 pdbSequence.setName(getId() + "|" + pdbSequence.getName());
141 PDBEntry entry = new PDBEntry();
142 entry.setId(getId());
143 entry.setFakedPDBId(!isPPDBIdAvailable());
144 entry.setType(getStructureFileType());
145 if (chain.id != null)
147 entry.setChainCode(chain.id);
151 entry.setFile(inFile.getAbsolutePath());
155 entry.setFile(getDataName());
158 DBRefEntry sourceDBRef = new DBRefEntry();
159 sourceDBRef.setAccessionId(getId());
160 sourceDBRef.setSource(DBRefSource.PDB);
161 // TODO: specify version for 'PDB' database ref if it is read from a file.
162 // TODO: decide if jalview.io should be creating primary refs!
163 sourceDBRef.setVersion("");
164 pdbSequence.addPDBId(entry);
165 pdbSequence.addDBRef(sourceDBRef);
166 SequenceI chainseq = pdbSequence;
167 seqs.addElement(chainseq);
168 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
170 if (chainannot != null && visibleChainAnnotation)
172 for (int ai = 0; ai < chainannot.length; ai++)
174 chainannot[ai].visible = visibleChainAnnotation;
175 annotations.addElement(chainannot[ai]);
182 * filetype of structure file - default is PDB
184 String structureFileType = PDBEntry.Type.PDB.toString();
186 protected void setStructureFileType(String structureFileType)
188 this.structureFileType = structureFileType;
192 * filetype of last file processed
196 public String getStructureFileType()
198 return structureFileType;
201 @SuppressWarnings({ "unchecked", "rawtypes" })
202 protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
205 // System.out.println("this is a PDB format and RNA sequence");
206 // note: we use reflection here so that the applet can compile and run
207 // without the HTTPClient bits and pieces needed for accessing Annotate3D
211 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
214 // TODO: use the PDB ID of the structure if one is available, to save
215 // bandwidth and avoid uploading the whole structure to the service
216 Object annotate3d = cl.getConstructor(new Class[] {})
217 .newInstance(new Object[] {});
218 AlignmentI al = ((AlignmentI) cl
219 .getMethod("getRNAMLFor", new Class[]
221 .invoke(annotate3d, new Object[]
222 { new FileParse(getDataName(), dataSourceType) }));
223 for (SequenceI sq : al.getSequences())
225 if (sq.getDatasetSequence() != null)
227 if (sq.getDatasetSequence().getAllPDBEntries() != null)
229 sq.getDatasetSequence().getAllPDBEntries().clear();
234 if (sq.getAllPDBEntries() != null)
236 sq.getAllPDBEntries().clear();
240 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
242 } catch (ClassNotFoundException x)
244 // ignore classnotfounds - occurs in applet
248 @SuppressWarnings("unchecked")
249 protected void replaceAndUpdateChains(List<SequenceI> prot, AlignmentI al,
250 String pep, boolean b)
252 List<List<? extends Object>> replaced = AlignSeq
253 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
255 for (PDBChain ch : getChains())
258 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
261 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
270 // set shadow entry for chains
271 ch.shadow = (SequenceI) replaced.get(1).get(p);
272 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
273 .getMappingFromS1(false);
279 * Predict secondary structure for RNA and/or protein sequences and add as
282 * @param rnaSequences
283 * @param proteinSequences
285 protected void addSecondaryStructure(List<SequenceI> rnaSequences,
286 List<SequenceI> proteinSequences)
289 * Currently using Annotate3D for RNA, but only if the 'use external
290 * prediction' flag is set
292 if (externalSecondaryStructure && rnaSequences.size() > 0)
296 processPdbFileWithAnnotate3d(rnaSequences);
297 } catch (Exception x)
299 System.err.println("Exceptions when dealing with RNA in pdb file");
306 * Currently using JMol PDB parser for peptide
308 if (proteinSequences.size() > 0)
312 processWithJmolParser(proteinSequences);
313 } catch (Exception x)
316 "Exceptions from Jmol when processing data in pdb file");
322 @SuppressWarnings({ "unchecked", "rawtypes" })
323 private void processWithJmolParser(List<SequenceI> prot) throws Exception
328 Class cl = Class.forName("jalview.ext.jmol.JmolParser");
331 final Constructor constructor = cl
332 .getConstructor(new Class[]
333 { FileParse.class });
334 final Object[] args = new Object[] {
335 new FileParse(getDataName(), dataSourceType) };
337 StructureImportSettings.setShowSeqFeatures(false);
338 StructureImportSettings.setVisibleChainAnnotation(false);
339 StructureImportSettings
340 .setProcessSecondaryStructure(predictSecondaryStructure);
341 StructureImportSettings
342 .setExternalSecondaryStructure(externalSecondaryStructure);
343 StructureImportSettings
344 .setTemperatureFactorType(temperatureFactorType);
345 Object jmf = constructor.newInstance(args);
346 AlignmentI al = new Alignment((SequenceI[]) cl
347 .getMethod("getSeqsAsArray", new Class[] {}).invoke(jmf));
348 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
350 for (SequenceI sq : al.getSequences())
352 if (sq.getDatasetSequence() != null)
354 sq.getDatasetSequence().getAllPDBEntries().clear();
358 sq.getAllPDBEntries().clear();
361 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
363 } catch (ClassNotFoundException q)
366 StructureImportSettings.setShowSeqFeatures(true);
370 * Answers the first PDBChain found matching the given id, or null if none is
376 public PDBChain findChain(String id)
378 for (PDBChain chain : getChains())
380 if (chain.id.equals(id))
388 public void makeResidueList()
390 for (PDBChain chain : getChains())
392 chain.makeResidueList(visibleChainAnnotation);
396 public void makeCaBondList()
398 for (PDBChain chain : getChains())
400 chain.makeCaBondList();
404 public void setChargeColours()
406 for (PDBChain chain : getChains())
408 chain.setChargeColours();
412 public void setColours(jalview.schemes.ColourSchemeI cs)
414 for (PDBChain chain : getChains())
416 chain.setChainColours(cs);
420 public void setChainColours()
423 for (PDBChain chain : getChains())
425 chain.setChainColours(Color.getHSBColor(1.0f / i++, .4f, 1.0f));
429 public static boolean isRNA(SequenceI seq)
431 int length = seq.getLength();
432 for (int i = 0; i < length; i++)
434 char c = seq.getCharAt(i);
435 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))
444 * make a friendly ID string.
447 * @return truncated dataName to after last '/' and pruned .extension if
450 protected String safeName(String dataName)
453 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
455 dataName = dataName.substring(p + 1);
457 if (dataName.indexOf(".") > -1)
459 dataName = dataName.substring(0, dataName.lastIndexOf("."));
464 public String getId()
469 public void setId(String id)
474 public Vector<PDBChain> getChains()
479 public void setChains(Vector<PDBChain> chains)
481 this.chains = chains;
484 public Type getDbRefType()
489 public void setDbRefType(String dbRefType)
491 this.dbRefType = Type.getType(dbRefType);
494 public void setDbRefType(Type dbRefType)
496 this.dbRefType = dbRefType;
500 * Returns a descriptor for suitable feature display settings with
502 * <li>ResNums or insertions features visible</li>
503 * <li>insertions features coloured red</li>
504 * <li>ResNum features coloured by label</li>
505 * <li>Insertions displayed above (on top of) ResNums</li>
509 public FeatureSettingsModelI getFeatureColourScheme()
511 return new PDBFeatureSettings();
515 * Answers true if the structure file has a PDBId
519 public boolean isPPDBIdAvailable()
521 return pdbIdAvailable;
524 public void setPDBIdAvailable(boolean pdbIdAvailable)
526 this.pdbIdAvailable = pdbIdAvailable;