JAL-3144 SequenceFetcher without JDatabaseTree
[jalview.git] / src / jalview / fts / service / uniprot / UniprotFTSPanel.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.uniprot;
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.HashMap;
33 import java.util.HashSet;
34 import java.util.Map;
35
36 @SuppressWarnings("serial")
37 public class UniprotFTSPanel extends GFTSPanel
38 {
39
40   private static String defaultFTSFrameTitle = MessageManager
41           .getString("label.uniprot_sequence_fetcher");
42
43   private static Map<String, Integer> tempUserPrefs = new HashMap<>();
44
45   private static final String UNIPROT_FTS_CACHE_KEY = "CACHE.UNIPROT_FTS";
46
47   private static final String UNIPROT_AUTOSEARCH = "FTS.UNIPROT.AUTOSEARCH";
48
49   /**
50    * Constructor given an (optional) sequence fetcher panel to revert to on
51    * clicking the 'Back' button
52    * 
53    * @param fetcher
54    */
55   public UniprotFTSPanel(SequenceFetcher fetcher)
56   {
57     super(fetcher);
58     pageLimit = UniProtFTSRestClient.getInstance()
59             .getDefaultResponsePageSize();
60     this.seqFetcher = fetcher;
61     this.progressIndicator = (fetcher == null) ? null
62             : fetcher.getProgressIndicator();
63   }
64
65   @Override
66   public void searchAction(boolean isFreshSearch)
67   {
68     mainFrame.requestFocusInWindow();
69     if (isFreshSearch)
70     {
71       offSet = 0;
72     }
73     new Thread()
74     {
75       @Override
76       public void run()
77       {
78         reset();
79         String searchInput = getTypedText();
80         if (searchInput.length() > 0)
81         {
82           setSearchInProgress(true);
83           long startTime = System.currentTimeMillis();
84           searchInput = getTypedText();
85           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
86                   .getSelectedItem()).getAltCode();
87           wantedFields = UniProtFTSRestClient.getInstance()
88                   .getAllDefaultDisplayedFTSDataColumns();
89           String searchTerm = decodeSearchTerm(searchInput, searchTarget);
90
91           FTSRestRequest request = new FTSRestRequest();
92           request.setFieldToSearchBy(searchTarget);
93           request.setSearchTerm(searchTerm);
94           request.setOffSet(offSet);
95           request.setWantedFields(wantedFields);
96           FTSRestClientI uniProtRestClient = UniProtFTSRestClient
97                   .getInstance();
98           FTSRestResponse resultList;
99           try
100           {
101             resultList = uniProtRestClient.executeRequest(request);
102           } catch (Exception e)
103           {
104             setErrorMessage(e.getMessage());
105             checkForErrors();
106             setSearchInProgress(false);
107             return;
108           }
109
110           if (resultList.getSearchSummary() != null
111                   && resultList.getSearchSummary().size() > 0)
112           {
113             getResultTable().setModel(FTSRestResponse.getTableModel(request,
114                     resultList.getSearchSummary()));
115             FTSRestResponse.configureTableColumn(getResultTable(),
116                     wantedFields, tempUserPrefs);
117             getResultTable().setVisible(true);
118           }
119
120           long endTime = System.currentTimeMillis();
121           totalResultSetCount = resultList.getNumberOfItemsFound();
122           resultSetCount = resultList.getSearchSummary() == null ? 0
123                   : resultList.getSearchSummary().size();
124           String result = (resultSetCount > 0)
125                   ? MessageManager.getString("label.results")
126                   : MessageManager.getString("label.result");
127           if (isPaginationEnabled() && resultSetCount > 0)
128           {
129             updateSearchFrameTitle(defaultFTSFrameTitle + " - " + result
130                     + " "
131                     + totalNumberformatter
132                             .format(Integer.valueOf(offSet + 1))
133                     + " to "
134                     + totalNumberformatter
135                             .format(Integer
136                                     .valueOf(offSet + resultSetCount))
137                     + " of "
138                     + totalNumberformatter
139                             .format(Integer.valueOf(totalResultSetCount))
140                     + " " + " (" + (endTime - startTime) + " milli secs)");
141           }
142           else
143           {
144             updateSearchFrameTitle(defaultFTSFrameTitle + " - "
145                     + resultSetCount + " " + result + " ("
146                     + (endTime - startTime) + " milli secs)");
147           }
148           setSearchInProgress(false);
149           refreshPaginatorState();
150           updateSummaryTableSelections();
151         }
152         txt_search.updateCache();
153       }
154     }.start();
155
156   }
157
158   public String decodeSearchTerm(String enteredText, String targetField)
159   {
160     int searchTargetLength = targetField.equalsIgnoreCase("Search All") ? 0
161             : targetField.length() + 1;
162     String searchTarget = targetField.equalsIgnoreCase("Search All") ? ""
163             : targetField + ":";
164     String foundSearchTerms = enteredText;
165     StringBuilder foundSearchTermsBuilder = new StringBuilder();
166     if (enteredText.contains(";"))
167     {
168       String[] searchTerms = enteredText.split(";");
169       for (String searchTerm : searchTerms)
170       {
171         foundSearchTermsBuilder.append(searchTarget).append(searchTerm)
172                 .append(" OR ");
173       }
174       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
175       foundSearchTerms = foundSearchTermsBuilder.toString();
176       if (foundSearchTerms.contains(" OR "))
177       {
178         foundSearchTerms = foundSearchTerms.substring(searchTargetLength,
179                 endIndex);
180       }
181     }
182     return foundSearchTerms;
183   }
184
185   @Override
186   public boolean isPaginationEnabled()
187   {
188     return true;
189   }
190
191   @Override
192   public void okAction()
193   {
194     disableActionButtons();
195     StringBuilder selectedIds = new StringBuilder();
196     HashSet<String> selectedIdsSet = new HashSet<>();
197     int primaryKeyColIndex = 0;
198     try
199     {
200       primaryKeyColIndex = getFTSRestClient()
201               .getPrimaryKeyColumIndex(wantedFields, false);
202     } catch (Exception e)
203     {
204       e.printStackTrace();
205     }
206     int[] selectedRows = getResultTable().getSelectedRows();
207     for (int summaryRow : selectedRows)
208     {
209       String idStr = getResultTable()
210               .getValueAt(summaryRow, primaryKeyColIndex).toString();
211       selectedIdsSet.add(idStr);
212     }
213     selectedIdsSet.addAll(paginatorCart);
214     for (String selectedId : selectedIdsSet)
215     {
216       selectedIds.append(selectedId).append(";");
217     }
218
219     String ids = selectedIds.toString();
220     seqFetcher.setQuery(ids);
221     Thread worker = new Thread(seqFetcher);
222     worker.start();
223     delayAndEnableActionButtons();
224   }
225
226   @Override
227   public FTSRestClientI getFTSRestClient()
228   {
229     return UniProtFTSRestClient.getInstance();
230   }
231
232   @Override
233   public String getFTSFrameTitle()
234   {
235     return defaultFTSFrameTitle;
236   }
237
238   @Override
239   public Map<String, Integer> getTempUserPrefs()
240   {
241     return tempUserPrefs;
242   }
243
244   @Override
245   public String getCacheKey()
246   {
247     return UNIPROT_FTS_CACHE_KEY;
248   }
249
250   @Override
251   public String getAutosearchPreference()
252   {
253     return UNIPROT_AUTOSEARCH;
254   }
255 }