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