Clover icon

Coverage Report

  1. Project Clover database Wed Jan 7 2026 02:49:01 GMT
  2. Package jalview.ws.dbsources

File Pdb.java

 

Coverage histogram

../../../img/srcFileCovDistChart1.png
57% of files have more coverage

Code metrics

30
72
12
1
336
206
35
0.49
6
12
2.92

Classes

Class Line # Actions
Pdb 54 72 35
0.070175447%
 

Contributing tests

This file is covered by 191 tests. .

Source view

1   
2    /*
3    * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
4    * Copyright (C) $$Year-Rel$$ The Jalview Authors
5    *
6    * This file is part of Jalview.
7    *
8    * Jalview is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License
10    * as published by the Free Software Foundation, either version 3
11    * of the License, or (at your option) any later version.
12    *
13    * Jalview is distributed in the hope that it will be useful, but
14    * WITHOUT ANY WARRANTY; without even the implied warranty
15    * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16    * PURPOSE. See the GNU General Public License for more details.
17    *
18    * You should have received a copy of the GNU General Public License
19    * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
20    * The Jalview Authors are detailed in the 'AUTHORS' file.
21    */
22    package jalview.ws.dbsources;
23   
24    import jalview.api.FeatureSettingsModelI;
25    import jalview.bin.Cache;
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.io.DataSourceType;
34    import jalview.io.FileFormat;
35    import jalview.io.FileFormatI;
36    import jalview.io.FormatAdapter;
37    import jalview.io.PDBFeatureSettings;
38    import jalview.structure.StructureImportSettings;
39    import jalview.util.MessageManager;
40    import jalview.util.Platform;
41    import jalview.ws.ebi.EBIFetchClient;
42   
43    import java.io.File;
44    import java.util.ArrayList;
45    import java.util.List;
46    import java.util.Locale;
47   
48    import com.stevesoft.pat.Regex;
49   
50    /**
51    * @author JimP
52    *
53    */
 
54    public class Pdb extends EbiFileRetrievedProxy
55    {
56    private static final String SEPARATOR = "|";
57   
58    private static final String COLON = ":";
59   
60    private static final int PDB_ID_LENGTH = 4;
61   
62    private static Regex ACCESSION_REGEX;
63   
 
64  65 toggle public Pdb()
65    {
66  65 super();
67    }
68   
69    /*
70    * (non-Javadoc)
71    *
72    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
73    */
 
74  0 toggle @Override
75    public String getAccessionSeparator()
76    {
77  0 return null;
78    }
79   
80    /*
81    * (non-Javadoc)
82    *
83    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
84    */
 
85  0 toggle @Override
86    public Regex getAccessionValidator()
87    {
88  0 if (ACCESSION_REGEX == null)
89    {
90  0 ACCESSION_REGEX = Platform
91    .newRegex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");
92    }
93  0 return ACCESSION_REGEX;
94    }
95   
96    /*
97    * (non-Javadoc)
98    *
99    * @see jalview.ws.DbSourceProxy#getDbSource()
100    */
 
101  8386 toggle @Override
102    public String getDbSource()
103    {
104  8386 return DBRefSource.PDB;
105    }
106   
107    /*
108    * (non-Javadoc)
109    *
110    * @see jalview.ws.DbSourceProxy#getDbVersion()
111    */
 
112  0 toggle @Override
113    public String getDbVersion()
114    {
115  0 return "0";
116    }
117   
118    /*
119    * (non-Javadoc)
120    *
121    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
122    */
 
123  0 toggle @Override
124    public AlignmentI getSequenceRecords(String queries) throws Exception
125    {
126  0 updateConfiguration();
127  0 AlignmentI pdbAlignment = null;
128  0 String chain = null;
129  0 String id = null;
130  0 if (queries.indexOf(COLON) > -1)
131    {
132  0 chain = queries.substring(queries.indexOf(COLON) + 1);
133  0 id = queries.substring(0, queries.indexOf(COLON));
134    }
135    else
136    {
137  0 id = queries;
138    }
139   
140    /*
141    * extract chain code if it is appended to the id and we
142    * don't already have one
143    */
144  0 if (queries.length() > PDB_ID_LENGTH && chain == null)
145    {
146  0 chain = queries.substring(PDB_ID_LENGTH, PDB_ID_LENGTH + 1);
147  0 id = queries.substring(0, PDB_ID_LENGTH);
148    }
149   
150  0 if (!isValidReference(id))
151    {
152  0 jalview.bin.Console
153    .errPrintln("Ignoring invalid pdb query: '" + id + "'");
154  0 stopQuery();
155  0 return null;
156    }
157   
158    /*
159    * ensure that an mmCIF format structure file is saved with extension.cif,
160    * because the Chimera "open" command recognises this extension
161    */
162  0 Type pdbFileFormat = StructureImportSettings
163    .getDefaultStructureFileFormat();
164  0 String ext = pdbFileFormat.getExtension();
165  0 String fetchFormat = pdbFileFormat.getFormat();
166   
167  0 EBIFetchClient ebi = new EBIFetchClient();
168  0 File tmpFile = ebi.fetchDataAsFile("pdb:" + id, fetchFormat, ext);
169  0 file = tmpFile.getAbsolutePath();
170  0 stopQuery();
171  0 if (file == null)
172    {
173  0 return null;
174    }
175  0 try
176    {
177    // todo get rid of Type and use FileFormatI instead?
178  0 FileFormatI fileFormat = (pdbFileFormat == Type.PDB) ? FileFormat.PDB
179    : FileFormat.MMCif;
180  0 pdbAlignment = new FormatAdapter().readFile(tmpFile,
181    DataSourceType.FILE, fileFormat);
182  0 if (pdbAlignment != null)
183    {
184  0 List<SequenceI> toremove = new ArrayList<SequenceI>();
185  0 for (SequenceI pdbcs : pdbAlignment.getSequences())
186    {
187  0 String chid = null;
188    // Mapping map=null;
189  0 for (PDBEntry pid : pdbcs.getAllPDBEntries())
190    {
191  0 if (pid.getFile() == file)
192    {
193  0 chid = pid.getChainCode();
194   
195    }
196    }
197  0 if (chain == null || (chid != null && (chid.equals(chain)
198    || chid.trim().equals(chain.trim())
199    || (chain.trim().length() == 0 && chid.equals("_")))))
200    {
201    // FIXME seems to result in 'PDB|1QIP|1qip|A' - 1QIP is redundant.
202    // TODO: suggest simplify naming to 1qip|A as default name defined
203  0 if (legacyJalviewPDBeName)
204    {
205  0 pdbcs.setName(jalview.datamodel.DBRefSource.PDB + SEPARATOR + id
206    + SEPARATOR + pdbcs.getName());
207    } else {
208  0 if (simpleChainName)
209    {
210  0 int sep_pos = pdbcs.getName().indexOf(SEPARATOR);
211  0 String cid = pdbcs.getName().substring(0,sep_pos);
212  0 cid = cid.toLowerCase(Locale.ROOT);
213  0 pdbcs.setName(cid+"_"+pdbcs.getName().substring(sep_pos+1));
214  0 };
215    }
216    // Might need to add more metadata to the PDBEntry object
217    // like below
218    /*
219    * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
220    * entry.setId(id); if (entry.getProperty() == null)
221    * entry.setProperty(new Hashtable());
222    * entry.getProperty().put("chains", pdbchain.id + "=" +
223    * sq.getStart() + "-" + sq.getEnd());
224    * sq.getDatasetSequence().addPDBId(entry);
225    */
226    // Add PDB DB Refs
227    // We make a DBRefEtntry because we have obtained the PDB file from
228    // a
229    // verifiable source
230    // JBPNote - PDB DBRefEntry should also carry the chain and mapping
231    // information
232  0 DBRefEntry dbentry = new DBRefEntry(getDbSource(),
233  0 getDbVersion(), (chid == null ? id : id + chid));
234    // dbentry.setMap()
235  0 pdbcs.addDBRef(dbentry);
236    }
237    else
238    {
239    // mark this sequence to be removed from the alignment
240    // - since it's not from the right chain
241  0 toremove.add(pdbcs);
242    }
243    }
244    // now remove marked sequences
245  0 for (SequenceI pdbcs : toremove)
246    {
247  0 pdbAlignment.deleteSequence(pdbcs);
248  0 if (pdbcs.getAnnotation() != null)
249    {
250  0 for (AlignmentAnnotation aa : pdbcs.getAnnotation())
251    {
252  0 pdbAlignment.deleteAnnotation(aa);
253    }
254    }
255    }
256    }
257   
258  0 if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
259    {
260  0 throw new Exception(MessageManager.formatMessage(
261    "exception.no_pdb_records_for_chain", new String[]
262  0 { id, ((chain == null) ? "' '" : chain) }));
263    }
264   
265    } catch (Exception ex) // Problem parsing PDB file
266    {
267  0 stopQuery();
268  0 throw (ex);
269    }
270  0 return pdbAlignment;
271    }
272   
273    boolean simpleChainName=false;
274    boolean legacyJalviewPDBeName=true;
275   
276    private static String SIMPLE="SIMPLE";
277    private static String LEGACY="LEGACY";
278   
279    /**
280    * update the chain naming conventions based on the configuration
281    */
 
282  0 toggle private void updateConfiguration()
283    {
284  0 final String nameConf = Cache.getDefault(Cache.PDB_CHAIN_NAME, "SIMPLE");
285  0 simpleChainName = SIMPLE.equals(nameConf);
286  0 legacyJalviewPDBeName = LEGACY.equals(nameConf);
287    }
288   
289    /*
290    * (non-Javadoc)
291    *
292    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
293    */
 
294  0 toggle @Override
295    public boolean isValidReference(String accession)
296    {
297  0 Regex r = getAccessionValidator();
298  0 return r.search(accession.trim());
299    }
300   
301    /**
302    * human glyoxalase
303    */
 
304  0 toggle @Override
305    public String getTestQuery()
306    {
307  0 return "1QIP";
308    }
309   
 
310  8369 toggle @Override
311    public String getDbName()
312    {
313  8369 return "PDB"; // getDbSource();
314    }
315   
 
316  0 toggle @Override
317    public int getTier()
318    {
319  0 return 0;
320    }
321   
322    /**
323    * Returns a descriptor for suitable feature display settings with
324    * <ul>
325    * <li>ResNums or insertions features visible</li>
326    * <li>insertions features coloured red</li>
327    * <li>ResNum features coloured by label</li>
328    * <li>Insertions displayed above (on top of) ResNums</li>
329    * </ul>
330    */
 
331  48 toggle @Override
332    public FeatureSettingsModelI getFeatureColourScheme()
333    {
334  48 return new PDBFeatureSettings();
335    }
336    }