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