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