JAL-2136 Introduced DynamicData model, modified AnnotionFile to store Phyre meta...
[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   private String ftsFrameTitle = defaultFTSFrameTitle;
45
46   private static Map<String, Integer> tempUserPrefs = new HashMap<String, Integer>();
47
48   public UniprotFTSPanel(SequenceFetcher seqFetcher)
49   {
50     super();
51     pageLimit = UniProtFTSRestClient.getInstance()
52             .getDefaultResponsePageSize();
53     this.seqFetcher = seqFetcher;
54     this.progressIndicator = (seqFetcher == null) ? null : seqFetcher
55             .getProgressIndicator();
56   }
57
58   @Override
59   public void searchAction(boolean isFreshSearch)
60   {
61     if (isFreshSearch)
62     {
63       offSet = 0;
64     }
65     new Thread()
66     {
67       @Override
68       public void run()
69       {
70         ftsFrameTitle = defaultFTSFrameTitle;
71         reset();
72         if (getTypedText().length() > 0)
73         {
74           setSearchInProgress(true);
75           long startTime = System.currentTimeMillis();
76
77           String searchTarget = ((FTSDataColumnI) cmb_searchTarget
78                   .getSelectedItem()).getAltCode();
79
80           wantedFields = UniProtFTSRestClient.getInstance()
81                   .getAllDefaultDisplayedFTSDataColumns();
82           String searchTerm = decodeSearchTerm(txt_search.getText(),
83                   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       }
148     }.start();
149
150   }
151
152   public String decodeSearchTerm(String enteredText, String targetField)
153   {
154     int searchTargetLength = targetField.equalsIgnoreCase("Search All") ? 0
155             : targetField.length() + 1;
156     String searchTarget = targetField.equalsIgnoreCase("Search All") ? ""
157             : targetField + ":";
158     String foundSearchTerms = enteredText;
159     StringBuilder foundSearchTermsBuilder = new StringBuilder();
160     if (enteredText.contains(";"))
161     {
162       String[] searchTerms = enteredText.split(";");
163       for (String searchTerm : searchTerms)
164       {
165         foundSearchTermsBuilder.append(searchTarget).append(searchTerm)
166                 .append(" OR ");
167       }
168       int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
169       foundSearchTerms = foundSearchTermsBuilder.toString();
170       if (foundSearchTerms.contains(" OR "))
171       {
172         foundSearchTerms = foundSearchTerms.substring(searchTargetLength,
173                 endIndex);
174       }
175     }
176     return foundSearchTerms;
177   }
178
179   @Override
180   public boolean isPaginationEnabled()
181   {
182     return true;
183   }
184
185   @Override
186   public void okAction()
187   {
188     disableActionButtons();
189     StringBuilder selectedIds = new StringBuilder();
190     HashSet<String> selectedIdsSet = new HashSet<String>();
191     int primaryKeyColIndex = 0;
192     try
193     {
194       primaryKeyColIndex = getFTSRestClient().getPrimaryKeyColumIndex(
195               wantedFields, false);
196     } catch (Exception e)
197     {
198       e.printStackTrace();
199     }
200     int[] selectedRows = getResultTable().getSelectedRows();
201     for (int summaryRow : selectedRows)
202     {
203       String idStr = getResultTable().getValueAt(summaryRow,
204               primaryKeyColIndex).toString();
205       selectedIdsSet.add(idStr);
206     }
207     selectedIdsSet.addAll(paginatorCart);
208     for (String selectedId : selectedIdsSet)
209     {
210       selectedIds.append(selectedId).append(";");
211     }
212
213     String ids = selectedIds.toString();
214     // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
215     seqFetcher.getTextArea().setText(ids);
216     Thread worker = new Thread(seqFetcher);
217     worker.start();
218     delayAndEnableActionButtons();
219   }
220
221   @Override
222   public FTSRestClientI getFTSRestClient()
223   {
224     return UniProtFTSRestClient.getInstance();
225   }
226
227   @Override
228   public String getFTSFrameTitle()
229   {
230     return ftsFrameTitle;
231   }
232
233   @Override
234   public Map<String, Integer> getTempUserPrefs()
235   {
236     return tempUserPrefs;
237   }
238
239 }