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