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.datamodel.Alignment;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.DBRefEntry;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.FileParse;
31 import jalview.util.MessageManager;
33 import java.awt.Color;
34 import java.io.IOException;
35 import java.lang.reflect.Constructor;
36 import java.util.ArrayList;
37 import java.util.Hashtable;
38 import java.util.List;
39 import java.util.Vector;
41 public class PDBfile extends jalview.io.AlignFile
43 private static String CALC_ID_PREFIX = "JalviewPDB";
45 public Vector<PDBChain> chains;
50 * set to true to add derived sequence annotations (temp factor read from
51 * file, or computed secondary structure) to the alignment
53 private boolean visibleChainAnnotation = false;
56 * Set true to predict secondary structure (using JMol for protein, Annotate3D
59 private boolean predictSecondaryStructure = true;
62 * Set true (with predictSecondaryStructure=true) to predict secondary
63 * structure using an external service (currently Annotate3D for RNA only)
65 private boolean externalSecondaryStructure = false;
67 public PDBfile(boolean addAlignmentAnnotations,
68 boolean predictSecondaryStructure, boolean externalSecStr)
71 this.visibleChainAnnotation = addAlignmentAnnotations;
72 this.predictSecondaryStructure = predictSecondaryStructure;
73 this.externalSecondaryStructure = externalSecStr;
76 public PDBfile(boolean addAlignmentAnnotations,
77 boolean predictSecondaryStructure, boolean externalSecStr,
78 String file, String protocol) throws IOException
80 super(false, file, protocol);
81 this.visibleChainAnnotation = addAlignmentAnnotations;
82 this.predictSecondaryStructure = predictSecondaryStructure;
83 this.externalSecondaryStructure = externalSecStr;
87 public PDBfile(boolean addAlignmentAnnotations,
88 boolean predictSecondaryStructure, boolean externalSecStr,
89 FileParse source) throws IOException
92 this.visibleChainAnnotation = addAlignmentAnnotations;
93 this.predictSecondaryStructure = predictSecondaryStructure;
94 this.externalSecondaryStructure = externalSecStr;
105 public void parse() throws IOException
107 // TODO set the filename sensibly - try using data source name.
108 id = safeName(getDataName());
110 chains = new Vector<PDBChain>();
111 List<SequenceI> rna = new ArrayList<SequenceI>();
112 List<SequenceI> prot = new ArrayList<SequenceI>();
115 boolean modelFlag = false;
116 boolean terFlag = false;
120 String atomnam = null;
123 while ((line = nextLine()) != null)
125 if (line.indexOf("HEADER") == 0)
127 if (line.length() > 62)
130 if (line.length() > 67)
132 tid = line.substring(62, 67).trim();
136 tid = line.substring(62).trim();
138 if (tid.length() > 0)
145 // Were we to do anything with SEQRES - we start it here
146 if (line.indexOf("SEQRES") == 0)
150 if (line.indexOf("MODEL") == 0)
155 if (line.indexOf("TER") == 0)
160 if (modelFlag && line.indexOf("ENDMDL") == 0)
164 if (line.indexOf("ATOM") == 0
165 || (line.indexOf("HETATM") == 0 && !terFlag))
169 // Jalview is only interested in CA bonds????
170 atomnam = line.substring(12, 15).trim();
171 if (!atomnam.equals("CA") && !atomnam.equals("P"))
176 Atom tmpatom = new Atom(line);
177 tmpchain = findChain(tmpatom.chain);
178 if (tmpchain != null)
180 if (tmpatom.resNumIns.trim().equals(lastID))
182 // phosphorylated protein - seen both CA and P..
185 tmpchain.atoms.addElement(tmpatom);
189 tmpchain = new PDBChain(id, tmpatom.chain);
190 chains.addElement(tmpchain);
191 tmpchain.atoms.addElement(tmpatom);
193 lastID = tmpatom.resNumIns.trim();
203 id = inFile.getName();
205 for (PDBChain chain : chains)
207 SequenceI chainseq = postProcessChain(chain);
217 if (predictSecondaryStructure)
219 predictSecondaryStructure(rna, prot);
221 } catch (OutOfMemoryError er)
223 System.out.println("OUT OF MEMORY LOADING PDB FILE");
224 throw new IOException(
226 .getString("exception.outofmemory_loading_pdb_file"));
227 } catch (NumberFormatException ex)
231 System.err.println("Couldn't read number from line:");
232 System.err.println(line);
239 * Predict secondary structure for RNA and/or protein sequences and add as
242 * @param rnaSequences
243 * @param proteinSequences
245 protected void predictSecondaryStructure(List<SequenceI> rnaSequences,
246 List<SequenceI> proteinSequences)
249 * Currently using Annotate3D for RNA, but only if the 'use external
250 * prediction' flag is set
252 if (externalSecondaryStructure && rnaSequences.size() > 0)
256 processPdbFileWithAnnotate3d(rnaSequences);
257 } catch (Exception x)
259 System.err.println("Exceptions when dealing with RNA in pdb file");
266 * Currently using JMol PDB parser for peptide
268 if (proteinSequences.size() > 0)
272 processPdbFileWithJmol(proteinSequences);
273 } catch (Exception x)
276 .println("Exceptions from Jmol when processing data in pdb file");
283 * Process a parsed chain to construct and return a Sequence, and add it to
284 * the list of sequences parsed.
289 protected SequenceI postProcessChain(PDBChain chain)
291 SequenceI pdbSequence = chain.sequence;
292 pdbSequence.setName(id + "|" + pdbSequence.getName());
293 PDBEntry entry = new PDBEntry();
295 entry.setType(PDBEntry.Type.PDB);
296 entry.setProperty(new Hashtable());
297 if (chain.id != null)
299 // entry.getProperty().put("CHAIN", chains.elementAt(i).id);
300 entry.setChainCode(String.valueOf(chain.id));
304 entry.setFile(inFile.getAbsolutePath());
308 // TODO: decide if we should dump the datasource to disk
309 entry.setFile(getDataName());
311 pdbSequence.addPDBId(entry);
313 DBRefEntry sourceDBRef = new DBRefEntry();
314 sourceDBRef.setAccessionId(id);
315 sourceDBRef.setSource("PDB");
316 sourceDBRef.setStartRes(pdbSequence.getStart());
317 sourceDBRef.setEndRes(pdbSequence.getEnd());
318 pdbSequence.setSourceDBRef(sourceDBRef);
319 // PDBChain objects maintain reference to dataset
320 SequenceI chainseq = pdbSequence.deriveSequence();
321 seqs.addElement(chainseq);
323 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
325 if (chainannot != null && visibleChainAnnotation)
327 for (int ai = 0; ai < chainannot.length; ai++)
329 chainannot[ai].visible = visibleChainAnnotation;
330 annotations.addElement(chainannot[ai]);
336 public static boolean isCalcIdHandled(String calcId)
338 return calcId != null && (CALC_ID_PREFIX.equals(calcId));
341 public static boolean isCalcIdForFile(AlignmentAnnotation alan,
344 return alan.getCalcId() != null
345 && CALC_ID_PREFIX.equals(alan.getCalcId())
346 && pdbFile.equals(alan.getProperty("PDBID"));
349 public static String relocateCalcId(String calcId,
350 Hashtable<String, String> alreadyLoadedPDB) throws Exception
352 int s = CALC_ID_PREFIX.length(), end = calcId
353 .indexOf(CALC_ID_PREFIX, s);
354 String between = calcId.substring(s, end - 1);
355 return CALC_ID_PREFIX + alreadyLoadedPDB.get(between) + ":"
356 + calcId.substring(end);
359 private void markCalcIds()
361 for (SequenceI sq : seqs)
363 if (sq.getAnnotation() != null)
365 for (AlignmentAnnotation aa : sq.getAnnotation())
367 String oldId = aa.getCalcId();
372 aa.setCalcId(CALC_ID_PREFIX);
373 aa.setProperty("PDBID", id);
374 aa.setProperty("oldCalcId", oldId);
380 private void processPdbFileWithJmol(List<SequenceI> prot)
385 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
388 final Constructor constructor = cl
389 .getConstructor(new Class[] { FileParse.class });
390 final Object[] args = new Object[] { new FileParse(getDataName(),
392 Object jmf = constructor.newInstance(args);
393 AlignmentI al = new Alignment((SequenceI[]) cl.getMethod(
394 "getSeqsAsArray", new Class[] {}).invoke(jmf));
395 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
397 for (SequenceI sq : al.getSequences())
399 if (sq.getDatasetSequence() != null)
401 sq.getDatasetSequence().getAllPDBEntries().clear();
405 sq.getAllPDBEntries().clear();
408 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
410 } catch (ClassNotFoundException q)
415 private void replaceAndUpdateChains(List<SequenceI> prot, AlignmentI al,
416 String pep, boolean b)
418 List<List<? extends Object>> replaced = AlignSeq
419 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
421 for (PDBChain ch : chains)
424 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
427 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
436 // set shadow entry for chains
437 ch.shadow = (SequenceI) replaced.get(1).get(p);
438 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
439 .getMappingFromS1(false);
444 private void processPdbFileWithAnnotate3d(List<SequenceI> rna)
447 // System.out.println("this is a PDB format and RNA sequence");
448 // note: we use reflection here so that the applet can compile and run
449 // without the HTTPClient bits and pieces needed for accessing Annotate3D
453 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
456 // TODO: use the PDB ID of the structure if one is available, to save
457 // bandwidth and avoid uploading the whole structure to the service
458 Object annotate3d = cl.getConstructor(new Class[] {}).newInstance(
460 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
461 new Class[] { FileParse.class }).invoke(annotate3d,
462 new Object[] { new FileParse(getDataName(), type) }));
463 for (SequenceI sq : al.getSequences())
465 if (sq.getDatasetSequence() != null)
467 if (sq.getDatasetSequence().getAllPDBEntries() != null)
469 sq.getDatasetSequence().getAllPDBEntries().clear();
474 if (sq.getAllPDBEntries() != null)
476 sq.getAllPDBEntries().clear();
480 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
482 } catch (ClassNotFoundException x)
484 // ignore classnotfounds - occurs in applet
490 * make a friendly ID string.
493 * @return truncated dataName to after last '/'
495 private String safeName(String dataName)
498 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
500 dataName = dataName.substring(p + 1);
505 public void makeResidueList()
507 for (int i = 0; i < chains.size(); i++)
509 chains.elementAt(i).makeResidueList(visibleChainAnnotation);
513 public void makeCaBondList()
515 for (int i = 0; i < chains.size(); i++)
517 chains.elementAt(i).makeCaBondList();
521 public PDBChain findChain(String id)
523 for (int i = 0; i < chains.size(); i++)
525 if (chains.elementAt(i).id.equals(id))
527 return chains.elementAt(i);
534 public void setChargeColours()
536 for (int i = 0; i < chains.size(); i++)
538 chains.elementAt(i).setChargeColours();
542 public void setColours(jalview.schemes.ColourSchemeI cs)
544 for (int i = 0; i < chains.size(); i++)
546 chains.elementAt(i).setChainColours(cs);
550 public void setChainColours()
552 for (int i = 0; i < chains.size(); i++)
554 // divide by zero --> infinity --> 255 ;-)
555 chains.elementAt(i).setChainColours(
556 Color.getHSBColor(1.0f / i, .4f, 1.0f));
560 public static boolean isRNA(SequenceI seq)
562 for (char c : seq.getSequence())
564 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))