JAL-2101 JAL-2071 JAL-1563 Added fix to enable searching for old entry names in Unipr...
[jalview.git] / src / jalview / fts / service / pdb / PDBFTSPanel.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
22 package jalview.fts.service.pdb;
23
24 import jalview.fts.api.FTSDataColumnI;
25 import jalview.fts.api.FTSRestClientI;
26 import jalview.fts.core.FTSRestRequest;
27 import jalview.fts.core.FTSRestResponse;
28 import jalview.fts.core.GFTSPanel;
29 import jalview.gui.SequenceFetcher;
30 import jalview.util.MessageManager;
31
32 import java.util.HashSet;
33
34 @SuppressWarnings("serial")
35 public class PDBFTSPanel extends GFTSPanel
36 {
37   private static String defaultFTSFrameTitle = MessageManager
38           .getString("label.pdb_sequence_fetcher");
39
40   private String ftsFrameTitle = defaultFTSFrameTitle;
41
42   public PDBFTSPanel(SequenceFetcher seqFetcher)
43   {
44     pageLimit = PDBFTSRestClient.getInstance().getDefaultResponsePageSize();
45     this.seqFetcher = seqFetcher;
46     this.progressIdicator = (seqFetcher == null) ? null : seqFetcher
47             .getProgressIndicator();
48   }
49
50
51   @Override
52   public void searchAction(boolean isFreshSearch)
53   {
54     if (isFreshSearch)
55     {
56       offSet = 0;
57     }
58     new Thread()
59     {
60       @Override
61       public void run()
62       {
63         ftsFrameTitle = defaultFTSFrameTitle;
64         reset();
65         boolean allowEmptySequence = false;
66         if (getTypedText().length() > 0)
67         {
68           setSearchInProgress(true);
69           long startTime = System.currentTimeMillis();
70
71           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
72                   .getSelectedItem()).getCode();
73           wantedFields = PDBFTSRestClient.getInstance()
74                   .getAllDefaulDisplayedDataColumns();
75           String searchTerm = decodeSearchTerm(txt_search.getText(),
76                   searchTarget);
77
78           FTSRestRequest request = new FTSRestRequest();
79           request.setAllowEmptySeq(allowEmptySequence);
80           request.setResponseSize(100);
81           request.setFieldToSearchBy("(" + searchTarget + ":");
82           request.setSearchTerm(searchTerm + ")");
83           request.setOffSet(offSet);
84           request.setWantedFields(wantedFields);
85           FTSRestClientI pdbRestCleint = PDBFTSRestClient.getInstance();
86           FTSRestResponse resultList;
87           try
88           {
89             resultList = pdbRestCleint.executeRequest(request);
90           } catch (Exception e)
91           {
92             setErrorMessage(e.getMessage());
93             checkForErrors();
94             return;
95           }
96
97           if (resultList.getSearchSummary() != null
98                   && resultList.getSearchSummary().size() > 0)
99           {
100             getResultTable().setModel(
101                     FTSRestResponse.getTableModel(request,
102                     resultList.getSearchSummary()));
103             FTSRestResponse.configureTableColumn(getResultTable(),
104                     wantedFields);
105             getResultTable().setVisible(true);
106           }
107
108           long endTime = System.currentTimeMillis();
109           totalResultSetCount = resultList.getNumberOfItemsFound();
110           resultSetCount = resultList.getSearchSummary() == null ? 0
111                   : resultList.getSearchSummary().size();
112           String result = (resultSetCount > 0) ? MessageManager
113                   .getString("label.results") : MessageManager
114                   .getString("label.result");
115          
116           if (isPaginationEnabled() && resultSetCount > 0)
117           {
118             updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
119                     + " "
120                     + totalNumberformatter.format((Number) (offSet + 1))
121                     + " to "
122                     + totalNumberformatter
123                             .format((Number) (offSet + resultSetCount))
124                     + " of "
125                     + totalNumberformatter
126                             .format((Number) totalResultSetCount)
127                     + " " + " (" + (endTime - startTime) + " milli secs)");
128           }
129           else
130           {
131             updateSearchFrameTitle(defaultFTSFrameTitle + " - "
132                     + resultSetCount + " " + result + " ("
133                     + (endTime - startTime) + " milli secs)");
134           }
135           
136           setSearchInProgress(false);
137           refreshPaginatorState();
138           updateSummaryTableSelections();
139         }
140       }
141     }.start();
142   }
143
144   public static String decodeSearchTerm(String enteredText,
145           String targetField)
146   {
147     String foundSearchTerms = enteredText;
148     StringBuilder foundSearchTermsBuilder = new StringBuilder();
149     if (enteredText.contains(";"))
150     {
151       String[] searchTerms = enteredText.split(";");
152       for (String searchTerm : searchTerms)
153       {
154         if (searchTerm.contains(":"))
155         {
156           foundSearchTermsBuilder.append(targetField).append(":")
157                   .append(searchTerm.split(":")[0]).append(" OR ");
158         }
159         else
160         {
161           foundSearchTermsBuilder.append(targetField).append(":")
162                   .append(searchTerm).append(" OR ");
163         }
164       }
165       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
166       foundSearchTerms = foundSearchTermsBuilder.toString();
167       if (foundSearchTerms.contains(" OR "))
168       {
169         foundSearchTerms = foundSearchTerms.substring(
170                 targetField.length() + 1, endIndex);
171       }
172     }
173     else if (enteredText.contains(":"))
174     {
175       foundSearchTerms = foundSearchTerms.split(":")[0];
176     }
177     return foundSearchTerms;
178   }
179
180   @Override
181   public void okAction()
182   {
183     // mainFrame.dispose();
184     disableActionButtons();
185     StringBuilder selectedIds = new StringBuilder();
186     HashSet<String> selectedIdsSet = new HashSet<String>();
187     int primaryKeyColIndex = 0;
188     try
189     {
190       primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
191               wantedFields,
192               false);
193     } catch (Exception e)
194     {
195       e.printStackTrace();
196     }
197     int[] selectedRows = getResultTable().getSelectedRows();
198     String searchTerm = txt_search.getText();
199     for (int summaryRow : selectedRows)
200     {
201       String idStr = getResultTable().getValueAt(summaryRow,
202               primaryKeyColIndex)
203               .toString();
204       selectedIdsSet.add(getPDBIdwithSpecifiedChain(idStr, searchTerm));
205     }
206
207     for (String idStr : paginatorCart)
208     {
209       selectedIdsSet.add(getPDBIdwithSpecifiedChain(idStr, searchTerm));
210     }
211
212     for (String selectedId : selectedIdsSet)
213     {
214       selectedIds.append(selectedId).append(";");
215     }
216
217     String ids = selectedIds.toString();
218     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
219     seqFetcher.getTextArea().setText(ids);
220     Thread worker = new Thread(seqFetcher);
221     worker.start();
222     delayAndEnableActionButtons();
223   }
224
225
226   public static String getPDBIdwithSpecifiedChain(String pdbId,
227           String searchTerm)
228   {
229     String pdbIdWithChainCode = "";
230     if (searchTerm.contains(";"))
231     {
232       String[] foundTerms = searchTerm.split(";");
233       for (String foundTerm : foundTerms)
234       {
235         if (foundTerm.contains(pdbId))
236         {
237           pdbIdWithChainCode = foundTerm;
238         }
239       }
240     }
241     else if (searchTerm.contains(pdbId))
242     {
243       pdbIdWithChainCode = searchTerm;
244     }
245     else
246     {
247       pdbIdWithChainCode = pdbId;
248     }
249     return pdbIdWithChainCode;
250   }
251
252   @Override
253   public FTSRestClientI getFTSRestClient()
254   {
255     return PDBFTSRestClient.getInstance();
256   }
257
258   @Override
259   public String getFTSFrameTitle()
260   {
261     return ftsFrameTitle;
262   }
263
264   @Override
265   public boolean isPaginationEnabled()
266   {
267     return true;
268   }
269
270 }