+
+ /**
+ * Builds a query string for a given sequences using its DBRef entries
+ *
+ * @param seq
+ * the sequences to build a query for
+ * @return the built query string
+ */
+
+ public static String buildQuery(SequenceI seq)
+ {
+ HashSet<String> seqRefs = new LinkedHashSet<String>();
+ String seqName = seq.getName();
+ String[] names = seqName.toLowerCase().split("\\|");
+ for (String name : names)
+ {
+ // System.out.println("Found name : " + name);
+ name.trim();
+ if (isValidSeqName(name))
+ {
+ seqRefs.add(name);
+ }
+ }
+
+ if (seq.getAllPDBEntries() != null)
+ {
+ for (PDBEntry entry : seq.getAllPDBEntries())
+ {
+ if (isValidSeqName(entry.getId()))
+ {
+ seqRefs.add(entry.getId());
+ }
+ }
+ }
+
+ if (seq.getDBRef() != null && seq.getDBRef().length != 0)
+ {
+ int count = 0;
+ for (DBRefEntry dbRef : seq.getDBRef())
+ {
+ if (isValidSeqName(getDBRefId(dbRef)))
+ {
+ seqRefs.add(getDBRefId(dbRef));
+ }
+ ++count;
+ if (count > 10)
+ {
+ break;
+ }
+ }
+ }
+
+ StringBuilder queryBuilder = new StringBuilder();
+ for (String seqRef : seqRefs)
+ {
+ queryBuilder.append("text:").append(seqRef).append(" OR ");
+ }
+ int endIndex = queryBuilder.lastIndexOf(" OR ");
+
+ if (queryBuilder.toString().length() < 6)
+ {
+ return null;
+ }
+ String query = queryBuilder.toString().substring(5, endIndex);
+ return query;
+ }
+
+ /**
+ * Ensures sequence ref names are not less than 3 characters and does not
+ * contain a database name
+ *
+ * @param seqName
+ * @return
+ */
+ public static boolean isValidSeqName(String seqName)
+ {
+ // System.out.println("seqName : " + seqName);
+ String ignoreList = "pdb,uniprot,swiss-prot";
+ if (seqName.length() < 3)
+ {
+ return false;
+ }
+ if (seqName.contains(":"))
+ {
+ return false;
+ }
+ seqName = seqName.toLowerCase();
+ for (String ignoredEntry : ignoreList.split(","))
+ {
+ if (seqName.contains(ignoredEntry))
+ {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public static String getDBRefId(DBRefEntry dbRef)
+ {
+ String ref = dbRef.getAccessionId().replaceAll("GO:", "");
+ return ref;
+ }
+
+ /**
+ * Filters a given list of discovered structures based on supplied argument
+ *
+ * @param fieldToFilterBy
+ * the field to filter by
+ */
+ public void filterResultSet(final String fieldToFilterBy)
+ {
+ Thread filterThread = new Thread(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ long startTime = System.currentTimeMillis();
+ lbl_loading.setVisible(true);
+ Collection<PDBDocField> wantedFields = PDBDocFieldPreferences
+ .getStructureSummaryFields();
+ Collection<PDBResponseSummary> filteredResponse = new HashSet<PDBResponseSummary>();
+ HashSet<String> errors = new HashSet<String>();
+ for (SequenceI seq : selectedSequences)
+ {
+ PDBRestRequest pdbRequest = new PDBRestRequest();
+ pdbRequest.setAllowEmptySeq(false);
+ pdbRequest.setResponseSize(1);
+ pdbRequest.setFieldToSearchBy("(text:");
+ pdbRequest.setFieldToSortBy(fieldToFilterBy,
+ !chk_invertFilter.isSelected());
+ pdbRequest.setSearchTerm(buildQuery(seq) + ")");
+ pdbRequest.setWantedFields(wantedFields);
+ pdbRequest.setAssociatedSequence(seq);
+ pdbRestCleint = new PDBRestClient();
+ PDBRestResponse resultList;
+ try
+ {
+ resultList = pdbRestCleint.executeRequest(pdbRequest);
+ } catch (Exception e)
+ {
+ e.printStackTrace();
+ errors.add(e.getMessage());
+ continue;
+ }
+ lastPdbRequest = pdbRequest;
+ if (resultList.getSearchSummary() != null
+ && !resultList.getSearchSummary().isEmpty())
+ {
+ filteredResponse.addAll(resultList.getSearchSummary());
+ }
+ }
+
+ String totalTime = (System.currentTimeMillis() - startTime)
+ + " milli secs";
+ if (!filteredResponse.isEmpty())
+ {
+ final int filterResponseCount = filteredResponse.size();
+ Collection<PDBResponseSummary> reorderedStructuresSet = new LinkedHashSet<PDBResponseSummary>();
+ reorderedStructuresSet.addAll(filteredResponse);
+ reorderedStructuresSet.addAll(discoveredStructuresSet);
+ tbl_summary.setModel(PDBRestResponse.getTableModel(
+ lastPdbRequest, reorderedStructuresSet));
+
+ // Update table selection model here
+ tbl_summary.addRowSelectionInterval(0, filterResponseCount - 1);
+ mainFrame.setTitle(MessageManager.formatMessage(
+ "label.structure_chooser_filter_time",
+ totalTime));
+ }
+ else
+ {
+ mainFrame.setTitle(MessageManager.formatMessage(
+ "label.structure_chooser_filter_time",
+ totalTime));
+ if (errors.size() > 0)
+ {
+ StringBuilder errorMsg = new StringBuilder();
+ for (String error : errors)
+ {
+ errorMsg.append(error).append("\n");
+ }
+ JOptionPane.showMessageDialog(null, errorMsg.toString(),
+ MessageManager.getString("label.pdb_web-service_error"),
+ JOptionPane.ERROR_MESSAGE);
+ }
+ }
+
+ lbl_loading.setVisible(false);
+
+ validateSelections();
+ }
+ });
+ filterThread.start();
+ }
+
+ /**
+ * Handles action event for btn_pdbFromFile
+ */