2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
24 import jalview.jbgui.GPDBSearchPanel;
25 import jalview.jbgui.PDBDocFieldPreferences;
26 import jalview.util.MessageManager;
27 import jalview.ws.dbsources.PDBRestClient;
28 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
29 import jalview.ws.uimodel.PDBRestRequest;
30 import jalview.ws.uimodel.PDBRestResponse;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.Comparator;
36 import java.util.HashSet;
37 import java.util.List;
39 import javax.swing.table.DefaultTableModel;
41 @SuppressWarnings("serial")
42 public class PDBSearchPanel extends GPDBSearchPanel
44 private SequenceFetcher seqFetcher;
46 private IProgressIndicator progressIdicator;
48 private Collection<PDBDocField> wantedFields;
50 public PDBSearchPanel(SequenceFetcher seqFetcher)
52 this.seqFetcher = seqFetcher;
53 this.progressIdicator = (seqFetcher == null) ? null : seqFetcher
54 .getProgressIndicator();
59 * Action performed when an input is detected on txt_search field.
62 public void txt_search_ActionPerformed()
64 errorWarning.setLength(0);
65 lbl_warning.setVisible(false);
66 btn_ok.setEnabled(false);
67 boolean allowEmptySequence = false;
68 mainFrame.setTitle(MessageManager
69 .getString("label.pdb_sequence_getcher"));
70 tbl_summary.setModel(new DefaultTableModel());
71 if (txt_search.getText().trim().length() > 0)
73 long startTime = System.currentTimeMillis();
75 String searchTarget = ((PDBDocField) cmb_searchTarget
76 .getSelectedItem()).getCode();
78 wantedFields = PDBDocFieldPreferences
79 .getSearchSummaryFields();
81 String searchTerm = decodeSearchTerm(txt_search.getText(),
84 PDBRestRequest request = new PDBRestRequest();
85 request.setAllowEmptySeq(allowEmptySequence);
86 request.setResponseSize(100);
87 request.setFieldToSearchBy("(" + searchTarget + ":");
88 request.setSearchTerm(searchTerm + ")");
89 request.setWantedFields(wantedFields);
90 // System.out.println(">>>>>>>>>>>>>> " + request.getQuery());
91 PDBRestClient pdbRestCleint = new PDBRestClient();
92 PDBRestResponse resultList;
95 resultList = pdbRestCleint.executeRequest(request);
98 // System.out.println(">>>>>>>>>>>>>>>" + e.getMessage());
100 errorWarning.append(e.getMessage());
105 if (resultList.getSearchSummary() != null)
107 tbl_summary.setModel(PDBRestResponse.getTableModel(request,
108 resultList.getSearchSummary()));
111 long endTime = System.currentTimeMillis();
112 int resultSetCount = resultList.getNumberOfItemsFound();
113 String result = (resultSetCount > 1) ? MessageManager
114 .getString("label.results") : MessageManager
115 .getString("label.result");
116 mainFrame.setTitle(frameTitle + " - " + resultSetCount + " " + result
117 + " (" + (endTime - startTime) + " milli secs)");
121 public static String decodeSearchTerm(String enteredText,
124 String foundSearchTerms = enteredText;
125 StringBuilder foundSearchTermsBuilder = new StringBuilder();
126 if (enteredText.contains(";"))
128 String[] searchTerms = enteredText.split(";");
129 for (String searchTerm : searchTerms)
131 if (searchTerm.contains(":"))
133 foundSearchTermsBuilder.append(targetField).append(":")
134 .append(searchTerm.split(":")[0]).append(" OR ");
138 foundSearchTermsBuilder.append(targetField).append(":")
139 .append(searchTerm).append(" OR ");
142 int endIndex = foundSearchTermsBuilder.lastIndexOf(" OR ");
143 foundSearchTerms = foundSearchTermsBuilder.toString();
144 if (foundSearchTerms.contains(" OR "))
146 foundSearchTerms = foundSearchTerms.substring(
147 targetField.length() + 1, endIndex);
150 else if (enteredText.contains(":"))
152 foundSearchTerms = foundSearchTerms.split(":")[0];
154 return foundSearchTerms;
159 public void btn_ok_ActionPerformed()
161 loadSelectedPDBSequencesToAlignment();
165 public void btn_back_ActionPerformed()
168 new SequenceFetcher(progressIdicator);
172 public void btn_cancel_ActionPerformed()
177 public void transferToSequenceFetcher(String ids)
179 // mainFrame.dispose();
180 seqFetcher.textArea.setText(ids);
181 Thread worker = new Thread(seqFetcher);
186 * Add the discovered/selected sequences to a target alignment window
188 public void loadSelectedPDBSequencesToAlignment()
190 // mainFrame.dispose();
191 StringBuilder selectedIds = new StringBuilder();
192 HashSet<String> selectedIdsSet = new HashSet<String>();
193 int pdbIdCol = PDBRestClient.getPDBIdColumIndex(wantedFields, false);
194 int[] selectedRows = tbl_summary.getSelectedRows();
195 for (int summaryRow : selectedRows)
197 String pdbIdStr = tbl_summary.getValueAt(summaryRow, pdbIdCol)
199 String searchTerm = txt_search.getText();
200 selectedIdsSet.add(getPDBIdwithSpecifiedChain(pdbIdStr, searchTerm));
203 for (String selectedId : selectedIdsSet)
205 selectedIds.append(selectedId).append(";");
208 String ids = selectedIds.toString();
209 // System.out.println(">>>>>>>>>>>>>>>> selected Ids: " + ids);
210 seqFetcher.textArea.setText(ids);
211 Thread worker = new Thread(seqFetcher);
215 public static String getPDBIdwithSpecifiedChain(String pdbId,
218 String pdbIdWithChainCode = "";
219 if (searchTerm.contains(";"))
221 String[] foundTerms = searchTerm.split(";");
222 for (String foundTerm : foundTerms)
224 if (foundTerm.contains(pdbId))
226 pdbIdWithChainCode = foundTerm;
230 else if (searchTerm.contains(pdbId))
232 pdbIdWithChainCode = searchTerm;
236 pdbIdWithChainCode = pdbId;
238 return pdbIdWithChainCode;
242 * Populates search target combo-box options
244 public void populateCmbSearchTargetOptions()
246 List<PDBDocField> searchableTargets = new ArrayList<PDBDocField>();
247 searchableTargets.add(PDBDocField.PDB_ID);
248 searchableTargets.add(PDBDocField.PFAM_ACCESSION);
249 searchableTargets.add(PDBDocField.MOLECULE_TYPE);
250 searchableTargets.add(PDBDocField.MOLECULE_NAME);
251 searchableTargets.add(PDBDocField.UNIPROT_ACCESSION);
252 searchableTargets.add(PDBDocField.GENE_NAME);
253 searchableTargets.add(PDBDocField.GENUS);
254 searchableTargets.add(PDBDocField.ALL);
256 Collections.sort(searchableTargets, new Comparator<PDBDocField>()
259 public int compare(PDBDocField o1, PDBDocField o2)
261 return o1.getName().compareTo(o2.getName());
265 for (PDBDocField searchTarget : searchableTargets)
267 cmb_searchTarget.addItem(searchTarget);
271 public void checkForErrors()
273 lbl_warning.setVisible(false);
274 if (errorWarning.length() > 0)
276 lbl_warning.setToolTipText(JvSwingUtils.wrapTooltip(true,
277 errorWarning.toString()));
278 lbl_warning.setVisible(true);