JAL-1919 Fixed failing tests, enabled HETATM processing from structure file to be...
[jalview.git] / src / jalview / ws / dbsources / Pdb.java
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.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.FormatAdapter;
32 import jalview.io.PDBFeatureSettings;
33 import jalview.structure.StructureImportSettings;
34 import jalview.util.MessageManager;
35 import jalview.ws.ebi.EBIFetchClient;
36
37 import java.util.ArrayList;
38 import java.util.List;
39 import java.util.Vector;
40
41 import com.stevesoft.pat.Regex;
42
43 /**
44  * @author JimP
45  * 
46  */
47 public class Pdb extends EbiFileRetrievedProxy
48 {
49   public Pdb()
50   {
51     super();
52   }
53
54   public static final String FEATURE_INSERTION = "INSERTION";
55
56   public static final String FEATURE_RES_NUM = "RESNUM";
57
58   /*
59    * (non-Javadoc)
60    * 
61    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
62    */
63   @Override
64   public String getAccessionSeparator()
65   {
66     // TODO Auto-generated method stub
67     return null;
68   }
69
70   /*
71    * (non-Javadoc)
72    * 
73    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
74    */
75   @Override
76   public Regex getAccessionValidator()
77   {
78     return new Regex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");
79   }
80
81   /*
82    * (non-Javadoc)
83    * 
84    * @see jalview.ws.DbSourceProxy#getDbSource()
85    */
86   @Override
87   public String getDbSource()
88   {
89     return DBRefSource.PDB;
90   }
91
92   /*
93    * (non-Javadoc)
94    * 
95    * @see jalview.ws.DbSourceProxy#getDbVersion()
96    */
97   @Override
98   public String getDbVersion()
99   {
100     return "0";
101   }
102
103   /*
104    * (non-Javadoc)
105    * 
106    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
107    */
108   @Override
109   public AlignmentI getSequenceRecords(String queries) throws Exception
110   {
111     AlignmentI pdbAlignment = null;
112     Vector result = new Vector();
113     String chain = null;
114     String id = null;
115     if (queries.indexOf(":") > -1)
116     {
117       chain = queries.substring(queries.indexOf(":") + 1);
118       id = queries.substring(0, queries.indexOf(":"));
119     }
120     else
121     {
122       id = queries;
123     }
124     if (queries.length() > 4 && chain == null)
125     {
126       chain = queries.substring(4, 5);
127       id = queries.substring(0, 4);
128     }
129     if (!isValidReference(id))
130     {
131       System.err.println("Ignoring invalid pdb query: '" + id + "'");
132       stopQuery();
133       return null;
134     }
135     String ext = StructureImportSettings.getCurrentDefaultFormat()
136             .equalsIgnoreCase("mmcif") ? ".cif"
137             : ".xml";
138     EBIFetchClient ebi = new EBIFetchClient();
139     file = ebi.fetchDataAsFile("pdb:" + id,
140             StructureImportSettings.getCurrentDefaultFormat().toLowerCase(),
141             ext)
142             .getAbsolutePath();
143     stopQuery();
144     if (file == null)
145     {
146       return null;
147     }
148     try
149     {
150
151       pdbAlignment = new FormatAdapter().readFile(file,
152               jalview.io.AppletFormatAdapter.FILE,
153               StructureImportSettings.getCurrentDefaultFormat());
154       if (pdbAlignment != null)
155       {
156         List<SequenceI> toremove = new ArrayList<SequenceI>();
157         for (SequenceI pdbcs : pdbAlignment.getSequences())
158         {
159           String chid = null;
160           // Mapping map=null;
161           for (PDBEntry pid : pdbcs.getAllPDBEntries())
162           {
163             if (pid.getFile() == file)
164             {
165               chid = pid.getChainCode();
166
167             }
168             ;
169
170           }
171           if (chain == null
172                   || (chid != null && (chid.equals(chain)
173                           || chid.trim().equals(chain.trim()) || (chain
174                           .trim().length() == 0 && chid.equals("_")))))
175           {
176             pdbcs.setName(jalview.datamodel.DBRefSource.PDB + "|" + id
177                     + "|" + pdbcs.getName());
178             // Might need to add more metadata to the PDBEntry object
179             // like below
180             /*
181              * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
182              * entry.setId(id); if (entry.getProperty() == null)
183              * entry.setProperty(new Hashtable());
184              * entry.getProperty().put("chains", pdbchain.id + "=" +
185              * sq.getStart() + "-" + sq.getEnd());
186              * sq.getDatasetSequence().addPDBId(entry);
187              */
188             // Add PDB DB Refs
189             // We make a DBRefEtntry because we have obtained the PDB file from
190             // a
191             // verifiable source
192             // JBPNote - PDB DBRefEntry should also carry the chain and mapping
193             // information
194             DBRefEntry dbentry = new DBRefEntry(getDbSource(),
195                     getDbVersion(), (chid == null ? id : id + chid));
196             // dbentry.setMap()
197             pdbcs.addDBRef(dbentry);
198           }
199           else
200           {
201             // mark this sequence to be removed from the alignment
202             // - since it's not from the right chain
203             toremove.add(pdbcs);
204           }
205         }
206         // now remove marked sequences
207         for (SequenceI pdbcs : toremove)
208         {
209           pdbAlignment.deleteSequence(pdbcs);
210           if (pdbcs.getAnnotation() != null)
211           {
212             for (AlignmentAnnotation aa : pdbcs.getAnnotation())
213             {
214               pdbAlignment.deleteAnnotation(aa);
215             }
216           }
217         }
218       }
219
220       if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
221       {
222         throw new Exception(MessageManager.formatMessage(
223                 "exception.no_pdb_records_for_chain", new String[] { id,
224                     ((chain == null) ? "' '" : chain) }));
225       }
226
227     } catch (Exception ex) // Problem parsing PDB file
228     {
229       stopQuery();
230       throw (ex);
231     }
232     return pdbAlignment;
233   }
234
235   /*
236    * (non-Javadoc)
237    * 
238    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
239    */
240   @Override
241   public boolean isValidReference(String accession)
242   {
243     Regex r = getAccessionValidator();
244     return r.search(accession.trim());
245   }
246
247   /**
248    * human glyoxalase
249    */
250   @Override
251   public String getTestQuery()
252   {
253     return "1QIP";
254   }
255
256   @Override
257   public String getDbName()
258   {
259     return "PDB"; // getDbSource();
260   }
261
262   @Override
263   public int getTier()
264   {
265     return 0;
266   }
267
268
269   /**
270    * Returns a descriptor for suitable feature display settings with
271    * <ul>
272    * <li>ResNums or insertions features visible</li>
273    * <li>insertions features coloured red</li>
274    * <li>ResNum features coloured by label</li>
275    * <li>Insertions displayed above (on top of) ResNums</li>
276    * </ul>
277    */
278   @Override
279   public FeatureSettingsModelI getFeatureColourScheme()
280   {
281     return new PDBFeatureSettings();
282   }
283 }