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