Merge branch 'Dbfetch_hotfix-JAL-1238' into develop
[jalview.git] / src / jalview / ws / dbsources / Pdb.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.ws.dbsources;
19
20 import jalview.datamodel.Alignment;
21 import jalview.datamodel.DBRefEntry;
22 import jalview.datamodel.DBRefSource;
23 import jalview.datamodel.PDBEntry;
24 import jalview.datamodel.SequenceI;
25
26 import java.util.Vector;
27
28 import MCview.PDBChain;
29 import MCview.PDBfile;
30
31 import com.stevesoft.pat.Regex;
32
33 import jalview.datamodel.AlignmentI;
34 import jalview.io.FormatAdapter;
35 import jalview.ws.ebi.EBIFetchClient;
36 import jalview.ws.seqfetcher.DbSourceProxy;
37
38 /**
39  * @author JimP
40  * 
41  */
42 public class Pdb extends EbiFileRetrievedProxy implements DbSourceProxy
43 {
44   public Pdb()
45   {
46     super();
47     addDbSourceProperty(DBRefSource.PROTSEQDB);
48   }
49
50   /*
51    * (non-Javadoc)
52    * 
53    * @see jalview.ws.DbSourceProxy#getAccessionSeparator()
54    */
55   public String getAccessionSeparator()
56   {
57     // TODO Auto-generated method stub
58     return null;
59   }
60
61   /*
62    * (non-Javadoc)
63    * 
64    * @see jalview.ws.DbSourceProxy#getAccessionValidator()
65    */
66   public Regex getAccessionValidator()
67   {
68     return new Regex("([1-9][0-9A-Za-z]{3}):?([ _A-Za-z0-9]?)");
69   }
70
71   /*
72    * (non-Javadoc)
73    * 
74    * @see jalview.ws.DbSourceProxy#getDbSource()
75    */
76   public String getDbSource()
77   {
78     return DBRefSource.PDB;
79   }
80
81   /*
82    * (non-Javadoc)
83    * 
84    * @see jalview.ws.DbSourceProxy#getDbVersion()
85    */
86   public String getDbVersion()
87   {
88     return "0";
89   }
90
91   /*
92    * (non-Javadoc)
93    * 
94    * @see jalview.ws.DbSourceProxy#getSequenceRecords(java.lang.String[])
95    */
96   public AlignmentI getSequenceRecords(String queries) throws Exception
97   {
98     AlignmentI pdbfile = null;
99     Vector result = new Vector();
100     String chain = null;
101     String id = null;
102     if (queries.indexOf(":") > -1)
103     {
104       chain = queries.substring(queries.indexOf(":") + 1);
105       id = queries.substring(0, queries.indexOf(":"));
106     }
107     else
108     {
109       id = queries;
110     }
111     if (queries.length() > 4 && chain == null)
112     {
113       chain = queries.substring(4, 5);
114       id = queries.substring(0, 4);
115     }
116     if (!isValidReference(id))
117     {
118       System.err.println("Ignoring invalid pdb query: '" + id + "'");
119       stopQuery();
120       return null;
121     }
122     EBIFetchClient ebi = new EBIFetchClient();
123     file = ebi.fetchDataAsFile("pdb:" + id, "pdb", "raw").getAbsolutePath();
124     stopQuery();
125     if (file == null)
126     {
127       return null;
128     }
129     try
130     {
131
132       pdbfile = new FormatAdapter().readFile(file,
133               jalview.io.AppletFormatAdapter.FILE, "PDB");
134       if (pdbfile != null)
135       {
136         for (SequenceI pdbcs : pdbfile.getSequences())
137         {
138           String chid = null;
139           // Mapping map=null;
140           for (PDBEntry pid : (Vector<PDBEntry>) pdbcs.getPDBId())
141           {
142             if (pid.getFile() == file)
143             {
144               chid = (String) pid.getProperty().get("CHAIN");
145
146             }
147             ;
148
149           }
150           if (chain == null
151                   || (chid != null && (chid.equals(chain)
152                           || chid.trim().equals(chain.trim()) || (chain
153                           .trim().length() == 0 && chid.equals("_")))))
154           {
155             pdbcs.setName(jalview.datamodel.DBRefSource.PDB + "|" + id
156                     + "|" + pdbcs.getName());
157             // Might need to add more metadata to the PDBEntry object
158             // like below
159             /*
160              * PDBEntry entry = new PDBEntry(); // Construct the PDBEntry
161              * entry.setId(id); if (entry.getProperty() == null)
162              * entry.setProperty(new Hashtable());
163              * entry.getProperty().put("chains", pdbchain.id + "=" +
164              * sq.getStart() + "-" + sq.getEnd());
165              * sq.getDatasetSequence().addPDBId(entry);
166              */
167             // Add PDB DB Refs
168             // We make a DBRefEtntry because we have obtained the PDB file from
169             // a
170             // verifiable source
171             // JBPNote - PDB DBRefEntry should also carry the chain and mapping
172             // information
173             DBRefEntry dbentry = new DBRefEntry(getDbSource(),
174                     getDbVersion(), (chid == null ? id : id + chid));
175             // dbentry.setMap()
176             pdbcs.addDBRef(dbentry);
177           }
178           else
179           {
180             // remove this sequence from the alignment - since it's not from the
181             // right chain
182             pdbfile.deleteSequence(pdbcs);
183           }
184         }
185       }
186       if (pdbfile == null || pdbfile.getHeight() < 1)
187       {
188         throw new Exception("No PDB Records for " + id + " chain "
189                 + ((chain == null) ? "' '" : chain));
190       }
191     } catch (Exception ex) // Problem parsing PDB file
192     {
193       stopQuery();
194       throw (ex);
195     }
196     return pdbfile;
197   }
198
199   /*
200    * (non-Javadoc)
201    * 
202    * @see jalview.ws.DbSourceProxy#isValidReference(java.lang.String)
203    */
204   public boolean isValidReference(String accession)
205   {
206     Regex r = getAccessionValidator();
207     return r.search(accession.trim());
208   }
209
210   /**
211    * obtain human glyoxalase chain A sequence
212    */
213   public String getTestQuery()
214   {
215     return "1QIPA";
216   }
217
218   public String getDbName()
219   {
220     return "PDB"; // getDbSource();
221   }
222
223   @Override
224   public int getTier()
225   {
226     return 0;
227   }
228 }