-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathENV.pm
executable file
·1741 lines (1556 loc) · 56.3 KB
/
ENV.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
package ENV;
require Exporter;
use Carp;
use Bio::Seq;
use Bio::Seq::RichSeq;
use Bio::SeqIO;
use strict;
use vars qw (@ISA @EXPORT);
use DBI;
@ISA = qw(Exporter);
@EXPORT = qw(connect get_sequences load_SeqFeature load_Sequence load_feature_annotations delete_feature_annotations
get_sequence_by_feature_id get_accession_by_seq_id get_feature_ids_by_seq_id get_features_by_feature_id
get_sequence_by_seq_id get_seq_id_by_seq_accession link_seq_to_set get_seq_sets load_sequence_sets load_PrimarySeq
load_sequence_SeqFeature load_sequences load_sequence_accessions load_sequence_annotations get_sequence_by_accession
set_name_to_id get_seq_features_by_set_id get_sequences_by_set_id get_feature_id_by_accession get_mainroles
get_subroles set_id_to_seq_ids seq_id_to_SeqObj add_features_to_SeqObj get_evidence_for_feature
get_ev_role_by_feature_id get_accession_by_feature_id get_features_by_seq_id load_seq_feat_mappings
set_id_to_set_name get_locus_tag_by_feature_id get_set_names seq_id_to_set_name is_current update_product
insert_feature_accessions update_feature_mapping set_name_to_set_id get_feature_id_by_coords max_locus_tag
seq_id_to_set_id delete_feature_evidence protein_from_coords set_ids delete_sequence_features);
sub connect {
my $argref = shift;
my $dbh;
my ($user, $db, $password);
if (defined $argref->{'u'} && $argref->{'u'}) { $user = $argref->{'u'} }
elsif (defined $argref->{'U'} && $argref->{'U'}) { $user = $argref->{'U'} }
elsif (defined $argref->{'user'} && $argref->{'user'}) { $user = $argref->{'user'} }
else { $user = $ENV{'USER'}; }
if (defined $argref->{'p'}) { $password = $argref->{'p'} }
elsif (defined $argref->{'P'}) { $password = $argref->{'P'} }
elsif (defined $argref->{'pswd'}) { $password = $argref->{'pswd'} }
else {
warn "No password provided. Trying read-only anonymous access.\n";
$user = 'access';
$password = 'mySQL@cce55';
}
if (defined $argref->{'D'}) { $db = $argref->{'D'} }
my $host = $ENV{DBSERVER} ? $ENV{DBSERVER} : 'localhost';
$dbh = DBI->connect("dbi:mysql:host=$host;database=$db", $user, $password);
if (! $dbh) { die "Couldn't connect with $host / $db / $user / $password\n";}
print STDERR "Connected to $host:$db as $user\n";
return $dbh;
}
sub get_set_names {
my $dbh = shift;
my $set_q = "select set_id, name from sequence_sets where is_current=1";
return $dbh->selectall_hashref($set_q, 'set_id');
}
sub get_all_sequences {
my $dbh = shift;
my $limit_ref = shift;
my $sequence_q = "SELECT s.seq_id, sequence, seq_accession"
. " FROM sequences s, sequence_accessions a"
. " WHERE s.seq_id = a.seq_id"
. " AND a.seq_acc_description = 'accession_number'";
# my $sth = $dbh->prepare($sequence_q);
# $sth->exectute;
# while ($row = $sth->
my $r = $dbh->selectall_hashref($sequence_q, 'seq_id');
return $r;
}
sub get_sequence_by_accession {
my $dbh = shift;
my $accession = shift;
my $set_id = shift;
my $sequence_q = "SELECT s.seq_id, sequence, seq_accession"
. " FROM sequences s, sequence_accessions a"
. " WHERE s.seq_id = a.seq_id"
. " AND a.seq_accession = \"$accession\"";
if ($set_id) {
$sequence_q = "SELECT s.seq_id, sequence, seq_accession"
. " FROM sequences s, sequence_accessions a, seq_set_link l"
. " WHERE l.set_id = $set_id"
. " AND a.seq_id=l.seq_id"
. " AND a.seq_accession = \"$accession\""
. " AND s.seq_id = a.seq_id";
}
my $r = $dbh->selectall_hashref($sequence_q, 'seq_id');
if (scalar(keys %$r) > 1) { die "ERROR: Sequence accession '$accession' has multiple seq_ids in database. Dying...\n";}
foreach my $sid (keys %$r) {
return Bio::Seq->new(-seq => $r->{$sid}->{'sequence'},
-id => $sid,
-accession => $r->{$sid}->{'seq_accession'});
}
}
sub get_sequence_by_seq_id {
my $dbh = shift;
my @seq_ids = @_;
if (! @seq_ids) { warn "No seq_ids provided to get_sequence_by_seq_id."; return; }
my $data = {};
my $sequence_q = "SELECT s.seq_id, sequence"
. " FROM sequences s"
. " WHERE s.seq_id in (". join (",",@seq_ids) . ")";
my $r = $dbh->selectall_hashref($sequence_q, 'seq_id') or warn "$sequence_q $DBI::errstr";
foreach my $sid (keys %$r) {
$data->{$sid} = Bio::Seq->new(-seq => $r->{$sid}->{'sequence'},
-id => $sid,
-accession => $r->{$sid}->{'seq_accession'});
# grab annotation for header
my $annotation_q = "SELECT sa.data_type, sa.value"
. " FROM sequence_annotations sa" #, env.data_types d"
. " WHERE sa.seq_id=$sid";
# . " AND sa.data_type_id=d.id";
my $annotations = $dbh->selectall_arrayref($annotation_q) or warn "$annotation_q $DBI::errstr";
my $desc;
foreach (@$annotations) {
$desc .= "$_->[0]=$_->[1];";
}
$data->{$sid}->desc($desc);
}
return $data;
}
sub get_sequences_by_set_id {
my $dbh = shift;
my $set_id = shift;
my $retn = {}; # this holds the returned sequence objects
# get sequence
my $sq = "select s.seq_id, sequence, seq_length, gc, category"
. " FROM sequences s, seq_set_link l"
. " WHERE set_id=$set_id"
. " AND s.seq_id=l.seq_id"
. " AND s.iscurrent=1"
;
my $seq_ref = $dbh->selectall_hashref($sq, 'seq_id');
# for each sequence, build a sequence obj
foreach my $seq_id (keys %$seq_ref) {
my $SeqObj = Bio::Seq::RichSeq->new(-seq => $seq_ref->{$seq_id}->{'sequence'},
-molecule => 'DNA',
# -display_name => $rv[1],
# -accession_number => $rv[1],
# -id => $rv[1],
# -primary_id => $rv[1],
-alphabet => 'dna',
);
$retn->{$seq_id} = $SeqObj;
}
# get sequence annotations
my $nq = "select a.seq_id, data_type, value"
. " FROM sequence_annotations a, seq_set_link l"
. " WHERE set_id=$set_id"
. " AND a.seq_id=l.seq_id";
my $nsth = $dbh->prepare($nq);
$nsth->execute();
while (my @rv = $nsth->fetchrow_array) {
push @{$seq_ref->{$rv[0]}->{'annotation'}->{$rv[1]}}, $rv[2] if (defined ($seq_ref->{$rv[0]}));
}
# get sequence accessions
my $xq = "select a.seq_id, seq_acc_source, seq_accession"
. " FROM sequence_accessions a, seq_set_link l"
. " WHERE set_id=$set_id"
. " AND a.seq_id=l.seq_id";
my $xsth = $dbh->prepare($xq);
$xsth->execute();
while (my @rv = $xsth->fetchrow_array) {
# my $acc = $rv[1] ? "$rv[1]|$rv[2]" : $rv[2];
my $acc = $rv[2];
push @{$seq_ref->{$rv[0]}->{'accessions'}}, $acc if (defined ($seq_ref->{$rv[0]}));
}
return $seq_ref;
}
sub get_seq_id_by_seq_accession {
my $dbh = shift;
my $accession = shift;
my $seq_id_q = "SELECT distinct x.seq_id FROM sequence_accessions x, sequences s"
. " WHERE seq_accession = \"$accession\""
. " AND s.seq_id=x.seq_id"
. " AND s.iscurrent=1";
my $r = $dbh->selectall_arrayref($seq_id_q);
return $r->[0]->[0];
}
sub get_accession_by_seq_id {
my $dbh = shift;
my $seq_id = shift;
my $seq_id_q = "SELECT seq_accession FROM sequence_accessions"
. " WHERE seq_id = \"$seq_id\"";
my $r = $dbh->selectcol_arrayref($seq_id_q);
return $r;
}
sub get_accession_by_feature_id {
my $dbh = shift;
my $f_id = shift;
my $src = shift;
my $f_id_q = "SELECT accession FROM feature_accessions"
. " WHERE feature_id = \"$f_id\"";
if ($src) {
$f_id_q .= " AND source=\"$src\"";
my $r = $dbh->selectcol_arrayref($f_id_q);
return $r;
}
}
sub get_locus_tag_by_feature_id {
my $dbh = shift;
my $f_id = shift;
my $f_id_q = "SELECT accession FROM feature_accessions"
. " WHERE feature_id = \"$f_id\""
. " AND source = \"locus_tag\"";
my $r = $dbh->selectcol_arrayref($f_id_q);
return $r;
}
sub get_feature_id_by_accession {
my $dbh = shift;
my $accession = shift;
if ($accession =~ /^\s*$/) {
warn "Empty accession requested ($accession)\n";
return;
}
my $f_acc_q = "SELECT distinct feature_id FROM feature_accessions"
. " WHERE accession = ?";
my $sth1 = $dbh->prepare($f_acc_q);
my $f_id_q = "SELECT feature_id FROM sequence_features WHERE feature_id=?";
my $sth2 = $dbh->prepare($f_id_q);
my @a = split(/\|/, $accession);
foreach my $acc (@a) {
$sth1->execute($acc);
my $r = $sth1->fetchall_arrayref();
if (!@$r) {
# if there is no feature_id associated with the acc and
# the accession is numerical, is it a feature_id?
if ($acc =~ /^\d+$/) {
$sth2->execute($acc);
$r = $sth2->fetchall_arrayref();
}
}
if (@$r > 0) {
warn "$accession yielded more than one feature_id: " . join(" ", @$r)
. ". Returning the first.\n" if (@$r > 1);
return $r->[0]->[0];
}
}
}
sub load_SeqFeature {
my $dbh = shift;
my $seq_id = shift;
my $feato = shift; # Bio::SeqFeature::Generic object
my $seqobj = shift; # as above, except for the parent DNA sequence
my $SO_term = shift;
my $source = shift;
my $prefix = shift;
if (!defined $seqobj) {
my $seqstruct = get_sequence_by_seq_id($dbh, $seq_id);
$seqobj = Bio::Seq->new(-seq => $seqstruct->{$seq_id}->{sequence},
-alphabet => 'dna');
}
# could check for split location with
# if ( $feato->location->isa('Bio::Location::SplitLocationI')
# see BioPerl or CPAN for more info
# start just gives the start. If we want to see the extendable, use to_FTstring or start_pos_type
my $start = $feato->start;
my $min_partial = $feato->location->start_pos_type eq "BEFORE" ||
$feato->location->min_start != $feato->location->max_start ? 1 : 0;
my $end = $feato->end;
my $max_partial = $feato->location->end_pos_type eq "AFTER" ||
$feato->location->min_end != $feato->location->max_end ? 1 : 0;
my $pseudo = $feato->has_tag('pseudo') ? 5 : 0;
# since peseudogenes don't have a CDS entry...
my $feat_type = $feato->primary_tag eq "gene" && $pseudo > 0 ? "CDS" : $feato->primary_tag;
$feat_type = $feat_type eq "repeat" ? "repeat_region" : $feat_type;
if ($feat_type eq "rna") {
$feat_type = "misc_RNA";
# look in the product field for an INSDC feat_type
my @prod = $feato->get_tag_values("product");
foreach my $p (@prod) {
if ($p =~ /((r|t|tm|m|nc|precursor_)RNA)/) { $feat_type = $1; last;}
}
}
my $prot = "";
my $phase = 0;
my @all_tags = $feato->get_all_tags;
if ($feato->primary_tag eq "CDS") {
# if ($feat_type_id == 1) { # if this is a translated gene...
if (grep /\btranslation\b/, @all_tags) {
($prot) = $feato->get_tag_values("translation");
} else {
my $protobj = protein_from_coords($seqobj, $feato);
$prot = $protobj->seq;
}
if (defined $feato->frame) {
$phase = $feato->frame;
} else {
my @codon_start = $feato->annotation->get_Annotations('codon_start');
if (defined $codon_start[0]) {
$phase = $codon_start[0]->value - 1;
}
}
}
$prot =~ s/\*$//;
my $sequence_features_i = "INSERT sequence_features"
. " (feat_type_id, feat_type, product, inserted_by, date_inserted)"
. " SELECT id, \"$feat_type\", \"$prot\", USER(), NOW()"
. " FROM INSDC.feature_key"
. " WHERE feature_key = \"$feat_type\"";
# print $sequence_features_i, "\n";
my $row1 = $dbh->do($sequence_features_i);
if (! defined $row1) {
warn "\n!!!Couldn't execute '$sequence_features_i': $DBI::errstr\n";
return;
}
my $strand = $feato->strand;
my $translation_coords = $feato->location->to_FTstring;
my $feat_id = $dbh->last_insert_id("%", "%", "", "");
if ($feat_id == 0) { die "Got a feature_id of 0 for $seq_id $feat_type $start/$end\n"; }
my $seq_feat_mappings_i = sprintf "INSERT seq_feat_mappings"
. " (seq_id, feature_id, feat_min, feat_max, strand,"
. " phase, min_partial, max_partial, pseudo, translation_coords)"
. " VALUES ($seq_id, $feat_id, $start, $end, '$strand',"
. " '$phase', $min_partial, $max_partial, $pseudo, '$translation_coords')";
# print $seq_feat_mappings_i, "\n";
my $row2 = $dbh->do($seq_feat_mappings_i);
if (! defined $row2) {
warn "\n!!!Couldn't execute '$seq_feat_mappings_i'\n";
return;
}
# insert an accession. Use the locus_tag, if present
my $accession = $feato->primary_id;
if ($feato->has_tag('locus_tag')) {
$accession = [$feato->get_tag_values('locus_tag')]->[0];
}
elsif ($feato->display_name) {
$accession = $feato->display_name;
}
my $feat_acc_i = "INSERT feature_accessions"
. " (feature_id, source, prefix, accession)"
. " VALUES ($feat_id, \"$source\", \"$prefix\", \"$accession\")" ;
my $row3 = $dbh->do($feat_acc_i);
# insert annotation
if (grep /^product$/, @all_tags) {
my $feat_ann_i = "INSERT feature_annotations"
. " (feature_id, data_type_id, value, source, ann_rank)"
. " VALUES ($feat_id, 66, \"" . [$feato->get_tag_values('product')]->[0] . "\", \"$source\", 10)";
my $row4 = $dbh->do($feat_ann_i);
if (! defined $row4) { print STDERR "$feat_ann_i\n" }
}
if (grep /^EC$/, @all_tags) {
foreach my $ec (@{$feato->get_tag_values('EC')}) {
my $feat_ann_i = "INSERT feature_annotations"
. " (feature_id, data_type_id, value, source, ann_rank)"
. " VALUES ($feat_id, 1, \"$ec\", \"$source\", 10)";
my $row4 = $dbh->do($feat_ann_i);
if (! defined $row4) { print STDERR "$feat_ann_i\n" }
}
}
if (grep /^sso$/i, @all_tags) { # KBase SEED sequence ortholog
my $feat_ann_i = "INSERT feature_annotations"
. " (feature_id, data_type_id, value, source, ann_rank)"
. " VALUES ($feat_id, 23, \"" . [$feato->get_tag_values('sso')]->[0] . "\", \"$source\", 10)";
my $row4 = $dbh->do($feat_ann_i);
if (! defined $row4) { print STDERR "$feat_ann_i\n" }
}
return $feat_id;
}
sub protein_from_coords {
my ($seqobj, $featobj) = @_;
if (ref $seqobj eq "SCALAR") {
# we've been passed a seq_id, so get the seqobj
# $seqobj = get_sequence_by_seq_id($dbh, $seqobj);
}
# Cut gene sequence from contig
my $featseqobj = $seqobj->trunc($featobj->location);
# Translate sequences to aa if applicable
my $complete = $featobj->location->start_pos_type ne "EXACT" ||
$featobj->location->end_pos_type ne "EXACT" ? 0 : 1;
my $table = 11;
if ($featobj->has_tag("transl_table")) {
($table) = $featobj->get_tag_values("transl_table");
}
my $protobj = $featseqobj->translate(-complete => $complete,
#-frame => $phase,
-codontable_id => $table);
return ($protobj);
}
sub delete_sequence_features {
my $dbh = shift;
my @feat_id = @_;
my $ortho_d = "DELETE FROM ortho_group WHERE feature_id IN (" . join(",", @feat_id) . ")";
my $proteomics_d = "DELETE FROM proteomics_data WHERE feature_id IN (" . join(",", @feat_id) . ")";
my $feature_accessions_d = "DELETE FROM feature_accessions"
. " WHERE feature_id IN (" . join(",", @feat_id) . ")";
my $feature_annotations_d = "DELETE FROM feature_accessions"
. " WHERE feature_id IN (" . join(",", @feat_id) . ")";
my $feature_evidence_d = "DELETE FROM feature_evidence"
. " WHERE feature_id IN (" . join(",", @feat_id) . ")";
my $seq_feat_mapping_d = "DELETE FROM seq_feat_mappings m WHERE feature_id IN (" . join(",", @feat_id) . ")";
my $feature_d = "DELETE FROM sequence_features WHERE feature_id IN (" . join(",", @feat_id) . ")";
my $transcriptomics_d = "DELETE FROM transcriptomics_data WHERE feature_id IN (" . join(",", @feat_id) . ")";
for my $q ($ortho_d, $transcriptomics_d, $proteomics_d, $feature_annotations_d,
$feature_accessions_d, $feature_evidence_d, $seq_feat_mapping_d, $feature_d) {
my $d = $dbh->do($q);
if (! defined $d) {
warn "Couldn't execute '$q'";
}
}
}
sub delete_feature_accessions {
my $dbh = shift;
my $feat_id = shift;
my $cond_ref = shift;
my $feature_accessions_d = "DELETE FROM feature_accessions"
. " WHERE feature_id=$feat_id";
if (%$cond_ref) {
foreach my $cond(keys %$cond_ref) {
$feature_accessions_d .= " AND $cond = \'$cond_ref->{$cond}\'";
}
}
my $d = $dbh->do($feature_accessions_d);
if (! defined $d) {
warn "Couldn't execute '$feature_accessions_d'";
return;
}
}
sub delete_feature_annotations {
my $dbh = shift;
my $feat_id = shift;
my $cond_ref = shift;
my $feature_annotations_d = "DELETE FROM feature_annotations"
. " WHERE feature_id=$feat_id";
if (%$cond_ref) {
foreach my $cond(keys %$cond_ref) {
$feature_annotations_d .= " AND $cond = \'$cond_ref->{$cond}\'";
}
}
my $d = $dbh->do($feature_annotations_d);
if (! defined $d) {
warn "Couldn't execute '$feature_annotations_d'";
return;
}
}
sub delete_feature_evidence {
my $dbh = shift;
my $feat_id = shift;
my $cond_ref = shift;
my $feature_evidence_d = "DELETE FROM feature_evidence"
. " WHERE feature_id=$feat_id";
if (%$cond_ref) {
foreach my $cond(keys %$cond_ref) {
$feature_evidence_d .= " AND $cond = \'$cond_ref->{$cond}\'";
}
}
my $d = $dbh->do($feature_evidence_d);
if (! defined $d) {
warn "Couldn't execute '$feature_evidence_d'";
return;
}
}
sub load_feature_annotations {
my $dbh = shift;
my $feat_id = shift;
my $featObj = shift;
my $annObj = $featObj->annotation;
my $source = shift;
my $rank = shift;
my $feature_annotation_i = "INSERT feature_annotations"
. " (feature_id, data_type_id, value, edit_by, date_edit, ann_rank, source)"
. " SELECT $feat_id, d.id, ?, USER(), NOW(), ?, ?"
. " FROM INSDC.qualifier d"
. " WHERE d.qualifier=?";
if ($annObj->get_all_annotation_keys) {
foreach my $data_type ($annObj->get_all_annotation_keys()) {
my @values = $annObj->get_Annotations($data_type);
for (my $i = 0; $i < @values; $i++) {
if ($values[$i]->value eq "") { next }
if ($data_type eq "db_xref") {
my ($type, $value) = split/\:/, $values[$i]->value;
$dbh->do($feature_annotation_i, {}, ($value, 0, $type, $source)) or warn $DBI::errstr;
} else {
if (! $rank) { $rank = 9 }
$dbh->do($feature_annotation_i, {}, ($values[$i]->value, $rank, $source, $data_type)) or warn $DBI::errstr;
}
}
}
} else {
my @all_tags = $featObj->get_all_tags;
foreach my $tag(@all_tags) {
if ($tag =~ /^product$/) {
my $feat_ann_i = "INSERT feature_annotations"
. " (feature_id, data_type_id, value, source, ann_rank)"
. " VALUES ($feat_id, 66, \"" . [$featObj->get_tag_values('product')]->[0] . "\", \"$source\", 10)";
my $row4 = $dbh->do($feat_ann_i);
if (! defined $row4) { print STDERR "$feat_ann_i\n" }
}
elsif ($tag =~ /^gene$/) {
my $gene = [$featObj->get_tag_values('gene')]->[0];
my $feat_ann_i = "INSERT feature_annotations"
. " (feature_id, data_type_id, value, source, ann_rank)"
. " VALUES ($feat_id, 35, \"$gene\", \"$source\", 10)";
my $row4 = $dbh->do($feat_ann_i);
if (! defined $row4) { print STDERR "$feat_ann_i\n" }
}
elsif ($tag =~ /^(EC(_number)?)$/) {
my $t = $1;
foreach my $ec ($featObj->get_tag_values($t)) {
my $feat_ann_i = "INSERT feature_annotations"
. " (feature_id, data_type_id, value, source, ann_rank)"
. " VALUES ($feat_id, 1, \"$ec\", \"$source\", 10)";
my $row4 = $dbh->do($feat_ann_i);
if (! defined $row4) { print STDERR "$feat_ann_i\n" }
}
}
elsif ($tag =~ /^sso$/i) { # KBase SEED sequence ortholog
my $feat_ann_i = "INSERT feature_annotations"
. " (feature_id, data_type_id, value, source, ann_rank)"
. " VALUES ($feat_id, 23, \"" . [$featObj->get_tag_values('sso')]->[0] . "\", \"$source\", 10)";
my $row4 = $dbh->do($feat_ann_i);
if (! defined $row4) { print STDERR "$feat_ann_i\n" }
}
}
}
}
sub load_PrimarySeq {
my $dbh = shift;
my $seqo = shift;
my $fail = 0;
# error checking
if (! defined $dbh ) { warn "No dbh provided"; return }
if (! defined $seqo) { warn "No seqo provided"; return }
if (ref $seqo !~ /HASH/) { warn "seqo is not a hash reference! ($seqo)"; return }
my $seq = $seqo->seq;
if (! $seq) { warn "No sequence in object"; return }
my $seq_id = &load_sequences($dbh, $seq);
my $acc = $seqo->primary_id ? $seqo->primary_id :
$seqo->display_name ? $seqo->display_name : undef;
if (! defined $acc) { warn "No accession found in object"; return }
print STDERR "$acc loaded as seq_id $seq_id\n";
my ($prefix, $source);
my $acc_ref = {};
my @acc = split/\|/, $acc;
foreach my $a(@acc) {
$source = "undetermined";
if ($a eq "quiver") { next }
if ($a =~ /^\d{13}$/) { $source = "uid" }
elsif ($a =~ /^[A-Z]{4}\w\d{2}T[FR]/) { $source = "seq_name" }
$acc_ref->{$source} = $acc;
}
my $row2 = &load_sequence_accessions($dbh, $seq_id, {$source => $acc});
if ($row2 > 0) {
$fail = 1;
}
my $desc = $seqo->desc;
my %DESC; # holds key/value pairs inside brackets
# NCBI: identify organism and remove from description
if ($desc =~ /(\{\s*(.+);.*\}\s*)/) {
$DESC{'organism'} = $2;
$desc =~ s/\Q$1\E//;
}
# identify other key/value pairs and remove from description
# Look for Genbank format:
if ($desc =~ /\[\w+=[^\]]+\]/) {
while ($desc =~ /(\[(\w+)=([^\]]+)\])/g) {
$DESC{$2}=$3;
$desc =~ s/\Q$1\E\s*//;
}
}
# look for JTC format
elsif ($desc =~ /\/\w+=\S+/) {
while ($desc =~ /(\/(\w+)=(\S+))/) {
$DESC{$2} = $3;
$desc =~ s/\Q$1\E\s*//;
}
}
# what's left is description
$DESC{'description'} = $desc if ($desc =~ /\S+/);
my $row4 = &load_sequence_annotations($dbh, $seq_id, \%DESC);
if ($row4 > 0) {
$fail = 1;
}
if ($fail) {
warn "Rolling back insertion due to failure.";
$dbh->rollback;
return;
} else {
# $dbh->commit;
return ($seq_id);
}
}
sub load_sequence_SeqFeature {
my $dbh = shift;
my $seqo = shift; # Bio::SeqFeatureI object
my $fail = 0;
# error checking
if (! defined $dbh ) { warn "No dbh provided"; return }
if (! defined $seqo) { warn "No seqo provided"; return }
if (ref $seqo !~ /HASH/) { warn "seqo is not a hash reference! ($seqo)"; return }
my $seq;
if (ref $seqo eq "Bio::Seq::RichSeq") {
$seq = $seqo->seq;
} elsif (ref $seqo eq "Bio::SeqFeatureI") {
$seq = $seqo->seq->seq;
}
if (! $seq) { warn "No sequence in object"; return }
my $seq_id = &load_sequences($dbh, $seq);
my $acc = $seqo->display_name? $seqo->display_name :
$seqo->seq_id ? $seqo->seq_id :
$seqo->primary_id ? $seqo->primary_id : undef
;
if (! defined $acc) { warn "No accession found in object"; return }
# we should always have a primary_id
my $source = "undetermined";
if ($acc =~ /^\d{13}$/) {
$source = "uid";
}
elsif ($acc =~ /^[A-Z]{4}\w\d{2}T[FR]/) { $source = "seq_name" }
# elsif (defined $seqo->source_tag) { $source = $seqo->source_tag }
my $row2 = &load_sequence_accessions($dbh, $seq_id, {$source => $acc});
if ($row2 > 0) {
$fail = 1;
}
my $desc = "";
if (ref $seqo eq "Bio::Seq::RichSeq") {
$desc = $seqo->description;
} elsif (ref $seqo eq "Bio::SeqFeatureI") {
$desc = $seqo->seq->description;
}
my %DESC; # holds key/value pairs inside brackets
# NCBI: identify organism and remove from description
if ($desc =~ /(\{\s*(.+);.*\}\s*)/) {
$DESC{'organism'} = $2;
$desc =~ s/\Q$1\E//;
}
# identify other key/value pairs and remove from description
# Look for Genbank format:
if ($desc =~ /\[\w+=[^\]]+\]/) {
while ($desc =~ /(\[(\w+)=([^\]]+)\])/g) {
$DESC{$2}=$3;
$desc =~ s/\Q$1\E\s*//;
}
}
# look for JTC format
elsif ($desc =~ /\/\w+=\S+/) {
while ($desc =~ /(\/(\w+)=(\S+))/) {
$DESC{$2} = $3;
$desc =~ s/\Q$1\E\s*//;
}
}
# what's left is description
$DESC{'description'} = $desc if ($desc =~ /\S+/);
my $row4 = &load_sequence_annotations($dbh, $seq_id, \%DESC);
if ($row4 > 0) {
$fail = 1;
}
if ($fail) {
warn "Rolling back insertion due to failure.";
$dbh->rollback;
return;
} else {
# $dbh->commit;
return ($seq_id);
}
}
sub load_sequences {
my $dbh = shift;
my $seq = shift;
my $len = length($seq);
my $gcseq = $seq;
$gcseq =~ s/[^gcsGCS]//g;
my $gc = sprintf "%.2f", (length($gcseq)/$len);
my $sequences_i = "INSERT sequences"
. " (sequence, date_inserted, inserted_by, seq_length, gc, iscurrent)"
. " VALUES (\"$seq\", NOW(), USER(), $len, $gc, 1)";
my $row1 = $dbh->do($sequences_i);
if (! defined $row1) {
warn "Couldn't execute '$sequences_i'";
return;
}
my $seq_id = $dbh->{'mysql_insertid'}
or die "no insert id?";
return $seq_id;
}
sub get_sequence_by_feature_id {
my $dbh = shift;
my @feature_ids = @_;
# make a temporary table of all the seq_feat mappings
my $temp_table_q = "CREATE TEMPORARY TABLE tmp_feat_maps"
. " LIKE seq_feat_mappings";
$dbh->do($temp_table_q) || warn "Couldn't create temporary table :: $DBI::errstr";
# populate it with all the relevant data
my $temp_table_i = "INSERT INTO tmp_feat_maps"
. " SELECT * from seq_feat_mappings WHERE feature_id IN (" . join (",", @feature_ids) . ")";
$dbh->do($temp_table_i) || warn "Population of temporary table failed : $DBI::errstr";
# now grab all the sequences
my $subseq_q = "SELECT t.*, substring(s.sequence, t.feat_min, (t.feat_max - t.feat_min + 1)) AS gene_seq"
. " FROM tmp_feat_maps t, sequences s"
. " WHERE s.seq_id = t.seq_id";
my $sequences = $dbh->selectall_hashref($subseq_q, "feature_id");
# if the strand is -1, we need to reverse complement
while (my ($id, $ref) = each %$sequences) {
if ($ref->{'strand'} == -1) {
$ref->{'gene_seq'} = revcom(uc($ref->{'gene_seq'}))->seq;
}
}
# grab annotation for header
my $annotation_q = "SELECT fa.feature_id, q.qualifier, fa.value"
. " FROM feature_annotations fa, tmp_feat_maps t, INSDC.qualifier q"
. " WHERE fa.feature_id=t.feature_id"
. " AND q.id=fa.data_type_id";
my $annotations = $dbh->selectall_arrayref($annotation_q);
foreach (@$annotations) {
$sequences->{$_->[0]}->{'annotation'}->{$_->[1]} = $_->[2];
}
return $sequences;
}
sub get_features_by_seq_id {
my $dbh = shift;
my $seq_id = shift;
my $feat_id_ref = &get_feature_ids_by_seq_id($dbh, $seq_id);
return (&get_features_by_feature_id($dbh, @$feat_id_ref));
}
sub get_features_by_feature_id {
my $dbh = shift;
my @feature_ids = @_;
if (!@feature_ids) { warn "Hey! No feature_ids provided to ENV::get_features_by_feature_id!!\n"; return;}
# grab all the sequences
my $prot_q = "SELECT feature_id, feat_type, product"
. " FROM sequence_features"
. " WHERE feature_id in (" . join(",",@feature_ids) . ")"
;
my $sequences = $dbh->selectall_hashref($prot_q, "feature_id");
my $mq = "select m.feature_id, l.set_id, m.seq_id, feat_min, feat_max, strand,"
. " phase, min_partial, max_partial, pseudo, translation_coords"
. " FROM seq_feat_mappings m"
. " INNER JOIN sequences s ON s.seq_id=m.seq_id"
. " INNER JOIN seq_set_link l ON m.seq_id=l.seq_id"
. " WHERE m.feature_id in (" . join(",", @feature_ids) . ")"
. " AND s.iscurrent=1";
my $msth = $dbh->prepare($mq);
my $r1 = $msth->execute;
if (! defined $r1) { die "Died on \n$mq\n" }
while (my $rv = $msth->fetchrow_hashref()) {
# Allow lookup of seq_id by feature_id and set_id:
$sequences->{$rv->{feature_id}}->{'seq_id'}->{$rv->{set_id}} = $rv->{seq_id};
$sequences->{$rv->{feature_id}}->{'location'}->{$rv->{seq_id}} = $rv;
}
# grab annotation for header
my $annotations = get_feature_annotations_by_feature_id($dbh, @feature_ids);
#!!!! Right now this annotation structure is different than that returned by
#!!!! get_seq_features_by_set_id.
foreach my $a(@$annotations) {
push @{$sequences->{$a->[0]}->{'annotation'}->{$a->[3]}->{$a->[4]}->{$a->[1]}}, $a->[2];
}
# grab the accessions
my $accessions_q = "SELECT feature_id, source, prefix, accession"
. " FROM feature_accessions"
. " WHERE feature_id in (" . join (",",@feature_ids) . ")";
my $accessions = $dbh->selectall_arrayref($accessions_q);
foreach my $a(@$accessions) {
my $acc = $a->[2] ? "$a->[2]|$a->[3]" : $a->[3];
push @{$sequences->{$a->[0]}->{'accessions'}->{$a->[1]}}, $acc;
}
# grab the set
my $set_q = "SELECT m.feature_id, s.set_id, s.name"
. " FROM sequence_sets s, seq_set_link l, seq_feat_mappings m"
. " WHERE m.feature_id in (" . join(",",@feature_ids) . ")"
. " AND l.seq_id=m.seq_id"
. " AND l.set_id=s.set_id"
. " AND s.is_current=1";
my $sets = $dbh->selectall_arrayref($set_q);
foreach my $s(@$sets) {
$sequences->{$s->[0]}->{'set'}->{'id'} = $s->[1];
$sequences->{$s->[0]}->{'set'}->{'name'} = $s->[2];
}
return $sequences;
}
sub get_feature_annotations_by_feature_id {
my $dbh = shift;
my @feature_ids = @_;
if (!@feature_ids) { warn "Hey! No feature_ids provided to ENV::get_features_by_feature_id!!\n"; return;}
# grab annotation for header
my $annotation_q = "SELECT fa.feature_id, d.qualifier, fa.value, fa.ann_rank, fa.source"
. " FROM feature_annotations fa, INSDC.qualifier d"
. " WHERE feature_id in (" . join(",",@feature_ids) . ")"
. " AND d.id=fa.data_type_id"
. " ORDER BY ann_rank";
my $annotations = $dbh->selectall_arrayref($annotation_q);
return $annotations;
}
sub get_ev_role_by_feature_id {
my $dbh = shift;
my $fid = shift;
my $q = "SELECT distinct subrole, LEFT(subrole,2) AS mainrole FROM egad.ev_role_link erl, feature_evidence fe"
. " WHERE fe.feature_id=$fid"
. " AND erl.ev_acc=fe.ev_accession";
my $r = $dbh->selectall_arrayref($q);
my %R = ('mainrole' => [],
'subrole' => []);
foreach my $row (@$r) {
push @{$R{'subrole'}}, $row->[0];
push @{$R{'mainrole'}}, $row->[1];
}
return \%R;
}
sub get_feature_ids_by_seq_id {
my $dbh = shift;
my $seq_id = shift;
my $feat_id_q = "SELECT m.feature_id"
. " FROM seq_feat_mappings m, sequence_features f"
. " WHERE seq_id = $seq_id"
. " AND f.feature_id=m.feature_id AND is_current=1";
return $dbh->selectcol_arrayref($feat_id_q);
}
sub get_feature_id_by_coords {
my $dbh = shift;
my $seq_id = shift;
my $feat_type = shift;
my ($strand, $start, $end) = @_;
my @feat_ids;
# my $feat_id_q = "SELECT distinct feature_id"
# . " FROM seq_feat_mappings"
# . " WHERE seq_id = $seq_id"
# . " AND feat_min=? AND feat_max = ? ";
my $feat_id_q = "SELECT distinct sfm.feature_id FROM seq_feat_mappings sfm, sequence_features f"
. " WHERE seq_id = $seq_id"
. " AND strand = ?"
. " AND (sfm.feat_min = ? OR sfm.feat_max = ?)"
. " AND f.feature_id=sfm.feature_id and f.feat_type = \"$feat_type\"";
my ($min, $max) = sort {$a <=> $b } ($start, $end);
my $ids = $dbh->selectcol_arrayref($feat_id_q, {}, ($strand, $min, $max));
if (@$ids > 1) { print STDERR "WARN: $seq_id:$start/$end returned multiple feature_ids: @$ids. Using the first.\n"; }
return @$ids[0];
}
# Populates seq_set_link to link seq_ids to a set_id
sub link_seq_to_set {
my $dbh = shift;
my $set_id = shift;
my @seq_id = @_;
foreach my $seq_id(@seq_id) {
my $seq_set_link_i = "INSERT seq_set_link (seq_id, set_id)"
. " VALUES ($seq_id, $set_id)";
my $row = $dbh->do($seq_set_link_i);
if (! defined $row) {
warn "Couldn't execute '$seq_set_link_i': $dbh->errstr";
} else {
print STDERR "seq_id $seq_id was linked to set_id $set_id\n";
}
}
}
# Loads the sequence_sets table
sub load_sequence_sets {
my $dbh = shift;
my $name = shift;
my $desc = shift;
my $ncbi_proj_id =shift;
my $sequence_sets_i = "INSERT sequence_sets (name, description, ncbi_proj_id, is_current)"
. " VALUES (\"$name\", \"$desc\", \"$ncbi_proj_id\", 1)";
my $row = $dbh->do($sequence_sets_i);
if (! defined $row) {
warn "Couldn't execute '$sequence_sets_i'";
}
my ($set_id) = $dbh->selectrow_array("select last_insert_id()") or die "no insert id?";
return $set_id;
}
sub get_seq_sets {
my $dbh = shift;
my $set_q = "SELECT set_id, name, description"
. " FROM sequence_sets WHERE is_current=1";
my $r = $dbh->selectall_hashref($set_q, 'set_id');
return $r;
}
sub load_sequence_accessions {
my $dbh = shift;
my $seq_id = shift;
my $acc_ref = shift;
my $fails = 0;
while (my ($source, $acc) = each %$acc_ref) {
my $sequence_accessions_i = "INSERT sequence_accessions"
. " (seq_id, seq_accession, seq_acc_source, seq_acc_description)"
. " VALUES ($seq_id, \"$acc\", \"$source\", 'accession')";
my $r = $dbh->do($sequence_accessions_i);
if (! defined $r) {
$fails += 1;
warn "Couldn't execute $sequence_accessions_i: $DBI::errstr";
}
}
return $fails;
}
sub load_sequence_annotations {
my $dbh = shift;
my $seq_id = shift;
my $annotations_ref = shift;
my $fails = 0;
while (my ($key, $value) = each %$annotations_ref) {
if ($key !~ /.+/ || $value !~ /.+/) { next }
my $sequence_annotations_i = "INSERT sequence_annotations (seq_id, data_type, value)"
. " VALUES ($seq_id, \"$key\", \"$value\")";
# . " SELECT $seq_id, id, \"$value\""
# . " FROM env.data_types"
# . " WHERE name='$key'";