Merge pull request #4073 from solgenomics/topic/fix_another_type_id_issue
[sgn.git] / t / unit_mech / AJAX / SequenceMetadata.t
blob148e233bfbd1c471d36b0bf0939ac8d8b0487a3a
1 use strict;
3 use lib 't/lib';
5 use Test::More tests => 16;
7 use Data::Dumper;
8 use JSON;
9 use File::Copy;
11 use SGN::Test::Fixture;
12 use SGN::Test::Data qw/create_test/;
13 use SGN::Test::WWW::Mechanize;
14 use LWP::UserAgent;
16 use SGN::Model::Cvterm;
17 use CXGN::Genotype::SequenceMetadata;
20 my $t = SGN::Test::Fixture->new();
21 my $mech = SGN::Test::WWW::Mechanize->new;
22 my $ua = LWP::UserAgent->new;
23 my $schema = $t->bcs_schema;
24 my $dbh = $mech->context->dbc->dbh;
26 my $smd = CXGN::Genotype::SequenceMetadata->new(bcs_schema => $schema);
27 my $smd_script_dir = $t->config->{basepath} . $smd->shell_script_dir;
28 my $test_file = $t->config->{basepath} . "/t/data/sequence_metadata/gwas_sgn.gff3";
30 my $genus_name = "SMDTest";
31 my $species_name = "SMDtest";
32 my $species = $genus_name . ' ' . $species_name;
33 my ($organism_rs) = $schema->resultset("Organism::Organism")->create({genus => $genus_name, species => $species_name});
34 my $feature_organism_id = $organism_rs->organism_id;
35 my $feature_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'chromosome', 'sequence')->cvterm_id();
36 my $type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'GWAS Results', 'sequence_metadata_types')->cvterm_id();
38 my $query_reference_genome_name = "Test Reference Genome";
39 my $query_feature_name = "SMD_TEST_CHR_1A";
40 my $query_feature_id;
41 my $query_type_name = "GWAS Results";
42 my $query_type_id;
43 my $query_protocol_name = "Test Protocol";
44 my $query_protocol_id;
47 # Get features from test file
48 my $script = $smd_script_dir . "/get_unique_features.sh";
49 my $cmd = "bash " . $script . " \"" . $test_file . "\"";
50 my @features = `$cmd`;
51 chomp(@features);
53 # Add features to database
54 foreach my $feature (@features) {
55     if ( $feature !~ /^#/ ) {
56         my ($feature_rs) = $schema->resultset("Sequence::Feature")->create({
57             organism_id => $feature_organism_id,
58             name => $feature,
59             uniquename => $feature,
60             type_id => $feature_type_id,
61             is_obsolete => 'f'
62         });
63     }
66 # Get attributes from test file
67 my $attributes_script = $smd_script_dir . "/get_unique_attributes.sh";
68 my $attributes_cmd = "bash " . $attributes_script . " \"" . $test_file . "\"";
69 my @attributes = `$attributes_cmd`;
70 chomp(@attributes);
76 ## TEST: LOGIN
77 ## tests = 3
79 $mech->post_ok('/brapi/v1/token', [ "username"=> "janedoe", "password"=> "secretpw", "grant_type"=> "password" ], "BrAPI Login");
80 my $response = decode_json $mech->content;
81 is($response->{'metadata'}->{'status'}->[2]->{'message'}, 'Login Successfull', "Login Succssful");
82 my $sgn_session_id = $response->{access_token};
83 isnt($sgn_session_id, '', "SGN Session Token");
87 ## 
88 ## TEST: VERIFY GFF FILE
89 ## tests = 2
91 my %verification_body = (
92     file => [ $test_file, 'sequence_metadata_upload' ],
93     species => $species,
94     "use_existing_protocol" => "false",
95     "new_protocol_attribute_count" => scalar @attributes
97 my $verification_attribute_count = 1;
98 foreach my $a (@attributes) {
99     $verification_body{'new_protocol_attribute_key_' . $verification_attribute_count} = $a;
100     $verification_attribute_count++;
103 my $verification_response = $ua->post(
104     'http://localhost:3010/ajax/sequence_metadata/file_upload_verify',
105     Cookie => 'sgn_session_id=' . $sgn_session_id,
106     Content_Type => 'form-data',
107     Content => \%verification_body
109 my $verification_message = decode_json $verification_response->decoded_content;
110 my $processed_file = $verification_message->{'results'}->{'processed_filepath'};
111 is($verification_message->{'results'}->{'processed'}, 1, "Process GFF File");
112 is($verification_message->{'results'}->{'verified'}, 1, "Verify GFF File");
117 ## TEST 3: STORE GFF FILE
118 ## tests = 2
120 my %store_body = (
121     "processed_filepath" => $processed_file,
122     "use_existing_protocol" => "false",
123     "new_protocol_name" => $query_protocol_name,
124     "new_protocol_description" => "Protocol used for testing the sequence metadata storage",
125     "new_protocol_sequence_metadata_type" => $type_id,
126     "new_protocol_reference_genome" => $query_reference_genome_name,
127     "new_protocol_species" => $species,
128     "new_protocol_score_description" => "score description",
129     "new_protocol_attribute_count" => scalar @attributes
131 my $store_attribute_count = 1;
132 foreach my $a (@attributes) {
133     $store_body{'new_protocol_attribute_key_' . $store_attribute_count} = $a;
134     $store_attribute_count++;
137 my $store_response = $ua->post(
138     'http://localhost:3010/ajax/sequence_metadata/store',
139     Cookie => 'sgn_session_id=' . $sgn_session_id,
140     Content_Type => 'form-data',
141     Content => \%store_body
143 my $store_message = decode_json $store_response->decoded_content;
144 my $nd_protocol_id = $store_message->{'results'}->{'nd_protocol_id'};
145 is($store_message->{'results'}->{'stored'}, 1, "Store GFF File");
146 cmp_ok($store_message->{'results'}->{'chunks'}, 'gt', 0, "Stored Chunks");
151 ## TEST: GET FEATURES
152 ## tests = 1
154 $mech->get('/ajax/sequence_metadata/features');
155 my $features_response = decode_json($mech->content)->{'features'};
156 my $missing_features = 0;
157 foreach my $ff (@features) {
158     if ( $ff !~ /^#/ ) {
159         my $found = 0;
160         foreach my $rf (@$features_response) {
161             if ( $rf->{'feature_name'} eq $ff ) {
162                 $found = 1;
163                 if ( $ff eq $query_feature_name ) {
164                     $query_feature_id = $rf->{'feature_id'};
165                 }
166             }
167         }
168         if ( !$found ) {
169             $missing_features++;
170         }
171     }
173 is($missing_features, 0, "Stored and Retrieved Features");
178 ## TEST: GET DATA TYPES
179 ## tests = 1
181 $mech->get('/ajax/sequence_metadata/types');
182 my $types_response = decode_json($mech->content)->{'types'};
183 foreach my $type (@$types_response) {
184     if ( $type->{'type_name'} eq $query_type_name ) {
185         $query_type_id = $type->{'type_id'};
186     }
188 cmp_ok(scalar @$types_response, 'gt', 0, "Retrieved Data Types");
193 ## TEST: GET PROTOCOLS
194 ## tests = 1
196 $mech->get('/ajax/sequence_metadata/protocols');
197 my $protocols_response = decode_json($mech->content)->{'protocols'};
198 my $found_protocol = 0;
199 foreach my $protocol (@$protocols_response) {
200     if ( $protocol->{'nd_protocol_name'} eq $query_protocol_name ) {
201         if ( $protocol->{'nd_protocol_id'} == $nd_protocol_id ) {
202             $found_protocol = 1;
203             $query_protocol_id = $protocol->{'nd_protocol_id'};
204         }
205     }
207 is($found_protocol, 1, "Stored and Retrieved Protocol");
212 ## TEST: QUERY
213 ## tests = 6
215 isnt($query_feature_id, undef, "Query Feature ID Defined");
216 isnt($query_type_id, undef, "Query Type ID Defined");
217 isnt($query_protocol_id, undef, "Query Protocol ID Defined");
219 my $params = "feature_id=$query_feature_id&type_id=$query_type_id&nd_protocol_id=$query_protocol_id&";
220 $params .= "reference_genome=$query_reference_genome_name&start=20000000&end=22000000&";
221 $params .= "attribute=score|$query_protocol_id|gt|0.03,Locus|$query_protocol_id|eq|TraesCS1A02G038400";
223 # JSON Format
224 $mech->get("/ajax/sequence_metadata/query?$params&format=JSON");
225 my $json_response = decode_json($mech->content);
226 is_deeply($json_response, {
227     'results' => [
228         {
229             'start' => 21040550,
230             'featureprop_json_id' => 1,
231             'type_id' => $query_type_id,
232             'end' => 21040550,
233             'type_name' => $query_type_name,
234             'nd_protocol_id' => $query_protocol_id,
235             'attributes' => {
236                     'qvalue' => '0.0455793811186712',
237                     'Variable' => 'CO_321:0001138',
238                     'zvalue' => '3.3484904423247',
239                     'ID' => 'Ex_c64327_523',
240                     'Trait' => 'SDS sedimentation',
241                     'Locus' => 'TraesCS1A02G038400',
242                     'Population' => 'TCAP90K_SpringAM_panel x SW-AMPanel_2012_Saskatoon',
243                     'pvalue' => '0.000812530833156448'
244                 },
245             'feature_id' => $query_feature_id,
246             'nd_protocol_name' => $query_protocol_name,
247             'feature_name' => $query_feature_name,
248             'score' => '0.0455793811186712',
249             'links' => {}
250         }
251     ]
252 }, 'Query JSON Response');
255 # GA4GH Format
256 $mech->get("/ajax/sequence_metadata/query?$params&format=GA4GH");
257 my $ga4gh_response = decode_json($mech->content);
258 is_deeply($ga4gh_response, {
259     'features' => [
260         {
261             'end' => 21040550,
262             'feature_set_id' => $query_protocol_id,
263             'reference_name' => $query_feature_name,
264             'parent_id' => $query_feature_id,
265             'start' => 21040550,
266             'attributes' => {
267                 'Population' => [
268                     'TCAP90K_SpringAM_panel x SW-AMPanel_2012_Saskatoon'
269                 ],
270                 'pvalue' => [
271                     '0.000812530833156448'
272                 ],
273                 'zvalue' => [
274                     '3.3484904423247'
275                 ],
276                 'Locus' => [
277                     'TraesCS1A02G038400'
278                 ],
279                 'bb_metadata' => {
280                     'type_name' => [
281                         'GWAS Results'
282                     ],
283                     'type_id' => [
284                         $query_type_id
285                     ],
286                     'nd_protocol_id' => [
287                         $query_protocol_id
288                     ],
289                     'nd_protocol_name' => [
290                         $query_protocol_name
291                     ]
292                 },
293                 'ID' => [
294                     'Ex_c64327_523'
295                 ],
296                 'Variable' => [
297                     'CO_321:0001138'
298                 ],
299                 'Trait' => [
300                     'SDS sedimentation'
301                 ],
302                 'score' => [
303                     '0.0455793811186712'
304                 ],
305                 'qvalue' => [
306                     '0.0455793811186712'
307                 ]
308             },
309             'id' => '1.0'
310         }
311     ]
312 }, "Query GA4GH Response");
314 # GFF Format
315 $mech->get("/ajax/sequence_metadata/query?$params&format=gff");
316 my $gff_response = $mech->content;
317 like(
318     $gff_response,
319     qr/$query_feature_name\t\.\t\.\t21040550\t21040550\t0\.0455793811186712\t\.\t\.\t.*/,
320     "Query GFF Response"
326 ## CLEANUP
329 # Remove the SMD feature props
330 $dbh->do("DELETE FROM public.featureprop_json WHERE nd_protocol_id = ?", undef, $nd_protocol_id);
332 # Remove the features
333 foreach my $feature (@features) {
334     if ( $feature !~ /^#/ ) {
335         $dbh->do("DELETE FROM public.feature WHERE uniquename = ?", undef, $feature);
336     }
339 # Delete test organism
340 $organism_rs->delete();
342 # Remove the SMD nd_protocol
343 $dbh->do("DELETE FROM public.nd_protocolprop WHERE nd_protocol_id = ?", undef, $nd_protocol_id);
344 $dbh->do("DELETE FROM public.nd_protocol WHERE nd_protocol_id = ?", undef, $nd_protocol_id);
349 done_testing();