minor fixes
[sgn.git] / js / solGS / listTypeSelectionPopulation.js
blobf89084591fc62a46a3cfae90619597e5dcccbf35
3 /**
4 selection population upload from lists
5 and files. Run prediction model on uploaded selection population 
6 and display output.
8 Isaak Y Tecle 
9 iyt2@cornell.edu
12 JSAN.use("CXGN.List");
13 JSAN.use("jquery.blockUI");
16 jQuery(document).ready( function() {
17     var list = new CXGN.List();
18     var listMenu = list.listSelect("prediction_genotypes", ["accessions"]);
19     var relevant =[];   
20         
21     if (listMenu.match(/option/) != null) {
22             
23         jQuery("#prediction_genotypes_list").append(listMenu);
25         } else {
26             
27             jQuery("#prediction_genotypes_list").append("<select><option>no lists found</option></select>");
28         }
29                
30     });
33 jQuery(document).ready( function() { 
34         var listId;
35         
36         jQuery("<option>", {value: '', selected: true}).prependTo("#prediction_genotypes_list_select");
37         jQuery("#prediction_genotypes_list_select").change(function() {
38            
39                 listId = jQuery(this).find("option:selected").val();              
40              
41                 if(listId) {
42                     var cnt = 1;
43                     jQuery("#prediction_genotypes_list_upload").click(function() {
44                         if(cnt === 1) {         
45                             loadGenotypesList(listId);
46                         }
47                         cnt = cnt + 1 ;
48                     });
49                 }
50             });       
51     });
54 function getGenotypesList(listId) {
55     
56     var list = new CXGN.List();
57     var genotypesList;
58     
59     if (! listId == "") {
60         genotypesList = list.getListData(listId);
61     }
63     var listName = list.listNameById(listId);
64     var listType;// = list.getListType(listId);
66     return {'name'      : listName,
67             'list'      : genotypesList.elements,
68             'list_type' : listType,
69             };
73 function loadGenotypesList(listId) {
74      
75     var genoList       = getGenotypesList(listId);
76     var listName       = genoList.name;
77     var list           = genoList.list;
78     var modelId        = getModelId();
79     var traitId        = getTraitId();
80     var selectionPopId = listId;
81     
82     if ( list.length === 0) {       
83         alert('The list is empty. Please select a list with content.' );
84     }
85     else {  
87         jQuery.blockUI.defaults.applyPlatformOpacityRules = false;
88         jQuery.blockUI({message: 'Please wait..'});
90         list = JSON.stringify(list);
91    
92         jQuery.ajax({
93                 type: 'POST',
94                     dataType: 'json',
95                     data: {'id': 'uploaded_' + listId, 'name': listName, 'list': list},
96                     url: '/solgs/upload/prediction/genotypes/list',
97                    
98                     success: function(response) {
99                    
100                     if (response.status == 'success') {
101     
102                         var uploadedSelPops = jQuery("#uploaded_selection_pops_table").doesExist();
103                        
104                         if (uploadedSelPops == false) {  
105                             
106                             uploadedSelPops = getUserUploadedSelPop(listId);                    
107                             jQuery("#uploaded_selection_populations").append(uploadedSelPops).show();
108                            
109                         }
110                         else {
111                             
112                             var url =   '\'/solgs/model/'+ modelId + '/uploaded/prediction/'+ selectionPopId + '\'' ;
113                             var listIdArg = '\'' + listId +'\'';
114                             var listSource = '\'from_db\'';
115                        
116                             var popIdName   = {id : 'uploaded_' + listId, name: listName, pop_type: 'list_selection'};
117                             popIdName       = JSON.stringify(popIdName);
118                             var hiddenInput =  '<input type="hidden" value=\'' + popIdName + '\'/>';
119                             
120                             var addRow = '<tr><td>'
121                                 +'<a href="#" onclick="javascript:loadPredictionOutput(' + url + ',' 
122                                 + listIdArg + ',' + listSource + '); return false;">' + '<data>'+ hiddenInput + '</data>'
123                                 + listName + '</a>'
124                                 + '</td>'
125                                 + '<td id="list_prediction_output_' + listId +  '">'
126                                 + '<a href="#" onclick="javascript:loadPredictionOutput(' + url + ',' 
127                                 + listIdArg + ',' + listSource + '); return false;">' 
128                                 + '[ Predict ]'+ '</a>'             
129                                 + '</td><tr>';
131                             var tdId = '#list_prediction_output_' + listId;
132                             var addedRow = jQuery(tdId).doesExist();
133                            
134                             if (addedRow == false) {
135                                 jQuery("#uploaded_selection_pops_table tr:last").after(addRow);
137                             }                          
138                         }
139                         jQuery.unblockUI();                        
140                       
141                     } else {
142                        
143                         alert("Error occured while uploading the list of selection genotypes.");                      
144                         jQuery.unblockUI();   
145                     }
146                      
147                 },
148                     error: function(res) {
149                  
150                     jQuery.unblockUI();
151                     alert("Error occured while uploading the list of selection genotypes.\n\n" + res.responseText);
152                 }            
153             });        
154     }
158 jQuery.fn.doesExist = function(){
159         return jQuery(this).length > 0;
160  };
163 function getModelId () {
164   
165     var modelId;
166     var modelIdExists = jQuery("#model_id").doesExist();
167     var comboPopsIdExists = jQuery("#combo_pops_id").doesExist();
168    
169     if ( modelIdExists == true ) {        
170         modelId = jQuery("#model_id").val();
171     }
172     else if ( comboPopsIdExists == true ) {      
173         modelId = jQuery("#combo_pops_id").val();
174     }
175   
176     return modelId;
180 function getTraitId () {
181   
182     var traitId;
183     var traitIdExists = jQuery("#trait_id").doesExist();
185     if ( traitIdExists == true ) {        
186         traitId = jQuery("#trait_id").val();
187     }
188     
189     return traitId;
193 function getUserUploadedSelPop (listId) {
194    
195     var genoList       = getGenotypesList(listId);
196     var listName       = genoList.name;
197     var list           = genoList.list;
198     var modelId        = getModelId();
199     var traitId        = getTraitId();
200     var selectionPopId = listId;
201    
202     var url         =   '\'/solgs/model/'+ modelId + '/uploaded/prediction/'+ selectionPopId + '\'' ;
203     var listIdArg   = '\'' + listId +'\'';
204     var listSource  = '\'from_db\'';
205     var popIdName   = {'id' : 'uploaded_' + listId, 'name' : listName, 'pop_type': 'list_selection'};
206     popIdName       = JSON.stringify(popIdName);
207     var hiddenInput =  '<input type="hidden" value=\'' + popIdName + '\'/>';
208   
209     var uploadedSelPop ='<table id="uploaded_selection_pops_table" style="width:100%;text-align:left"><tr>'
210                                 + '<th>List-based selection population</th>'
211                                 + '<th>Predict GEBVs</th>'
212                                 +'</tr>'
213                                 + '<tr>'
214                                 + '<td>'
215                                 + '<a href="#" onclick="javascript:loadPredictionOutput(' + url + ',' 
216                                 + listIdArg + ',' + listSource + '); return false;">' + '<data>'+ hiddenInput + '</data>'
217                                 + listName + '</a>'
218                                 + '</td>'
219                                 + '<td id="list_prediction_output_' + listId +  '">'
220                                 + '<a href="#" onclick="javascript:loadPredictionOutput(' + url + ',' 
221                                 + listIdArg + ',' + listSource + '); return false;">' 
222                                 + '[ Predict ]'+ '</a>'
223                                 + '</td></tr></table>';
225     return uploadedSelPop;
229 function loadPredictionOutput (url, listId, listSource) {
230    
231     var traitId = getTraitId();
232     var modelId = getModelId();
233    
234     jQuery.blockUI.defaults.applyPlatformOpacityRules = false;
235     jQuery.blockUI({message: 'Please wait..'});
236    
237     jQuery.ajax({
238         type: 'POST',
239         url: url,
240         dataType: 'json',
241         data: {
242             'uploaded_prediction': 1, 
243             'trait_id': traitId, 
244             'model_id': modelId, 
245             'prediction_id': listId,
246             'list_source': listSource,
247         },
248                 
249         success: function (response) {
250                   
251             if (response.status == 'success') {
252                     
253                 var tdId = '#list_prediction_output_' + listId;
254                 jQuery(tdId).html(response.output);
255                                  
256                 var page = document.URL; 
257                     
258                 if (page.match('/traits/all/population/') != null) {
259                     listSelectionIndexPopulations();
260                     listGenCorPopulations();                 
261                 }
262                     
263                 jQuery.unblockUI();        
264             }
265             else {                
266                 if(response.status == 'failed') {
267                     alert("Error occured while uploading the list of selection genotypes.");
268                 } else {
269                     alert(response.status);  
270                 }
271                   
272                 jQuery.unblockUI();                 
273             }
274         },
275                 
276         error: function(response) {
277             alert('error: ' + res.responseText);
279         }                       
280     });
281