+ /**
+ * Initializes parameters used by the Structure Chooser Panel
+ */
+ public void init()
+ {
+ if (!Jalview.isHeadlessMode())
+ {
+ progressBar = new ProgressBar(this.statusPanel, this.statusBar);
+ }
+
+ // ensure a filter option is in force for search
+ populateFilterComboBox(true, cachedPDBExists);
+ Thread discoverPDBStructuresThread = new Thread(new Runnable()
+ {
+ @Override
+ public void run()
+ {
+ long startTime = System.currentTimeMillis();
+ updateProgressIndicator(MessageManager
+ .getString("status.loading_cached_pdb_entries"), startTime);
+ loadLocalCachedPDBEntries();
+ updateProgressIndicator(null, startTime);
+ updateProgressIndicator(MessageManager
+ .getString("status.searching_for_pdb_structures"),
+ startTime);
+ fetchStructuresMetaData();
+ // revise filter options if no results were found
+ populateFilterComboBox(isStructuresDiscovered(), cachedPDBExists);
+ updateProgressIndicator(null, startTime);
+ mainFrame.setVisible(true);
+ updateCurrentView();
+ }
+ });
+ discoverPDBStructuresThread.start();
+ }
+
+ /**
+ * Updates the progress indicator with the specified message
+ *
+ * @param message
+ * displayed message for the operation
+ * @param id
+ * unique handle for this indicator
+ */
+ public void updateProgressIndicator(String message, long id)
+ {
+ if (progressIndicator != null)
+ {
+ progressIndicator.setProgressBar(message, id);
+ }
+ }
+
+ /**
+ * Retrieve meta-data for all the structure(s) for a given sequence(s) in a
+ * selection group
+ */
+ public void fetchStructuresMetaData()
+ {
+ long startTime = System.currentTimeMillis();
+ pdbRestCleint = PDBFTSRestClient.getInstance();
+ Collection<FTSDataColumnI> wantedFields = pdbDocFieldPrefs
+ .getStructureSummaryFields();
+
+ discoveredStructuresSet = new LinkedHashSet<FTSData>();
+ HashSet<String> errors = new HashSet<String>();
+ for (SequenceI seq : selectedSequences)
+ {
+ FTSRestRequest pdbRequest = new FTSRestRequest();
+ pdbRequest.setAllowEmptySeq(false);
+ pdbRequest.setResponseSize(500);
+ pdbRequest.setFieldToSearchBy("(");
+ FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption
+ .getSelectedItem());
+ pdbRequest.setFieldToSortBy(selectedFilterOpt.getValue(),
+ !chk_invertFilter.isSelected());
+ pdbRequest.setWantedFields(wantedFields);
+ pdbRequest.setSearchTerm(buildQuery(seq) + ")");
+ pdbRequest.setAssociatedSequence(seq);
+ FTSRestResponse 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())
+ {
+ discoveredStructuresSet.addAll(resultList.getSearchSummary());
+ }
+ }
+
+ int noOfStructuresFound = 0;
+ String totalTime = (System.currentTimeMillis() - startTime)
+ + " milli secs";
+ if (discoveredStructuresSet != null
+ && !discoveredStructuresSet.isEmpty())
+ {
+ getResultTable().setModel(
+ FTSRestResponse.getTableModel(lastPdbRequest,
+ discoveredStructuresSet));
+ noOfStructuresFound = discoveredStructuresSet.size();
+ mainFrame.setTitle(MessageManager.formatMessage(
+ "label.structure_chooser_no_of_structures",
+ noOfStructuresFound, totalTime));
+ }
+ else
+ {
+ mainFrame.setTitle(MessageManager
+ .getString("label.structure_chooser_manual_association"));
+ if (errors.size() > 0)
+ {
+ StringBuilder errorMsg = new StringBuilder();
+ for (String error : errors)
+ {
+ errorMsg.append(error).append("\n");
+ }
+ JvOptionPane.showMessageDialog(this, errorMsg.toString(),
+ MessageManager.getString("label.pdb_web-service_error"),
+ JvOptionPane.ERROR_MESSAGE);
+ }
+ }
+ }
+
+ public void loadLocalCachedPDBEntries()
+ {
+ ArrayList<CachedPDB> entries = new ArrayList<CachedPDB>();
+ for (SequenceI seq : selectedSequences)
+ {
+ if (seq.getDatasetSequence() != null
+ && seq.getDatasetSequence().getAllPDBEntries() != null)
+ {
+ for (PDBEntry pdbEntry : seq.getDatasetSequence()
+ .getAllPDBEntries())
+ {
+ if (pdbEntry.getFile() != null)
+ {
+ entries.add(new CachedPDB(seq, pdbEntry));
+ }
+ }
+ }
+ }
+ cachedPDBExists = !entries.isEmpty();
+ PDBEntryTableModel tableModelx = new PDBEntryTableModel(entries);
+ tbl_local_pdb.setModel(tableModelx);
+ }
+
+ /**
+ * 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)
+ {
+ boolean isPDBRefsFound = false;
+ boolean isUniProtRefsFound = false;
+ StringBuilder queryBuilder = new StringBuilder();
+ Set<String> seqRefs = new LinkedHashSet<String>();
+
+ if (seq.getAllPDBEntries() != null
+ && queryBuilder.length() < MAX_QLENGTH)
+ {
+ for (PDBEntry entry : seq.getAllPDBEntries())
+ {
+ if (isValidSeqName(entry.getId()))
+ {
+ queryBuilder.append("pdb_id:")
+ .append(entry.getId().toLowerCase()).append(" OR ");
+ isPDBRefsFound = true;
+ }
+ }
+ }
+
+ if (seq.getDBRefs() != null && seq.getDBRefs().length != 0)
+ {
+ for (DBRefEntry dbRef : seq.getDBRefs())
+ {
+ if (isValidSeqName(getDBRefId(dbRef))
+ && queryBuilder.length() < MAX_QLENGTH)
+ {
+ if (dbRef.getSource().equalsIgnoreCase(DBRefSource.UNIPROT))
+ {
+ queryBuilder.append("uniprot_accession:")
+ .append(getDBRefId(dbRef)).append(" OR ");
+ queryBuilder.append("uniprot_id:").append(getDBRefId(dbRef))
+ .append(" OR ");
+ isUniProtRefsFound = true;
+ }
+ else if (dbRef.getSource().equalsIgnoreCase(DBRefSource.PDB))
+ {
+
+ queryBuilder.append("pdb_id:")
+ .append(getDBRefId(dbRef).toLowerCase()).append(" OR ");
+ isPDBRefsFound = true;
+ }
+ else
+ {
+ seqRefs.add(getDBRefId(dbRef));
+ }
+ }
+ }
+ }
+
+ if (!isPDBRefsFound && !isUniProtRefsFound)
+ {
+ String seqName = seq.getName();
+ seqName = sanitizeSeqName(seqName);
+ String[] names = seqName.toLowerCase().split("\\|");
+ for (String name : names)
+ {
+ // System.out.println("Found name : " + name);
+ name.trim();
+ if (isValidSeqName(name))
+ {
+ seqRefs.add(name);
+ }
+ }
+
+ 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(0, endIndex);
+ return query;
+ }
+
+ /**
+ * Remove the following special characters from input string +, -, &, !, (, ),
+ * {, }, [, ], ^, ", ~, *, ?, :, \
+ *
+ * @param seqName
+ * @return
+ */
+ static String sanitizeSeqName(String seqName)
+ {
+ Objects.requireNonNull(seqName);
+ return seqName.replaceAll("\\[\\d*\\]", "")
+ .replaceAll("[^\\dA-Za-z|_]", "").replaceAll("\\s+", "+");
+ }
+
+ /**
+ * 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)