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.AlignmentAnnotation.TFType;
34 import jalview.datamodel.AlignmentI;
35 import jalview.datamodel.DBRefEntry;
36 import jalview.datamodel.DBRefSource;
37 import jalview.datamodel.PDBEntry;
38 import jalview.datamodel.PDBEntry.Type;
39 import jalview.datamodel.SequenceI;
40 import jalview.structure.StructureImportSettings;
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 public StructureFile(Object inFile, DataSourceType sourceType)
74 this(inFile, sourceType, null);
77 public StructureFile(Object inFile, DataSourceType sourceType,
78 AlignmentAnnotation.TFType tempfacType) throws IOException
80 super(inFile, sourceType, tempfacType);
83 public StructureFile(FileParse fp) throws IOException
88 public void addSettings(boolean addAlignmentAnnotations,
89 boolean predictSecondaryStructure, boolean externalSecStr)
91 this.visibleChainAnnotation = addAlignmentAnnotations;
92 this.predictSecondaryStructure = predictSecondaryStructure;
93 this.externalSecondaryStructure = externalSecStr;
96 public void xferSettings()
98 this.visibleChainAnnotation = StructureImportSettings
99 .isVisibleChainAnnotation();
100 this.predictSecondaryStructure = StructureImportSettings
101 .isProcessSecondaryStructure();
102 this.externalSecondaryStructure = StructureImportSettings
103 .isExternalSecondaryStructure();
107 public StructureFile(boolean parseImmediately, Object dataObject,
108 DataSourceType sourceType) throws IOException
110 super(parseImmediately, dataObject, sourceType);
113 public StructureFile(boolean a, FileParse fp) throws IOException
118 public StructureFile()
122 protected SequenceI postProcessChain(PDBChain chain)
124 SequenceI pdbSequence = chain.sequence;
125 pdbSequence.setName(getId() + "|" + pdbSequence.getName());
126 PDBEntry entry = new PDBEntry();
127 entry.setId(getId());
128 entry.setFakedPDBId(!isPPDBIdAvailable());
129 entry.setType(getStructureFileType());
130 if (chain.id != null)
132 entry.setChainCode(chain.id);
136 entry.setFile(inFile.getAbsolutePath());
140 entry.setFile(getDataName());
143 DBRefEntry sourceDBRef = new DBRefEntry();
144 sourceDBRef.setAccessionId(getId());
145 sourceDBRef.setSource(DBRefSource.PDB);
146 // TODO: specify version for 'PDB' database ref if it is read from a file.
147 // TODO: decide if jalview.io should be creating primary refs!
148 sourceDBRef.setVersion("");
149 pdbSequence.addPDBId(entry);
150 pdbSequence.addDBRef(sourceDBRef);
151 SequenceI chainseq = pdbSequence;
152 seqs.addElement(chainseq);
153 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
155 if (chainannot != null && visibleChainAnnotation)
157 for (int ai = 0; ai < chainannot.length; ai++)
159 chainannot[ai].visible = visibleChainAnnotation;
160 annotations.addElement(chainannot[ai]);
167 * filetype of structure file - default is PDB
169 String structureFileType = PDBEntry.Type.PDB.toString();
171 protected void setStructureFileType(String structureFileType)
173 this.structureFileType = structureFileType;
177 * filetype of last file processed
181 public String getStructureFileType()
183 return structureFileType;
186 @SuppressWarnings({ "unchecked", "rawtypes" })
187 protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
190 // System.out.println("this is a PDB format and RNA sequence");
191 // note: we use reflection here so that the applet can compile and run
192 // without the HTTPClient bits and pieces needed for accessing Annotate3D
196 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
199 // TODO: use the PDB ID of the structure if one is available, to save
200 // bandwidth and avoid uploading the whole structure to the service
201 Object annotate3d = cl.getConstructor(new Class[] {})
202 .newInstance(new Object[] {});
203 AlignmentI al = ((AlignmentI) cl
204 .getMethod("getRNAMLFor", new Class[]
206 .invoke(annotate3d, new Object[]
207 { new FileParse(getDataName(), dataSourceType) }));
208 for (SequenceI sq : al.getSequences())
210 if (sq.getDatasetSequence() != null)
212 if (sq.getDatasetSequence().getAllPDBEntries() != null)
214 sq.getDatasetSequence().getAllPDBEntries().clear();
219 if (sq.getAllPDBEntries() != null)
221 sq.getAllPDBEntries().clear();
225 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
227 } catch (ClassNotFoundException x)
229 // ignore classnotfounds - occurs in applet
233 @SuppressWarnings("unchecked")
234 protected void replaceAndUpdateChains(List<SequenceI> prot, AlignmentI al,
235 String pep, boolean b)
237 List<List<? extends Object>> replaced = AlignSeq
238 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
240 for (PDBChain ch : getChains())
243 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
246 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
255 // set shadow entry for chains
256 ch.shadow = (SequenceI) replaced.get(1).get(p);
257 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
258 .getMappingFromS1(false);
264 * Predict secondary structure for RNA and/or protein sequences and add as
267 * @param rnaSequences
268 * @param proteinSequences
270 protected void addSecondaryStructure(List<SequenceI> rnaSequences,
271 List<SequenceI> proteinSequences)
274 * Currently using Annotate3D for RNA, but only if the 'use external
275 * prediction' flag is set
277 if (externalSecondaryStructure && rnaSequences.size() > 0)
281 processPdbFileWithAnnotate3d(rnaSequences);
282 } catch (Exception x)
284 System.err.println("Exceptions when dealing with RNA in pdb file");
291 * Currently using JMol PDB parser for peptide
293 if (proteinSequences.size() > 0)
297 processWithJmolParser(proteinSequences);
298 } catch (Exception x)
301 "Exceptions from Jmol when processing data in pdb file");
307 @SuppressWarnings({ "unchecked", "rawtypes" })
308 private void processWithJmolParser(List<SequenceI> prot) throws Exception
313 Class cl = Class.forName("jalview.ext.jmol.JmolParser");
316 final Constructor constructor = cl
317 .getConstructor(new Class[]
318 { FileParse.class });
319 final Object[] args = new Object[] {
320 new FileParse(getDataName(), dataSourceType) };
322 StructureImportSettings.setShowSeqFeatures(false);
323 StructureImportSettings.setVisibleChainAnnotation(false);
324 StructureImportSettings
325 .setProcessSecondaryStructure(predictSecondaryStructure);
326 StructureImportSettings
327 .setExternalSecondaryStructure(externalSecondaryStructure);
328 Object jmf = constructor.newInstance(args);
329 AlignmentI al = new Alignment((SequenceI[]) cl
330 .getMethod("getSeqsAsArray", new Class[] {}).invoke(jmf));
331 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
333 for (SequenceI sq : al.getSequences())
335 if (sq.getDatasetSequence() != null)
337 sq.getDatasetSequence().getAllPDBEntries().clear();
341 sq.getAllPDBEntries().clear();
344 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
346 } catch (ClassNotFoundException q)
349 StructureImportSettings.setShowSeqFeatures(true);
353 * Answers the first PDBChain found matching the given id, or null if none is
359 public PDBChain findChain(String id)
361 for (PDBChain chain : getChains())
363 if (chain.id.equals(id))
371 public void makeResidueList()
373 for (PDBChain chain : getChains())
375 chain.makeResidueList(visibleChainAnnotation);
379 public void makeCaBondList()
381 for (PDBChain chain : getChains())
383 chain.makeCaBondList();
387 public void setChargeColours()
389 for (PDBChain chain : getChains())
391 chain.setChargeColours();
395 public void setColours(jalview.schemes.ColourSchemeI cs)
397 for (PDBChain chain : getChains())
399 chain.setChainColours(cs);
403 public void setChainColours()
406 for (PDBChain chain : getChains())
408 chain.setChainColours(Color.getHSBColor(1.0f / i++, .4f, 1.0f));
412 public static boolean isRNA(SequenceI seq)
414 int length = seq.getLength();
415 for (int i = 0; i < length; i++)
417 char c = seq.getCharAt(i);
418 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))
427 * make a friendly ID string.
430 * @return truncated dataName to after last '/' and pruned .extension if
433 protected String safeName(String dataName)
436 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
438 dataName = dataName.substring(p + 1);
440 if (dataName.indexOf(".") > -1)
442 dataName = dataName.substring(0, dataName.lastIndexOf("."));
447 public String getId()
452 public void setId(String id)
457 public Vector<PDBChain> getChains()
462 public void setChains(Vector<PDBChain> chains)
464 this.chains = chains;
467 public Type getDbRefType()
472 public void setDbRefType(String dbRefType)
474 this.dbRefType = Type.getType(dbRefType);
477 public void setDbRefType(Type dbRefType)
479 this.dbRefType = dbRefType;
483 * Returns a descriptor for suitable feature display settings with
485 * <li>ResNums or insertions features visible</li>
486 * <li>insertions features coloured red</li>
487 * <li>ResNum features coloured by label</li>
488 * <li>Insertions displayed above (on top of) ResNums</li>
492 public FeatureSettingsModelI getFeatureColourScheme()
494 return new PDBFeatureSettings();
498 * Answers true if the structure file has a PDBId
502 public boolean isPPDBIdAvailable()
504 return pdbIdAvailable;
507 public void setPDBIdAvailable(boolean pdbIdAvailable)
509 this.pdbIdAvailable = pdbIdAvailable;