message dialog and js
[sgn.git] / mason / brapiclient / phenotypes_search.mas
blob9b252ce626b2c6d70c000a90b85f268b0b41fbd2
1 <%args>
2 </%args>
4 <& /util/import_javascript.mas, classes => [ 'brapi.Table' ] &>
6 <& /page/page_title.mas, title=>"Phenotype Retrieval" &>
8 <h4>Database Select</h4>
9 <select name="brapi_home_phenotypes_url_select" id="brapi_home_phenotypes_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>Phenotype Search</h4>
23 <div id="brapi_home_phenotype_search_div" class="well">
25     <input name="brapi_home_phenosearch_germplasmDbIds" id="brapi_home_phenosearch_germplasmDbIds" class="form-control" type="text" placeholder="GermplasmDbIds e.g. 139" />
26     <input name="brapi_home_phenosearch_studyDbIds" id="brapi_home_phenosearch_studyDbIds" class="form-control" type="text" placeholder="StudyDbIds e.g. 139,137" />
27     <input name="brapi_home_phenosearch_observationVariableDbIds" id="brapi_home_phenoseach_observationVariableDbIds" class="form-control" type="text" placeholder="ObservationDbIds e.g. 70741,70666,70773" />
28     <input name="brapi_home_phenosearch_locationDbIds" id="brapi_home_phenosearch_locationDbIds" class="form-control" type="text" placeholder="LocationDbIds e.g. 23" value="23"/>
29     <input name="brapi_home_phenosearch_years" id="brapi_home_phenosearch_years" class="form-control" type="text" placeholder="Years e.g. 2014,2015" />
30     <select name="brapi_home_phenosearch_observations_level" id="brapi_home_phenosearch_observations_level" class="form-control" />
31         <option value="all">All</option>
32         <option value="plot">Plot</option>
33         <option value="plant">Plant</option>
34     </select>
35     <br/>
36     <button class="btn btn-primary" name="brapi_home_phenosearch_submit" id="brapi_home_phenosearch_submit">Search Phenotypes</button>
37     <br/><br/>
38     <div id="brapi_phenosearch_data_result_div">
39     </div>
41 </div>
43 <br/>
44 <h4>Phenotypes For a Study in Table Format</h4>
46 <div id="brapi_home_phenotype_study_table_div" class="well">
48     <select name="brapi_phenotype_studies_table_format" id="brapi_phenotype_studies_table_format" class="form-control" />
49         <option value="csv">CSV</option>
50         <option value="tsv">TSV</option>
51         <option value="xls">Excel (xls)</option>
52         <option value="json">JSON</option>
53     </select>
55     <div id="brapi_home_phenotype_study_table_studies_div">
56     </div>
58     <div id="brapi_pheno_studies_table_data_result_div">
59     </div>
61 </div>
63 <br/>
64 <h4>Phenotypes For a Study in ObservationUnits Format</h4>
66 <div id="brapi_home_phenotype_study_observation_units_div" class="well">
68     <div id="brapi_home_phenotype_study_observation_units_studies_div">
69     </div>
71     <div id="brapi_pheno_studies_observation_units_data_result_div">
72     </div>
74 </div>
76 <br/>
77 <h4>Phenotypes For a Study in Granular Format</h4>
79 <div id="brapi_home_phenotype_study_granular_div" class="well">
81     <div id="brapi_home_phenotype_study_granular_studies_div">
82     </div>
84     <div id="brapi_pheno_studies_granular_data_result_div">
85     </div>
87 </div>
89 <script>
91 var selected_results = [];
92 var studies_return_url;
94 jQuery(document).ready(function() {
96     brapi_phenotypes_get_studies(jQuery('#brapi_home_phenotypes_url_select').val(), 'brapi_home_phenotype_study_table_studies_div');
97     brapi_phenotypes_get_studies(jQuery('#brapi_home_phenotypes_url_select').val(), 'brapi_home_phenotype_study_observation_units_studies_div');
98     brapi_phenotypes_get_studies(jQuery('#brapi_home_phenotypes_url_select').val(), 'brapi_home_phenotype_study_granular_studies_div');
100     jQuery('#brapi_home_phenotypes_url_select').change(function(){
101         brapi_phenotypes_get_studies(jQuery('#brapi_home_phenotypes_url_select').val(), 'brapi_home_phenotype_study_table_studies_div');
102         brapi_phenotypes_get_studies(jQuery('#brapi_home_phenotypes_url_select').val(), 'brapi_home_phenotype_study_observation_units_studies_div');
103         brapi_phenotypes_get_studies(jQuery('#brapi_home_phenotypes_url_select').val(), 'brapi_home_phenotype_study_granular_studies_div');
104     });
106     jQuery("#brapi_home_phenosearch_submit").click(function() {
107     
108         var observationVariableDbIds = jQuery('#brapi_home_phenosearch_observationVariableDbIds').val();
109         if (observationVariableDbIds){
110             observationVariableDbIds = observationVariableDbIds.split(',');
111         } else {
112             observationVariableDbIds = [];
113         }
114         var germplasmDbIds = jQuery('#brapi_home_phenosearch_germplasmDbIds').val();
115         if (germplasmDbIds){
116             germplasmDbIds = germplasmDbIds.split(',');
117         } else {
118             germplasmDbIds = [];
119         }
120         var studyDbIds = jQuery('#brapi_home_phenosearch_studyDbIds').val();
121         if (studyDbIds){
122             studyDbIds = studyDbIds.split(',');
123         } else {
124             studyDbIds = [];
125         }
126         var locationDbIds = jQuery('#brapi_home_phenosearch_locationDbIds').val();
127         if (locationDbIds){
128             locationDbIds = locationDbIds.split(',');
129         } else {
130             locationDbIds = [];
131         }
132         var seasonDbIds = jQuery('#brapi_home_phenosearch_years').val();
133         if (seasonDbIds){
134             seasonDbIds = seasonDbIds.split(',');
135         } else {
136             seasonDbIds = [];
137         }
139         var return_url = jQuery('#brapi_home_phenotypes_url_select').val()+'/brapi/v1/phenotypes-search';
140         jQuery.ajax( {
141           'url': return_url,
142           'data' : {
143             'observationVariableDbIds':observationVariableDbIds,
144             'germplasmDbIds':germplasmDbIds,
145             'observationLevel':jQuery('#brapi_home_phenosearch_observations_level').val(),
146             'studyDbIds':studyDbIds,
147             'locationDbIds':locationDbIds,
148             'seasonDbIds':seasonDbIds
149           },
150           'method': 'POST',
151           'beforeSend': function() {
152             jQuery('#working_modal').modal('show');
153           },
154           'success': function(response) {
155             jQuery('#working_modal').modal('hide');
156             console.log(response);
157             brapi_create_paginated_table(response.result.data, response.metadata.pagination, 'brapi_phenosearch_data_result_div', return_url, undefined, {}, ['observationUnitName','replicate','germplasmName','blockNumber', 'X', 'Y', 'entryType', 'studyName','studyLocation','observationLevel','observations'], 'observationUnitName', []);
159           },
160           error: function(response) {
161             jQuery('#working_modal').modal('hide');
162             alert('An error occurred');
163           }
164         });
165     });
167     jQuery(document).on('click', '#brapi_table_select_submit_brapi_home_phenotype_study_table_studies_div', function(){
168         jQuery.each(jQuery('input[name="brapi_table_select_'+studies_return_url+'"]:checked'), function(){
169             if (jQuery.inArray(jQuery(this).val(), selected_results) == -1){
170                 selected_results.push(jQuery(this).val());
171             }
172         });
173         console.log(selected_results);
175         var format = jQuery('#brapi_phenotype_studies_table_format').val();
176         var return_url = jQuery('#brapi_home_phenotypes_url_select').val()+'/brapi/v1/studies/'+selected_results[0]+'/table?format='+format;
177         jQuery.ajax( {
178             'url': return_url,
179             'beforeSend': function(){
180                 jQuery('#working_modal').modal('show');
181             },
182             'success': function(response) {
183                 //console.log(response);
184                 if (response.metadata.datafiles.length > 0){
185                     var html = "<h4>Download Study Phenotype File(s) From Here: <br/>";
186                     for (var j=0; j<response.metadata.datafiles.length; j++){
187                         html = html + "<a href='"+response.metadata.datafiles[j]+"'>"+response.metadata.datafiles[j]+"</a><br/>";
188                     }
189                     jQuery('#brapi_pheno_studies_table_data_result_div').html(html);
190                 } else {
191                     brapi_create_paginated_table(response.result.data, response.metadata.pagination, 'brapi_pheno_studies_table_data_result_div', return_url, undefined, {}, ['headerRow','observationVariableDbIds','observationVariableNames','data'], 'observationVariableNames', []);
192                 }
193                 jQuery('#working_modal').modal('hide');
194             },
195             error: function(response) {
196                 jQuery('#working_modal').modal('hide');
197                 alert('An error occurred');
198             }
199         });
201     });
203     jQuery(document).on('click', '#brapi_table_select_submit_brapi_home_phenotype_study_observation_units_studies_div', function(){
204         jQuery.each(jQuery('input[name="brapi_table_select_'+studies_return_url+'"]:checked'), function(){
205             if (jQuery.inArray(jQuery(this).val(), selected_results) == -1){
206                 selected_results.push(jQuery(this).val());
207             }
208         });
209         console.log(selected_results);
211         var return_url = jQuery('#brapi_home_phenotypes_url_select').val()+'/brapi/v1/studies/'+selected_results[0]+'/observationunits';
212         jQuery.ajax( {
213             'url': return_url,
214             'beforeSend': function(){
215                 jQuery('#working_modal').modal('show');
216             },
217             'success': function(response) {
218                 //console.log(response);
219                 if (response.metadata.datafiles.length > 0){
220                     var html = "<h4>Download Study Phenotype File(s) From Here: <br/>";
221                     for (var j=0; j<response.metadata.datafiles.length; j++){
222                         html = html + "<a href='"+response.metadata.datafiles[j]+"'>"+response.metadata.datafiles[j]+"</a><br/>";
223                     }
224                     jQuery('#brapi_pheno_studies_observation_units_data_result_div').html(html);
225                 } else {
226                     brapi_create_paginated_table(response.result.data, response.metadata.pagination, 'brapi_pheno_studies_observation_units_data_result_div', return_url, undefined, {}, ['observationUnitName','replicate','germplasmName','blockNumber', 'X', 'Y', 'entryType', 'plotNumber','observations'], 'observationUnitName', []);
227                 }
228                 jQuery('#working_modal').modal('hide');
229             },
230             error: function(response) {
231                 jQuery('#working_modal').modal('hide');
232                 alert('An error occurred');
233             }
234         });
236     });
238     jQuery(document).on('click', '#brapi_table_select_submit_brapi_home_phenotype_study_granular_studies_div', function(){
239         jQuery.each(jQuery('input[name="brapi_table_select_'+studies_return_url+'"]:checked'), function(){
240             if (jQuery.inArray(jQuery(this).val(), selected_results) == -1){
241                 selected_results.push(jQuery(this).val());
242             }
243         });
244         console.log(selected_results);
246         var return_url = jQuery('#brapi_home_phenotypes_url_select').val()+'/brapi/v1/studies/'+selected_results[0]+'/observations';
247         jQuery.ajax( {
248             'url': return_url,
249             'beforeSend': function(){
250                 jQuery('#working_modal').modal('show');
251             },
252             'success': function(response) {
253                 //console.log(response);
254                 if (response.metadata.datafiles.length > 0){
255                     var html = "<h4>Download Study Phenotype File(s) From Here: <br/>";
256                     for (var j=0; j<response.metadata.datafiles.length; j++){
257                         html = html + "<a href='"+response.metadata.datafiles[j]+"'>"+response.metadata.datafiles[j]+"</a><br/>";
258                     }
259                     jQuery('#brapi_pheno_studies_granular_data_result_div').html(html);
260                 } else {
261                     brapi_create_paginated_table(response.result.data, response.metadata.pagination, 'brapi_pheno_studies_granular_data_result_div', return_url, undefined, {}, ['observationUnitName','observationLevel','observationDbId','observationVariableName','germplasmName','observationTimestamp', 'operator', 'value'], 'observationDbId', []);
262                 }
263                 jQuery('#working_modal').modal('hide');
264             },
265             error: function(response) {
266                 jQuery('#working_modal').modal('hide');
267                 alert('An error occurred');
268             }
269         });
271     });
276 function brapi_phenotypes_get_studies(url, div_id) {
277     studies_return_url = url+'/brapi/v1/studies-search';
278     jQuery.ajax( {
279         'url': url+'/brapi/v1/studies-search',
280         'beforeSend': function(){
281             jQuery('#working_modal').modal('show');
282         },
283         'success': function(response) {
284             //console.log(response);
285             brapi_create_paginated_table(response.result.data, response.metadata.pagination, div_id, studies_return_url, undefined, {}, ['studyName','trialName','programName','locationName'], 'studyDbId', []);
286             jQuery('#working_modal').modal('hide');
287         },
288         error: function(response) {
289             jQuery('#working_modal').modal('hide');
290             alert('An error occurred');
291         }
292     });
295 </script>