Skip to content

Commit

Permalink
Merge pull request #3618 from solgenomics/topic/progress_tool
Browse files Browse the repository at this point in the history
Topic/progress tool
  • Loading branch information
lukasmueller authored Jul 15, 2021
2 parents 9e7315c + c4be6da commit 752492a
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/SGN/Controller/AJAX/BreedersToolbox.pm
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,36 @@ sub delete_uploaded_phenotype_files : Path('/ajax/breeders/phenotyping/delete/')
$c->stash->{rest} = {success => 1};
}

sub progress : Path('/ajax/progress') Args(0) {
my $self = shift;
my $c = shift;

my $trait_id = $c->req->param("trait_id");

print STDERR "Trait id = $trait_id\n";

my $schema = $c->dbic_schema("Bio::Chado::Schema");
my $dbh = $schema->storage->dbh();

my $q = "select projectprop.value, avg(phenotype.value::REAL), stddev(phenotype.value::REAL),count(*) from phenotype join cvterm on(cvalue_id=cvterm_id) join nd_experiment_phenotype using(phenotype_id) join nd_experiment_project using(nd_experiment_id) join projectprop using(project_id) where cvterm.cvterm_id=? and phenotype.value not in ('-', 'miss','#VALUE!','..') and projectprop.type_id=(SELECT cvterm_id FROM cvterm where name='project year') group by projectprop.type_id, projectprop.value order by projectprop.value";

my $h = $dbh->prepare($q);

$h->execute($trait_id);

my $data = [];

while (my ($year, $mean, $stddev, $count) = $h->fetchrow_array()) {
push @$data, [ $year, sprintf("%.2f", $mean), sprintf("%.2f", $stddev), $count ];
}

print STDERR "Data = ".Dumper($data);

$c->stash->{rest} = { data => $data };


}



1;
7 changes: 7 additions & 0 deletions lib/SGN/Controller/Static.pm
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ sub test_authentication :Path('/test_authentication/') :Args(0) {
$c->stash->{template} = '/test/test_authenticate.mas';
}

sub progress :Path('/progress') Args(0) {
my $self = shift;
my $c = shift;
$c->stash->{template} = '/breeders_toolbox/progress.mas';
}


sub solanaceae_project_afri :Path('/solanaceae-project/afri-sol/') {
my ($self, $c) = @_;
$c->stash->{template} = '/links/afri_sol.mas';
Expand Down
132 changes: 132 additions & 0 deletions mason/breeders_toolbox/progress.mas
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@

<%args>

</%args>

<script src="https://cdn.jsdelivr.net/npm/vega@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@5"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6"></script>

<& /page/page_title.mas, title => "Breeding Progress" &>

<& /util/import_javascript.mas, classes => [ 'CXGN.BreedersToolbox.HTMLSelect' ] &>

<table class="table">
<tr>
<th width="100">Select Trait</th>

</tr>

<tr>
<td>
<div id="trait_selector_div">[loading...]</div>
</td>


</tr>
</table>

<button class="btn" id="display_stats_button">Show Stats</button>

<table id="progress_stats_datatable">
</table>
<br />
<button class="btn" id="display_graph_button">Show Graph</button>
<div id="trait_averages_bargraph"></div>
<script>
jQuery(window).ready(function () {
get_select_box('traits', 'trait_selector_div', { 'name': 'trait_progress', 'id': 'trait_progress_select', 'empty': 1 });

});


jQuery('#display_stats_button').click(function () {
var trait_id = jQuery('#trait_progress_select').val();
jQuery('#progress_stats_datatable').DataTable({
ajax: '/ajax/progress?trait_id=' + trait_id,
destroy: true,
columns: [
{ "title": "year" },
{ "title": "average" },
{ "title": "std dev" },
{ "title": "observations [count]" }
]
});
});

jQuery('#display_graph_button').click(function () {
//alert('success!');
var trait_id = jQuery('#trait_progress_select').val();
jQuery.ajax({
url: '/ajax/progress?trait_id=' + trait_id,
success: function (r) {
var years = [];
var averages = [];
var errorUp = [];
var errorDown = [];
for (var i = 0; i < r.data.length; i++) {
years.push(r.data[i][0]);
averages.push(r.data[i][1]);
errorUp.push(r.data[i][1] / 1.0 + r.data[i][2] / 1.0); // im aware
errorDown.push(r.data[i][1] / 1.0 - r.data[i][2] / 1.0);
}
var graphData = [];
for (var i = 0; i < r.data.length; i++) {
graphData.push({ "Year": years[i], "Average": averages[i], "mean + stddev": errorUp[i], "mean - stddev": errorDown[i] })
}
var v = {
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": 600,
"height": 600,
"padding": 5,
"data": {
"values": graphData
},
"encoding": {
"x": {
//"bin": true,
"field": "Year",
"type": "ordinal",
"axis": { "labelAngle": 0 },
},
},
"layer": [
{
"mark": {
"type": "bar",
"fill": "#4C78A8",
"stroke": "black",
},
"encoding": {
"y": {
"field": "Average",
"type": "quantitative",
},
},
},
{
"mark": { "type": "errorband" },
"encoding": {
"y": {
"field": "mean + stddev",
"type": "quantitative",
"scale": { "zero": false },
"title": "Average"
},
"y2": { "field": "mean - stddev" },
"color": {"value": "navy"}
},
},

]
}
vegaEmbed("#trait_averages_bargraph", v);
},

error: function (e) {
alert('cannot show graph');

}
});
});
</script>

0 comments on commit 752492a

Please sign in to comment.