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.DBRefSource;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.FileParse;
32 import jalview.util.MessageManager;
34 import java.awt.Color;
35 import java.io.IOException;
36 import java.lang.reflect.Constructor;
37 import java.util.ArrayList;
38 import java.util.Hashtable;
39 import java.util.List;
40 import java.util.Vector;
42 public class PDBfile extends jalview.io.AlignFile
44 private static String CALC_ID_PREFIX = "JalviewPDB";
46 public Vector<PDBChain> chains;
51 * set to true to add derived sequence annotations (temp factor read from
52 * file, or computed secondary structure) to the alignment
54 private boolean visibleChainAnnotation = false;
57 * Set true to predict secondary structure (using JMol for protein, Annotate3D
60 private boolean predictSecondaryStructure = true;
63 * Set true (with predictSecondaryStructure=true) to predict secondary
64 * structure using an external service (currently Annotate3D for RNA only)
66 private boolean externalSecondaryStructure = false;
68 public PDBfile(boolean addAlignmentAnnotations,
69 boolean predictSecondaryStructure, boolean externalSecStr)
72 this.visibleChainAnnotation = addAlignmentAnnotations;
73 this.predictSecondaryStructure = predictSecondaryStructure;
74 this.externalSecondaryStructure = externalSecStr;
77 public PDBfile(boolean addAlignmentAnnotations,
78 boolean predictSecondaryStructure, boolean externalSecStr,
79 String file, String protocol) throws IOException
81 super(false, file, protocol);
82 this.visibleChainAnnotation = addAlignmentAnnotations;
83 this.predictSecondaryStructure = predictSecondaryStructure;
84 this.externalSecondaryStructure = externalSecStr;
88 public PDBfile(boolean addAlignmentAnnotations,
89 boolean predictSecondaryStructure, boolean externalSecStr,
90 FileParse source) throws IOException
93 this.visibleChainAnnotation = addAlignmentAnnotations;
94 this.predictSecondaryStructure = predictSecondaryStructure;
95 this.externalSecondaryStructure = externalSecStr;
100 public String print()
106 public void parse() throws IOException
108 // TODO set the filename sensibly - try using data source name.
109 id = safeName(getDataName());
111 chains = new Vector<PDBChain>();
112 List<SequenceI> rna = new ArrayList<SequenceI>();
113 List<SequenceI> prot = new ArrayList<SequenceI>();
116 boolean modelFlag = false;
117 boolean terFlag = false;
121 String atomnam = null;
124 while ((line = nextLine()) != null)
126 if (line.indexOf("HEADER") == 0)
128 if (line.length() > 62)
131 if (line.length() > 67)
133 tid = line.substring(62, 67).trim();
137 tid = line.substring(62).trim();
139 if (tid.length() > 0)
146 // Were we to do anything with SEQRES - we start it here
147 if (line.indexOf("SEQRES") == 0)
151 if (line.indexOf("MODEL") == 0)
156 if (line.indexOf("TER") == 0)
161 if (modelFlag && line.indexOf("ENDMDL") == 0)
165 if (line.indexOf("ATOM") == 0
166 || (line.indexOf("HETATM") == 0 && !terFlag))
170 // Jalview is only interested in CA bonds????
171 atomnam = line.substring(12, 15).trim();
172 if (!atomnam.equals("CA") && !atomnam.equals("P"))
177 Atom tmpatom = new Atom(line);
178 tmpchain = findChain(tmpatom.chain);
179 if (tmpchain != null)
181 if (tmpatom.resNumIns.trim().equals(lastID))
183 // phosphorylated protein - seen both CA and P..
186 tmpchain.atoms.addElement(tmpatom);
190 tmpchain = new PDBChain(id, tmpatom.chain);
191 chains.addElement(tmpchain);
192 tmpchain.atoms.addElement(tmpatom);
194 lastID = tmpatom.resNumIns.trim();
204 id = inFile.getName();
206 for (PDBChain chain : chains)
208 SequenceI chainseq = postProcessChain(chain);
218 if (predictSecondaryStructure)
220 predictSecondaryStructure(rna, prot);
222 } catch (OutOfMemoryError er)
224 System.out.println("OUT OF MEMORY LOADING PDB FILE");
225 throw new IOException(
227 .getString("exception.outofmemory_loading_pdb_file"));
228 } catch (NumberFormatException ex)
232 System.err.println("Couldn't read number from line:");
233 System.err.println(line);
240 * Predict secondary structure for RNA and/or protein sequences and add as
243 * @param rnaSequences
244 * @param proteinSequences
246 protected void predictSecondaryStructure(List<SequenceI> rnaSequences,
247 List<SequenceI> proteinSequences)
250 * Currently using Annotate3D for RNA, but only if the 'use external
251 * prediction' flag is set
253 if (externalSecondaryStructure && rnaSequences.size() > 0)
257 processPdbFileWithAnnotate3d(rnaSequences);
258 } catch (Exception x)
260 System.err.println("Exceptions when dealing with RNA in pdb file");
267 * Currently using JMol PDB parser for peptide
269 if (proteinSequences.size() > 0)
273 processPdbFileWithJmol(proteinSequences);
274 } catch (Exception x)
277 .println("Exceptions from Jmol when processing data in pdb file");
284 * Process a parsed chain to construct and return a Sequence, and add it to
285 * the list of sequences parsed.
290 protected SequenceI postProcessChain(PDBChain chain)
292 SequenceI pdbSequence = chain.sequence;
293 pdbSequence.setName(id + "|" + pdbSequence.getName());
294 PDBEntry entry = new PDBEntry();
296 entry.setType(PDBEntry.Type.PDB);
297 entry.setProperty(new Hashtable());
298 if (chain.id != null)
300 // entry.getProperty().put("CHAIN", chains.elementAt(i).id);
301 entry.setChainCode(String.valueOf(chain.id));
305 entry.setFile(inFile.getAbsolutePath());
309 // TODO: decide if we should dump the datasource to disk
310 entry.setFile(getDataName());
312 pdbSequence.addPDBId(entry);
314 DBRefEntry sourceDBRef = new DBRefEntry();
315 sourceDBRef.setAccessionId(id);
316 sourceDBRef.setSource(DBRefSource.PDB);
317 sourceDBRef.setStartRes(pdbSequence.getStart());
318 sourceDBRef.setEndRes(pdbSequence.getEnd());
319 pdbSequence.setSourceDBRef(sourceDBRef);
320 // PDBChain objects maintain reference to dataset
321 SequenceI chainseq = pdbSequence.deriveSequence();
322 seqs.addElement(chainseq);
324 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
326 if (chainannot != null && visibleChainAnnotation)
328 for (int ai = 0; ai < chainannot.length; ai++)
330 chainannot[ai].visible = visibleChainAnnotation;
331 annotations.addElement(chainannot[ai]);
337 public static boolean isCalcIdHandled(String calcId)
339 return calcId != null && (CALC_ID_PREFIX.equals(calcId));
342 public static boolean isCalcIdForFile(AlignmentAnnotation alan,
345 return alan.getCalcId() != null
346 && CALC_ID_PREFIX.equals(alan.getCalcId())
347 && pdbFile.equals(alan.getProperty("PDBID"));
350 public static String relocateCalcId(String calcId,
351 Hashtable<String, String> alreadyLoadedPDB) throws Exception
353 int s = CALC_ID_PREFIX.length(), end = calcId
354 .indexOf(CALC_ID_PREFIX, s);
355 String between = calcId.substring(s, end - 1);
356 return CALC_ID_PREFIX + alreadyLoadedPDB.get(between) + ":"
357 + calcId.substring(end);
360 private void markCalcIds()
362 for (SequenceI sq : seqs)
364 if (sq.getAnnotation() != null)
366 for (AlignmentAnnotation aa : sq.getAnnotation())
368 String oldId = aa.getCalcId();
373 aa.setCalcId(CALC_ID_PREFIX);
374 aa.setProperty("PDBID", id);
375 aa.setProperty("oldCalcId", oldId);
381 private void processPdbFileWithJmol(List<SequenceI> prot)
386 Class cl = Class.forName("jalview.ext.jmol.PDBFileWithJmol");
389 final Constructor constructor = cl
390 .getConstructor(new Class[] { FileParse.class });
391 final Object[] args = new Object[] { new FileParse(getDataName(),
393 Object jmf = constructor.newInstance(args);
394 AlignmentI al = new Alignment((SequenceI[]) cl.getMethod(
395 "getSeqsAsArray", new Class[] {}).invoke(jmf));
396 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
398 for (SequenceI sq : al.getSequences())
400 if (sq.getDatasetSequence() != null)
402 sq.getDatasetSequence().getAllPDBEntries().clear();
406 sq.getAllPDBEntries().clear();
409 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
411 } catch (ClassNotFoundException q)
416 private void replaceAndUpdateChains(List<SequenceI> prot, AlignmentI al,
417 String pep, boolean b)
419 List<List<? extends Object>> replaced = AlignSeq
420 .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
422 for (PDBChain ch : chains)
425 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
428 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
437 // set shadow entry for chains
438 ch.shadow = (SequenceI) replaced.get(1).get(p);
439 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
440 .getMappingFromS1(false);
445 private void processPdbFileWithAnnotate3d(List<SequenceI> rna)
448 // System.out.println("this is a PDB format and RNA sequence");
449 // note: we use reflection here so that the applet can compile and run
450 // without the HTTPClient bits and pieces needed for accessing Annotate3D
454 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
457 // TODO: use the PDB ID of the structure if one is available, to save
458 // bandwidth and avoid uploading the whole structure to the service
459 Object annotate3d = cl.getConstructor(new Class[] {}).newInstance(
461 AlignmentI al = ((AlignmentI) cl.getMethod("getRNAMLFor",
462 new Class[] { FileParse.class }).invoke(annotate3d,
463 new Object[] { new FileParse(getDataName(), type) }));
464 for (SequenceI sq : al.getSequences())
466 if (sq.getDatasetSequence() != null)
468 if (sq.getDatasetSequence().getAllPDBEntries() != null)
470 sq.getDatasetSequence().getAllPDBEntries().clear();
475 if (sq.getAllPDBEntries() != null)
477 sq.getAllPDBEntries().clear();
481 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
483 } catch (ClassNotFoundException x)
485 // ignore classnotfounds - occurs in applet
491 * make a friendly ID string.
494 * @return truncated dataName to after last '/'
496 private String safeName(String dataName)
499 while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
501 dataName = dataName.substring(p + 1);
506 public void makeResidueList()
508 for (int i = 0; i < chains.size(); i++)
510 chains.elementAt(i).makeResidueList(visibleChainAnnotation);
514 public void makeCaBondList()
516 for (int i = 0; i < chains.size(); i++)
518 chains.elementAt(i).makeCaBondList();
522 public PDBChain findChain(String id)
524 for (int i = 0; i < chains.size(); i++)
526 if (chains.elementAt(i).id.equals(id))
528 return chains.elementAt(i);
535 public void setChargeColours()
537 for (int i = 0; i < chains.size(); i++)
539 chains.elementAt(i).setChargeColours();
543 public void setColours(jalview.schemes.ColourSchemeI cs)
545 for (int i = 0; i < chains.size(); i++)
547 chains.elementAt(i).setChainColours(cs);
551 public void setChainColours()
553 for (int i = 0; i < chains.size(); i++)
555 // divide by zero --> infinity --> 255 ;-)
556 chains.elementAt(i).setChainColours(
557 Color.getHSBColor(1.0f / i, .4f, 1.0f));
561 public static boolean isRNA(SequenceI seq)
563 for (char c : seq.getSequence())
565 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))