Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / mason / brapiclient / germplasm_search.mas
blob1733926f326f2f4d3cab2678f5436714737f93b9
1 <%args>
2 </%args>
4 <& /util/import_javascript.mas, classes => [ 'brapi.Table' ] &>
6 <& /page/page_title.mas, title=>"Germplasm Search and Exchange" &>
8 <h4>Database Select</h4>
9 <select name="brapi_home_url_select" id="brapi_home_url_select" class="form-control" />
10     <option value="">local</option>
11     <option value="https://cassbase.org">cassbase.org</option>
12     <option value="https://cassavabase.org">cassavabase.org</option>
13     <option value="https://musabase.org">musabase.org</option>
14     <option value="https://sweetpotatobase.org">sweetpotatobase.org</option>
15     <option value="https://yambase.org">yambase.org</option>
16     <option value="https://fernbase.org">fernbase.org</option>
17     <option value="https://solgenomics.net">solgenomics.net</option>
18     <option value="https://www.crop-diversity.org/mgis">crop-diversity.org</option>
19 </select>
21 <h4>Germplasm Search</h4>
23 <div id="brapi_germplasm_search_home_div" class="well">
25 <input name="brapi_germplasm_search_germplasmName" id="brapi_germplasm_search_germplasmname" class="form-control" type="text" placeholder="GermplasmName(s) e.g. TMEB419,TMEB529" />
26 <input name="brapi_germplasm_search_accessionNumber" id="brapi_germplasm_search_accessionNumber" class="form-control" type="text" placeholder="AccessionNumber" />
27 <input name="brapi_germplasm_search_germplasmGenus" id="brapi_germplasm_search_germplasmGenus" class="form-control" type="text" placeholder="GermplasmGenus" />
28 <input name="brapi_germplasm_search_germplasmSpecies" id="brapi_germplasm_search_germplasmSpecies" class="form-control" type="text" placeholder="GermplasmSpecies" />
29 <input name="brapi_germplasm_search_germplasmDbId" id="brapi_germplasm_search_germplasmDbId" class="form-control" type="text" placeholder="GermplasmDbId" />
30 <input name="brapi_germplasm_search_germplasmPUI" id="brapi_germplasm_search_germplasmPUI" class="form-control" type="text" placeholder="GermplasmPUI" />
31 <select name="brapi_germplasm_search_matchMethod" id="brapi_germplasm_search_matchMethod" class="form-control" />
32     <option value="exact">Exact</option>
33     <option value="wildcard">Wildcard. Wildcard allows % or * for multiple characters and ? for single characters.</option>
34 </select>
35 <br/>
36 <button class="btn btn-primary" name="brapi_germplasm_search_submit" id="brapi_germplasm_search_submit">Search</button>
38 <br/><br/>
40 <div id="brapi_germplasm_search_result_div">
41 </div>
42 <div id="brapi_germplasm_search_result_div_collection">
43 </div>
45 <div class="modal fade" id="brapi_germplasm_saved_message_modal" name="brapi_germplasm_saved_message_modal" tabindex="-1" role="dialog" aria-labelledby="brapiGermplasmSearchSavedDialog">
46     <div class="modal-dialog" role="document">
47         <div class="modal-content">
48             <div class="modal-header" style="text-align: center">
49                 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
50                 <h4 class="modal-title" id="brapiGermplasmSearchSavedDialog">BrAPI Germplasm Exchange</h4>
51             </div>
52             <div class="modal-body">
53                 <div class="container-fluid">
54                     <div id="brapi_germplasm_saved_message"></div>
55                 </div>
56             </div>
57             <div class="modal-footer">
58                 <button id="close_brapi_germplasm_saved_message_modal" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
59             </div>
60         </div>
61     </div>
62 </div>
64 </div>
66 <script>
68 var allowed_organism_results = [];
70 jQuery(document).ready(function() {
71     var return_url;
72     var selected_results = [];
73     var selected_organism_results = [];
75     jQuery("#brapi_germplasm_search_submit").click(function() {
76         var germplasmNames = jQuery("#brapi_germplasm_search_germplasmname").val();
77         if (germplasmNames){
78             germplasmNames = germplasmNames.split(',');
79         } else {
80             germplasmNames = [];
81         }
82         var accessionNumbers = jQuery("#brapi_germplasm_search_accessionNumber").val();
83         if (accessionNumbers){
84             accessionNumbers = accessionNumbers.split(',');
85         } else {
86             accessionNumbers = [];
87         }
88         var genusNames = jQuery("#brapi_germplasm_search_germplasmGenus").val();
89         if (genusNames){
90             genusNames = genusNames.split(',');
91         } else {
92             genusNames = [];
93         }
94         var subtaxaNames = jQuery("#brapi_germplasm_search_germplasmSubTaxa").val();
95         if (subtaxaNames){
96             subtaxaNames = subtaxaNames.split(',');
97         } else {
98             subtaxaNames = [];
99         }
100         var speciesNames = jQuery("#brapi_germplasm_search_germplasmSpecies").val();
101         if (speciesNames){
102             speciesNames = speciesNames.split(',');
103         } else {
104             speciesNames = [];
105         }
106         var germplasmDbIds = jQuery("#brapi_germplasm_search_germplasmDbId").val();
107         if (germplasmDbIds){
108             germplasmDbIds = germplasmDbIds.split(',');
109         } else {
110             germplasmDbIds = [];
111         }
112         var germplasmPUIs = jQuery("#brapi_germplasm_search_germplasmPUI").val();
113         if (germplasmPUIs){
114             germplasmPUIs = germplasmPUIs.split(',');
115         } else {
116             germplasmPUIs = [];
117         }
118         var search_query_data = { 
119             'germplasmNames' :  germplasmNames,
120             'accessionNumbers' : accessionNumbers,
121             'germplasmGenus' : genusNames,
122             'germplasmSubTaxa' : subtaxaNames,
123             'germplasmSpecies' : speciesNames,
124             'germplasmDbIds' : germplasmDbIds,
125             'germplasmPUIs' : germplasmPUIs,
126             'matchMethod' : jQuery("#brapi_germplasm_search_matchMethod").val(),
127         };
128         return_url = jQuery('#brapi_home_url_select').val()+'/brapi/v1/germplasm-search';
129         jQuery.ajax( {
130           'url': jQuery('#brapi_home_url_select').val()+'/brapi/v1/germplasm-search',
131           'method': 'POST',
132           'beforeSend': function(){
133             jQuery('#working_modal').modal('show');
134           },
135           'data': search_query_data,
136           'success': function(response) {
137             //console.log(response);
138             brapi_create_paginated_table(response.result.data, response.metadata.pagination, 'brapi_germplasm_search_result_div', return_url, { "germplasmName": ["germplasmPUI", ""] }, search_query_data, ['germplasmName','commonCropName','genus','species','accessionNumber','germplasmPUI','synonyms'], 'germplasmName', ['species']);
139             jQuery('#working_modal').modal('hide');
140           },
141           error: function(response) {
142             jQuery('#working_modal').modal('hide');
143             alert('An error occurred');
144           }
145         });
146     });
148     jQuery(document).on('click', '#brapi_table_select_submit_brapi_germplasm_search_result_div', function(){
149         jQuery.each(jQuery('input[name="brapi_table_select_'+return_url+'"]:checked'), function(){
150             if (jQuery.inArray(jQuery(this).val(), selected_results) == -1){
151                 selected_results.push(jQuery(this).val());
152             }
153             if (jQuery.inArray(jQuery(this).data("species"), selected_organism_results) == -1){
154                 selected_organism_results.push(jQuery(this).data("species"));
155             }
156         });
157         console.log(selected_results);
158         console.log(selected_organism_results);
160         jQuery.ajax({
161             'url': '/ajax/accession_list/verify',
162             'method': 'POST',
163             'beforeSend': function(){
164                 jQuery('#working_modal').modal('show');
165             },
166             'data': {
167                 'accession_list' : JSON.stringify(selected_results),
168                 'organism_list' : JSON.stringify(selected_organism_results),
169                 'do_fuzzy_search' : 'true'
170             },
171             'success': function(response){
172                 if(response.error){
173                     alert(response.error);
174                     jQuery('#working_modal').modal('hide');
175                     return false;
176                 }
177                 console.log(response);
179                 var html = '<h3>How Selected Organisms Compare to This Database:</h3>';
180                 html = html + '<table class="table table-bordered"><thead><tr><th>Species</th><th>Status</th><th>Possible Matches</th></tr></thead><tbody>';
181                 for (var i=0; i<response.found_organisms.length; i++){
182                     html = html + '<tr><td>' + response.found_organisms[i].unique_name + '</td><td class="bg-success">Exact Species Found in This Database</td><td>Exact Match</td></tr>';
183                     allowed_organism_results.push(response.found_organisms[i].unique_name);
184                 }
185                 for (var i=0; i<response.fuzzy_organisms.length; i++){
186                     html = html + '<tr><td>' + response.fuzzy_organisms[i].name + '</td><td class="bg-danger">Possible Species Matches Found in This Database, but Not Exact Match. Please Contact Us to Add Your Organism, or change Your Species to Match Our Species Name!</td>';
187                     html = html + '<td><select class="form-control">'
188                     for (var j=0; j<response.fuzzy_organisms[i].matches.length; j++){
189                         html = html + '<option value="'+response.fuzzy_organisms[i].matches[j].name+'">'+response.fuzzy_organisms[i].matches[j].name+'</option>'
190                     }
191                     html = html + '</select></td></tr>';
192                 }
193                 for (var i=0; i<response.absent_organisms.length; i++){
194                     html = html + '<tr><td>' + response.absent_organisms[i] + '</td><td class="bg-danger">Please Contact Us to Add Your Organism, or change Your Species to Match Our Species Name!</td><td>No Match</td></tr>';
195                 }
196                 html = html + '</tbody></table>';
198                 html = html + '<h3>How Selected GermplasmNames Compare to This Database:</h3>';
199                 html = html + '<table class="table table-bordered"><thead><tr><th>Select</th><th>GermplasmName</th><th>Status</th><th>Possible Matches</th></tr></thead><tbody>';
200                 for (var i=0; i<response.found.length; i++){
201                     html = html + '<tr><td><input type="checkbox" name="brapi_germplasm_search_collection_select" disabled/></td><td>' + response.found[i].unique_name + '</td><td class="bg-success">Exact GermplasmName Found in This Database</td><td>Exact Match</td></tr>';
202                 }
203                 for (var i=0; i<response.fuzzy.length; i++){
204                     html = html + '<tr><td><input type="checkbox" name="brapi_germplasm_search_collection_select" value="' + response.fuzzy[i].name + '"/></td><td>' + response.fuzzy[i].name + '</td><td class="bg-info">Possible GermplasmName Matches Found in This Database</td>';
205                     html = html + '<td><select class="form-control">'
206                     for (var j=0; j<response.fuzzy[i].matches.length; j++){
207                         html = html + '<option value="'+response.fuzzy[i].matches[j].name+'">'+response.fuzzy[i].matches[j].name+'</option>'
208                     }
209                     html = html + '</select></td></tr>';
210                 }
211                 for (var i=0; i<response.absent.length; i++){
212                     html = html + '<tr><td><input type="checkbox" name="brapi_germplasm_search_collection_select" value="' + response.absent[i] + '"/></td><td>' + response.absent[i] + '</td><td class="bg-warning">GermplasmName Not Found in This Database</td><td>No Match</td></tr>';
213                 }
214                 html = html + '</tbody></table><button class="btn btn-primary" name="brapi_germplasm_search_submit_final" id="brapi_germplasm_search_submit_final">Save in Database</button>';
215                 jQuery('#working_modal').modal('hide');
216                 jQuery('#brapi_germplasm_search_result_div_collection').html(html);
217             },
218             'error': function(response){
219                 jQuery('#working_modal').modal('hide');
220                 alert('An error occurred in fuzzy search!');
221             }
222         })
223     });
225     jQuery(document).on('click', '#brapi_germplasm_search_submit_final', function(){
226         jQuery('#working_modal').modal('show');
227         var selected_to_save = [];
228         jQuery.each(jQuery('input[name="brapi_germplasm_search_collection_select"]:checked'), function(){
229             if (jQuery.inArray(jQuery(this).val(), selected_to_save) == -1){
230                 selected_to_save.push(jQuery(this).val());
231             }
232         });
233         console.log(selected_to_save);
234         var to_save = [];
235         var num_requests = selected_to_save.length;
236         var request = 1;
237         for (var i=0; i<num_requests; i++){
238             jQuery.ajax({
239                 'method':'GET',
240                 'url': jQuery('#brapi_home_url_select').val()+'/brapi/v1/germplasm-search?matchMethod=exact&germplasmName='+selected_to_save[i],
241                 'success': function(response){
242                     //console.log(response);
243                     if (jQuery.inArray(response.result.data[0].species, allowed_organism_results) == -1){
244                         alert('Will Skip '+response.result.data[0].germplasmName+' because the species '+response.result.data[0].species+' is not in this database!');
245                     } else {
246                         to_save.push(response.result.data[0]);
247                     }
248                     if (request == num_requests){
249                         perform_save(to_save);
250                     }
251                     request++;
252                 },
253                 'error': function(response){
254                     alert('An error occurred getting details to save!');
255                 }
256             });
257         }
259     });
263 function perform_save(to_save){
264     console.log(to_save);
265     jQuery.ajax({
266         'url': '/ajax/accession_list/add',
267         'method': 'POST',
268         'data': {
269             'full_info' : JSON.stringify(to_save),
270             'allowed_organisms' : JSON.stringify(allowed_organism_results)
271         },
272         'success': function(response){
273             console.log(response);
274             if(response.error){
275                 alert(response.error);
276                 jQuery('#working_modal').modal('hide');
277                 return false;
278             }
279             if(response.success){
280                 var html = 'The following stocks were added!<br/>';
281                 for (var i=0; i<response.added.length; i++){
282                     html = html + '<a href="/stock/'+response.added[i][0]+'/view">'+response.added[i][1]+'</a><br/>';
283                 }
284                 jQuery('#brapi_germplasm_saved_message').html(html);
285                 jQuery('#brapi_germplasm_saved_message_modal').modal('show');
286             }
287             jQuery('#working_modal').modal('hide');
288         },
289         'error': function(response){
290             alert('An error occurred in fuzzy search!');
291             jQuery('#working_modal').modal('hide');
292         }
293     })
295 </script>