11fb9433703b4444d0fdf962924a7344d9cf3796
[jalview.git] / src / jalview / gui / StructureChooser.java
1 package jalview.gui;
2
3 import jalview.datamodel.DBRefEntry;
4 import jalview.datamodel.PDBEntry;
5 import jalview.datamodel.SequenceI;
6 import jalview.jbgui.GStructureChooser;
7 import jalview.util.MessageManager;
8 import jalview.ws.dbsources.PDBRestClient;
9 import jalview.ws.dbsources.PDBRestClient.PDBDocField;
10 import jalview.ws.uimodel.PDBSearchRequest;
11 import jalview.ws.uimodel.PDBSearchResponse;
12 import jalview.ws.uimodel.PDBSearchResponse.PDBResponseSummary;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
18
19 import javax.swing.JOptionPane;
20
21 @SuppressWarnings("serial")
22 public class StructureChooser extends GStructureChooser
23 {
24
25   private boolean structuresWereFound = false;
26
27   private SequenceI selectedSequence;
28
29   private SequenceI[] selectedSequences;
30
31   public StructureChooser(AlignmentPanel ap, final SequenceI sequence)
32   {
33     this.ap = ap;
34     this.selectedSequence = sequence;
35     selectedSequences = ((ap.av.getSelectionGroup() == null) ? new SequenceI[]
36     { sequence } : ap.av.getSequenceSelection());
37     fetchStructures();
38     populateFilterOptions();
39     updateCurrentView();
40   }
41
42   @Override
43   public void ok_ActionPerformed()
44   {
45     // TODO code to load selected structures to jmol or chimera
46   }
47
48   protected void populateFilterOptions()
49   {
50     if (structuresWereFound)
51     {
52       filterOptionsComboBox.addItem(new FilterOptions(
53               "- Filter Criteria -", "",
54               VIEWS_FILTER));
55       filterOptionsComboBox.addItem(new FilterOptions("All", "all",
56               VIEWS_FILTER));
57       filterOptionsComboBox.addItem(new FilterOptions("Best Coverage",
58               "coverage",
59               VIEWS_FILTER));
60       filterOptionsComboBox.addItem(new FilterOptions("Best Resolution",
61               PDBDocField.RESOLUTION.getCode(),
62               VIEWS_FILTER));
63       filterOptionsComboBox.addItem(new FilterOptions("Best Quality",
64               PDBDocField.OVERALL_QUALITY.getCode(),
65               VIEWS_FILTER));
66
67       // "number_of_polymers"), PROTEIN_CHAIN_COUNT(
68       // "Protein Chain Count", "number_of_protein_chains"),
69       // BOUND_MOLECULE_COUNT(
70       // "Bound Molecule Count", "number_of_bound_molecules"),
71       // POLYMER_RESIDUE_COUNT(
72       // "Polymer Residue Count", "number_of_polymer_residues"),
73       // UNIPROT_COVERAGE(
74     }
75     filterOptionsComboBox.addItem(new FilterOptions("Enter PDB Id", "-",
76             VIEWS_ENTER_ID));
77     filterOptionsComboBox.addItem(new FilterOptions("From File", "-",
78             VIEWS_FROM_FILE));
79   }
80
81   private void fetchStructures()
82   {
83     long startTime = System.currentTimeMillis();
84     // final SequenceI[] sequences = ((ap.av.getSelectionGroup() == null) ? new
85     // SequenceI[]
86     // { sequence }
87     // : ap.av.getSequenceSelection());
88
89     int foundStructures = 0;
90     List<PDBDocField> wantedFields = new ArrayList<PDBDocField>();
91     wantedFields.add(PDBDocField.MOLECULE_TYPE);
92     wantedFields.add(PDBDocField.PDB_ID);
93     wantedFields.add(PDBDocField.GENUS);
94     wantedFields.add(PDBDocField.GENE_NAME);
95     wantedFields.add(PDBDocField.TITLE);
96
97     PDBSearchRequest request = new PDBSearchRequest();
98     request.setAllowEmptySeq(false);
99     request.setResponseSize(500);
100     request.setSearchTarget("(text:");
101     request.setWantedFields(wantedFields);
102     
103     Collection<PDBResponseSummary> searchSummaries = new HashSet<PDBResponseSummary>();
104     for (SequenceI seq : selectedSequences)
105     {
106       request.setSearchTerm(buildQuery(seq) + ")");
107       PDBRestClient pdbRestCleint = new PDBRestClient();
108       PDBSearchResponse resultList = pdbRestCleint
109               .executeRequest(request);
110       if (resultList.getSearchSummary() != null
111               && !resultList.getSearchSummary().isEmpty())
112       {
113         searchSummaries.addAll(resultList.getSearchSummary());
114       }
115     }
116
117     foundStructures = searchSummaries.size();
118     if (searchSummaries != null)
119     {
120       jListFoundStructures.setModel(PDBSearchResponse
121               .getListModel(searchSummaries));
122       structuresWereFound = true;
123     }
124     String totalTime = (System.currentTimeMillis() - startTime)
125             + " milli secs";
126     mainFrame.setTitle("Structure Chooser - " + foundStructures
127             + " Found (" + totalTime + ")");
128   }
129
130   private String buildQuery(SequenceI seq)
131   {
132     String query = seq.getName();
133     StringBuilder queryBuilder = new StringBuilder();
134     int count = 0;
135     if (seq.getDBRef() != null && seq.getDBRef().length != 0)
136     {
137       for (DBRefEntry dbRef : seq.getDBRef())
138       {
139         queryBuilder.append("text:").append(dbRef.getAccessionId())
140                 .append(" OR ");
141         ++count;
142         if (count > 10)
143         {
144           break;
145         }
146       }
147       int endIndex = queryBuilder.lastIndexOf(" OR ");
148       query = queryBuilder.toString().substring(5, endIndex);
149     }
150     return query;
151   }
152
153   @Override
154   protected void stateChanged()
155   {
156     updateCurrentView();
157   }
158
159   int debounceCount = 0;
160   protected void updateCurrentView()
161   {
162     FilterOptions currentOption = ((FilterOptions) filterOptionsComboBox
163             .getSelectedItem());
164     switchableViewsLayout.show(switchableViewsPanel,
165             currentOption.getView());
166     ++debounceCount;
167     if (currentOption.getView() == VIEWS_FILTER && debounceCount % 2 == 0)
168     {
169       filterResultSet(currentOption.getValue());
170     }
171   }
172
173   public void filterResultSet(String filterTarget)
174   {
175     System.out.println("-----------------> Filter by : " + filterTarget);
176
177     List<PDBDocField> wantedFields = new ArrayList<PDBDocField>();
178     wantedFields.add(PDBDocField.MOLECULE_TYPE);
179     wantedFields.add(PDBDocField.PDB_ID);
180     wantedFields.add(PDBDocField.GENUS);
181     wantedFields.add(PDBDocField.GENE_NAME);
182     wantedFields.add(PDBDocField.TITLE);
183
184     PDBSearchRequest request = new PDBSearchRequest();
185     request.setAllowEmptySeq(false);
186     request.setResponseSize(1);
187     request.setSearchTarget("(text:");
188     request.setSortTarget(filterTarget, true);
189
190     request.setWantedFields(wantedFields);
191
192     Collection<PDBResponseSummary> searchSummaries = new HashSet<PDBResponseSummary>();
193     for (SequenceI seq : selectedSequences)
194     {
195       request.setSearchTerm(buildQuery(seq) + ")");
196       PDBRestClient pdbRestCleint = new PDBRestClient();
197       PDBSearchResponse resultList = pdbRestCleint.executeRequest(request);
198       if (resultList.getSearchSummary() != null
199               && !resultList.getSearchSummary().isEmpty())
200       {
201         searchSummaries.addAll(resultList.getSearchSummary());
202
203       }
204     }
205
206     for (PDBResponseSummary summary : searchSummaries)
207     {
208       jListFoundStructures.setSelectedValue(summary, true);
209       // jListFoundStructures.
210       break;
211     }
212     System.out.println("Found item count : " + searchSummaries.size());
213   }
214
215   public void pdbFromFile_actionPerformed()
216   {
217     jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(
218             jalview.bin.Cache.getProperty("LAST_DIRECTORY"));
219     chooser.setFileView(new jalview.io.JalviewFileView());
220     chooser.setDialogTitle(MessageManager.formatMessage(
221             "label.select_pdb_file_for", new String[]
222             { selectedSequence.getDisplayId(false) }));
223     chooser.setToolTipText(MessageManager.formatMessage(
224             "label.load_pdb_file_associate_with_sequence", new String[]
225             { selectedSequence.getDisplayId(false) }));
226
227     int value = chooser.showOpenDialog(null);
228
229     if (value == jalview.io.JalviewFileChooser.APPROVE_OPTION)
230     {
231       String choice = chooser.getSelectedFile().getPath();
232       jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice);
233       new AssociatePdbFileWithSeq().associatePdbWithSeq(choice,
234               jalview.io.AppletFormatAdapter.FILE, selectedSequence, true,
235               Desktop.instance);
236     }
237
238   }
239
240
241   // rpdbview.addActionListener(new ActionListener()
242   // {
243   //
244   // @Override
245   // public void actionPerformed(ActionEvent e)
246   // {
247   // new StructureViewer(ap.getStructureSelectionManager())
248   // .viewStructures(ap, pr, ap.av.collateForPDB(pr));
249   // }
250   // });
251
252   public void enterPDB_actionPerformed()
253   {
254     String id = JOptionPane.showInternalInputDialog(Desktop.desktop,
255             MessageManager.getString("label.enter_pdb_id"),
256             MessageManager.getString("label.enter_pdb_id"),
257             JOptionPane.QUESTION_MESSAGE);
258     if (id != null && id.length() > 0)
259     {
260       PDBEntry entry = new PDBEntry();
261       entry.setId(id.toUpperCase());
262       selectedSequence.getDatasetSequence().addPDBId(entry);
263     }
264   }
265
266   // public static void main(String[] args)
267   // {
268   // SwingUtilities.invokeLater(new Runnable()
269   // {
270   // public void run()
271   // {
272   // new StructureChooser(null, null);
273   // }
274   // });
275   // }
276
277
278 }