From e4a49e444d89dc2d8d35913813bddc3a0dabcad6 Mon Sep 17 00:00:00 2001 From: Charles Ofoegbu Date: Fri, 6 Mar 2015 08:59:38 +0000 Subject: [PATCH] JAL-1667 refactoring to support JAL-1668 --- src/jalview/gui/PDBFetchPanel.java | 154 +++-------- src/jalview/gui/StructureChooser.java | 309 ++++++++++++----------- src/jalview/jbgui/GPDBFetchPanel.java | 15 +- src/jalview/jbgui/GStructureChooser.java | 96 +++++-- src/jalview/ws/dbsources/PDBRestClient.java | 90 ++++++- src/jalview/ws/uimodel/PDBSearchRequest.java | 24 +- src/jalview/ws/uimodel/PDBSearchResponse.java | 119 ++++++++- src/jalview/ws/uimodel/PDBSummaryListModel.java | 144 ----------- 8 files changed, 493 insertions(+), 458 deletions(-) delete mode 100644 src/jalview/ws/uimodel/PDBSummaryListModel.java diff --git a/src/jalview/gui/PDBFetchPanel.java b/src/jalview/gui/PDBFetchPanel.java index c7e5362..f576910 100644 --- a/src/jalview/gui/PDBFetchPanel.java +++ b/src/jalview/gui/PDBFetchPanel.java @@ -2,12 +2,14 @@ package jalview.gui; import jalview.jbgui.GPDBFetchPanel; import jalview.ws.dbsources.PDBRestClient; +import jalview.ws.dbsources.PDBRestClient.PDBDocField; import jalview.ws.uimodel.PDBSearchRequest; import jalview.ws.uimodel.PDBSearchResponse; -import jalview.ws.uimodel.PDBSummaryListModel; -import jalview.ws.uimodel.PDBSummaryListModel.PDBDocField; +import jalview.ws.uimodel.PDBSearchResponse.PDBResponseSummary; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; import javax.swing.DefaultListModel; @@ -48,13 +50,13 @@ public class PDBFetchPanel extends GPDBFetchPanel public void searchStringAction() { boolean allowEmptySequence = false; - jListSearchResult.setModel(new DefaultListModel()); + jListSearchResult.setModel(new DefaultListModel()); mainFrame.setTitle("PDB Sequence Fetcher"); if (search.getText().trim().length() > 0) { long startTime = System.currentTimeMillis(); PDBRestClient pdbRestCleint = new PDBRestClient(); - String searchTarget = ((PDBSearchOptionList) searchTargetOptions + String searchTarget = ((PDBDocField) searchTargetOptions .getSelectedItem()).getCode(); PDBSearchRequest request = new PDBSearchRequest(); @@ -88,7 +90,7 @@ public class PDBFetchPanel extends GPDBFetchPanel @Override public void okActionPerformed() { - processPDBResponseData(); + importSelectedPDBSequencesToAlignment(); } @@ -104,11 +106,11 @@ public class PDBFetchPanel extends GPDBFetchPanel mainFrame.dispose(); } - private void processPDBResponseData() + private void importSelectedPDBSequencesToAlignment() { mainFrame.dispose(); StringBuilder selectedIds = new StringBuilder(); - for (PDBSummaryListModel dataSelected : jListSearchResult + for (PDBResponseSummary dataSelected : jListSearchResult .getSelectedValuesList()) { selectedIds.append(";").append(dataSelected.getPdbId()); @@ -119,125 +121,31 @@ public class PDBFetchPanel extends GPDBFetchPanel worker.start(); } - @SuppressWarnings("unchecked") - public void populateSearchOptionCombo() - + public void populateSearchOptionComboBox() { - List searchOptions = new ArrayList(); - searchOptions.add(new PDBSearchOptionList("PDB ID", "pdb_id", - FieldType.STRING)); - searchOptions.add(new PDBSearchOptionList("PFAM Name", "pfam_name", - FieldType.STRING)); - searchOptions.add(new PDBSearchOptionList("ALL", "text", - FieldType.STRING)); - searchOptions.add(new PDBSearchOptionList("Molecule Type", - "molecule_type", FieldType.STRING)); - searchOptions.add(new PDBSearchOptionList("Title", "title", - FieldType.STRING)); - searchOptions.add(new PDBSearchOptionList("Molecule Name", - "molecule_name", FieldType.STRING)); - searchOptions.add(new PDBSearchOptionList("Tax ID", "tax_id", - FieldType.STRING)); - searchOptions.add(new PDBSearchOptionList("UniProt Accession", - "uniprot_accession", FieldType.STRING)); - searchOptions.add(new PDBSearchOptionList("Gene Name", "gene_name", - FieldType.STRING)); - searchOptions.add(new PDBSearchOptionList("Genus", "genus", - FieldType.STRING)); - - java.util.Collections.sort(searchOptions); - - for (PDBSearchOptionList pdbSO : searchOptions) - { - searchTargetOptions.addItem(pdbSO); - } - } - - @SuppressWarnings("rawtypes") - public class PDBSearchOptionList implements Comparable - { - private String name; - - private String code; - - private boolean showInSummary; - - private boolean searchFilter; - - private FieldType type; - - public PDBSearchOptionList(String name, String code, FieldType type) - { - this.name = name; - this.code = code; - this.type = type; - } - - public String getName() - { - return name; - } - - public void setName(String name) - { - this.name = name; - } - - public String getCode() - { - return code; - } - - public void setCode(String code) - { - this.code = code; - } - - public boolean isShowInSummary() - { - return showInSummary; - } - - public void setShowInSummary(boolean showInSummary) - { - this.showInSummary = showInSummary; - } - - public boolean isSearchFilter() - { - return searchFilter; - } - - public void setSearchFilter(boolean searchFilter) - { - this.searchFilter = searchFilter; - } - - public FieldType getFieldType() - { - return type; - } - - public void setFieldType(FieldType type) - { - this.type = type; - } - - public String toString() - { - return name; - } - - public PDBSearchOptionList() - { - - } + List searchableTargets = new ArrayList(); + searchableTargets.add(PDBDocField.PDB_ID); + searchableTargets.add(PDBDocField.PFAM_ACCESSION); + searchableTargets.add(PDBDocField.MOLECULE_TYPE); + searchableTargets.add(PDBDocField.MOLECULE_NAME); + searchableTargets.add(PDBDocField.UNIPROT_ACCESSION); + searchableTargets.add(PDBDocField.GENE_NAME); + searchableTargets.add(PDBDocField.GENUS); + searchableTargets.add(PDBDocField.ALL); + + Collections.sort(searchableTargets, new Comparator() + { + @Override + public int compare(PDBDocField o1, PDBDocField o2) + { + return o1.getName().compareTo(o2.getName()); + } + }); - @Override - public int compareTo(Object o) + for (PDBDocField searchTarget : searchableTargets) { - return this.name.compareTo(((PDBSearchOptionList) o).getName()); + searchTargetOptions.addItem(searchTarget); } - } + } diff --git a/src/jalview/gui/StructureChooser.java b/src/jalview/gui/StructureChooser.java index 07e36ec..11fb943 100644 --- a/src/jalview/gui/StructureChooser.java +++ b/src/jalview/gui/StructureChooser.java @@ -6,10 +6,10 @@ import jalview.datamodel.SequenceI; import jalview.jbgui.GStructureChooser; import jalview.util.MessageManager; import jalview.ws.dbsources.PDBRestClient; +import jalview.ws.dbsources.PDBRestClient.PDBDocField; import jalview.ws.uimodel.PDBSearchRequest; import jalview.ws.uimodel.PDBSearchResponse; -import jalview.ws.uimodel.PDBSummaryListModel; -import jalview.ws.uimodel.PDBSummaryListModel.PDBDocField; +import jalview.ws.uimodel.PDBSearchResponse.PDBResponseSummary; import java.util.ArrayList; import java.util.Collection; @@ -17,161 +17,94 @@ import java.util.HashSet; import java.util.List; import javax.swing.JOptionPane; -import javax.swing.SwingUtilities; @SuppressWarnings("serial") public class StructureChooser extends GStructureChooser { + private boolean structuresWereFound = false; - private SequenceI sequence; + private SequenceI selectedSequence; + private SequenceI[] selectedSequences; public StructureChooser(AlignmentPanel ap, final SequenceI sequence) { this.ap = ap; - this.sequence = sequence; + this.selectedSequence = sequence; + selectedSequences = ((ap.av.getSelectionGroup() == null) ? new SequenceI[] + { sequence } : ap.av.getSequenceSelection()); fetchStructures(); + populateFilterOptions(); + updateCurrentView(); } @Override public void ok_ActionPerformed() { - jListFoundStructures.setSelectedValue("op1x", true); + // TODO code to load selected structures to jmol or chimera } - public void pdbFromFile_actionPerformed() - { - jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty("LAST_DIRECTORY")); - chooser.setFileView(new jalview.io.JalviewFileView()); - chooser.setDialogTitle(MessageManager.formatMessage( - "label.select_pdb_file_for", new String[] - { sequence.getDisplayId(false) })); - chooser.setToolTipText(MessageManager.formatMessage( - "label.load_pdb_file_associate_with_sequence", new String[] - { sequence.getDisplayId(false) })); - - int value = chooser.showOpenDialog(null); - - if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION) - { - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); - new AssociatePdbFileWithSeq().associatePdbWithSeq(choice, - jalview.io.AppletFormatAdapter.FILE, sequence, true, - Desktop.instance); - } - - } - - public void discoverPDB_actionPerformed() - { - - final SequenceI[] sequences = ((ap.av.getSelectionGroup() == null) ? new SequenceI[] - { sequence } - : ap.av.getSequenceSelection()); - Thread discpdb = new Thread(new Runnable() - { - @Override - public void run() - { - - new jalview.ws.DBRefFetcher(sequences, ap.alignFrame) - .fetchDBRefs(false); - } - - }); - discpdb.start(); - } - - // rpdbview.addActionListener(new ActionListener() - // { - // - // @Override - // public void actionPerformed(ActionEvent e) - // { - // new StructureViewer(ap.getStructureSelectionManager()) - // .viewStructures(ap, pr, ap.av.collateForPDB(pr)); - // } - // }); - - public void enterPDB_actionPerformed() + protected void populateFilterOptions() { - String id = JOptionPane.showInternalInputDialog(Desktop.desktop, - MessageManager.getString("label.enter_pdb_id"), - MessageManager.getString("label.enter_pdb_id"), - JOptionPane.QUESTION_MESSAGE); - if (id != null && id.length() > 0) + if (structuresWereFound) { - PDBEntry entry = new PDBEntry(); - entry.setId(id.toUpperCase()); - sequence.getDatasetSequence().addPDBId(entry); + filterOptionsComboBox.addItem(new FilterOptions( + "- Filter Criteria -", "", + VIEWS_FILTER)); + filterOptionsComboBox.addItem(new FilterOptions("All", "all", + VIEWS_FILTER)); + filterOptionsComboBox.addItem(new FilterOptions("Best Coverage", + "coverage", + VIEWS_FILTER)); + filterOptionsComboBox.addItem(new FilterOptions("Best Resolution", + PDBDocField.RESOLUTION.getCode(), + VIEWS_FILTER)); + filterOptionsComboBox.addItem(new FilterOptions("Best Quality", + PDBDocField.OVERALL_QUALITY.getCode(), + VIEWS_FILTER)); + + // "number_of_polymers"), PROTEIN_CHAIN_COUNT( + // "Protein Chain Count", "number_of_protein_chains"), + // BOUND_MOLECULE_COUNT( + // "Bound Molecule Count", "number_of_bound_molecules"), + // POLYMER_RESIDUE_COUNT( + // "Polymer Residue Count", "number_of_polymer_residues"), + // UNIPROT_COVERAGE( } - } - - public static void main(String[] args) - { - SwingUtilities.invokeLater(new Runnable() - { - public void run() - { - new StructureChooser(null, null); - } - }); - } - - @SuppressWarnings("unchecked") - protected void populateFilterOptions() - { - filterOptions.addItem(new FilterOptions("- Filter Criteria -", "", - VIEWS_AUTO)); - filterOptions.addItem(new FilterOptions("All", "all", VIEWS_AUTO)); - filterOptions - .addItem(new FilterOptions("Best Coverage", "", VIEWS_AUTO)); - filterOptions.addItem(new FilterOptions("Best Resolution", "", - VIEWS_AUTO)); - filterOptions - .addItem(new FilterOptions("Best Quality", "", VIEWS_AUTO)); - filterOptions.addItem(new FilterOptions("Enter PDB Id", "", VIEWS_ID)); - filterOptions.addItem(new FilterOptions("From File", "", VIEWS_FILE)); - } - - protected void updateCurrentView() - { - String currentView = ((FilterOptions) filterOptions.getSelectedItem()) - .getView(); - switchableViewsLayout.show(switchableViewsPanel, currentView); + filterOptionsComboBox.addItem(new FilterOptions("Enter PDB Id", "-", + VIEWS_ENTER_ID)); + filterOptionsComboBox.addItem(new FilterOptions("From File", "-", + VIEWS_FROM_FILE)); } private void fetchStructures() { long startTime = System.currentTimeMillis(); - final SequenceI[] sequences = ((ap.av.getSelectionGroup() == null) ? new SequenceI[] - { sequence } - : ap.av.getSequenceSelection()); + // final SequenceI[] sequences = ((ap.av.getSelectionGroup() == null) ? new + // SequenceI[] + // { sequence } + // : ap.av.getSequenceSelection()); + int foundStructures = 0; - PDBSearchRequest request = new PDBSearchRequest(); - request.setAllowEmptySeq(false); - request.setResponseSize(500); - request.setSearchTarget("(text:"); List wantedFields = new ArrayList(); wantedFields.add(PDBDocField.MOLECULE_TYPE); wantedFields.add(PDBDocField.PDB_ID); wantedFields.add(PDBDocField.GENUS); wantedFields.add(PDBDocField.GENE_NAME); wantedFields.add(PDBDocField.TITLE); + + PDBSearchRequest request = new PDBSearchRequest(); + request.setAllowEmptySeq(false); + request.setResponseSize(500); + request.setSearchTarget("(text:"); request.setWantedFields(wantedFields); - Collection searchSummaries = new HashSet(); - // searchSummaries. - for (SequenceI seq : sequences) + Collection searchSummaries = new HashSet(); + for (SequenceI seq : selectedSequences) { - // System.out.println("Selected : " + seq.getName()); request.setSearchTerm(buildQuery(seq) + ")"); - PDBRestClient pdbRestCleint = new PDBRestClient(); - PDBSearchResponse resultList = pdbRestCleint .executeRequest(request); if (resultList.getSearchSummary() != null @@ -186,8 +119,8 @@ public class StructureChooser extends GStructureChooser { jListFoundStructures.setModel(PDBSearchResponse .getListModel(searchSummaries)); + structuresWereFound = true; } - String totalTime = (System.currentTimeMillis() - startTime) + " milli secs"; mainFrame.setTitle("Structure Chooser - " + foundStructures @@ -203,7 +136,6 @@ public class StructureChooser extends GStructureChooser { for (DBRefEntry dbRef : seq.getDBRef()) { - System.out.println("dbref : " + dbRef.getAccessionId()); queryBuilder.append("text:").append(dbRef.getAccessionId()) .append(" OR "); ++count; @@ -212,62 +144,135 @@ public class StructureChooser extends GStructureChooser break; } } - // int beginIndex = queryBuilder. firstIndexOf("text"); int endIndex = queryBuilder.lastIndexOf(" OR "); query = queryBuilder.toString().substring(5, endIndex); } - System.out.println("------------> " + query); return query; } - public class FilterOptions + @Override + protected void stateChanged() { - private String name; - - private String value; - - private String view; + updateCurrentView(); + } - public FilterOptions(String name, String value, String view) + int debounceCount = 0; + protected void updateCurrentView() + { + FilterOptions currentOption = ((FilterOptions) filterOptionsComboBox + .getSelectedItem()); + switchableViewsLayout.show(switchableViewsPanel, + currentOption.getView()); + ++debounceCount; + if (currentOption.getView() == VIEWS_FILTER && debounceCount % 2 == 0) { - this.name = name; - this.value = value; - this.view = view; + filterResultSet(currentOption.getValue()); } + } - public String getName() - { - return name; - } + public void filterResultSet(String filterTarget) + { + System.out.println("-----------------> Filter by : " + filterTarget); - public void setName(String name) - { - this.name = name; - } + List wantedFields = new ArrayList(); + wantedFields.add(PDBDocField.MOLECULE_TYPE); + wantedFields.add(PDBDocField.PDB_ID); + wantedFields.add(PDBDocField.GENUS); + wantedFields.add(PDBDocField.GENE_NAME); + wantedFields.add(PDBDocField.TITLE); - public String getValue() - { - return value; - } + PDBSearchRequest request = new PDBSearchRequest(); + request.setAllowEmptySeq(false); + request.setResponseSize(1); + request.setSearchTarget("(text:"); + request.setSortTarget(filterTarget, true); - public void setValue(String value) + request.setWantedFields(wantedFields); + + Collection searchSummaries = new HashSet(); + for (SequenceI seq : selectedSequences) { - this.value = value; + request.setSearchTerm(buildQuery(seq) + ")"); + PDBRestClient pdbRestCleint = new PDBRestClient(); + PDBSearchResponse resultList = pdbRestCleint.executeRequest(request); + if (resultList.getSearchSummary() != null + && !resultList.getSearchSummary().isEmpty()) + { + searchSummaries.addAll(resultList.getSearchSummary()); + + } } - public String getView() + for (PDBResponseSummary summary : searchSummaries) { - return view; + jListFoundStructures.setSelectedValue(summary, true); + // jListFoundStructures. + break; } + System.out.println("Found item count : " + searchSummaries.size()); + } + + public void pdbFromFile_actionPerformed() + { + jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser( + jalview.bin.Cache.getProperty("LAST_DIRECTORY")); + chooser.setFileView(new jalview.io.JalviewFileView()); + chooser.setDialogTitle(MessageManager.formatMessage( + "label.select_pdb_file_for", new String[] + { selectedSequence.getDisplayId(false) })); + chooser.setToolTipText(MessageManager.formatMessage( + "label.load_pdb_file_associate_with_sequence", new String[] + { selectedSequence.getDisplayId(false) })); + + int value = chooser.showOpenDialog(null); - public void setView(String view) + if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION) { - this.view = view; + String choice = chooser.getSelectedFile().getPath(); + jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); + new AssociatePdbFileWithSeq().associatePdbWithSeq(choice, + jalview.io.AppletFormatAdapter.FILE, selectedSequence, true, + Desktop.instance); } - public String toString() + } + + + // rpdbview.addActionListener(new ActionListener() + // { + // + // @Override + // public void actionPerformed(ActionEvent e) + // { + // new StructureViewer(ap.getStructureSelectionManager()) + // .viewStructures(ap, pr, ap.av.collateForPDB(pr)); + // } + // }); + + public void enterPDB_actionPerformed() + { + String id = JOptionPane.showInternalInputDialog(Desktop.desktop, + MessageManager.getString("label.enter_pdb_id"), + MessageManager.getString("label.enter_pdb_id"), + JOptionPane.QUESTION_MESSAGE); + if (id != null && id.length() > 0) { - return this.name; + PDBEntry entry = new PDBEntry(); + entry.setId(id.toUpperCase()); + selectedSequence.getDatasetSequence().addPDBId(entry); } } + + // public static void main(String[] args) + // { + // SwingUtilities.invokeLater(new Runnable() + // { + // public void run() + // { + // new StructureChooser(null, null); + // } + // }); + // } + + } diff --git a/src/jalview/jbgui/GPDBFetchPanel.java b/src/jalview/jbgui/GPDBFetchPanel.java index 18c33cd..c7fa057 100644 --- a/src/jalview/jbgui/GPDBFetchPanel.java +++ b/src/jalview/jbgui/GPDBFetchPanel.java @@ -1,9 +1,9 @@ package jalview.jbgui; import jalview.gui.Desktop; -import jalview.gui.PDBFetchPanel.PDBSearchOptionList; import jalview.util.MessageManager; -import jalview.ws.uimodel.PDBSummaryListModel; +import jalview.ws.dbsources.PDBRestClient.PDBDocField; +import jalview.ws.uimodel.PDBSearchResponse.PDBResponseSummary; import java.awt.BorderLayout; import java.awt.Dimension; @@ -29,7 +29,7 @@ public abstract class GPDBFetchPanel extends JPanel protected JInternalFrame mainFrame = new JInternalFrame(frameTitle); - protected JComboBox searchTargetOptions = new JComboBox(); + protected JComboBox searchTargetOptions = new JComboBox(); protected JButton ok = new JButton(); @@ -39,7 +39,7 @@ public abstract class GPDBFetchPanel extends JPanel protected JTextField search = new JTextField(20); - protected JList jListSearchResult = new JList(); + protected JList jListSearchResult = new JList(); protected JScrollPane searchResultScroller = new JScrollPane( jListSearchResult); @@ -52,8 +52,6 @@ public abstract class GPDBFetchPanel extends JPanel private BorderLayout mainLayout = new BorderLayout(); - - public GPDBFetchPanel() { try @@ -119,7 +117,7 @@ public abstract class GPDBFetchPanel extends JPanel } }); - populateSearchOptionCombo(); + populateSearchOptionComboBox(); search.setFont(new java.awt.Font("Verdana", 0, 12)); search.getDocument().addDocumentListener(new DocumentListener() @@ -146,7 +144,6 @@ public abstract class GPDBFetchPanel extends JPanel inputPanel.add(searchTargetOptions); inputPanel.add(search); - this.setLayout(mainLayout); this.add(inputPanel, java.awt.BorderLayout.NORTH); this.add(resultPanel, java.awt.BorderLayout.CENTER); @@ -165,6 +162,6 @@ public abstract class GPDBFetchPanel extends JPanel public abstract void cancelActionPerformed(); - protected abstract void populateSearchOptionCombo(); + protected abstract void populateSearchOptionComboBox(); } diff --git a/src/jalview/jbgui/GStructureChooser.java b/src/jalview/jbgui/GStructureChooser.java index 69b1fe6..9569006 100644 --- a/src/jalview/jbgui/GStructureChooser.java +++ b/src/jalview/jbgui/GStructureChooser.java @@ -3,6 +3,7 @@ package jalview.jbgui; import jalview.gui.AlignmentPanel; import jalview.gui.Desktop; import jalview.util.MessageManager; +import jalview.ws.uimodel.PDBSearchResponse.PDBResponseSummary; import java.awt.BorderLayout; import java.awt.CardLayout; @@ -34,7 +35,7 @@ public abstract class GStructureChooser extends JPanel implements - protected JComboBox filterOptions = new JComboBox(); + protected JComboBox filterOptionsComboBox = new JComboBox(); protected AlignmentPanel ap; @@ -44,7 +45,7 @@ public abstract class GStructureChooser extends JPanel implements protected JButton cancel = new JButton(); - protected JButton selectFile = new JButton(); + protected JButton pdbFromFile = new JButton(); protected JTextField search = new JTextField(16); @@ -70,13 +71,13 @@ public abstract class GStructureChooser extends JPanel implements protected JCheckBox rememberSettings = new JCheckBox("Don't ask me again"); - protected static final String VIEWS_AUTO = "auto_view"; + protected static final String VIEWS_FILTER = "VIEWS_FILTER"; - protected static final String VIEWS_FILE = "file_view"; + protected static final String VIEWS_FROM_FILE = "VIEWS_FROM_FILE"; - protected static final String VIEWS_ID = "id_view"; + protected static final String VIEWS_ENTER_ID = "VIEWS_ENTER_ID"; - protected JList jListFoundStructures = new JList(); + protected JList jListFoundStructures = new JList(); protected JScrollPane foundStructuresScroller = new JScrollPane( jListFoundStructures); @@ -116,6 +117,16 @@ public abstract class GStructureChooser extends JPanel implements } }); + pdbFromFile.setFont(new java.awt.Font("Verdana", 0, 12)); + pdbFromFile.setText(" Select PDB File "); + pdbFromFile.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(ActionEvent e) + { + pdbFromFile_actionPerformed(); + } + }); + jListFoundStructures .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); jListFoundStructures.setLayoutOrientation(JList.VERTICAL); @@ -128,30 +139,28 @@ public abstract class GStructureChooser extends JPanel implements actionPanel.add(ok); actionPanel.add(cancel); - filterOptions.addItemListener(this); + filterOptionsComboBox.addItemListener(this); filterPanel.add(resultLabel); - filterPanel.add(filterOptions); + filterPanel.add(filterOptionsComboBox); idInputPanel.setLayout(new FlowLayout()); idInputPanel.add(search); - selectFile.setText(" Select File "); fileChooserPanel.setLayout(new FlowLayout()); - fileChooserPanel.add(selectFile); + fileChooserPanel.add(pdbFromFile); - switchableViewsPanel.add(fileChooserPanel, VIEWS_FILE); - switchableViewsPanel.add(idInputPanel, VIEWS_ID); - switchableViewsPanel.add(foundStructuresScroller, VIEWS_AUTO); + switchableViewsPanel.add(fileChooserPanel, VIEWS_FROM_FILE); + switchableViewsPanel.add(idInputPanel, VIEWS_ENTER_ID); + switchableViewsPanel.add(foundStructuresScroller, VIEWS_FILTER); this.setLayout(mainLayout); this.add(filterPanel, java.awt.BorderLayout.NORTH); this.add(switchableViewsPanel, java.awt.BorderLayout.CENTER); this.add(actionPanel, java.awt.BorderLayout.SOUTH); - populateFilterOptions(); - updateCurrentView(); + mainFrame.setVisible(true); mainFrame.setContentPane(this); mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); @@ -161,12 +170,67 @@ public abstract class GStructureChooser extends JPanel implements @Override public void itemStateChanged(ItemEvent e) { - updateCurrentView(); + stateChanged(); + } + + public class FilterOptions + { + private String name; + + private String value; + + private String view; + + public FilterOptions(String name, String value, String view) + { + this.name = name; + this.value = value; + this.view = view; + } + + public String getName() + { + return name; + } + + public void setName(String name) + { + this.name = name; + } + + public String getValue() + { + return value; + } + + public void setValue(String value) + { + this.value = value; + } + + public String getView() + { + return view; + } + + public void setView(String view) + { + this.view = view; + } + + public String toString() + { + return this.name; + } } + protected abstract void stateChanged(); + protected abstract void updateCurrentView(); protected abstract void ok_ActionPerformed(); protected abstract void populateFilterOptions(); + + protected abstract void pdbFromFile_actionPerformed(); } diff --git a/src/jalview/ws/dbsources/PDBRestClient.java b/src/jalview/ws/dbsources/PDBRestClient.java index bdc507f..8bc9ebf 100644 --- a/src/jalview/ws/dbsources/PDBRestClient.java +++ b/src/jalview/ws/dbsources/PDBRestClient.java @@ -2,8 +2,7 @@ package jalview.ws.dbsources; import jalview.ws.uimodel.PDBSearchRequest; import jalview.ws.uimodel.PDBSearchResponse; -import jalview.ws.uimodel.PDBSummaryListModel; -import jalview.ws.uimodel.PDBSummaryListModel.PDBDocField; +import jalview.ws.uimodel.PDBSearchResponse.PDBResponseSummary; import java.util.ArrayList; import java.util.Iterator; @@ -61,28 +60,62 @@ public class PDBRestClient String responseSize = (request.getResponseSize() == 0) ? "200" : String .valueOf(request.getResponseSize()); + String sortParam = (request.getSortTarget() == null || request + .getSortTarget().trim().isEmpty()) ? "" + : (request + .getSortTarget() + (request.isAscending() ? " asc" : " desc")); + WebResource webResource = client.resource(pdbSearchEndpoint) .queryParam("wt", "json").queryParam("fl", wantedFields) .queryParam("rows", responseSize) - .queryParam("q", query); + .queryParam("q", query) + .queryParam("sort", sortParam); ClientResponse clientResponse = webResource.accept( MediaType.APPLICATION_JSON).get(ClientResponse.class); String responseString = clientResponse.getEntity(String.class); if (clientResponse.getStatus() != 200) { + if (clientResponse.getStatus() == 400) + { + throw new RuntimeException(parseException(responseString)); + } + else + { throw new RuntimeException("Failed : HTTP error code : " + clientResponse.getStatus()); + } } // System.out.println("--------------> " + responseString); return parseResponse(responseString, request.getWantedFields()); } + private String parseException(String jsonResponse) + { + String errorMessage = "RunTime error"; + try + { + JSONParser jsonParser = new JSONParser(); + JSONObject jsonObj = (JSONObject) jsonParser.parse(jsonResponse); + JSONObject errorResponse = (JSONObject) jsonObj.get("error"); + errorMessage = errorResponse.get("msg").toString(); + + JSONObject responseHeader = (JSONObject) jsonObj + .get("responseHeader"); + errorMessage += responseHeader.get("params").toString(); + } catch (ParseException e) + { + e.printStackTrace(); + } + return errorMessage; + } + + @SuppressWarnings("unchecked") private PDBSearchResponse parseResponse(String jsonResponse, List wantedFields) { PDBSearchResponse searchResult = new PDBSearchResponse(); - List result = null; + List result = null; try { JSONParser jsonParser = new JSONParser(); @@ -96,7 +129,7 @@ public class PDBRestClient .valueOf(pdbResponse.get("numFound").toString()); if (numFound > 0) { - result = new ArrayList(); + result = new ArrayList(); JSONArray docs = (JSONArray) pdbResponse.get("docs"); for (Iterator docIter = docs.iterator(); docIter .hasNext();) @@ -104,7 +137,7 @@ public class PDBRestClient JSONObject doc = docIter.next(); // if (doc.get("molecule_sequence") != null) // { - result.add(new PDBSummaryListModel(doc, wantedFields)); + result.add(searchResult.new PDBResponseSummary(doc, wantedFields)); // } } searchResult.setItemsFound(numFound); @@ -136,4 +169,49 @@ public class PDBRestClient } + public enum PDBDocField + { + PDB_ID("PDB Id", "pdb_id"), TITLE("Title", "title"), MOLECULE_NAME( + "Molecule", "molecule_name"), MOLECULE_TYPE("Molecule Type", + "molecule_type"), MOLECULE_SEQUENCE("Sequence", + "molecule_sequence"), UNIPROT_FEATURES("Uniprot Features", + "uniprot_features"), PFAM_ACCESSION("PFAM Accession", + "pfam_accession"), INTERPRO_ACCESSION("InterPro Accession", + "interpro_accession"), UNIPROT_ACCESSION("UniProt Accession", + "uniprot_accession"), R_FACTOR("R Factor", "r_factor"), RESOLUTION( + "Resolution", "resolution"), DATA_QUALITY("Data Quality", + "data_quality"), OVERALL_QUALITY("Overall Quality", + "overall_quality"), POLYMER_COUNT("Polymer Count", + "number_of_polymers"), PROTEIN_CHAIN_COUNT( + "Protein Chain Count", "number_of_protein_chains"), BOUND_MOLECULE_COUNT( + "Bound Molecule Count", "number_of_bound_molecules"), POLYMER_RESIDUE_COUNT( + "Polymer Residue Count", "number_of_polymer_residues"), UNIPROT_COVERAGE( + "UniProt Coverage", "uniprot_coverage"), GENUS("GENUS", "genus"), GENE_NAME( + "Gene Name", "gene_name"), ALL("ALL", "text"); + + private String name; + + private String code; + + PDBDocField(String name, String code) + { + this.name = name; + this.code = code; + } + + public String getName() + { + return name; + } + + public String getCode() + { + return code; + } + + public String toString() + { + return name; + } + } } diff --git a/src/jalview/ws/uimodel/PDBSearchRequest.java b/src/jalview/ws/uimodel/PDBSearchRequest.java index bb24cf5..7e97b3f 100644 --- a/src/jalview/ws/uimodel/PDBSearchRequest.java +++ b/src/jalview/ws/uimodel/PDBSearchRequest.java @@ -1,6 +1,7 @@ package jalview.ws.uimodel; -import jalview.ws.uimodel.PDBSummaryListModel.PDBDocField; + +import jalview.ws.dbsources.PDBRestClient.PDBDocField; import java.util.ArrayList; import java.util.List; @@ -11,10 +12,14 @@ public class PDBSearchRequest private String searchTerm; + private String sortTarget; + private boolean allowEmptySeq; private int responseSize; + private boolean isAscending; + private List wantedFields = new ArrayList(); public String getSearchTarget() @@ -66,4 +71,21 @@ public class PDBSearchRequest { this.wantedFields = wantedFields; } + + public String getSortTarget() + { + return sortTarget; + } + + public void setSortTarget(String sortTarget, boolean isAscending) + { + this.sortTarget = sortTarget; + this.isAscending = isAscending; + } + + public boolean isAscending() + { + return isAscending; + } + } diff --git a/src/jalview/ws/uimodel/PDBSearchResponse.java b/src/jalview/ws/uimodel/PDBSearchResponse.java index 9f44102..9a6e1c6 100644 --- a/src/jalview/ws/uimodel/PDBSearchResponse.java +++ b/src/jalview/ws/uimodel/PDBSearchResponse.java @@ -1,16 +1,22 @@ package jalview.ws.uimodel; +import jalview.ws.dbsources.PDBRestClient.PDBDocField; + import java.util.Collection; +import java.util.List; +import java.util.Objects; import javax.swing.DefaultListModel; +import org.json.simple.JSONObject; + public class PDBSearchResponse { private int itemsFound; private String responseTime; - private Collection searchSummary; + private Collection searchSummary; public int getItemsFound() { @@ -32,25 +38,124 @@ public class PDBSearchResponse this.responseTime = responseTime; } - public Collection getSearchSummary() + public Collection getSearchSummary() { return searchSummary; } - public void setSearchSummary(Collection searchSummary) + public void setSearchSummary(Collection searchSummary) { this.searchSummary = searchSummary; } - public static DefaultListModel getListModel( - Collection summariesList) + public static DefaultListModel getListModel( + Collection summariesList) { - DefaultListModel defaultListModel = new DefaultListModel(); - for (PDBSummaryListModel summaryList : summariesList) + DefaultListModel defaultListModel = new DefaultListModel(); + for (PDBResponseSummary summaryList : summariesList) { defaultListModel.addElement(summaryList); } return defaultListModel; } + + public class PDBResponseSummary + { + private String pdbId; + + private String summary; + + private int width = 480; + + public PDBResponseSummary(JSONObject doc, List diplayFields) + { + StringBuilder summaryBuilder = new StringBuilder(); + + for (PDBDocField field : diplayFields) + { + if (field.equals(PDBDocField.MOLECULE_TYPE) + && doc.get(PDBDocField.MOLECULE_TYPE.getCode()) != null) + { + String moleculeType = doc + .get(PDBDocField.MOLECULE_TYPE.getCode()).toString(); + if (moleculeType.equalsIgnoreCase("protein")) + { + summaryBuilder.append(""); + } + if (moleculeType.equalsIgnoreCase("dna")) + { + summaryBuilder.append(""); + } + if (moleculeType.equalsIgnoreCase("rna")) + { + summaryBuilder.append(""); + } + if (moleculeType.equalsIgnoreCase("sugar")) + { + summaryBuilder.append(""); + } + } + else if (field.equals(PDBDocField.PDB_ID) + && doc.get(PDBDocField.PDB_ID.getCode()) != null) + { + this.pdbId = doc.get(PDBDocField.PDB_ID.getCode()).toString(); + } + else if (doc.get(field.getCode()) != null) + { + summaryBuilder.append(field.getName()).append(": ") + .append(doc.get(field.getCode())).append(" | "); + } + } + int endIndex = summaryBuilder.lastIndexOf(" | "); + String fSummary = summaryBuilder.toString().substring(0, endIndex); + this.summary = fSummary.trim(); + summaryBuilder = null; + } + + public String getPdbId() + { + return pdbId; + } + + public void setPdbId(String pdbId) + { + this.pdbId = pdbId; + } + + public String getSummary() + { + return summary; + } + + public void setSummary(String summary) + { + this.summary = summary; + } + + public String toString() + { + StringBuilder html = new StringBuilder(); + html.append("
"); + html.append(summary); + html.append("
"); + return html.toString(); + } + + @Override + public int hashCode() + { + return Objects.hash(this.pdbId, this.summary); + } + } + } diff --git a/src/jalview/ws/uimodel/PDBSummaryListModel.java b/src/jalview/ws/uimodel/PDBSummaryListModel.java deleted file mode 100644 index 3a68e05..0000000 --- a/src/jalview/ws/uimodel/PDBSummaryListModel.java +++ /dev/null @@ -1,144 +0,0 @@ -package jalview.ws.uimodel; - -import java.util.List; - -import javax.swing.DefaultListModel; - -import org.json.simple.JSONObject; - - -@SuppressWarnings( -{ "serial", "rawtypes" }) -public class PDBSummaryListModel extends DefaultListModel -{ - private String pdbId; - - private String summary; - - private int width = 480; - - - public PDBSummaryListModel(JSONObject doc, List diplayFields) - { - StringBuilder summaryBuilder = new StringBuilder(); - - for (PDBDocField field : diplayFields) - { - if (field.equals(PDBDocField.MOLECULE_TYPE) - && doc.get("molecule_type") != null) - { - String moleculeType = doc.get("molecule_type").toString(); - if (moleculeType.equalsIgnoreCase("protein")) - { - summaryBuilder.append(""); - } - if (moleculeType.equalsIgnoreCase("dna")) - { - summaryBuilder.append(""); - } - if (moleculeType.equalsIgnoreCase("rna")) - { - summaryBuilder.append(""); - } - if (moleculeType.equalsIgnoreCase("sugar")) - { - summaryBuilder.append(""); - } - }else if (doc.get(field.getCode()) != null) - { - summaryBuilder.append(field.getName()).append(": ") - .append(doc.get(field.getCode())).append(" | "); - } - } - int endIndex = summaryBuilder.lastIndexOf(" | "); - String fSummary = summaryBuilder.toString().substring(0, endIndex); - this.summary = fSummary.trim(); - - } - - public String getPdbId() - { - return pdbId; - } - - public void setPdbId(String pdbId) - { - this.pdbId = pdbId; - } - - public String getSummary() - { - return summary; - } - - public void setSummary(String summary) - { - this.summary = summary; - } - - - public String toString() - { - StringBuilder html = new StringBuilder(); - html.append("
"); - html.append(summary); - html.append("
"); - return html.toString(); - } - - - public enum PDBDocField - { - PDB_ID("PDB Id", "pdb_id"), TITLE("Title", "title"), MOLECULE_NAME( - "Molecule", "molecule_name"), MOLECULE_TYPE("Molecule Type", - "molecule_type"), MOLECULE_SEQUENCE("Sequence", - "molecule_sequence"), UNIPROT_FEATURES("Uniprot Features", - "uniprot_features"), PFAM_ACCESSION("PFAM Accession", - "pfam_accession"), INTERPRO_ACCESSION("InterPro Accession", - "interpro_accession"), UNIPROT_ACCESSION("UniProt Accession", - "uniprot_accession"), R_FACTOR("R Factor", "r_factor"), RESOLUTION( - "Resolution", "resolution"), DATA_QUALITY("Data Quality", - "data_quality"), OVERALL_QUALITY("Overall Quality", - "overall_quality"), POLYMER_COUNT("Polymer Count", - "number_of_polymers"), PROTEIN_CHAIN_COUNT( - "Protein Chain Count", "number_of_protein_chains"), BOUND_MOLECULE_COUNT( - "Bound Molecule Count", "number_of_bound_molecules"), POLYMER_RESIDUE_COUNT( - "Polymer Residue Count", "number_of_polymer_residues"), UNIPROT_COVERAGE( - "UniProt Coverage", "uniprot_coverage"), GENUS("GENUS", "genus"), GENE_NAME( - "Gene Name", "gene_name"); - - private String name; - - private String code; - - PDBDocField(String name, String code) - { - this.name = name; - this.code = code; - } - - public String getName() - { - return name; - } - - public String getCode() - { - return code; - } - - public String toString() - { - return name; - } - } -} -- 1.7.10.2