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
48 public enum StructureFileType
53 private PDBEntry.Type dbRefType;
56 * set to true to add derived sequence annotations (temp factor read from
57 * file, or computed secondary structure) to the alignment
59 protected boolean visibleChainAnnotation = false;
62 * Set true to predict secondary structure (using JMol for protein, Annotate3D
65 protected boolean predictSecondaryStructure = false;
68 * Set true (with predictSecondaryStructure=true) to predict secondary
69 * structure using an external service (currently Annotate3D for RNA only)
71 protected boolean externalSecondaryStructure = false;
73 private Vector<PDBChain> chains;
75 private boolean pdbIdAvailable;
77 public StructureFile(String inFile, String type) throws IOException
82 public StructureFile(FileParse fp) throws IOException
87 public void addSettings(boolean addAlignmentAnnotations,
88 boolean predictSecondaryStructure, boolean externalSecStr)
90 this.visibleChainAnnotation = addAlignmentAnnotations;
91 this.predictSecondaryStructure = predictSecondaryStructure;
92 this.externalSecondaryStructure = externalSecStr;
95 public void xferSettings()
97 this.visibleChainAnnotation = StructureImportSettings
98 .isVisibleChainAnnotation();
99 this.predictSecondaryStructure = StructureImportSettings
100 .isProcessSecondaryStructure();
101 this.externalSecondaryStructure = StructureImportSettings
102 .isExternalSecondaryStructure();
106 public StructureFile(boolean parseImmediately, String dataObject,
107 String type) throws IOException
109 super(parseImmediately, dataObject, type);
112 public StructureFile(boolean a, FileParse fp) throws IOException
117 public StructureFile()
121 protected SequenceI postProcessChain(PDBChain chain)
123 SequenceI pdbSequence = chain.sequence;
124 pdbSequence.setName(getId() + "|" + pdbSequence.getName());
125 PDBEntry entry = new PDBEntry();
126 entry.setId(getId());
127 entry.setType(getStructureFileType());
128 if (chain.id != null)
130 entry.setChainCode(chain.id);
134 entry.setFile(inFile.getAbsolutePath());
138 entry.setFile(getDataName());
141 DBRefEntry sourceDBRef = new DBRefEntry();
142 sourceDBRef.setAccessionId(getId());
143 sourceDBRef.setSource(DBRefSource.PDB);
144 // TODO: specify version for 'PDB' database ref if it is read from a file.
145 // TODO: decide if jalview.io should be creating primary refs!
146 sourceDBRef.setVersion("");
147 pdbSequence.addPDBId(entry);
148 pdbSequence.addDBRef(sourceDBRef);
149 SequenceI chainseq = pdbSequence;
150 seqs.addElement(chainseq);
151 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
153 if (chainannot != null && visibleChainAnnotation)
155 for (int ai = 0; ai < chainannot.length; ai++)
157 chainannot[ai].visible = visibleChainAnnotation;
158 annotations.addElement(chainannot[ai]);
165 * filetype of structure file - default is PDB
167 String structureFileType = PDBEntry.Type.PDB.toString();
169 protected void setStructureFileType(String structureFileType)
171 this.structureFileType = structureFileType;
175 * filetype of last file processed
179 public String getStructureFileType()
181 return structureFileType;
184 @SuppressWarnings({ "unchecked", "rawtypes" })
185 protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
188 // System.out.println("this is a PDB format and RNA sequence");
189 // note: we use reflection here so that the applet can compile and run
190 // without the HTTPClient bits and pieces needed for accessing Annotate3D
194 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
197 // TODO: use the PDB ID of the structure if one is available, to save
198 // bandwidth and avoid uploading the whole structure to the service
199 Object annotate3d = cl.getConstructor(new Class[] {}).newInstance(
201 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
202 new Class[] { FileParse.class }).invoke(annotate3d,
203 new Object[] { new FileParse(getDataName(), type) }));
204 for (SequenceI sq : al.getSequences())
206 if (sq.getDatasetSequence() != null)
208 if (sq.getDatasetSequence().getAllPDBEntries() != null)
210 sq.getDatasetSequence().getAllPDBEntries().clear();
215 if (sq.getAllPDBEntries() != null)
217 sq.getAllPDBEntries().clear();
221 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
223 } catch (ClassNotFoundException x)
225 // ignore classnotfounds - occurs in applet
229 @SuppressWarnings("unchecked")
230 protected void replaceAndUpdateChains(List<SequenceI> prot,
231 AlignmentI al, String pep, boolean b)
233 List<List<? extends Object>> replaced = AlignSeq
234 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
236 for (PDBChain ch : getChains())
239 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
242 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
251 // set shadow entry for chains
252 ch.shadow = (SequenceI) replaced.get(1).get(p);
253 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
254 .getMappingFromS1(false);
260 * Predict secondary structure for RNA and/or protein sequences and add as
263 * @param rnaSequences
264 * @param proteinSequences
266 protected void addSecondaryStructure(List<SequenceI> rnaSequences,
267 List<SequenceI> proteinSequences)
270 * Currently using Annotate3D for RNA, but only if the 'use external
271 * prediction' flag is set
273 if (externalSecondaryStructure && rnaSequences.size() > 0)
277 processPdbFileWithAnnotate3d(rnaSequences);
278 } catch (Exception x)
280 System.err.println("Exceptions when dealing with RNA in pdb file");
287 * Currently using JMol PDB parser for peptide
289 if (proteinSequences.size() > 0)
293 processWithJmolParser(proteinSequences);
294 } catch (Exception x)
297 .println("Exceptions from Jmol when processing data in pdb file");
303 @SuppressWarnings({ "unchecked", "rawtypes" })
304 private void processWithJmolParser(List<SequenceI> prot) throws Exception
309 Class cl = Class.forName("jalview.ext.jmol.JmolParser");
312 final Constructor constructor = cl
313 .getConstructor(new Class[] { FileParse.class });
314 final Object[] args = new Object[] { new FileParse(getDataName(),
317 StructureImportSettings.setShowSeqFeatures(false);
318 StructureImportSettings.setVisibleChainAnnotation(false);
319 StructureImportSettings
320 .setProcessSecondaryStructure(predictSecondaryStructure);
321 StructureImportSettings
322 .setExternalSecondaryStructure(externalSecondaryStructure);
323 Object jmf = constructor.newInstance(args);
324 AlignmentI al = new Alignment((SequenceI[]) cl.getMethod(
325 "getSeqsAsArray", new Class[] {}).invoke(jmf));
326 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
328 for (SequenceI sq : al.getSequences())
330 if (sq.getDatasetSequence() != null)
332 sq.getDatasetSequence().getAllPDBEntries().clear();
336 sq.getAllPDBEntries().clear();
339 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
341 } catch (ClassNotFoundException q)
344 StructureImportSettings.setShowSeqFeatures(true);
347 public PDBChain findChain(String id) throws Exception
349 for (PDBChain chain : getChains())
351 if (chain.id.equals(id))
356 throw new Exception("PDB chain not Found!");
359 public void makeResidueList()
361 for (PDBChain chain : getChains())
363 chain.makeResidueList(visibleChainAnnotation);
367 public void makeCaBondList()
369 for (PDBChain chain : getChains())
371 chain.makeCaBondList();
375 public void setChargeColours()
377 for (PDBChain chain : getChains())
379 chain.setChargeColours();
383 public void setColours(jalview.schemes.ColourSchemeI cs)
385 for (PDBChain chain : getChains())
387 chain.setChainColours(cs);
391 public void setChainColours()
394 for (PDBChain chain : getChains())
396 chain.setChainColours(Color.getHSBColor(1.0f / i++, .4f, 1.0f));
400 public static boolean isRNA(SequenceI seq)
402 for (char c : seq.getSequence())
404 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))
413 * make a friendly ID string.
416 * @return truncated dataName to after last '/'
418 protected String safeName(String dataName)
421 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
423 dataName = dataName.substring(p + 1);
428 public String getId()
433 public void setId(String id)
438 public Vector<PDBChain> getChains()
443 public void setChains(Vector<PDBChain> chains)
445 this.chains = chains;
448 public Type getDbRefType()
453 public void setDbRefType(String dbRefType)
455 this.dbRefType = Type.getType(dbRefType);
458 public void setDbRefType(Type dbRefType)
460 this.dbRefType = dbRefType;
464 * Returns a descriptor for suitable feature display settings with
466 * <li>ResNums or insertions features visible</li>
467 * <li>insertions features coloured red</li>
468 * <li>ResNum features coloured by label</li>
469 * <li>Insertions displayed above (on top of) ResNums</li>
473 public FeatureSettingsModelI getFeatureColourScheme()
475 return new PDBFeatureSettings();
479 * Answers true if the structure file has a PDBId
483 public boolean isPPDBIdAvailable()
485 return pdbIdAvailable;
488 public void setPDBIdAvailable(boolean pdbIdAvailable)
490 this.pdbIdAvailable = pdbIdAvailable;
493 public static boolean isStructureFile(String fileType)
495 if (fileType == null)
499 for (StructureFileType sfType : StructureFileType.values())
501 if (sfType.name().equalsIgnoreCase(fileType))