Merge branch 'develop' (JAL-4102 2.11.2.6 patch release) into features/r2_11_2_alphaf...
[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.api.FeatureSettingsModelI;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.DBRefEntry;
27 import jalview.datamodel.DBRefSource;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.FormatAdapter;
31 import jalview.io.PDBFeatureSettings;
32 import jalview.util.MessageManager;
33 import jalview.ws.ebi.EBIFetchClient;
34
35 import java.util.ArrayList;
36 import java.util.List;
37 import java.util.Vector;
38
39 import com.stevesoft.pat.Regex;
40
41 /**
42  * @author JimP
43  * 
44  */
45 public class Pdb extends EbiFileRetrievedProxy
46 {
47   public Pdb()
48   {
49     super();
50   }
51
52   public static final String FEATURE_INSERTION = "INSERTION";
53
54   public static final String FEATURE_RES_NUM = "RESNUM";
55
56   private static String currentDefaultFormat = DBRefSource.PDB;
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 = getCurrentDefaultFormat().equalsIgnoreCase("mmcif") ? ".cif"
136             : ".xml";
137     EBIFetchClient ebi = new EBIFetchClient();
138     file = ebi.fetchDataAsFile("pdb:" + id,
139 <<<<<<< HEAD
140             getCurrentDefaultFomart().toLowerCase(), ext)
141 =======
142             getCurrentDefaultFormat().toLowerCase(), "raw", ext)
143 >>>>>>> develop
144             .getAbsolutePath();
145     stopQuery();
146     if (file == null)
147     {
148       return null;
149     }
150     try
151     {
152
153       pdbAlignment = new FormatAdapter().readFile(file,
154               jalview.io.AppletFormatAdapter.FILE,
155               getCurrentDefaultFormat());
156       if (pdbAlignment != null)
157       {
158         List<SequenceI> toremove = new ArrayList<SequenceI>();
159         for (SequenceI pdbcs : pdbAlignment.getSequences())
160         {
161           String chid = null;
162           // Mapping map=null;
163           for (PDBEntry pid : pdbcs.getAllPDBEntries())
164           {
165             if (pid.getFile() == file)
166             {
167               chid = pid.getChainCode();
168
169             }
170             ;
171
172           }
173           if (chain == null
174                   || (chid != null && (chid.equals(chain)
175                           || chid.trim().equals(chain.trim()) || (chain
176                           .trim().length() == 0 && chid.equals("_")))))
177           {
178             pdbcs.setName(jalview.datamodel.DBRefSource.PDB + "|" + id
179                     + "|" + pdbcs.getName());
180             // Might need to add more metadata to the PDBEntry object
181             // like below
182             /*
183              * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
184              * entry.setId(id); if (entry.getProperty() == null)
185              * entry.setProperty(new Hashtable());
186              * entry.getProperty().put("chains", pdbchain.id + "=" +
187              * sq.getStart() + "-" + sq.getEnd());
188              * sq.getDatasetSequence().addPDBId(entry);
189              */
190             // Add PDB DB Refs
191             // We make a DBRefEtntry because we have obtained the PDB file from
192             // a
193             // verifiable source
194             // JBPNote - PDB DBRefEntry should also carry the chain and mapping
195             // information
196             DBRefEntry dbentry = new DBRefEntry(getDbSource(),
197                     getDbVersion(), (chid == null ? id : id + chid));
198             // dbentry.setMap()
199             pdbcs.addDBRef(dbentry);
200           }
201           else
202           {
203             // mark this sequence to be removed from the alignment
204             // - since it's not from the right chain
205             toremove.add(pdbcs);
206           }
207         }
208         // now remove marked sequences
209         for (SequenceI pdbcs : toremove)
210         {
211           pdbAlignment.deleteSequence(pdbcs);
212           if (pdbcs.getAnnotation() != null)
213           {
214             for (AlignmentAnnotation aa : pdbcs.getAnnotation())
215             {
216               pdbAlignment.deleteAnnotation(aa);
217             }
218           }
219         }
220       }
221
222       if (pdbAlignment == null || pdbAlignment.getHeight() < 1)
223       {
224         throw new Exception(MessageManager.formatMessage(
225                 "exception.no_pdb_records_for_chain", new String[] { id,
226                     ((chain == null) ? "' '" : chain) }));
227       }
228
229     } catch (Exception ex) // Problem parsing PDB file
230     {
231       stopQuery();
232       throw (ex);
233     }
234     return pdbAlignment;
235   }
236
237   /*
238    * (non-Javadoc)
239    * 
240    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
241    */
242   @Override
243   public boolean isValidReference(String accession)
244   {
245     Regex r = getAccessionValidator();
246     return r.search(accession.trim());
247   }
248
249   /**
250    * obtain human glyoxalase chain A sequence
251    */
252   @Override
253   public String getTestQuery()
254   {
255     return "1QIPA";
256   }
257
258   @Override
259   public String getDbName()
260   {
261     return "PDB"; // getDbSource();
262   }
263
264   @Override
265   public int getTier()
266   {
267     return 0;
268   }
269
270   public static String getCurrentDefaultFormat()
271   {
272     return currentDefaultFormat;
273   }
274
275   public static void setCurrentDefaultFormat(String currentDefaultFomart)
276   {
277     Pdb.currentDefaultFormat = currentDefaultFomart;
278   }
279
280   /**
281    * Returns a descriptor for suitable feature display settings with
282    * <ul>
283    * <li>ResNums or insertions features visible</li>
284    * <li>insertions features coloured red</li>
285    * <li>ResNum features coloured by label</li>
286    * <li>Insertions displayed above (on top of) ResNums</li>
287    * </ul>
288    */
289   @Override
290   public FeatureSettingsModelI getFeatureColourScheme()
291   {
292     return new PDBFeatureSettings();
293   }
294 }