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