1 package CXGN
::BrAPI
::v1
::Germplasm
;
5 use SGN
::Model
::Cvterm
;
7 use CXGN
::Chado
::Stock
;
8 use CXGN
::BrAPI
::Pagination
;
9 use CXGN
::BrAPI
::ErrorResponse
;
12 isa
=> 'Bio::Chado::Schema',
17 has
'metadata_schema' => (
18 isa
=> 'CXGN::Metadata::Schema',
23 has
'phenome_schema' => (
24 isa
=> 'CXGN::Phenome::Schema',
42 isa
=> 'ArrayRef[Maybe[HashRef]]',
47 sub germplasm_search
{
49 my $search_params = shift;
51 my $page_size = $self->page_size;
52 my $page = $self->page;
53 my $status = $self->status;
55 my @germplasm_names = $search_params->{germplasmName
} ? @
{$search_params->{germplasmName
}} : ();
56 my @accession_numbers = $search_params->{accessionNumber
} ? @
{$search_params->{accessionNumber
}} : ();
57 my @genus = $search_params->{germplasmGenus
} ? @
{$search_params->{germplasmGenus
}} : ();
58 my $subtaxa = $search_params->{germplasmSubTaxa
}->[0];
59 my @species = $search_params->{germplasmSpecies
} ? @
{$search_params->{germplasmSpecies
}} : ();
60 my @germplasm_ids = $search_params->{germplasmDbId
} ? @
{$search_params->{germplasmDbId
}} : ();
61 my @germplasm_puis = $search_params->{germplasmPUI
} ? @
{$search_params->{germplasmPUI
}} : ();
62 my $match_method = $search_params->{matchMethod
}->[0];
65 if ($match_method && ($match_method ne 'exact' && $match_method ne 'wildcard')) {
66 push @
$status, { 'error' => "matchMethod '$match_method' not recognized. Allowed matchMethods: wildcard, exact. Wildcard allows % or * for multiple characters and ? for single characters." };
70 my $accession_type_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($self->bcs_schema, 'accession', 'stock_type')->cvterm_id();
74 $search_params{'me.type_id'} = $accession_type_cvterm_id;
76 if (@germplasm_names && scalar(@germplasm_names)>0){
77 if (!$match_method || $match_method eq 'exact') {
78 $search_params{'me.uniquename'} = \
@germplasm_names;
79 } elsif ($match_method eq 'wildcard') {
81 foreach (@germplasm_names) {
83 push @wildcard_names, $_;
85 $search_params{'me.uniquename'} = { 'ilike' => \
@wildcard_names };
89 if (@germplasm_ids && scalar(@germplasm_ids)>0){
90 if (!$match_method || $match_method eq 'exact') {
91 $search_params{'me.stock_id'} = \
@germplasm_ids;
92 } elsif ($match_method eq 'wildcard') {
94 foreach (@germplasm_ids) {
96 push @wildcard_ids, $_;
98 $search_params{'me.stock_id::varchar(255)'} = { 'ilike' => \
@wildcard_ids };
102 #print STDERR Dumper \%search_params;
103 #$self->bcs_schema->storage->debug(1);
104 my $accession_number_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($self->bcs_schema(), 'accession number', 'stock_property')->cvterm_id();
105 if (@accession_numbers && scalar(@accession_numbers)>0) {
106 $search_params{'stockprops.type_id'} = $accession_number_cvterm_id;
107 $search_params{'stockprops.value'} = \
@accession_numbers;
108 push @join_params, 'stockprops';
110 my $accession_pui_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($self->bcs_schema(), 'PUI', 'stock_property')->cvterm_id();
111 if (@germplasm_puis && scalar(@germplasm_puis)>0) {
112 $search_params{'stockprops.type_id'} = $accession_pui_cvterm_id;
113 $search_params{'stockprops.value'} = \
@germplasm_puis;
114 push @join_params, 'stockprops';
116 if (@genus && scalar(@genus)>0) {
117 $search_params{'organism.genus'} = \
@genus;
119 if (@species && scalar(@species)>0) {
120 $search_params{'organism.species'} = \
@species;
123 push @join_params, 'organism';
124 my $rs = $self->bcs_schema()->resultset("Stock::Stock")->search( \
%search_params, {join=>\
@join_params, '+select'=>['organism.genus', 'organism.species', 'organism.common_name'], '+as'=>['genus','species','common_name'], order_by
=>'me.uniquename'} );
128 $total_count = $rs->count();
129 my $rs_slice = $rs->slice($page_size*$page, $page_size*($page+1)-1);
130 while (my $stock = $rs_slice->next()) {
131 my $stockprop_hash = CXGN
::Chado
::Stock
->new($self->bcs_schema, $stock->stock_id)->get_stockprop_hash();
133 my $donor_accessions = $stockprop_hash->{'donor'} ?
$stockprop_hash->{'donor'} : [];
134 my $donor_institutes = $stockprop_hash->{'donor institute'} ?
$stockprop_hash->{'donor institute'} : [];
135 my $donor_puis = $stockprop_hash->{'donor PUI'} ?
$stockprop_hash->{'donor PUI'} : [];
136 for (0 .. scalar(@
$donor_accessions)){
137 push @donor_array, { 'donorGermplasmName'=>$donor_accessions->[$_], 'donorAccessionNumber'=>$donor_accessions->[$_], 'donorInstituteCode'=>$donor_institutes->[$_], 'germplasmPUI'=>$donor_puis->[$_] };
140 germplasmDbId
=>$stock->stock_id,
141 defaultDisplayName
=>$stock->uniquename,
142 germplasmName
=>$stock->uniquename,
143 accessionNumber
=>$stockprop_hash->{'accession number'} ?
join ',', @
{$stockprop_hash->{'accession number'}} : '',
144 germplasmPUI
=>$stockprop_hash->{'PUI'} ?
join ',', @
{$stockprop_hash->{'PUI'}} : '',
145 pedigree
=>$self->germplasm_pedigree_string($stock->stock_id),
146 germplasmSeedSource
=>$stockprop_hash->{'seed source'} ?
join ',', @
{$stockprop_hash->{'seed source'}} : '',
147 synonyms
=> $stockprop_hash->{'stock_synonym'} ?
join ',', @
{$stockprop_hash->{'stock_synonym'}} : '',
148 commonCropName
=>$stock->get_column('common_name'),
149 instituteCode
=>$stockprop_hash->{'institute code'} ?
join ',', @
{$stockprop_hash->{'institute code'}} : '',
150 instituteName
=>$stockprop_hash->{'institute name'} ?
join ',', @
{$stockprop_hash->{'institute name'}} : '',
151 biologicalStatusOfAccessionCode
=>$stockprop_hash->{'biological status of accession code'} ?
join ',', @
{$stockprop_hash->{'biological status of accession code'}} : '',
152 countryOfOriginCode
=>$stockprop_hash->{'country of origin'} ?
join ',', @
{$stockprop_hash->{'country of origin'}} : '',
153 typeOfGermplasmStorageCode
=>$stockprop_hash->{'type of germplasm storage code'} ?
join ',', @
{$stockprop_hash->{'type of germplasm storage code'}} : '',
154 genus
=>$stock->get_column('genus'),
155 species
=>$stock->get_column('species'),
156 speciesAuthority
=>'',
158 subtaxaAuthority
=>'',
159 donors
=>\
@donor_array,
165 %result = (data
=> \
@data);
166 push @
$status, { 'success' => 'Germplasm-search result constructed' };
167 my $pagination = CXGN
::BrAPI
::Pagination
->pagination_response($total_count,$page_size,$page);
170 'pagination' => $pagination,
171 'result' => \
%result,
177 sub germplasm_pedigree_string
{
179 my $stock_id = shift;
180 my $s = CXGN
::Chado
::Stock
->new($self->bcs_schema, $stock_id);
181 my $pedigree_root = $s->get_parents('1');
182 my $pedigree_string = $pedigree_root ?
$pedigree_root->get_pedigree_string('1') : '';
183 return $pedigree_string;
186 sub germplasm_detail
{
188 my $stock_id = shift;
189 my $status = $self->status;
192 my $verify_id = $self->bcs_schema->resultset('Stock::Stock')->find({stock_id
=>$stock_id});
193 my $stock = CXGN
::Chado
::Stock
->new($self->bcs_schema(), $stock_id);
199 push @
$status, { 'error' => 'GermplasmDbId does not exist in the database' };
200 return CXGN
::BrAPI
::ErrorResponse
->return_error($status);
202 my $stockprop_hash = $stock->get_stockprop_hash();
205 my $donor_accessions = $stockprop_hash->{'donor'} ?
$stockprop_hash->{'donor'} : [];
206 my $donor_institutes = $stockprop_hash->{'donor institute'} ?
$stockprop_hash->{'donor institute'} : [];
207 my $donor_puis = $stockprop_hash->{'donor PUI'} ?
$stockprop_hash->{'donor PUI'} : [];
208 for (0 .. scalar(@
$donor_accessions)){
209 push @donor_array, { 'donorGermplasmName'=>$donor_accessions->[$_], 'donorAccessionNumber'=>$donor_accessions->[$_], 'donorInstituteCode'=>$donor_institutes->[$_], 'germplasmPUI'=>$donor_puis->[$_] };
213 germplasmDbId
=>$stock_id,
214 defaultDisplayName
=>$stock->get_name(),
215 germplasmName
=>$stock->get_uniquename(),
216 accessionNumber
=>$stock->get_uniquename(),
217 germplasmPUI
=>$stock->get_uniquename(),
218 pedigree
=>$self->germplasm_pedigree_string($stock_id),
219 germplasmSeedSource
=>$stockprop_hash->{'seed source'} ?
join ',', @
{$stockprop_hash->{'seed source'}} : '',
220 synonyms
=>$stockprop_hash->{'stock_synonym'} ?
join ',', @
{$stockprop_hash->{'stock_synonym'}} : '',
221 commonCropName
=>$stock->get_organism->common_name(),
222 instituteCode
=>$stockprop_hash->{'institute code'} ?
join ',', @
{$stockprop_hash->{'institute code'}} : '',
223 instituteName
=>$stockprop_hash->{'institute name'} ?
join ',', @
{$stockprop_hash->{'institute name'}} : '',
224 biologicalStatusOfAccessionCode
=>$stockprop_hash->{'biological status of accession code'} ?
join ',', @
{$stockprop_hash->{'biological status of accession code'}} : '',
225 countryOfOriginCode
=>$stockprop_hash->{'country of origin'} ?
join ',', @
{$stockprop_hash->{'country of origin'}} : '',
226 typeOfGermplasmStorageCode
=>$stockprop_hash->{'type of germplasm storage code'} ?
join ',', @
{$stockprop_hash->{'type of germplasm storage code'}} : '',
227 genus
=>$stock->get_organism->genus(),
228 species
=>$stock->get_organism->species(),
229 speciesAuthority
=>'',
231 subtaxaAuthority
=>'',
232 donors
=>\
@donor_array,
235 push @
$status, { 'success' => 'Germplasm-search result constructed' };
236 my $pagination = CXGN
::BrAPI
::Pagination
->pagination_response($total_count,1,0);
239 'pagination' => $pagination,
240 'result' => \
%result,
246 sub germplasm_pedigree
{
249 my $stock_id = $inputs->{stock_id
};
250 my $notation = $inputs->{notation
};
251 my $status = $self->status;
253 push @
$status, { 'info' => 'Notation not yet implemented. Returns a simple parent1/parent2 string.' };
254 if ($notation ne 'purdy') {
255 push @
$status, { 'error' => 'Unsupported notation code. Allowed notation: purdy' };
261 my $s = CXGN
::Chado
::Stock
->new($self->bcs_schema(), $stock_id);
264 my @direct_parents = $s->get_direct_parents();
266 germplasmDbId
=>$stock_id,
267 pedigree
=>$self->germplasm_pedigree_string($stock_id),
268 parent1Id
=>$direct_parents[0][0],
269 parent2Id
=>$direct_parents[1][0]
273 push @
$status, { 'success' => 'Germplasm-pedigree result constructed' };
274 my $pagination = CXGN
::BrAPI
::Pagination
->pagination_response($total_count,1,0);
277 'pagination' => $pagination,
278 'result' => \
%result,
284 sub germplasm_markerprofiles
{
286 my $stock_id = shift;
288 my $page_size = $self->page_size;
289 my $page = $self->page;
290 my $status = $self->status;
293 my $snp_genotyping_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($self->bcs_schema, 'snp genotyping', 'genotype_property')->cvterm_id();
295 my $rs = $self->bcs_schema->resultset('NaturalDiversity::NdExperiment')->search(
296 {'genotypeprops.type_id' => $snp_genotyping_cvterm_id, 'stock.stock_id'=>$stock_id},
297 {join=> [{'nd_experiment_genotypes' => {'genotype' => 'genotypeprops'} }, {'nd_experiment_protocols' => 'nd_protocol' }, {'nd_experiment_stocks' => 'stock'} ],
298 select=> ['genotypeprops.genotypeprop_id'],
299 as
=> ['genotypeprop_id'],
300 order_by
=>{ -asc
=>'genotypeprops.genotypeprop_id' }
304 my $rs_slice = $rs->slice($page_size*$page, $page_size*($page+1)-1);
305 while (my $gt = $rs_slice->next()) {
306 push @marker_profiles, $gt->get_column('genotypeprop_id');
308 my $total_count = scalar(@marker_profiles);
310 germplasmDbId
=>$stock_id,
311 markerProfiles
=>\
@marker_profiles
313 push @
$status, { 'success' => 'Germplasm-markerprofiles result constructed' };
314 my $pagination = CXGN
::BrAPI
::Pagination
->pagination_response($total_count,$page_size,$page);
317 'pagination' => $pagination,
318 'result' => \
%result,