Clover icon

Coverage Report

  1. Project Clover database Thu Nov 7 2024 17:01:39 GMT
  2. Package jalview.io

File StructureFile.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
0% of files have more coverage

Code metrics

38
136
41
1
552
401
70
0.51
3.32
41
1.71

Classes

Class Line # Actions
StructureFile 45 136 70
0.00%
 

Contributing tests

No tests hitting this source file were found.

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