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