Merge pull request #5191 from solgenomics/topic/quality_control
[sgn.git] / lib / CXGN / BrAPI / v1 / GenomeMaps.pm
blob1d017527ab8ddb90de0aa6bf064c70967da79628
1 package CXGN::BrAPI::v1::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::v1::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 $start = $page_size*$page;
42 my $end = $page_size*($page+1)-1;
44 my $map_factory = CXGN::Cview::MapFactory->new($self->bcs_schema()->storage->dbh(), $inputs->{config});
46 my @maps = $map_factory->get_all_maps();
47 my @data;
49 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";
50 my $sth = $self->bcs_schema->storage()->dbh()->prepare($query);
52 foreach my $m (@maps) {
53 my $map_version_id = $m->get_id();
54 if ($map_version_id =~ /\D/) { next; } # not a valid id
55 $sth->execute($map_version_id);
56 my ($map_id, $date_loaded, $marker_count) = $sth->fetchrow_array();
57 my $map_type = $m->get_type();
58 my $map_units = $m->get_units();
59 if ($map_type eq 'sequence'){
60 $map_type = 'Physical';
61 $map_units = 'Mb';
62 } else {
63 $map_type = 'Genetic';
64 $map_units = 'cM';
66 my $scientific_name = $m->get_organism();
67 my ($genus,$species) = undef;
68 if ($scientific_name) { ($genus,$species) = split(" ", $scientific_name) };
69 my %map_info = (
70 mapDbId => qq|$map_id|,
71 name => $m->get_long_name(),
72 species => $species,
73 type => $map_type,
74 unit => $map_units,
75 markerCount => $marker_count,
76 comments => $m->get_abstract(),
77 linkageGroupCount => $m->get_chromosome_count(),
78 commonCropName => $m->get_common_name(),
79 documentationURL => "https://brapi.org",
80 mapName => $m->get_short_name(),
81 publishedDate => $date_loaded,
82 scientificName => $scientific_name,
85 push @data, \%map_info;
88 my $total_count = scalar(@maps);
89 my %result = (data => \@data);
90 my @data_files;
91 my $pagination = CXGN::BrAPI::Pagination->pagination_response($total_count,$page_size,$page);
93 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination,
94 \@data_files, $status, 'Maps list result constructed');
98 =head2 detail
100 Usage: $brapi->detail()
101 Desc: returns the detail information of a map in brapi format
102 Ret:
103 Args:
104 Side Effects:
105 Example:
107 =cut
109 sub detail {
110 my $self = shift;
111 my $map_id = shift;
112 my $page_size = $self->page_size;
113 my $page = $self->page;
114 my $status = $self->status;
116 my $map_factory = CXGN::Cview::MapFactory->new($self->bcs_schema->storage()->dbh());
118 my $map = $map_factory->create( { map_id => $map_id });
119 my $map_type = $map->get_type();
120 my $map_units = $map->get_units();
121 if ($map_type eq 'sequence'){
122 $map_type = 'Physical';
123 $map_units = 'Mb';
124 } else {
125 $map_type = 'Genetic';
126 $map_units = 'cM';
129 my @data = ();
131 foreach my $chr ($map->get_chromosomes()) {
132 push @data, {
133 linkageGroupName => $chr->get_name(),
134 markerCount => scalar($chr->get_markers()),
135 maxPosition => $chr->get_length()
138 my ($data_window, $pagination) = CXGN::BrAPI::Pagination->paginate_array(\@data,$page_size,$page);
140 my %result = (
141 mapDbId => qq|$map_id|,
142 mapName => $map->get_short_name(),
143 type => $map_type,
144 unit => $map_units,
145 comments => $map->get_abstract(),
146 documentationURL => "https://brapi.org",
147 data => $data_window,
150 my @data_files;
151 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Maps detail result constructed');
154 =head2 positions
156 Usage: $brapi->positions()
157 Desc: returns all the positions and marker scores for a given
158 map and chromosome
159 Ret:
160 Args:
161 Side Effects:
162 Example:
164 =cut
166 sub positions {
167 my $self = shift;
168 my $inputs = shift;
169 my $map_id = $inputs->{map_id};
170 my $min = $inputs->{min};
171 my $max = $inputs->{max};
172 my @linkage_group_ids = $inputs->{linkage_group_ids} ? @{$inputs->{linkage_group_ids}} : ();
173 my $page_size = $self->page_size;
174 my $page = $self->page;
175 my $status = $self->status;
177 my $map_factory = CXGN::Cview::MapFactory->new($self->bcs_schema->storage()->dbh());
179 my $map = $map_factory->create( { map_id => $map_id });
181 my @data = ();
183 foreach my $chr ($map->get_chromosomes()) {
184 foreach my $m ($chr->get_markers()) {
185 if (@linkage_group_ids) {
186 if (grep $_ eq $chr->get_name(), @linkage_group_ids) {
187 push @data, {
188 markerDbId => $m->get_id(),
189 markerName => $m->get_name(),
190 location => $m->get_offset(),
191 linkageGroupName => $chr->get_name(),
195 else {
196 push @data, {
197 markerDbId => $m->get_id(),
198 markerName => $m->get_name(),
199 location => $m->get_offset(),
200 linkageGroupName => $chr->get_name()
205 my $marker_count = scalar(@data);
206 my ($data_window, $pagination) = CXGN::BrAPI::Pagination->paginate_array(\@data,$marker_count,$page); #set page size to total number of markers
208 my %result = ( data => $data_window );
209 my @data_files;
210 return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Maps detail result constructed');
213 =head2 genosort
215 Usage: genosort($a_chr, $a_pos, $b_chr, $b_pos)
216 Desc: sorts marker coordinates according to position for marker names
217 of the format S(\d+)_(.*)
218 Ret:
219 Args:
220 Side Effects:
221 Example:
223 =cut
225 sub genosort {
226 my ($a_chr, $a_pos, $b_chr, $b_pos);
227 if ($a =~ m/S(\d+)\_(.*)/) {
228 $a_chr = $1;
229 $a_pos = $2;
231 if ($b =~ m/S(\d+)\_(.*)/) {
232 $b_chr = $1;
233 $b_pos = $2;
236 if ($a_chr && $b_chr) {
237 if ($a_chr == $b_chr) {
238 return $a_pos <=> $b_pos;
240 return $a_chr <=> $b_chr;
241 } else {
242 return -1;
246 sub get_protocolprop_hash {
247 my $self = shift;
248 my $nd_protocol_id = shift;
249 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']});
250 my $prop_hash;
251 while (my $r = $prop_rs->next()){
252 push @{ $prop_hash->{$r->get_column('name')} }, $r->get_column('value');
255 return $prop_hash;