JAL-1834 removed quotes around truncated strings with ellipsis
[jalview.git] / src / jalview / gui / PDBSearchPanel.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
22 package jalview.gui;
23
24 import jalview.jbgui.GPDBSearchPanel;
25 import jalview.jbgui.PDBDocFieldPreferences;
26 import jalview.util.MessageManager;
27 import jalview.ws.dbsources.PDBRestClient;
28 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
29 import jalview.ws.uimodel.PDBRestRequest;
30 import jalview.ws.uimodel.PDBRestResponse;
31
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.Comparator;
36 import java.util.HashSet;
37 import java.util.List;
38
39 import javax.swing.table.DefaultTableModel;
40
41 @SuppressWarnings("serial")
42 public class PDBSearchPanel extends GPDBSearchPanel
43 {
44   private SequenceFetcher seqFetcher;
45
46   private IProgressIndicator progressIdicator;
47
48   private Collection<PDBDocField> wantedFields;
49
50   public PDBSearchPanel(SequenceFetcher seqFetcher)
51   {
52     this.seqFetcher = seqFetcher;
53     this.progressIdicator = (seqFetcher == null) ? null : seqFetcher
54             .getProgressIndicator();
55   }
56
57
58   /**
59    * Action performed when an input is detected on txt_search field.
60    */
61   @Override
62   public void txt_search_ActionPerformed()
63   {
64     errorWarning.setLength(0);
65     lbl_warning.setVisible(false);
66     btn_ok.setEnabled(false);
67     boolean allowEmptySequence = false;
68     mainFrame.setTitle(MessageManager
69             .getString("label.pdb_sequence_getcher"));
70     tbl_summary.setModel(new DefaultTableModel());
71     if (txt_search.getText().trim().length() > 0)
72     {
73       long startTime = System.currentTimeMillis();
74
75       String searchTarget = ((PDBDocField) cmb_searchTarget
76               .getSelectedItem()).getCode();
77
78       wantedFields = PDBDocFieldPreferences
79               .getSearchSummaryFields();
80
81       String searchTerm = decodeSearchTerm(txt_search.getText(),
82               searchTarget);
83
84       PDBRestRequest request = new PDBRestRequest();
85       request.setAllowEmptySeq(allowEmptySequence);
86       request.setResponseSize(100);
87       request.setFieldToSearchBy("(" + searchTarget + ":");
88       request.setSearchTerm(searchTerm + ")");
89       request.setWantedFields(wantedFields);
90       // System.out.println(">>>>>>>>>>>>>> " + request.getQuery());
91       PDBRestClient pdbRestCleint = new PDBRestClient();
92       PDBRestResponse resultList;
93       try
94       {
95         resultList = pdbRestCleint.executeRequest(request);
96       } catch (Exception e)
97       {
98         // System.out.println(">>>>>>>>>>>>>>>" + e.getMessage());
99         e.printStackTrace();
100         errorWarning.append(e.getMessage());
101         checkForErrors();
102         return;
103       }
104
105       if (resultList.getSearchSummary() != null)
106       {
107         tbl_summary.setModel(PDBRestResponse.getTableModel(request,
108                 resultList.getSearchSummary()));
109       }
110
111       long endTime = System.currentTimeMillis();
112       int resultSetCount = resultList.getNumberOfItemsFound();
113       String result = (resultSetCount > 1) ? MessageManager
114               .getString("label.results") : MessageManager
115               .getString("label.result");
116       mainFrame.setTitle(frameTitle + " - " + resultSetCount + " " + result
117               + " (" + (endTime - startTime) + " milli secs)");
118     }
119   }
120
121   public static String decodeSearchTerm(String enteredText,
122           String targetField)
123   {
124     String foundSearchTerms = enteredText;
125     StringBuilder foundSearchTermsBuilder = new StringBuilder();
126     if (enteredText.contains(";"))
127     {
128       String[] searchTerms = enteredText.split(";");
129       for (String searchTerm : searchTerms)
130       {
131         if (searchTerm.contains(":"))
132         {
133           foundSearchTermsBuilder.append(targetField).append(":")
134                   .append(searchTerm.split(":")[0]).append(" OR ");
135         }
136         else
137         {
138           foundSearchTermsBuilder.append(targetField).append(":")
139                   .append(searchTerm).append(" OR ");
140         }
141       }
142       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
143       foundSearchTerms = foundSearchTermsBuilder.toString();
144       if (foundSearchTerms.contains(" OR "))
145       {
146         foundSearchTerms = foundSearchTerms.substring(
147                 targetField.length() + 1, endIndex);
148       }
149     }
150     else if (enteredText.contains(":"))
151     {
152       foundSearchTerms = foundSearchTerms.split(":")[0];
153     }
154     return foundSearchTerms;
155   }
156
157
158   @Override
159   public void btn_ok_ActionPerformed()
160   {
161     loadSelectedPDBSequencesToAlignment();
162   }
163
164   @Override
165   public void btn_back_ActionPerformed()
166   {
167     mainFrame.dispose();
168     new SequenceFetcher(progressIdicator);
169   }
170
171   @Override
172   public void btn_cancel_ActionPerformed()
173   {
174     mainFrame.dispose();
175   }
176
177   public void transferToSequenceFetcher(String ids)
178   {
179     // mainFrame.dispose();
180     seqFetcher.textArea.setText(ids);
181     Thread worker = new Thread(seqFetcher);
182     worker.start();
183   }
184
185   /**
186    * Add the discovered/selected sequences to a target alignment window
187    */
188   public void loadSelectedPDBSequencesToAlignment()
189   {
190     // mainFrame.dispose();
191     StringBuilder selectedIds = new StringBuilder();
192     HashSet<String> selectedIdsSet = new HashSet<String>();
193     int pdbIdCol = PDBRestClient.getPDBIdColumIndex(wantedFields, false);
194     int[] selectedRows = tbl_summary.getSelectedRows();
195     for (int summaryRow : selectedRows)
196     {
197       String pdbIdStr = tbl_summary.getValueAt(summaryRow, pdbIdCol)
198               .toString();
199       String searchTerm = txt_search.getText();
200       selectedIdsSet.add(getPDBIdwithSpecifiedChain(pdbIdStr, searchTerm));
201     }
202
203     for (String selectedId : selectedIdsSet)
204     {
205       selectedIds.append(selectedId).append(";");
206     }
207
208     String ids = selectedIds.toString();
209     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
210     seqFetcher.textArea.setText(ids);
211     Thread worker = new Thread(seqFetcher);
212     worker.start();
213   }
214
215   public static String getPDBIdwithSpecifiedChain(String pdbId,
216           String searchTerm)
217   {
218     String pdbIdWithChainCode = "";
219     if (searchTerm.contains(";"))
220     {
221       String[] foundTerms = searchTerm.split(";");
222       for (String foundTerm : foundTerms)
223       {
224         if (foundTerm.contains(pdbId))
225         {
226           pdbIdWithChainCode = foundTerm;
227         }
228       }
229     }
230     else if (searchTerm.contains(pdbId))
231     {
232       pdbIdWithChainCode = searchTerm;
233     }
234     else
235     {
236       pdbIdWithChainCode = pdbId;
237     }
238     return pdbIdWithChainCode;
239   }
240
241   /**
242    * Populates search target combo-box options
243    */
244   public void populateCmbSearchTargetOptions()
245   {
246     List<PDBDocField> searchableTargets = new ArrayList<PDBDocField>();
247     searchableTargets.add(PDBDocField.PDB_ID);
248     searchableTargets.add(PDBDocField.PFAM_ACCESSION);
249     searchableTargets.add(PDBDocField.MOLECULE_TYPE);
250     searchableTargets.add(PDBDocField.MOLECULE_NAME);
251     searchableTargets.add(PDBDocField.UNIPROT_ACCESSION);
252     searchableTargets.add(PDBDocField.GENE_NAME);
253     searchableTargets.add(PDBDocField.GENUS);
254     searchableTargets.add(PDBDocField.ALL);
255
256     Collections.sort(searchableTargets, new Comparator<PDBDocField>()
257     {
258       @Override
259       public int compare(PDBDocField o1, PDBDocField o2)
260       {
261         return o1.getName().compareTo(o2.getName());
262       }
263     });
264
265     for (PDBDocField searchTarget : searchableTargets)
266     {
267       cmb_searchTarget.addItem(searchTarget);
268     }
269   }
270
271   public void checkForErrors()
272   {
273     lbl_warning.setVisible(false);
274     if (errorWarning.length() > 0)
275     {
276       lbl_warning.setToolTipText(JvSwingUtils.wrapTooltip(true,
277               errorWarning.toString()));
278       lbl_warning.setVisible(true);
279     }
280   }
281 }