Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.io

File StructureFile.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

38
125
32
1
504
362
62
0.5
3.91
32
1.94

Classes

Class Line # Actions
StructureFile 43 125 62
0.7333333573.3%
 

Contributing tests

This file is covered by 23 tests. .

Source view

1    /*
2    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3    * Copyright (C) $$Year-Rel$$ The Jalview Authors
4    *
5    * This file is part of Jalview.
6    *
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.
11    *
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.
16    *
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.
20    */
21    package jalview.io;
22   
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;
34   
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;
40   
41    import mc_view.PDBChain;
42   
 
43    public abstract class StructureFile extends AlignFile
44    {
45    private String id;
46   
47    private PDBEntry.Type dbRefType;
48   
49    /**
50    * set to true to add derived sequence annotations (temp factor read from
51    * file, or computed secondary structure) to the alignment
52    */
53    protected boolean visibleChainAnnotation = false;
54   
55    /**
56    * Set true to predict secondary structure (using JMol for protein, Annotate3D
57    * for RNA)
58    */
59    protected boolean predictSecondaryStructure = false;
60   
61    /**
62    * Set true (with predictSecondaryStructure=true) to predict secondary
63    * structure using an external service (currently Annotate3D for RNA only)
64    */
65    protected boolean externalSecondaryStructure = false;
66   
67    private Vector<PDBChain> chains;
68   
69    private boolean pdbIdAvailable;
70   
 
71  17 toggle public StructureFile(Object inFile, DataSourceType sourceType)
72    throws IOException
73    {
74  17 super(inFile, sourceType);
75    }
76   
 
77  5 toggle public StructureFile(FileParse fp) throws IOException
78    {
79  5 super(fp);
80    }
81   
 
82  70 toggle public void addSettings(boolean addAlignmentAnnotations,
83    boolean predictSecondaryStructure, boolean externalSecStr)
84    {
85  70 this.visibleChainAnnotation = addAlignmentAnnotations;
86  70 this.predictSecondaryStructure = predictSecondaryStructure;
87  70 this.externalSecondaryStructure = externalSecStr;
88    }
89   
 
90  79 toggle public void xferSettings()
91    {
92  79 this.visibleChainAnnotation = StructureImportSettings
93    .isVisibleChainAnnotation();
94  79 this.predictSecondaryStructure = StructureImportSettings
95    .isProcessSecondaryStructure();
96  79 this.externalSecondaryStructure = StructureImportSettings
97    .isExternalSecondaryStructure();
98   
99    }
100   
 
101  70 toggle public StructureFile(boolean parseImmediately, Object dataObject,
102    DataSourceType sourceType) throws IOException
103    {
104  70 super(parseImmediately, dataObject, sourceType);
105    }
106   
 
107  0 toggle public StructureFile(boolean a, FileParse fp) throws IOException
108    {
109  0 super(a, fp);
110    }
111   
 
112  1 toggle public StructureFile()
113    {
114    }
115   
 
116  163 toggle protected SequenceI postProcessChain(PDBChain chain)
117    {
118  163 SequenceI pdbSequence = chain.sequence;
119  163 pdbSequence.setName(getId() + "|" + pdbSequence.getName());
120  163 PDBEntry entry = new PDBEntry();
121  163 entry.setId(getId());
122  163 entry.setType(getStructureFileType());
123  163 if (chain.id != null)
124    {
125  163 entry.setChainCode(chain.id);
126    }
127  163 if (inFile != null)
128    {
129  139 entry.setFile(inFile.getAbsolutePath());
130    }
131    else
132    {
133  24 entry.setFile(getDataName());
134    }
135   
136  163 DBRefEntry sourceDBRef = new DBRefEntry();
137  163 sourceDBRef.setAccessionId(getId());
138  163 sourceDBRef.setSource(DBRefSource.PDB);
139    // TODO: specify version for 'PDB' database ref if it is read from a file.
140    // TODO: decide if jalview.io should be creating primary refs!
141  163 sourceDBRef.setVersion("");
142  163 pdbSequence.addPDBId(entry);
143  163 pdbSequence.addDBRef(sourceDBRef);
144  163 SequenceI chainseq = pdbSequence;
145  163 seqs.addElement(chainseq);
146  163 AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
147   
148  163 if (chainannot != null && visibleChainAnnotation)
149    {
150  188 for (int ai = 0; ai < chainannot.length; ai++)
151    {
152  94 chainannot[ai].visible = visibleChainAnnotation;
153  94 annotations.addElement(chainannot[ai]);
154    }
155    }
156  163 return chainseq;
157    }
158   
159    /**
160    * filetype of structure file - default is PDB
161    */
162    String structureFileType = PDBEntry.Type.PDB.toString();
163   
 
164  79 toggle protected void setStructureFileType(String structureFileType)
165    {
166  79 this.structureFileType = structureFileType;
167    }
168   
169    /**
170    * filetype of last file processed
171    *
172    * @return
173    */
 
174  163 toggle public String getStructureFileType()
175    {
176  163 return structureFileType;
177    }
178   
 
179  0 toggle @SuppressWarnings({ "unchecked", "rawtypes" })
180    protected void processPdbFileWithAnnotate3d(List<SequenceI> rna)
181    throws Exception
182    {
183    // System.out.println("this is a PDB format and RNA sequence");
184    // note: we use reflection here so that the applet can compile and run
185    // without the HTTPClient bits and pieces needed for accessing Annotate3D
186    // web service
187  0 try
188    {
189  0 Class cl = Class.forName("jalview.ws.jws1.Annotate3D");
190  0 if (cl != null)
191    {
192    // TODO: use the PDB ID of the structure if one is available, to save
193    // bandwidth and avoid uploading the whole structure to the service
194  0 Object annotate3d = cl.getConstructor(new Class[] {})
195    .newInstance(new Object[] {});
196  0 AlignmentI al = ((AlignmentI) cl
197    .getMethod("getRNAMLFor", new Class[]
198    { FileParse.class })
199    .invoke(annotate3d, new Object[]
200    { new FileParse(getDataName(), dataSourceType) }));
201  0 for (SequenceI sq : al.getSequences())
202    {
203  0 if (sq.getDatasetSequence() != null)
204    {
205  0 if (sq.getDatasetSequence().getAllPDBEntries() != null)
206    {
207  0 sq.getDatasetSequence().getAllPDBEntries().clear();
208    }
209    }
210    else
211    {
212  0 if (sq.getAllPDBEntries() != null)
213    {
214  0 sq.getAllPDBEntries().clear();
215    }
216    }
217    }
218  0 replaceAndUpdateChains(rna, al, AlignSeq.DNA, false);
219    }
220    } catch (ClassNotFoundException x)
221    {
222    // ignore classnotfounds - occurs in applet
223    }
224    }
225   
 
226  5 toggle @SuppressWarnings("unchecked")
227    protected void replaceAndUpdateChains(List<SequenceI> prot, AlignmentI al,
228    String pep, boolean b)
229    {
230  5 List<List<? extends Object>> replaced = AlignSeq
231    .replaceMatchingSeqsWith(seqs, annotations, prot, al, pep,
232    false);
233  5 for (PDBChain ch : getChains())
234    {
235  14 int p = 0;
236  14 for (SequenceI sq : (List<SequenceI>) replaced.get(0))
237    {
238  30 p++;
239  30 if (sq == ch.sequence || sq.getDatasetSequence() == ch.sequence)
240    {
241  14 p = -p;
242  14 break;
243    }
244    }
245  14 if (p < 0)
246    {
247  14 p = -p - 1;
248    // set shadow entry for chains
249  14 ch.shadow = (SequenceI) replaced.get(1).get(p);
250  14 ch.shadowMap = ((AlignSeq) replaced.get(2).get(p))
251    .getMappingFromS1(false);
252    }
253    }
254    }
255   
256    /**
257    * Predict secondary structure for RNA and/or protein sequences and add as
258    * annotations
259    *
260    * @param rnaSequences
261    * @param proteinSequences
262    */
 
263  5 toggle protected void addSecondaryStructure(List<SequenceI> rnaSequences,
264    List<SequenceI> proteinSequences)
265    {
266    /*
267    * Currently using Annotate3D for RNA, but only if the 'use external
268    * prediction' flag is set
269    */
270  5 if (externalSecondaryStructure && rnaSequences.size() > 0)
271    {
272  0 try
273    {
274  0 processPdbFileWithAnnotate3d(rnaSequences);
275    } catch (Exception x)
276    {
277  0 System.err.println("Exceptions when dealing with RNA in pdb file");
278  0 x.printStackTrace();
279   
280    }
281    }
282   
283    /*
284    * Currently using JMol PDB parser for peptide
285    */
286  5 if (proteinSequences.size() > 0)
287    {
288  5 try
289    {
290  5 processWithJmolParser(proteinSequences);
291    } catch (Exception x)
292    {
293  0 System.err.println(
294    "Exceptions from Jmol when processing data in pdb file");
295  0 x.printStackTrace();
296    }
297    }
298    }
299   
 
300  5 toggle @SuppressWarnings({ "unchecked", "rawtypes" })
301    private void processWithJmolParser(List<SequenceI> prot) throws Exception
302    {
303  5 try
304    {
305   
306  5 Class cl = Class.forName("jalview.ext.jmol.JmolParser");
307  5 if (cl != null)
308    {
309  5 final Constructor constructor = cl
310    .getConstructor(new Class[]
311    { FileParse.class });
312  5 final Object[] args = new Object[] {
313    new FileParse(getDataName(), dataSourceType) };
314   
315  5 StructureImportSettings.setShowSeqFeatures(false);
316  5 StructureImportSettings.setVisibleChainAnnotation(false);
317  5 StructureImportSettings
318    .setProcessSecondaryStructure(predictSecondaryStructure);
319  5 StructureImportSettings
320    .setExternalSecondaryStructure(externalSecondaryStructure);
321  5 Object jmf = constructor.newInstance(args);
322  5 AlignmentI al = new Alignment((SequenceI[]) cl
323    .getMethod("getSeqsAsArray", new Class[] {}).invoke(jmf));
324  5 cl.getMethod("addAnnotations", new Class[] { AlignmentI.class })
325    .invoke(jmf, al);
326  5 for (SequenceI sq : al.getSequences())
327    {
328  14 if (sq.getDatasetSequence() != null)
329    {
330  0 sq.getDatasetSequence().getAllPDBEntries().clear();
331    }
332    else
333    {
334  14 sq.getAllPDBEntries().clear();
335    }
336    }
337  5 replaceAndUpdateChains(prot, al, AlignSeq.PEP, false);
338    }
339    } catch (ClassNotFoundException q)
340    {
341    }
342  5 StructureImportSettings.setShowSeqFeatures(true);
343    }
344   
345    /**
346    * Answers the first PDBChain found matching the given id, or null if none
347    * is found
348    *
349    * @param id
350    * @return
351    */
 
352  27367 toggle public PDBChain findChain(String id)
353    {
354  27367 for (PDBChain chain : getChains())
355    {
356  48643 if (chain.id.equals(id))
357    {
358  27204 return chain;
359    }
360    }
361  163 return null;
362    }
363   
 
364  92 toggle public void makeResidueList()
365    {
366  92 for (PDBChain chain : getChains())
367    {
368  163 chain.makeResidueList(visibleChainAnnotation);
369    }
370    }
371   
 
372  92 toggle public void makeCaBondList()
373    {
374  92 for (PDBChain chain : getChains())
375    {
376  163 chain.makeCaBondList();
377    }
378    }
379   
 
380  0 toggle public void setChargeColours()
381    {
382  0 for (PDBChain chain : getChains())
383    {
384  0 chain.setChargeColours();
385    }
386    }
387   
 
388  0 toggle public void setColours(jalview.schemes.ColourSchemeI cs)
389    {
390  0 for (PDBChain chain : getChains())
391    {
392  0 chain.setChainColours(cs);
393    }
394    }
395   
 
396  0 toggle public void setChainColours()
397    {
398  0 int i = 0;
399  0 for (PDBChain chain : getChains())
400    {
401  0 chain.setChainColours(Color.getHSBColor(1.0f / i++, .4f, 1.0f));
402    }
403    }
404   
 
405  167 toggle public static boolean isRNA(SequenceI seq)
406    {
407  167 int length = seq.getLength();
408  294 for (int i = 0; i < length; i++)
409    {
410  293 char c = seq.getCharAt(i);
411  293 if ((c != 'A') && (c != 'C') && (c != 'G') && (c != 'U'))
412    {
413  166 return false;
414    }
415    }
416  1 return true;
417    }
418   
419    /**
420    * make a friendly ID string.
421    *
422    * @param dataName
423    * @return truncated dataName to after last '/' and pruned .extension if
424    * present
425    */
 
426  14 toggle protected String safeName(String dataName)
427    {
428  14 int p = 0;
429  ? while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
430    {
431  33 dataName = dataName.substring(p + 1);
432    }
433  14 if (dataName.indexOf(".") > -1)
434    {
435  12 dataName = dataName.substring(0, dataName.lastIndexOf("."));
436    }
437  14 return dataName;
438    }
439   
 
440  981 toggle public String getId()
441    {
442  981 return id;
443    }
444   
 
445  105 toggle public void setId(String id)
446    {
447  105 this.id = id;
448    }
449   
 
450  27959 toggle public Vector<PDBChain> getChains()
451    {
452  27959 return chains;
453    }
454   
 
455  92 toggle public void setChains(Vector<PDBChain> chains)
456    {
457  92 this.chains = chains;
458    }
459   
 
460  0 toggle public Type getDbRefType()
461    {
462  0 return dbRefType;
463    }
464   
 
465  13 toggle public void setDbRefType(String dbRefType)
466    {
467  13 this.dbRefType = Type.getType(dbRefType);
468    }
469   
 
470  14 toggle public void setDbRefType(Type dbRefType)
471    {
472  14 this.dbRefType = dbRefType;
473    }
474   
475    /**
476    * Returns a descriptor for suitable feature display settings with
477    * <ul>
478    * <li>ResNums or insertions features visible</li>
479    * <li>insertions features coloured red</li>
480    * <li>ResNum features coloured by label</li>
481    * <li>Insertions displayed above (on top of) ResNums</li>
482    * </ul>
483    */
 
484  13 toggle @Override
485    public FeatureSettingsModelI getFeatureColourScheme()
486    {
487  13 return new PDBFeatureSettings();
488    }
489   
490    /**
491    * Answers true if the structure file has a PDBId
492    *
493    * @return
494    */
 
495  0 toggle public boolean isPPDBIdAvailable()
496    {
497  0 return pdbIdAvailable;
498    }
499   
 
500  79 toggle public void setPDBIdAvailable(boolean pdbIdAvailable)
501    {
502  79 this.pdbIdAvailable = pdbIdAvailable;
503    }
504    }