Merge pull request #5280 from solgenomics/5714_phenotype_genotype_data_check
[sgn.git] / lib / CXGN / BrAPI / v2 / GenomeMaps.pm
blob4bbf35c8840a7778b4d8659fc7b184a971b25654
1 package CXGN::BrAPI::v2::GenomeMaps;
3 use Moose;
4 use Data::Dumper;
5 use SGN::Model::Cvterm;
6 use JSON;
7 use CXGN::Cview::MapFactory;
8 use CXGN::BrAPI::Pagination;
9 use CXGN::BrAPI::JSONResponse;
11 extends 'CXGN::BrAPI::v2::Common';
13 =head2 list
15 Usage: $brapi->list()
16 Desc: lists all available maps.
17 Ret: returns a hash with all the map info
18 for each map, the following keys are present:
19 mapDbId
20 name
21 species
22 type
23 unit
24 markerCount
25 comments
26 linkageGroupCount
27 see brapi documentation for more information.
28 Args: usual brapi args (pageSize etc)
29 Side Effects:
30 Example:
32 =cut
34 sub list {
35 my $self = shift;
36 my $inputs = shift;
37 my $page_size = $self->page_size;
38 my $page = $self->page;
39 my $status = $self->status;
41 my $crop_id = $inputs->{commonCropName} || ($inputs->{commonCropNames} || ());
42 my $scientific_id = $inputs->{scientificName} || ($inputs->{scientificNames} || ());
43 my $type_id = $inputs->{type} || ($inputs->{types} || ());
44 my $map_id = $inputs->{mapDbId} || ($inputs->{mapDbIds} || ());
45 # mapPUI
46 # programDbId
47 # trialDbId
48 # studyDbId
49 my @maps;
51 my $start = $page_size*$page;
52 my $end = $page_size*($page+1)-1;
54 my $map_factory = CXGN::Cview::MapFactory->new($self->bcs_schema()->storage->dbh(), $inputs->{config});
56 if ( $map_id ) {
57 @maps = $map_factory->create( { map_id => $map_id->[0] });
58 } else {
59 @maps = $map_factory->get_all_maps();
62 my @data;
63 my $passes_search;
65 my $query = "SELECT map_id, date_loaded, count(distinct(location_id)) FROM sgn.map_version JOIN marker_location using (map_version_id) WHERE map_version_id=? GROUP BY 1,2";
66 my $sth = $self->bcs_schema->storage()->dbh()->prepare($query);
68 foreach my $m (@maps) {
69 my $map_version_id = $m->get_id();
70 if ($map_version_id =~ /\D/) { next; } # not a valid id
71 $sth->execute($map_version_id);
72 my ($map_id, $date_loaded, $marker_count) = $sth->fetchrow_array();
74 my $map_type = $m->get_type();
75 my $map_units = $m->get_units();
76 if ($map_type eq 'sequence'){
77 $map_type = 'Physical';
78 $map_units = 'Mb';
79 } else {
80 $map_type = 'Genetic';
81 $map_units = 'cM';
83 my $scientific_name = $m->get_organism();
84 my $common_name = $m->get_common_name();
86 $passes_search = 1;
87 if ( $crop_id && ! grep { $_ eq $common_name } @{$crop_id} ) { $passes_search = 0;};
88 if ( $scientific_id && ! grep { $_ eq $scientific_name } @{$scientific_id} ) { $passes_search = 0;};
89 if ( $type_id && ! grep { $_ eq $map_type } @{$type_id} ) { $passes_search = 0;};
91 if ( $passes_search ){
92 my %map_info = (
93 additionalInfo => {name => $m->get_long_name()},
94 comments => $m->get_abstract(),
95 commonCropName => $common_name,
96 documentationURL => "https://brapi.org",
97 linkageGroupCount => $m->get_chromosome_count(),
98 mapDbId => qq|$map_id|,
99 mapName => $m->get_short_name(),
100 mapPUI => undef,
101 markerCount => $marker_count,
102 publishedDate => $date_loaded,
103 scientificName => $scientific_name,
104 type => $map_type,
105 unit => $map_units,
107 push @data, \%map_info;
111 my $total_count = scalar(@maps);
112 my %result = (data => \@data);
113 my @data_files;
114 my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,$page_size,$page);
116 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination,
117 \@data_files, $status, 'Maps list result constructed');
121 =head2 detail
123 Usage: $brapi->detail()
124 Desc: returns the detail information of a map in brapi format
125 Ret:
126 Args:
127 Side Effects:
128 Example:
130 =cut
133 sub detail {
134 my $self = shift;
135 my $map_id = shift;
136 my $page_size = $self->page_size;
137 my $page = $self->page;
138 my $status = $self->status;
139 my %result;
141 my $map_factory = CXGN::Cview::MapFactory->new($self->bcs_schema->storage()->dbh());
142 my $map = $map_factory->create( { map_id => $map_id });
144 if ($map){
145 my $map_type = $map->get_type();
146 my $map_units = $map->get_units();
147 if ($map_type eq 'sequence'){
148 $map_type = 'Physical';
149 $map_units = 'Mb';
150 } else {
151 $map_type = 'Genetic';
152 $map_units = 'cM';
155 my $scientific_name = $map->get_organism();
157 my $query = "SELECT map_id, date_loaded, count(distinct(location_id)) FROM sgn.map_version JOIN marker_location using (map_version_id) WHERE map_version_id=? GROUP BY 1,2";
158 my $sth = $self->bcs_schema->storage()->dbh()->prepare($query);
159 my $map_version_id = $map->get_id();
160 if ($map_version_id =~ /\D/) { next; }
161 $sth->execute($map_version_id);
162 my ($map_id1, $date_loaded, $marker_count) = $sth->fetchrow_array();
164 %result = (
165 additionalInfo => { name => $map->get_long_name() },
166 comments => $map->get_abstract(),
167 commonCropName => $map->get_common_name(),
168 documentationURL => "https://brapi.org",
169 linkageGroupCount => $map->get_chromosome_count(),
170 mapDbId => qq|$map_id|,
171 mapName => $map->get_short_name(),
172 mapPUI => undef,
173 markerCount => $marker_count,
174 publishedDate => $date_loaded,
175 scientificName => $scientific_name,
176 type => $map_type,
177 unit => $map_units,
181 my @data_files;
182 my $pagination = CXGN::BrAPI::Pagination->pagination_response(1,$page_size,$page);
184 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Maps detail result constructed');
188 sub linkagegroups {
189 my $self = shift;
190 my $inputs = shift;
191 my $map_id = $inputs->{map_id};
192 my $page_size = $self->page_size;
193 my $page = $self->page;
194 my $status = $self->status;
195 my $total_count = 0;
196 my @data = ();
198 my $map_factory = CXGN::Cview::MapFactory->new($self->bcs_schema->storage()->dbh());
199 my $map = $map_factory->create( { map_id => $map_id });
201 if ($map){
202 foreach my $chr ($map->get_chromosomes()) {
203 push @data, {
204 additionalInfo => {},
205 linkageGroupName => $chr->get_name(),
206 markerCount => scalar($chr->get_markers()),
207 maxPosition => $chr->get_length()
209 $total_count++;
213 my @data_files;
214 my %result = (data => \@data);
215 my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,$page_size,$page);
217 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Maps detail result constructed');
220 # =head2 genosort
222 # Usage: genosort($a_chr, $a_pos, $b_chr, $b_pos)
223 # Desc: sorts marker coordinates according to position for marker names
224 # of the format S(\d+)_(.*)
225 # Ret:
226 # Args:
227 # Side Effects:
228 # Example:
230 # =cut
232 # sub genosort {
233 # my ($a_chr, $a_pos, $b_chr, $b_pos);
234 # if ($a =~ m/S(\d+)\_(.*)/) {
235 # $a_chr = $1;
236 # $a_pos = $2;
238 # if ($b =~ m/S(\d+)\_(.*)/) {
239 # $b_chr = $1;
240 # $b_pos = $2;
243 # if ($a_chr && $b_chr) {
244 # if ($a_chr == $b_chr) {
245 # return $a_pos <=> $b_pos;
247 # return $a_chr <=> $b_chr;
248 # } else {
249 # return -1;
253 # sub get_protocolprop_hash {
254 # my $self = shift;
255 # my $nd_protocol_id = shift;
256 # my $prop_rs = $self->bcs_schema->resultset('NaturalDiversity::NdProtocolprop')->search({'me.nd_protocol_id' => $nd_protocol_id}, {join=>['type'], +select=>['type.name', 'me.value'], +as=>['name', 'value']});
257 # my $prop_hash;
258 # while (my $r = $prop_rs->next()){
259 # push @{ $prop_hash->{$r->get_column('name')} }, $r->get_column('value');
262 # return $prop_hash;