X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FStructureChooser.java;h=ea2d50dcd2bf5636b56d061d60f74a3506cd0ed9;hb=023163e0fe8837c1ddb75eb30abc058f7747b5c2;hp=23b7fcf1ca9392937e2dba73f57d11cba22b2f48;hpb=26b115b0a77d521da92a06572d9b7819c2d0d49a;p=jalview.git diff --git a/src/jalview/gui/StructureChooser.java b/src/jalview/gui/StructureChooser.java index 23b7fcf..ea2d50d 100644 --- a/src/jalview/gui/StructureChooser.java +++ b/src/jalview/gui/StructureChooser.java @@ -31,11 +31,15 @@ import jalview.datamodel.SequenceI; import jalview.fts.api.FTSData; import jalview.fts.api.FTSDataColumnI; import jalview.fts.api.FTSRestClientI; +import jalview.fts.core.FTSDataColumnPreferences; import jalview.fts.core.FTSRestRequest; import jalview.fts.core.FTSRestResponse; import jalview.fts.service.pdb.PDBFTSRestClient; +import jalview.gui.structurechooser.PDBStructureChooserQuerySource; +import jalview.gui.structurechooser.StructureChooserQuerySource; import jalview.io.DataSourceType; import jalview.jbgui.GStructureChooser; +import jalview.jbgui.GStructureChooser.FilterOption; import jalview.structure.StructureMapping; import jalview.structure.StructureSelectionManager; import jalview.util.MessageManager; @@ -71,8 +75,6 @@ public class StructureChooser extends GStructureChooser { private static final String AUTOSUPERIMPOSE = "AUTOSUPERIMPOSE"; - private static int MAX_QLENGTH = 7820; - private SequenceI selectedSequence; private SequenceI[] selectedSequences; @@ -81,9 +83,13 @@ public class StructureChooser extends GStructureChooser private Collection discoveredStructuresSet; - private FTSRestRequest lastPdbRequest; + private StructureChooserQuerySource data; - private FTSRestClientI pdbRestClient; + @Override + protected FTSDataColumnPreferences getFTSDocFieldPrefs() + { + return data.getDocFieldPrefs(); + } private String selectedPdbFileName; @@ -96,11 +102,17 @@ public class StructureChooser extends GStructureChooser public StructureChooser(SequenceI[] selectedSeqs, SequenceI selectedSeq, AlignmentPanel ap) { + // which FTS engine to use + data = StructureChooserQuerySource + .getTDBfts(); + initDialog(); + this.ap = ap; this.selectedSequence = selectedSeq; this.selectedSequences = selectedSeqs; this.progressIndicator = (ap == null) ? null : ap.alignFrame; init(); + } /** @@ -165,8 +177,7 @@ public class StructureChooser extends GStructureChooser if (view.isLinkedWith(ap)) { - targetView.insertItemAt(viewHandler, - linkedViewsAt++); + targetView.insertItemAt(viewHandler, linkedViewsAt++); } else { @@ -217,36 +228,35 @@ public class StructureChooser extends GStructureChooser void fetchStructuresMetaData() { long startTime = System.currentTimeMillis(); - pdbRestClient = PDBFTSRestClient.getInstance(); - Collection wantedFields = pdbDocFieldPrefs + Collection wantedFields = data.getDocFieldPrefs() .getStructureSummaryFields(); discoveredStructuresSet = new LinkedHashSet<>(); HashSet errors = new HashSet<>(); + + FilterOption selectedFilterOpt = ((FilterOption) cmb_filterOption + .getSelectedItem()); + 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 = pdbRestClient.executeRequest(pdbRequest); + resultList = data.fetchStructuresMetaData(seq, wantedFields, + selectedFilterOpt, !chk_invertFilter.isSelected()); + // null response means the FTSengine didn't yield a query for this + // consider designing a special exception if we really wanted to be OOCrazy + if (resultList==null) + { + continue; + } } catch (Exception e) { e.printStackTrace(); errors.add(e.getMessage()); continue; } - lastPdbRequest = pdbRequest; if (resultList.getSearchSummary() != null && !resultList.getSearchSummary().isEmpty()) { @@ -260,8 +270,8 @@ public class StructureChooser extends GStructureChooser if (discoveredStructuresSet != null && !discoveredStructuresSet.isEmpty()) { - getResultTable().setModel(FTSRestResponse - .getTableModel(lastPdbRequest, discoveredStructuresSet)); + getResultTable() + .setModel(data.getTableModel(discoveredStructuresSet)); noOfStructuresFound = discoveredStructuresSet.size(); mainFrame.setTitle(MessageManager.formatMessage( "label.structure_chooser_no_of_structures", @@ -309,147 +319,6 @@ public class StructureChooser extends GStructureChooser } /** - * 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 - */ - - static String buildQuery(SequenceI seq) - { - boolean isPDBRefsFound = false; - boolean isUniProtRefsFound = false; - StringBuilder queryBuilder = new StringBuilder(); - Set seqRefs = new LinkedHashSet<>(); - - 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; - } - } - } - - List refs = seq.getDBRefs(); - if (refs != null && refs.size() != 0) - { - for (int ib = 0, nb = refs.size(); ib < nb; ib++) - { - DBRefEntry dbRef = refs.get(ib); - 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 - */ - 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; - } - - 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 @@ -463,50 +332,27 @@ public class StructureChooser extends GStructureChooser public void run() { long startTime = System.currentTimeMillis(); - pdbRestClient = PDBFTSRestClient.getInstance(); lbl_loading.setVisible(true); - Collection wantedFields = pdbDocFieldPrefs + Collection wantedFields = data.getDocFieldPrefs() .getStructureSummaryFields(); Collection filteredResponse = new HashSet<>(); HashSet errors = new HashSet<>(); for (SequenceI seq : selectedSequences) { - FTSRestRequest pdbRequest = new FTSRestRequest(); - if (fieldToFilterBy.equalsIgnoreCase("uniprot_coverage")) - { - pdbRequest.setAllowEmptySeq(false); - pdbRequest.setResponseSize(1); - pdbRequest.setFieldToSearchBy("("); - pdbRequest.setSearchTerm(buildQuery(seq) + ")"); - pdbRequest.setWantedFields(wantedFields); - pdbRequest.setAssociatedSequence(seq); - pdbRequest.setFacet(true); - pdbRequest.setFacetPivot(fieldToFilterBy + ",entry_entity"); - pdbRequest.setFacetPivotMinCount(1); - } - else - { - pdbRequest.setAllowEmptySeq(false); - pdbRequest.setResponseSize(1); - pdbRequest.setFieldToSearchBy("("); - pdbRequest.setFieldToSortBy(fieldToFilterBy, - !chk_invertFilter.isSelected()); - pdbRequest.setSearchTerm(buildQuery(seq) + ")"); - pdbRequest.setWantedFields(wantedFields); - pdbRequest.setAssociatedSequence(seq); - } + FTSRestResponse resultList; try { - resultList = pdbRestClient.executeRequest(pdbRequest); + resultList = data.selectFirstRankedQuery(seq, wantedFields, + fieldToFilterBy, !chk_invertFilter.isSelected()); + } catch (Exception e) { e.printStackTrace(); errors.add(e.getMessage()); continue; } - lastPdbRequest = pdbRequest; if (resultList.getSearchSummary() != null && !resultList.getSearchSummary().isEmpty()) { @@ -522,8 +368,8 @@ public class StructureChooser extends GStructureChooser Collection reorderedStructuresSet = new LinkedHashSet<>(); reorderedStructuresSet.addAll(filteredResponse); reorderedStructuresSet.addAll(discoveredStructuresSet); - getResultTable().setModel(FTSRestResponse - .getTableModel(lastPdbRequest, reorderedStructuresSet)); + getResultTable() + .setModel(data.getTableModel(reorderedStructuresSet)); FTSRestResponse.configureTableColumn(getResultTable(), wantedFields, tempUserPrefs); @@ -567,7 +413,8 @@ public class StructureChooser extends GStructureChooser @Override protected void pdbFromFile_actionPerformed() { - // TODO: JAL-3048 not needed for Jalview-JS until JSmol dep and StructureChooser + // TODO: JAL-3048 not needed for Jalview-JS until JSmol dep and + // StructureChooser // works jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser( jalview.bin.Cache.getProperty("LAST_DIRECTORY")); @@ -629,8 +476,8 @@ public class StructureChooser extends GStructureChooser if (cachedPDBExist) { FilterOption cachedOption = new FilterOption( - MessageManager.getString("label.cached_structures"), - "-", VIEWS_LOCAL_PDB, false); + MessageManager.getString("label.cached_structures"), "-", + VIEWS_LOCAL_PDB, false); cmb_filterOption.addItem(cachedOption); cmb_filterOption.setSelectedItem(cachedOption); } @@ -851,7 +698,7 @@ public class StructureChooser extends GStructureChooser } return found; } - + /** * Handles the 'New View' action */ @@ -896,37 +743,11 @@ public class StructureChooser extends GStructureChooser if (currentView == VIEWS_FILTER) { - int pdbIdColIndex = restable.getColumn("PDB Id") - .getModelIndex(); - int refSeqColIndex = restable.getColumn("Ref Sequence") - .getModelIndex(); int[] selectedRows = restable.getSelectedRows(); PDBEntry[] pdbEntriesToView = new PDBEntry[selectedRows.length]; - int count = 0; List selectedSeqsToView = new ArrayList<>(); - for (int row : selectedRows) - { - String pdbIdStr = restable - .getValueAt(row, pdbIdColIndex).toString(); - SequenceI selectedSeq = (SequenceI) restable - .getValueAt(row, refSeqColIndex); - selectedSeqsToView.add(selectedSeq); - PDBEntry pdbEntry = selectedSeq.getPDBEntry(pdbIdStr); - if (pdbEntry == null) - { - pdbEntry = getFindEntry(pdbIdStr, - selectedSeq.getAllPDBEntries()); - } + pdbEntriesToView = data.collectSelectedRows(restable,selectedRows,selectedSeqsToView); - if (pdbEntry == null) - { - pdbEntry = new PDBEntry(); - pdbEntry.setId(pdbIdStr); - pdbEntry.setType(PDBEntry.Type.PDB); - selectedSeq.getDatasetSequence().addPDBId(pdbEntry); - } - pdbEntriesToView[count++] = pdbEntry; - } SequenceI[] selectedSeqs = selectedSeqsToView .toArray(new SequenceI[selectedSeqsToView.size()]); sViewer = launchStructureViewer(ssm, pdbEntriesToView, ap, @@ -1000,10 +821,8 @@ public class StructureChooser extends GStructureChooser DataSourceType.FILE, selectedSequence, true, Desktop.instance); - sViewer = launchStructureViewer( - ssm, new PDBEntry[] - { fileEntry }, ap, - new SequenceI[] + sViewer = launchStructureViewer(ssm, new PDBEntry[] { fileEntry }, + ap, new SequenceI[] { selectedSequence }); } SwingUtilities.invokeLater(new Runnable() @@ -1036,21 +855,6 @@ public class StructureChooser extends GStructureChooser } } - private PDBEntry getFindEntry(String id, Vector pdbEntries) - { - Objects.requireNonNull(id); - Objects.requireNonNull(pdbEntries); - PDBEntry foundEntry = null; - for (PDBEntry entry : pdbEntries) - { - if (entry.getId().equalsIgnoreCase(id)) - { - return entry; - } - } - return foundEntry; - } - /** * Answers a structure viewer (new or existing) configured to superimpose * added structures or not according to the user's choice @@ -1058,8 +862,7 @@ public class StructureChooser extends GStructureChooser * @param ssm * @return */ - StructureViewer getTargetedStructureViewer( - StructureSelectionManager ssm) + StructureViewer getTargetedStructureViewer(StructureSelectionManager ssm) { Object sv = targetView.getSelectedItem(); @@ -1076,8 +879,7 @@ public class StructureChooser extends GStructureChooser * @return */ private StructureViewer launchStructureViewer( - StructureSelectionManager ssm, - final PDBEntry[] pdbEntriesToView, + StructureSelectionManager ssm, final PDBEntry[] pdbEntriesToView, final AlignmentPanel alignPanel, SequenceI[] sequences) { long progressId = sequences.hashCode(); @@ -1140,8 +942,9 @@ public class StructureChooser extends GStructureChooser } if (pdbEntriesToView.length > 1) { - setProgressBar(MessageManager.getString( - "status.fetching_3d_structures_for_selected_entries"), + setProgressBar( + MessageManager.getString( + "status.fetching_3d_structures_for_selected_entries"), progressId); theViewer.viewStructures(pdbEntriesToView, sequences, alignPanel); } @@ -1149,7 +952,7 @@ public class StructureChooser extends GStructureChooser { setProgressBar(MessageManager.formatMessage( "status.fetching_3d_structures_for", - pdbEntriesToView[0].getId()),progressId); + pdbEntriesToView[0].getId()), progressId); theViewer.viewStructures(pdbEntriesToView[0], sequences, alignPanel); } setProgressBar(null, progressId); @@ -1194,7 +997,8 @@ public class StructureChooser extends GStructureChooser && !discoveredStructuresSet.isEmpty(); } - protected int PDB_ID_MIN = 3;// or: (Jalview.isJS() ? 3 : 1); // Bob proposes this. + protected int PDB_ID_MIN = 3;// or: (Jalview.isJS() ? 3 : 1); // Bob proposes + // this. // Doing a search for "1" or "1c" is valuable? // Those work but are enormously slow. @@ -1202,51 +1006,54 @@ public class StructureChooser extends GStructureChooser protected void txt_search_ActionPerformed() { String text = txt_search.getText().trim(); - if (text.length() >= PDB_ID_MIN) - new Thread() - { - - @Override - public void run() + if (text.length() >= PDB_ID_MIN) + new Thread() { - errorWarning.setLength(0); - isValidPBDEntry = false; - if (text.length() > 0) + + @Override + public void run() { - String searchTerm = text.toLowerCase(); - searchTerm = searchTerm.split(":")[0]; - // System.out.println(">>>>> search term : " + searchTerm); - List wantedFields = new ArrayList<>(); - FTSRestRequest pdbRequest = new FTSRestRequest(); - pdbRequest.setAllowEmptySeq(false); - pdbRequest.setResponseSize(1); - pdbRequest.setFieldToSearchBy("(pdb_id:"); - pdbRequest.setWantedFields(wantedFields); - pdbRequest.setSearchTerm(searchTerm + ")"); - pdbRequest.setAssociatedSequence(selectedSequence); - pdbRestClient = PDBFTSRestClient.getInstance(); - wantedFields.add(pdbRestClient.getPrimaryKeyColumn()); - FTSRestResponse resultList; - try - { - resultList = pdbRestClient.executeRequest(pdbRequest); - } catch (Exception e) - { - errorWarning.append(e.getMessage()); - return; - } finally + errorWarning.setLength(0); + isValidPBDEntry = false; + if (text.length() > 0) { - validateSelections(); - } - if (resultList.getSearchSummary() != null - && resultList.getSearchSummary().size() > 0) - { - isValidPBDEntry = true; + // TODO move this pdb id search into the PDB specific + // FTSSearchEngine + // for moment, it will work fine as is because it is self-contained + String searchTerm = text.toLowerCase(); + searchTerm = searchTerm.split(":")[0]; + // System.out.println(">>>>> search term : " + searchTerm); + List wantedFields = new ArrayList<>(); + FTSRestRequest pdbRequest = new FTSRestRequest(); + pdbRequest.setAllowEmptySeq(false); + pdbRequest.setResponseSize(1); + pdbRequest.setFieldToSearchBy("(pdb_id:"); + pdbRequest.setWantedFields(wantedFields); + pdbRequest.setSearchTerm(searchTerm + ")"); + pdbRequest.setAssociatedSequence(selectedSequence); + FTSRestClientI pdbRestClient = PDBFTSRestClient.getInstance(); + wantedFields.add(pdbRestClient.getPrimaryKeyColumn()); + FTSRestResponse resultList; + try + { + resultList = pdbRestClient.executeRequest(pdbRequest); + } catch (Exception e) + { + errorWarning.append(e.getMessage()); + return; + } finally + { + validateSelections(); + } + if (resultList.getSearchSummary() != null + && resultList.getSearchSummary().size() > 0) + { + isValidPBDEntry = true; + } } + validateSelections(); } - validateSelections(); - } - }.start(); + }.start(); } @Override @@ -1393,4 +1200,11 @@ public class StructureChooser extends GStructureChooser { return sViewer == null ? null : sViewer.sview; } + + @Override + protected void setFTSDocFieldPrefs(FTSDataColumnPreferences newPrefs) + { + data.setDocFieldPrefs(newPrefs); + + } }