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