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