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, DataSourceType sourceType)
80 super(inFile, sourceType);
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, String 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.setType(getStructureFileType());
129 if (chain.id != null)
131 entry.setChainCode(chain.id);
135 entry.setFile(inFile.getAbsolutePath());
139 entry.setFile(getDataName());
142 DBRefEntry sourceDBRef = new DBRefEntry();
143 sourceDBRef.setAccessionId(getId());
144 sourceDBRef.setSource(DBRefSource.PDB);
145 // TODO: specify version for 'PDB' database ref if it is read from a file.
146 // TODO: decide if jalview.io should be creating primary refs!
147 sourceDBRef.setVersion("");
148 pdbSequence.addPDBId(entry);
149 pdbSequence.addDBRef(sourceDBRef);
150 SequenceI chainseq = pdbSequence;
151 seqs.addElement(chainseq);
152 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
154 if (chainannot != null && visibleChainAnnotation)
156 for (int ai = 0; ai < chainannot.length; ai++)
158 chainannot[ai].visible = visibleChainAnnotation;
159 annotations.addElement(chainannot[ai]);
166 * filetype of structure file - default is PDB
168 String structureFileType = PDBEntry.Type.PDB.toString();
170 protected void setStructureFileType(String structureFileType)
172 this.structureFileType = structureFileType;
176 * filetype of last file processed
180 public String getStructureFileType()
182 return structureFileType;
185 @SuppressWarnings({ "unchecked", "rawtypes" })
186 protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
189 // System.out.println("this is a PDB format and RNA sequence");
190 // note: we use reflection here so that the applet can compile and run
191 // without the HTTPClient bits and pieces needed for accessing Annotate3D
195 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
198 // TODO: use the PDB ID of the structure if one is available, to save
199 // bandwidth and avoid uploading the whole structure to the service
200 Object annotate3d = cl.getConstructor(new Class[] {}).newInstance(
202 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
203 new Class[] { FileParse.class }).invoke(annotate3d,
204 new Object[] { new FileParse(getDataName(), dataSourceType) }));
205 for (SequenceI sq : al.getSequences())
207 if (sq.getDatasetSequence() != null)
209 if (sq.getDatasetSequence().getAllPDBEntries() != null)
211 sq.getDatasetSequence().getAllPDBEntries().clear();
216 if (sq.getAllPDBEntries() != null)
218 sq.getAllPDBEntries().clear();
222 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
224 } catch (ClassNotFoundException x)
226 // ignore classnotfounds - occurs in applet
230 @SuppressWarnings("unchecked")
231 protected void replaceAndUpdateChains(List<SequenceI> prot,
232 AlignmentI al, String pep, boolean b)
234 List<List<? extends Object>> replaced = AlignSeq
235 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
237 for (PDBChain ch : getChains())
240 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
243 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
252 // set shadow entry for chains
253 ch.shadow = (SequenceI) replaced.get(1).get(p);
254 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
255 .getMappingFromS1(false);
261 * Predict secondary structure for RNA and/or protein sequences and add as
264 * @param rnaSequences
265 * @param proteinSequences
267 protected void addSecondaryStructure(List<SequenceI> rnaSequences,
268 List<SequenceI> proteinSequences)
271 * Currently using Annotate3D for RNA, but only if the 'use external
272 * prediction' flag is set
274 if (externalSecondaryStructure && rnaSequences.size() > 0)
278 processPdbFileWithAnnotate3d(rnaSequences);
279 } catch (Exception x)
281 System.err.println("Exceptions when dealing with RNA in pdb file");
288 * Currently using JMol PDB parser for peptide
290 if (proteinSequences.size() > 0)
294 processWithJmolParser(proteinSequences);
295 } catch (Exception x)
298 .println("Exceptions from Jmol when processing data in pdb file");
304 @SuppressWarnings({ "unchecked", "rawtypes" })
305 private void processWithJmolParser(List<SequenceI> prot) throws Exception
310 Class cl = Class.forName("jalview.ext.jmol.JmolParser");
313 final Constructor constructor = cl.getConstructor(new Class[] {FileParse.class });
314 final Object[] args = new Object[] { new FileParse(getDataName(), dataSourceType) };
316 StructureImportSettings.setShowSeqFeatures(false);
317 StructureImportSettings.setVisibleChainAnnotation(false);
318 StructureImportSettings
319 .setProcessSecondaryStructure(predictSecondaryStructure);
320 StructureImportSettings
321 .setExternalSecondaryStructure(externalSecondaryStructure);
322 Object jmf = constructor.newInstance(args);
323 AlignmentI al = new Alignment((SequenceI[]) cl.getMethod(
324 "getSeqsAsArray", new Class[] {}).invoke(jmf));
325 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
327 for (SequenceI sq : al.getSequences())
329 if (sq.getDatasetSequence() != null)
331 sq.getDatasetSequence().getAllPDBEntries().clear();
335 sq.getAllPDBEntries().clear();
338 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
340 } catch (ClassNotFoundException q)
343 StructureImportSettings.setShowSeqFeatures(true);
346 public PDBChain findChain(String id) throws Exception
348 for (PDBChain chain : getChains())
350 if (chain.id.equals(id))
355 throw new Exception("PDB chain not Found!");
358 public void makeResidueList()
360 for (PDBChain chain : getChains())
362 chain.makeResidueList(visibleChainAnnotation);
366 public void makeCaBondList()
368 for (PDBChain chain : getChains())
370 chain.makeCaBondList();
374 public void setChargeColours()
376 for (PDBChain chain : getChains())
378 chain.setChargeColours();
382 public void setColours(jalview.schemes.ColourSchemeI cs)
384 for (PDBChain chain : getChains())
386 chain.setChainColours(cs);
390 public void setChainColours()
393 for (PDBChain chain : getChains())
395 chain.setChainColours(Color.getHSBColor(1.0f / i++, .4f, 1.0f));
399 public static boolean isRNA(SequenceI seq)
401 for (char c : seq.getSequence())
403 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))
412 * make a friendly ID string.
415 * @return truncated dataName to after last '/'
417 protected String safeName(String dataName)
420 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
422 dataName = dataName.substring(p + 1);
427 public String getId()
432 public void setId(String id)
437 public Vector<PDBChain> getChains()
442 public void setChains(Vector<PDBChain> chains)
444 this.chains = chains;
447 public Type getDbRefType()
452 public void setDbRefType(String dbRefType)
454 this.dbRefType = Type.getType(dbRefType);
457 public void setDbRefType(Type dbRefType)
459 this.dbRefType = dbRefType;
463 * Returns a descriptor for suitable feature display settings with
465 * <li>ResNums or insertions features visible</li>
466 * <li>insertions features coloured red</li>
467 * <li>ResNum features coloured by label</li>
468 * <li>Insertions displayed above (on top of) ResNums</li>
472 public FeatureSettingsModelI getFeatureColourScheme()
474 return new PDBFeatureSettings();
478 * Answers true if the structure file has a PDBId
482 public boolean isPPDBIdAvailable()
484 return pdbIdAvailable;
487 public void setPDBIdAvailable(boolean pdbIdAvailable)
489 this.pdbIdAvailable = pdbIdAvailable;
492 public static boolean isStructureFile(String fileType)
494 if (fileType == null)
498 for (StructureFileType sfType : StructureFileType.values())
500 if (sfType.name().equalsIgnoreCase(fileType))