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 private String paeMatrix = null;
75 private boolean alphaFoldModel;
77 public void setPAEMatrix(String paeFilename)
79 paeMatrix = paeFilename;
82 public String getPAEMatrix()
87 public boolean hasPAEMatrix()
89 return paeMatrix != null;
92 public void setTemperatureFactorType(StructureImportSettings.TFType t)
94 this.temperatureFactorType = t;
97 public StructureImportSettings.TFType getTemperatureFactorType()
99 return temperatureFactorType;
102 public void setAlphafoldModel(boolean afm)
104 alphaFoldModel = afm;
107 public boolean isAlphafoldModel()
109 return alphaFoldModel;
112 public StructureFile(Object inFile, DataSourceType sourceType)
115 this(inFile, sourceType, null);
118 public StructureFile(Object inFile, DataSourceType sourceType,
119 StructureImportSettings.TFType tempfacType) throws IOException
121 super(false, inFile, sourceType);
122 this.setTemperatureFactorType(tempfacType);
126 public StructureFile(FileParse fp) throws IOException
131 public void addSettings(boolean addAlignmentAnnotations,
132 boolean predictSecondaryStructure, boolean externalSecStr)
134 this.visibleChainAnnotation = addAlignmentAnnotations;
135 this.predictSecondaryStructure = predictSecondaryStructure;
136 this.externalSecondaryStructure = externalSecStr;
139 public void xferSettings()
141 this.visibleChainAnnotation = StructureImportSettings
142 .isVisibleChainAnnotation();
143 this.predictSecondaryStructure = StructureImportSettings
144 .isProcessSecondaryStructure();
145 this.externalSecondaryStructure = StructureImportSettings
146 .isExternalSecondaryStructure();
147 this.temperatureFactorType = StructureImportSettings
148 .getTemperatureFactorType();
151 public StructureFile(boolean parseImmediately, Object dataObject,
152 DataSourceType sourceType) throws IOException
154 super(parseImmediately, dataObject, sourceType);
157 public StructureFile(boolean a, FileParse fp) throws IOException
162 public StructureFile()
166 protected SequenceI postProcessChain(PDBChain chain)
168 SequenceI pdbSequence = chain.sequence;
169 pdbSequence.setName(getId() + "|" + pdbSequence.getName());
170 PDBEntry entry = new PDBEntry();
171 entry.setId(getId());
172 entry.setFakedPDBId(!isPPDBIdAvailable());
173 entry.setType(getStructureFileType());
174 if (chain.id != null)
176 entry.setChainCode(chain.id);
180 entry.setFile(inFile.getAbsolutePath());
184 entry.setFile(getDataName());
187 DBRefEntry sourceDBRef = new DBRefEntry();
188 sourceDBRef.setAccessionId(getId());
189 sourceDBRef.setSource(DBRefSource.PDB);
190 // TODO: specify version for 'PDB' database ref if it is read from a file.
191 // TODO: decide if jalview.io should be creating primary refs!
192 sourceDBRef.setVersion("");
193 pdbSequence.addPDBId(entry);
194 pdbSequence.addDBRef(sourceDBRef);
195 SequenceI chainseq = pdbSequence;
196 seqs.addElement(chainseq);
197 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
199 if (chainannot != null && visibleChainAnnotation)
201 for (int ai = 0; ai < chainannot.length; ai++)
203 chainannot[ai].visible = visibleChainAnnotation;
204 annotations.addElement(chainannot[ai]);
211 * filetype of structure file - default is PDB
213 String structureFileType = PDBEntry.Type.PDB.toString();
215 protected void setStructureFileType(String structureFileType)
217 this.structureFileType = structureFileType;
221 * filetype of last file processed
225 public String getStructureFileType()
227 return structureFileType;
230 @SuppressWarnings({ "unchecked", "rawtypes" })
231 protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
234 // System.out.println("this is a PDB format and RNA sequence");
235 // note: we use reflection here so that the applet can compile and run
236 // without the HTTPClient bits and pieces needed for accessing Annotate3D
240 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
243 // TODO: use the PDB ID of the structure if one is available, to save
244 // bandwidth and avoid uploading the whole structure to the service
245 Object annotate3d = cl.getConstructor(new Class[] {})
246 .newInstance(new Object[] {});
247 AlignmentI al = ((AlignmentI) cl
248 .getMethod("getRNAMLFor", new Class[]
250 .invoke(annotate3d, new Object[]
251 { new FileParse(getDataName(), dataSourceType) }));
252 for (SequenceI sq : al.getSequences())
254 if (sq.getDatasetSequence() != null)
256 if (sq.getDatasetSequence().getAllPDBEntries() != null)
258 sq.getDatasetSequence().getAllPDBEntries().clear();
263 if (sq.getAllPDBEntries() != null)
265 sq.getAllPDBEntries().clear();
269 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
271 } catch (ClassNotFoundException x)
273 // ignore classnotfounds - occurs in applet
277 @SuppressWarnings("unchecked")
278 protected void replaceAndUpdateChains(List<SequenceI> prot, AlignmentI al,
279 String pep, boolean b)
281 List<List<? extends Object>> replaced = AlignSeq
282 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
284 for (PDBChain ch : getChains())
287 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
290 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
299 // set shadow entry for chains
300 ch.shadow = (SequenceI) replaced.get(1).get(p);
301 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
302 .getMappingFromS1(false);
308 * Predict secondary structure for RNA and/or protein sequences and add as
311 * @param rnaSequences
312 * @param proteinSequences
314 protected void addSecondaryStructure(List<SequenceI> rnaSequences,
315 List<SequenceI> proteinSequences)
318 * Currently using Annotate3D for RNA, but only if the 'use external
319 * prediction' flag is set
321 if (externalSecondaryStructure && rnaSequences.size() > 0)
325 processPdbFileWithAnnotate3d(rnaSequences);
326 } catch (Exception x)
328 System.err.println("Exceptions when dealing with RNA in pdb file");
335 * Currently using JMol PDB parser for peptide
337 if (proteinSequences.size() > 0)
341 processWithJmolParser(proteinSequences);
342 } catch (Exception x)
345 "Exceptions from Jmol when processing data in pdb file");
351 @SuppressWarnings({ "unchecked", "rawtypes" })
352 private void processWithJmolParser(List<SequenceI> prot) throws Exception
357 Class cl = Class.forName("jalview.ext.jmol.JmolParser");
360 final Constructor constructor = cl
361 .getConstructor(new Class[]
362 { FileParse.class });
363 final Object[] args = new Object[] {
364 new FileParse(getDataName(), dataSourceType) };
366 StructureImportSettings.setShowSeqFeatures(false);
367 StructureImportSettings.setVisibleChainAnnotation(false);
368 StructureImportSettings
369 .setProcessSecondaryStructure(predictSecondaryStructure);
370 StructureImportSettings
371 .setExternalSecondaryStructure(externalSecondaryStructure);
372 StructureImportSettings
373 .setTemperatureFactorType(temperatureFactorType);
374 Object jmf = constructor.newInstance(args);
375 AlignmentI al = new Alignment((SequenceI[]) cl
376 .getMethod("getSeqsAsArray", new Class[] {}).invoke(jmf));
377 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
379 for (SequenceI sq : al.getSequences())
381 if (sq.getDatasetSequence() != null)
383 sq.getDatasetSequence().getAllPDBEntries().clear();
387 sq.getAllPDBEntries().clear();
390 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
392 } catch (ClassNotFoundException q)
395 StructureImportSettings.setShowSeqFeatures(true);
399 * Answers the first PDBChain found matching the given id, or null if none is
405 public PDBChain findChain(String id)
407 for (PDBChain chain : getChains())
409 if (chain.id.equals(id))
417 public void makeResidueList()
419 for (PDBChain chain : getChains())
421 chain.makeResidueList(visibleChainAnnotation);
425 public void makeCaBondList()
427 for (PDBChain chain : getChains())
429 chain.makeCaBondList();
433 public void setChargeColours()
435 for (PDBChain chain : getChains())
437 chain.setChargeColours();
441 public void setColours(jalview.schemes.ColourSchemeI cs)
443 for (PDBChain chain : getChains())
445 chain.setChainColours(cs);
449 public void setChainColours()
452 for (PDBChain chain : getChains())
454 chain.setChainColours(Color.getHSBColor(1.0f / i++, .4f, 1.0f));
458 public static boolean isRNA(SequenceI seq)
460 int length = seq.getLength();
461 for (int i = 0; i < length; i++)
463 char c = seq.getCharAt(i);
464 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))
473 * make a friendly ID string.
476 * @return truncated dataName to after last '/' and pruned .extension if
479 protected String safeName(String dataName)
482 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
484 dataName = dataName.substring(p + 1);
486 if (dataName.indexOf(".") > -1)
488 dataName = dataName.substring(0, dataName.lastIndexOf("."));
493 public String getId()
498 public void setId(String id)
503 public Vector<PDBChain> getChains()
508 public void setChains(Vector<PDBChain> chains)
510 this.chains = chains;
513 public Type getDbRefType()
518 public void setDbRefType(String dbRefType)
520 this.dbRefType = Type.getType(dbRefType);
523 public void setDbRefType(Type dbRefType)
525 this.dbRefType = dbRefType;
529 * Returns a descriptor for suitable feature display settings with
531 * <li>ResNums or insertions features visible</li>
532 * <li>insertions features coloured red</li>
533 * <li>ResNum features coloured by label</li>
534 * <li>Insertions displayed above (on top of) ResNums</li>
538 public FeatureSettingsModelI getFeatureColourScheme()
540 return new PDBFeatureSettings();
544 * Answers true if the structure file has a PDBId
548 public boolean isPPDBIdAvailable()
550 return pdbIdAvailable;
553 public void setPDBIdAvailable(boolean pdbIdAvailable)
555 this.pdbIdAvailable = pdbIdAvailable;