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