forked from omics-database/TransAtlasDB
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tad-import.pl
executable file
·2150 lines (1984 loc) · 114 KB
/
tad-import.pl
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/env perl
use warnings;
use strict;
use Pod::Usage;
use Getopt::Long;
use File::Spec;
use File::Basename;
use threads;
use Thread::Queue;
use POSIX;
use Cwd qw(abs_path);
use lib dirname(abs_path $0) . '/lib';
use CC::Create;
use CC::Parse;
our $VERSION = '$ Version: 1 $';
our $DATE = '$ Date: 2017-05-05 05:14:00 (Fri, 05 May 2017) $';
our $AUTHOR= '$ Author:Modupe Adetunji <[email protected]> $';
#--------------------------------------------------------------------------------
our ($verbose, $efile, $help, $man, $nosql, $vnosql, $gnosql, $cnosql, $log, $transaction);
our ($metadata, $tab, $excel, $datadb, $gene, $variant, $all, $vep, $annovar, $delete); #command options
our ($file2consider,$connect); #connection and file details
my ($sth,$dbh,$schema); #connect to database;
our ($sheetid, %NAME, %ORGANIZATION);
#data2db options
our ($found);
our (@allgeninfo);
our ($str, $ann, $ref, $seq,$allstart, $allend) = (0,0,0,0,0,0); #for log file
our ($refgenome, $refgenomename, $stranded, $sequences, $annotationfile, $mparameters, $gparameters, $cparameters, $vparameters); #for annotation file
my $additional;
#genes import
our ($bamfile, $fastqcfolder, $alignfile, $staralignfile, $version, $readcountfile, $starcountfile, $genesfile, $deletionsfile, $insertionsfile, $transcriptsgtf, $junctionsfile, $variantfile, $vepfile, $annofile);
our ($kallistofile, $kallistologfile, $salmonfile, $salmonlogfile);
our ($totalreads, $mapped, $alignrate, $deletions, $insertions, $junctions, $genes, $mappingtool, $annversion, $diffexpress, $counttool);
my (%ARFPKM,%CHFPKM, %BEFPKM, %CFPKM, %DFPKM, %TPM, %cfpkm, %dfpkm, %tpm, %DHFPKM, %DLFPKM, %dhfpkm, %dlfpkm, %ALL);
my (%HASHDBVARIANT, @VAR, @threads, $queue);
#variant import
our ( %VCFhash, %DBSNP, %extra, %VEPhash, %ANNOhash );
our ($varianttool, $verd, $variantclass, $vcount);
our ($itsnp,$itindel,$itvariants) = (0,0,0);
#nosql append
my (@nosqlrow, $showcase);
#date
my $date = `date +%Y-%m-%d`;
#--------------------------------------------------------------------------------
sub printerr; #declare error routine
our ($ibis, $ardea) = fastbit_name(); #ibis and ardea location
our $default = DEFAULTS(); #default error contact
processArguments(); #Process input
my %all_details = %{connection($connect, $default)}; #get connection details
if (length($ibis) < 1){ ($ibis, $ardea) = ($all_details{'FastBit-ibis'}, $all_details{'FastBit-ardea'}); } #alternative for ibis and ardea location
#PROCESSING METADATA
if ($metadata){
$dbh = mysql($all_details{'MySQL-databasename'}, $all_details{'MySQL-username'}, $all_details{'MySQL-password'}); #connect to mysql
if ($tab) { #unix tab delimited file
printerr "JOB:\t Importing Sample Information from tab-delimited file => $file2consider\n"; #status
my %filecontent = %{ tabcontent($file2consider) }; #get content from tab-delimited file
foreach my $row (sort keys %filecontent){
undef %NAME; undef %ORGANIZATION;
if (exists $filecontent{$row}{'sample name'}) { #sample name
$filecontent{$row}{'organism part'} = lc($filecontent{$row}{'organism part'});
my $sheetid = "$filecontent{$row}{'first name'} $filecontent{$row}{'middle initial'} $filecontent{$row}{'last name'}"; #scientist name
if (length $sheetid > 3) { #Person Name
$sth = $dbh->prepare("select personid from Person where personid = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if person is not in the database
$sth = $dbh->prepare("insert into Person (personid, firstname, lastname, middleinitial) values (?,?,?,?)");
$sth->execute($sheetid, $filecontent{$row}{'first name'}, $filecontent{$row}{'last name'}, $filecontent{$row}{'middle initial'}) or die "\nERROR:\t Complication in Person table\n";
}
$NAME{$sheetid} = $sheetid;
} else {
undef $sheetid;
}
$sheetid = $filecontent{$row}{'organization'}; #organization name
if ($sheetid) { #Organization Name
$sth = $dbh->prepare("select organizationname from Organization where organizationname = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if person is not in the database
$sth = $dbh->prepare("insert into Organization (organizationname) values ('$sheetid')");
$sth->execute() or die "\nERROR:\t Complication in Organization table\n";
}
$ORGANIZATION{$sheetid} = $sheetid;
} else {
undef $sheetid;
}
if (exists $filecontent{$row}{'organism'}) { #organism name
$sheetid = $filecontent{$row}{'organism'};
$sth = $dbh->prepare("select organism from Organism where organism = '$sheetid'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if is not in the database
$sth = $dbh->prepare("insert into Organism (organism) values ('$sheetid')");
$sth->execute() or die "\nERROR:\t Complication in Organism table\n";
}
undef $sheetid;
} else {
die "\nFAILED:\t Error in tab-delimited file \"$file2consider\".\n\tCheck => ROW: $row, COLUMN: \"Organism\"\n";
} #end if for animal info
if (exists $filecontent{$row}{'derived from'}) { #animal id
$sheetid = uc($filecontent{$row}{'derived from'});
$sth = $dbh->prepare("select animalid from Animal where animalid = '$sheetid'"); $sth->execute(); $found =$sth->fetch();
unless ($found) {
printerr "NOTICE:\t Importing $sheetid to Animal table\n";
$sth = $dbh->prepare("insert into Animal (animalid, organism) values (?,?)");
$sth->execute($sheetid, $filecontent{$row}{'organism'} ) or die "\nERROR:\t Complication in Animal table\n";
} else {
$verbose and printerr "Duplicate: AnimalID '$sheetid' already exists in Animal table. Moving on\n";
}
undef $sheetid;
} else {
die "\nFAILED:\t Error in tab-delimited file \"$file2consider\".\n\tCheck => ROW: $row, COLUMN: \"Derived From\"\n";
}
if (exists $filecontent{$row}{'organism part'}) { #organism part / tissue
$sheetid = $filecontent{$row}{'organism part'};
$sth = $dbh->prepare("select tissue from Tissue where tissue = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if is not in the database
$sth = $dbh->prepare("insert into Tissue (tissue) values ('$sheetid')");
$sth->execute() or die "\nERROR:\t Complication in Tissue table\n";
}
undef $sheetid;
}
$sheetid = uc($filecontent{$row}{'sample name'}); #Sample Table
$sth = $dbh->prepare("select sampleid from Sample where sampleid = '$sheetid'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if sample is not in the database
printerr "NOTICE:\t Importing $sheetid to Sample table\n";
$sth = $dbh->prepare("insert into Sample (sampleid, tissue, derivedfrom, description,date) values (?,?,?,?,?)");
$sth->execute($sheetid, $filecontent{$row}{'organism part'}, $filecontent{$row}{'derived from'}, $filecontent{$row}{'sample description'},$date) or die "\nERROR:\t Complication in Sample table\n";
} else {
printerr "Duplicate: SampleID '$sheetid' already exists in Sample table. Moving on.\n";
printerr "Optional: To delete $sheetid ; Execute: tad-import.pl -delete $sheetid\n";
}
foreach (keys %NAME) {
$sth = $dbh->prepare("select sampleid, personid from SamplePerson where sampleid = '$sheetid' and personid = '$_'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if sample-person is not in the database
$sth = $dbh->prepare("insert into SamplePerson (sampleid, personid) values (?,?)");
$sth->execute($sheetid, $_) or die "\nERROR:\t Complication in SamplePerson table\n";
}
}
foreach (keys %ORGANIZATION) {
$sth = $dbh->prepare("select sampleid, organizationname from SampleOrganization where sampleid = '$sheetid' and organizationname = '$_'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if sample-organization is not in the database
$sth = $dbh->prepare("insert into SampleOrganization (sampleid, organizationname) values (?,?)");
$sth->execute($sheetid, $_) or die "\nERROR:\t Complication in SampleOrganization table\n";
}
}
} else {
pod2usage("\nFAILED:\t Error in tab-delimited file \"$file2consider\".\n\tCheck => ROW: $row, COLUMN: \"Sample Name\"");
} #end of if sample name is real
} #end of foreach file content
} #end of tab unix option
else { #import faang excel sheet
my ($sheetid, @excelcontent, %columnpos); #metadata excel
printerr "JOB:\t Importing Sample Information from excel file => $file2consider\n"; #status
@excelcontent = excelcontent($file2consider); #get excel content
for (@excelcontent){s/%%//g;}
foreach (@excelcontent) {
my @array = split "\n";
my @header = split('\?abc\?',lc($array[1]));
if ($array[0] =~ /person/) { #Working with Person Sheet;
undef %columnpos; #undefined column position
if ($#array > 1) {
foreach my $no (0..$#header) {
$columnpos{$header[$no]} = $no;
}#end foreach : put header info into a Dictionary
foreach my $ne (2..$#array) {
my @value = split('\?abc\?', $array[$ne]);
if (length $value[0] > 1) {
$sheetid = "$value[$columnpos{'person first name'}] $value[$columnpos{'person initials'}] $value[$columnpos{'person last name'}]";
$sth = $dbh->prepare("select personid from Person where personid = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if person is not in the database
$sth = $dbh->prepare("insert into Person (personid, lastname, middleinitial, firstname, email, role) values (?,?,?,?,?,?)");
$sth->execute($sheetid, $value[$columnpos{'person last name'}], $value[$columnpos{'person initials'}], $value[$columnpos{'person first name'}], $value[$columnpos{'person email'}], $value[$columnpos{'person role'}]) or die "\nERROR:\t Complication in Person table\n";
}
$NAME{$sheetid} = $sheetid;
} else { #end if : insert into database
undef $sheetid;
} #end else table has no information
} #end foreach : getting information into the database and into dictionary
} else { #end if : if there is content in thetable
undef $sheetid; #make sure sheetid is undefined if there is no content in the sheet
}
}
if ($array[0] =~ /organization/) { #Working with Organization Sheet;
undef %columnpos; #undefine column position
if ($#array > 1) {
foreach my $no (0..$#header) {
$columnpos{$header[$no]} = $no;
}#end foreach : put header info into a Dictionary
foreach my $ne (2..$#array) {
my @value = split('\?abc\?', $array[$ne]);
if (length $value[0] > 1) {
$sheetid = $value[$columnpos{'organization name'}];
$sth = $dbh->prepare("select organizationname from Organization where organizationname = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if organization is not in the database
$sth = $dbh->prepare("insert into Organization (organizationname,address, URL, role) values (?,?,?,?)");
$sth->execute($sheetid, $value[$columnpos{'organization address'}], $value[$columnpos{'organization uri'}], $value[$columnpos{'organization role'}]) or die "\nERROR:\t Complication in Organization table\n";
}
$ORGANIZATION{$sheetid} = $sheetid;
} else { #end if : insert into database
undef $sheetid;
} #end else table has no information
} #end foreach : getting information into the database and into dictionary
} else { #end if : if there is content in thetable
undef $sheetid; #make sure sheetid is undefined if there is no content in the sheet
} #end else
} #end if organization
if ($array[0] =~ /animal/) { #Working with Animal Sheet;
my %ANIMAL = (material=>'Material', organism => 'Organism', sex => 'Sex', breed => 'Breed');
undef %columnpos; #undefine column position
if ($#array > 1) {
foreach my $no (0..$#header) {
if ($header[$no] =~ /[material|organism|sex|breed|health]/) {
$columnpos{$header[$no]} = $no;
$no = $no+2;
} elsif ($header[$no] =~ /[birth|placental|pregnancy]/) {
$columnpos{$header[$no]} = $no;
$no = $no+2;
} else {
$columnpos{$header[$no]} = $no;
}
} #end foreach : put header info into a Dictionary
foreach my $ne (2..$#array) {
my @value = split('\?abc\?', $array[$ne]);
if (length $value[0] > 1) {
foreach my $id (sort keys %ANIMAL) {
if (length $value[$columnpos{$id}] > 1) {
$sheetid = "$value[$columnpos{$id}]";
my $loc = $columnpos{$id};
$sth = $dbh->prepare("select $id from $ANIMAL{$id} where $id = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if material is not in the database
$sth = $dbh->prepare("insert into $ANIMAL{$id} ($id, termref, termid) values (?,?,?)");
$sth->execute($sheetid, $value[$loc+1], $value[$loc+2] ) or die "\nERROR:\t Complication in $ANIMAL{$id} table\n";
}
}
}
if (length $value[$columnpos{'health status'}] > 1) {
$sheetid = "$value[$columnpos{'health status'}]";
my $loc = $columnpos{'health status'};
$sth = $dbh->prepare("select health from HealthStatus where health = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if material is not in the database
$sth = $dbh->prepare("insert into HealthStatus (health,termref, termid) values (?,?,?)");
$sth->execute($sheetid, $value[$loc+1], $value[$loc+2] ) or die "\nERROR:\t Complication in HealthStatus table\n";
}
}
$sheetid = uc($value[$columnpos{'sample name'}]); #Animal Table
$sth = $dbh->prepare("select animalid from Animal where animalid = '$sheetid'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if animal is not in the database
printerr "NOTICE:\t Importing $sheetid to Animal table\n";
$sth = $dbh->prepare("insert into Animal (animalid, project, material, organism, sex, health, breed, description) values (?,?,?,?,?,?,?,?)");
$sth->execute($sheetid, $value[$columnpos{'project'}], $value[$columnpos{'material'}], $value[$columnpos{'organism'}], $value[$columnpos{'sex'}], $value[$columnpos{'health status'}], $value[$columnpos{'breed'}],$value[$columnpos{'sample description'}]) or die "\nERROR:\t Complication in Animal table\n";
} else {
$verbose and printerr "Duplicate: AnimalID '$sheetid' already exists in Animal table. Moving on\n";
}
$sth = $dbh->prepare("select animalid from AnimalStats where animalid = '$sheetid'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if animalstats is not in the database
my ($loc, $birthdate, $birthlocation, $birthloclatitude, $birthloclongitude, $birthweight, $placentaweight, $pregnancylength) = ();
if ($value[$columnpos{'birth date'}]) { $birthdate = "$value[$columnpos{'birth date'}] \($value[$columnpos{'birth date'}+1]\)"; }
if ($value[$columnpos{'birth location'}]) { $birthlocation = "$value[$columnpos{'birth location'}] \($value[$columnpos{'birth location'}+1]\)"; }
if ($value[$columnpos{'birth location latitude'}]) { $birthloclatitude = "$value[$columnpos{'birth location latitude'}] \($value[$columnpos{'birth location latitude'}+1]\)"; }
if ($value[$columnpos{'birth location longitude'}]) { $birthloclongitude = "$value[$columnpos{'birth location longitude'}] \($value[$columnpos{'birth location longitude'}+1]\)"; }
if ($value[$columnpos{'birth weight'}]) { $birthweight = "$value[$columnpos{'birth weight'}] \($value[$columnpos{'birth weight'}+1]\)"; }
if ($value[$columnpos{'placental weight'}]) { $placentaweight = "$value[$columnpos{'placental weight'}] \($value[$columnpos{'placental weight'}+1]\)"; }
if ($value[$columnpos{'pregnancy length'}]) { $pregnancylength = "$value[$columnpos{'pregnancy length'}] \($value[$columnpos{'pregnancy length'}+1]\)"; }
$sth = $dbh->prepare("insert into AnimalStats (animalid, birthdate, birthlocation, birthloclatitude, birthloclongitude, birthweight, placentalweight, pregnancylength, deliveryease, deliverytiming, pedigree) values (?,?,?,?,?,?,?,?,?,?,?)");
$sth->execute($sheetid, $birthdate, $birthlocation, $birthloclatitude, $birthloclongitude, $birthweight, $placentaweight, $pregnancylength, $value[$columnpos{'delivery ease'}], $value[$columnpos{'delivery timing'}], $value[$columnpos{'pedigree'}]) or die "\nERROR:\t Complication in AnimalStats table\n";
} else {
$verbose and printerr "Duplicate: AnimalID '$sheetid' already exists in AnimalStats table. Moving on\n";
}
} else { #end if : insert into database
undef $sheetid;
} #end else table has no information
} #end foreach : getting information into the database
} else { #end if : if there is content in thetable
undef $sheetid; #make sure sheetid is undefined if there is no content in the sheet
} #end else
} #end if animal
if ($array[0] =~ /specimen/) { #Working with Specimen Sheet;
undef %columnpos; #undefine column position
if ($#array > 1) {
foreach my $no (0..$#header) {
if ($header[$no] =~ /[material|organism|health|developmental]/) {
$columnpos{$header[$no]} = $no;
$no = $no+2;
} elsif ($header[$no] =~ /[date|animal age|number|volume|size|weight|gestational]/) {
$columnpos{$header[$no]} = $no;
$no = $no+2;
} else {
$columnpos{$header[$no]} = $no;
}
} #end foreach : put header info into a Dictionary
foreach my $ne (2..$#array) { #each row
my @value = split('\?abc\?', $array[$ne]);
if (length $value[0] > 1) {
if (length $value[$columnpos{'material'}] > 1) {
$sheetid = "$value[$columnpos{'material'}]";
my $loc = $columnpos{'material'};
$sth = $dbh->prepare("select material from Material where material = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if material is not in the database
$sth = $dbh->prepare("insert into Material (material,termref, termid) values (?,?,?)");
$sth->execute($sheetid, $value[$loc+1], $value[$loc+2] ) or die "\nERROR:\t Complication in Material table\n";
}
}
if (length $value[$columnpos{'organism part'}] > 1) {
$value[$columnpos{'organism part'}] = lc($value[$columnpos{'organism part'}]);
$sheetid = "$value[$columnpos{'organism part'}]";
my $loc = $columnpos{'organism part'};
$sth = $dbh->prepare("select tissue from Tissue where tissue = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if is not in the database
$sth = $dbh->prepare("insert into Tissue (tissue,termref, termid) values (?,?,?)");
$sth->execute($sheetid, $value[$loc+1], $value[$loc+2] ) or die "\nERROR:\t Complication in Tissue table\n";
}
}
if (length $value[$columnpos{'developmental stage'}] > 1) {
$sheetid = "$value[$columnpos{'developmental stage'}]";
my $loc = $columnpos{'developmental stage'};
$sth = $dbh->prepare("select developmentalstage from DevelopmentalStage where developmentalstage = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # ifis not in the database
$sth = $dbh->prepare("insert into DevelopmentalStage (developmentalstage,termref, termid) values (?,?,?)");
$sth->execute($sheetid, $value[$loc+1], $value[$loc+2] ) or die "\nERROR:\t Complication in DevelopmentalStage table\n";
}
}
if (length $value[$columnpos{'health status at collection'}] > 1) {
$sheetid = "$value[$columnpos{'health status at collection'}]";
my $loc = $columnpos{'health status at collection'};
$sth = $dbh->prepare("select health from HealthStatus where health = '$sheetid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) { # if is not in the database
$sth = $dbh->prepare("insert into HealthStatus (health,termref, termid) values (?,?,?)");
$sth->execute($sheetid, $value[$loc+1], $value[$loc+2] ) or die "\nERROR:\t Complication in HealthStatus table\n";
}
}
$sheetid = uc($value[$columnpos{'sample name'}]); #Sample Table
$sth = $dbh->prepare("select sampleid from Sample where sampleid = '$sheetid'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if sample is not in the database
printerr "NOTICE:\t Importing $sheetid to Sample table\n";
$sth = $dbh->prepare("insert into Sample (sampleid, project, material, tissue, derivedfrom, availability, developmentalstage, health, description, date) values (?,?,?,?,?,?,?,?,?,?)");
$sth->execute($sheetid, $value[$columnpos{'project'}], $value[$columnpos{'material'}], $value[$columnpos{'organism part'}], uc($value[$columnpos{'derived from'}]), $value[$columnpos{'availability'}], $value[$columnpos{'developmental stage'}], $value[$columnpos{'health status at collection'}],$value[$columnpos{'sample description'}], $date) or die "\nERROR:\t Complication in Sample table\n";
} else {
printerr "Duplicate: SampleID '$sheetid' already exists in Sample table. Moving on\n";
printerr "Optional: To delete $sheetid ; Execute: tad-import.pl -delete $sheetid\n";
}
$sth = $dbh->prepare("select sampleid from SampleStats where sampleid = '$sheetid'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if samplestats is not in the database
my ($specimendate, $agecollect, $noofpieces, $specimenvolume, $specimensize, $specimenweight, $gestage) = ();
if ($value[$columnpos{'specimen collection date'}]) { $specimendate = "$value[$columnpos{'specimen collection date'}] \($value[$columnpos{'specimen collection date'}+1]\)"; }
if ($value[$columnpos{'animal age at collection'}]) { $agecollect = "$value[$columnpos{'animal age at collection'}] \($value[$columnpos{'animal age at collection'}+1]\)"; }
if ($value[$columnpos{'number of pieces'}]) { $noofpieces = "$value[$columnpos{'number of pieces'}] \($value[$columnpos{'number of pieces'}+1]\)"; }
if ($value[$columnpos{'specimen volume'}]) { $specimenvolume = "$value[$columnpos{'pecimen volume'}] \($value[$columnpos{'pecimen volume'}+1]\)"; }
if ($value[$columnpos{'specimen size'}]) { $specimensize = "$value[$columnpos{'specimen size'}] \($value[$columnpos{'specimen size'}+1]\)"; }
if ($value[$columnpos{'specimen weight'}]) { $specimenweight = "$value[$columnpos{'specimen weight'}] \($value[$columnpos{'specimen weight'}+1]\)"; }
if ($value[$columnpos{'gestational age at sample collection'}]) { $gestage = "$value[$columnpos{'gestational age at sample collection'}] \($value[$columnpos{'gestational age at sample collection'}+1]\)"; }
$sth = $dbh->prepare("insert into SampleStats (sampleid, collectionprotocol, collectiondate, ageatcollection, fastedstatus, noofpieces, specimenvol, specimensize, specimenwgt, specimenpictureurl, gestationalage) values (?,?,?,?,?,?,?,?,?,?,?)");
$sth->execute($sheetid, $value[$columnpos{'specimen collection protocol'}], $specimendate, $agecollect, $value[$columnpos{'fasted status'}], $noofpieces, $specimenvolume, $specimensize, $specimenweight, $value[$columnpos{'specimen picture url'}], $gestage) or die "\nERROR:\t Complication in SampleStats table\n";
} else {
$verbose and printerr "Duplicate: SampleID '$sheetid' already exists in SampleStats table. Moving on\n";
}
foreach (keys %NAME) {
$sth = $dbh->prepare("select sampleid, personid from SamplePerson where sampleid = '$sheetid' and personid = '$_'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if sample-person is not in the database
$sth = $dbh->prepare("insert into SamplePerson (sampleid, personid) values (?,?)");
$sth->execute($sheetid, $_) or die "\nERROR:\t Complication in SamplePerson table\n";
}
}
foreach (keys %ORGANIZATION) {
$sth = $dbh->prepare("select sampleid, organizationname from SampleOrganization where sampleid = '$sheetid' and organizationname = '$_'"); $sth->execute(); $found =$sth->fetch();
unless ($found) { # if sample-organization is not in the database
$sth = $dbh->prepare("insert into SampleOrganization (sampleid, organizationname) values (?,?)");
$sth->execute($sheetid, $_) or die "\nERROR:\t Complication in SampleOrganization table\n";
}
}
} else { #end if : insert into database
undef $sheetid;
} #end else table has no information
} #end foreach : getting information into the database
} else { #end if : if there is content in thetable
undef $sheetid; #make sure sheetid is undefined if there is no content in the sheet
}#end else
} #end if specimen
} #end foreach @excelcontent
} #end if excel
} #end if metadata
#PROCESSING DATA IMPORT
if ($datadb){
printerr "JOB:\t Importing Transcriptome analysis Information => $file2consider\n"; #status
if ($variant){
printerr "TASK:\t Importing ONLY Variant Information => $file2consider\n"; #status
} elsif ($all) {
printerr "TASK:\t Importing BOTH Gene Expression profiling and Variant Information => $file2consider\n"; #status
} else {
printerr "TASK:\t Importing ONLY Gene Expression Profiling information => $file2consider\n"; #status
}
$dbh = mysql($all_details{"MySQL-databasename"}, $all_details{'MySQL-username'}, $all_details{'MySQL-password'}); #connect to mysql
my $dataid = (split("\/", $file2consider))[-1];
`find $file2consider` or pod2usage ("ERROR: Can not locate \"$file2consider\"");
opendir (DIR, $file2consider) or pod2usage ("Error: $file2consider is not a folder, please specify your sample directory location "); close (DIR);
my @foldercontent = split("\n", `find $file2consider -type f -print0 | xargs -0 ls -tr `); #get details of the folder
foreach (grep /\.gtf/, @foldercontent) { unless (`head -n 3 $_ | wc -l` <= 0 && $_ =~ /skipped/) { $transcriptsgtf = $_; } }
$fastqcfolder = (grep /fastqc.zip$/, @foldercontent)[0]; unless ($fastqcfolder) { $fastqcfolder = (grep /fastqc_data.txt$/, @foldercontent)[0]; }
$alignfile = (grep /summary.txt/, @foldercontent)[0];
$genesfile = (grep /genes.fpkm/, @foldercontent)[0];
$deletionsfile = (grep /deletions.bed/, @foldercontent)[0];
$insertionsfile = (grep /insertions.bed/, @foldercontent)[0];
$junctionsfile = (grep /junctions.bed/, @foldercontent)[0];
$bamfile = (grep /.bam$/, @foldercontent)[0];
$variantfile = (grep /.vcf$/, @foldercontent)[0];
$vepfile = (grep /.vep.txt$/, @foldercontent)[0];
$annofile = (grep /anno.txt$/, @foldercontent)[0];
$readcountfile = (grep /.counts$/, @foldercontent)[0];
$starcountfile = (grep /ReadsPerGene.out.tab$/, @foldercontent)[0];
$kallistofile = (grep /.tsv$/, @foldercontent)[0];
$kallistologfile = (grep /run_info.json/, @foldercontent)[0];
$salmonfile = (grep /.sf$/, @foldercontent)[0];
$salmonlogfile = (grep /cmd_info.json$/, @foldercontent)[0];
$staralignfile = (grep /Log.final.out$/, @foldercontent)[0];
$sth = $dbh->prepare("select sampleid from Sample where sampleid = '$dataid'"); $sth->execute(); $found = $sth->fetch();
if ($found) { # if sample is not in the database
$sth = $dbh->prepare("select sampleid from MapStats where sampleid = '$dataid'"); $sth->execute(); $found = $sth->fetch();
LOGFILE();
unless ($found) {
#open alignment summary file
unless ($kallistologfile || $salmonlogfile) {
if ($alignfile) {
`head -n 1 $alignfile` =~ /^(\d+)\sreads/; $totalreads = $1;
open(ALIGN,"<", $alignfile) or die "\nFAILED:\t Can not open Alignment summary file '$alignfile'\n";
while (<ALIGN>){
chomp;
if (/Input/){my $line = $_; $line =~ /Input.*:\s+(\d+)$/;$totalreads = $1;}
if (/overall/) { my $line = $_; $line =~ /^(\d+.\d+)%\s/; $alignrate = $1;}
if (/overall read mapping rate/) {
if ($mappingtool){
unless ($mappingtool =~ /TopHat/i){
die "\nERROR:\t Inconsistent Directory Structure, $mappingtool SAM file with TopHat align_summary.txt file found\n";
}
} else { $mappingtool = "TopHat"; }
}
if (/overall alignment rate/) {
if ($mappingtool){
unless ($mappingtool =~ /hisat/i){
die "\nERROR:\t Inconsistent Directory Structure, $mappingtool LOG file with HISAT align_summary.txt file found\n";
}
} else { $mappingtool = "HISAT";}
}
} close ALIGN;
$mapped = ceil($totalreads * $alignrate/100);
} elsif ($staralignfile && $mappingtool =~ /star/i) {
`grep "Number of input reads" $staralignfile` =~ /\s(\d+)$/; $totalreads = $1;
`grep "Uniquely mapped reads %" $staralignfile` =~ /\s(\S+)\%$/; $alignrate = $1;
} else {
if ($mappingtool =~ /star/) {die "\nFAILED:\t Can not find STAR Alignment summary file as 'Log.final.out'\n";}
else {die "\nFAILED:\t Can not find Alignment summary file as 'align_summary.txt'\n";}
}
}
$deletions = undef; $insertions = undef; $junctions = undef;
if ($deletionsfile){ $deletions = `cat $deletionsfile | wc -l`; $deletions--; }
if ($insertionsfile){ $insertions = `cat $insertionsfile | wc -l`; $insertions--; }
if ($junctionsfile){ $junctions = `cat $junctionsfile | wc -l`; $junctions--; }
#INSERT INTO DATABASE:
#MapStats table
if ($mappingtool) { printerr "NOTICE:\t Importing $mappingtool alignment information for $dataid to MapStats table ..."; }
$sth = $dbh->prepare("insert into MapStats (sampleid, totalreads, mappedreads, alignmentrate, deletions, insertions, junctions, date ) values (?,?,?,?,?,?,?,?)");
$sth ->execute($dataid, $totalreads, $mapped, $alignrate, $deletions, $insertions, $junctions, $date) or die "\nERROR:\t Complication in MapStats table, consult documentation\n";
if ($mappingtool) { printerr " Done\n"; }
#metadata table
if ($mappingtool) { printerr "NOTICE:\t Importing $mappingtool alignment information for $dataid to Metadata table ..."; }
$sth = $dbh->prepare("insert into Metadata (sampleid, refgenome, annfile, stranded, sequencename, mappingtool ) values (?,?,?,?,?,?)");
$sth ->execute($dataid, $refgenomename, $annotationfile, $stranded,$sequences, $mappingtool) or die "\nERROR:\t Complication in Metadata table, consult documentation\n";
#Insert DataSyntaxes
if ($mparameters) {
$sth = $dbh->prepare("insert into CommandSyntax (sampleid, mappingsyntax ) values (?,?)");
$mparameters =~ s/\"//g;
$sth ->execute($dataid, $mparameters) or die "\nERROR:\t Complication in CommandSyntax table, consult documentation\n";
}
if ($mappingtool) { printerr " Done\n"; }
#toggle options
unless ($variant) {
GENES_FPKM($dataid);
READ_COUNT($dataid);
if ($all){
DBVARIANT($variantfile, $dataid);
printerr " Done\n";
#variant annotation specifications
if ($vep) {
printerr "TASK:\t Importing Variant annotation from VEP => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
VEPVARIANT($vepfile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
if ($annovar) {
printerr "TASK:\t Importing Variant annotation from ANNOVAR => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
ANNOVARIANT($annofile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
}
}
else { #variant option selected
DBVARIANT($variantfile, $dataid);
printerr " Done\n";
#variant annotation specifications
if ($vep) {
printerr "TASK:\t Importing Variant annotation from VEP => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
VEPVARIANT($vepfile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
if ($annovar) {
printerr "TASK:\t Importing Variant annotation from ANNOVAR => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
ANNOVARIANT($annofile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
}
} else { #end unless found in MapStats table
printerr "NOTICE:\t $dataid already in MapStats table... Moving on \n";
$additional .= "Optional: To delete '$dataid' Alignment information ; Execute: tad-import.pl -delete $dataid \n";
$sth = $dbh->prepare("select sampleid from Metadata where sampleid = '$dataid'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
printerr "NOTICE:\t Importing $mappingtool alignment information for $dataid to Metadata table ...";
$sth = $dbh->prepare("insert into Metadata (sampleid, refgenome, annfile, stranded, sequencename, mappingtool) values (?,?,?,?,?,?)");
$sth ->execute($dataid, $refgenomename, $annotationfile, $stranded,$sequences, $mappingtool) or die "\nERROR:\t Complication in Metadata table, consult documentation\n";
#Insert DataSyntaxes
$sth = $dbh->prepare("insert into CommandSyntax (sampleid, mappingsyntax ) values (?,?)");
$mparameters =~ s/\"//g;
$sth ->execute($dataid, $mparameters) or die "\nERROR:\t Complication in CommandSyntax table, consult documentation\n";
printerr " Done\n";
} #end else found in MapStats table
#toggle options
unless ($variant) {
GENES_FPKM($dataid); #GENES
READ_COUNT($dataid); #READCOUNTS
if ($all){
my $variantstatus = $dbh->selectrow_array("select status from VarSummary where sampleid = '$dataid' and status = 'done'");
unless ($variantstatus){ #checking if completed in VarSummary table
$verbose and printerr "NOTICE:\t Removed incomplete records for $dataid in all Variants tables\n";
$sth = $dbh->prepare("delete from VarAnnotation where sampleid = '$dataid'"); $sth->execute();
$sth = $dbh->prepare("delete from VarResult where sampleid = '$dataid'"); $sth->execute();
$sth = $dbh->prepare("delete from VarSummary where sampleid = '$dataid'"); $sth->execute();
DBVARIANT($variantfile, $dataid);
printerr " Done\n";
#variant annotation specifications
if ($vep) {
printerr "TASK:\t Importing Variant annotation from VEP => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
VEPVARIANT($vepfile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
if ($annovar) {
printerr "TASK:\t Importing Variant annotation from ANNOVAR => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
ANNOVARIANT($annofile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
} else {
printerr "NOTICE:\t $dataid already in VarResult table... Moving on \n";
if ($vep || $annovar) {
my $variantstatus = $dbh->selectrow_array("select annversion from VarSummary where sampleid = '$dataid'");
my $nosqlstatus = $dbh->selectrow_array("select nosql from VarSummary where sampleid = '$dataid'");
unless ($variantstatus && $nosqlstatus){ #if annversion or nosqlstatus is not specified
$verbose and printerr "NOTICE:\t Removed incomplete records for $dataid in VarAnnotation table\n";
$sth = $dbh->prepare("delete from VarAnnotation where sampleid = '$dataid'"); $sth->execute();
if ($vep) {
printerr "TASK:\t Importing Variant annotation from VEP => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
VEPVARIANT($vepfile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
if ($annovar) {
printerr "TASK:\t Importing Variant annotation from ANNOVAR => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
ANNOVARIANT($annofile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
} else { #end unless annversion is previously specified
printerr "NOTICE:\t $dataid already in VarAnnotation table... Moving on\n";
}
} #end if annversion is previously specified
$additional .= "Optional: To delete '$dataid' Variant information ; Execute: tad-import.pl -delete $dataid \n";
} #end unless it's already in variants table
} #end if "all" option
} #end unless default option is specified
else { #variant option selected
my $variantstatus = $dbh->selectrow_array("select status from VarSummary where sampleid = '$dataid' and status = 'done'");
unless ($variantstatus){ #checking if completed in VarSummary table
$verbose and printerr "NOTICE:\t Removed incomplete records for $dataid in all Variants tables\n";
$sth = $dbh->prepare("delete from VarAnnotation where sampleid = '$dataid'"); $sth->execute();
$sth = $dbh->prepare("delete from VarResult where sampleid = '$dataid'"); $sth->execute();
$sth = $dbh->prepare("delete from VarSummary where sampleid = '$dataid'"); $sth->execute();
DBVARIANT($variantfile, $dataid);
printerr " Done\n";
#variant annotation specifications
if ($vep) {
printerr "TASK:\t Importing Variant annotation from VEP => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
VEPVARIANT($vepfile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
if ($annovar) {
printerr "TASK:\t Importing Variant annotation from ANNOVAR => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
ANNOVARIANT($annofile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
} else { #if completed in VarSummary table
printerr "NOTICE:\t $dataid already in VarResult table... Moving on \n";
if ($vep || $annovar) { #checking if vep or annovar was specified
my $variantstatus = $dbh->selectrow_array("select annversion from VarSummary where sampleid = '$dataid'");
my $nosqlstatus = $dbh->selectrow_array("select nosql from VarSummary where sampleid = '$dataid'");
unless ($variantstatus && $nosqlstatus){ #if annversion or nosqlstatus is not specified
$verbose and printerr "NOTICE:\t Removed incomplete records for $dataid in all Variant Annotation tables\n";
$sth = $dbh->prepare("delete from VarAnnotation where sampleid = '$dataid'"); $sth->execute();
if ($vep) {
printerr "TASK:\t Importing Variant annotation from VEP => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
VEPVARIANT($vepfile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
if ($annovar) {
printerr "TASK:\t Importing Variant annotation from ANNOVAR => $file2consider\n"; #status
printerr "NOTICE:\t Importing $dataid - Variant Annotation to VarAnnotation table ...";
ANNOVARIANT($annofile, $dataid); printerr " Done\n";
NOSQL($dataid);
}
} else { #end unless annversion is previously specified
printerr "NOTICE:\t $dataid already in VarAnnotation table... Moving on \n";
}
} #end if annversion is previously specified
$additional .= "Optional: To delete '$dataid' Variant information ; Execute: tad-import.pl -delete $dataid \n";
} # end else already in VarSummary table;
} #end if "variant" option
} #unless & else exists in Mapstats
} else {
pod2usage("FAILED: \"$dataid\" sample information is not in the database. Make sure the metadata has be previously imported using '-metadata'");
} #end if data in sample table
}
if ($delete){ #delete section
my (%KEYDELETE, $decision);
my ($i,$alldelete) = (0,0);
unless ($log) { printerr "JOB:\t Deleting Existing Records in Database\n"; } #status
$dbh = mysql($all_details{'MySQL-databasename'}, $all_details{'MySQL-username'}, $all_details{'MySQL-password'}); #connect to mysql
$sth = $dbh->prepare("select sampleid from Sample where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
if ($found) {
unless ($log) { printerr "NOTICE:\t This module deletes records from ALL database systems for TransAtlasDB. Proceed with caution\n"; }
$sth = $dbh->prepare("select sampleid from Sample where sampleid = '$delete'"); $sth->execute(); $found =$sth->fetch();
if ($found) {
$i++; $KEYDELETE{$i} = "Sample Information";
}
$sth = $dbh->prepare("select sampleid from MapStats where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
if ($found) {
$i++; $KEYDELETE{$i} = "Alignment Information";
}
$sth = $dbh->prepare("select sampleid from GeneStats where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
if ($found) {
$i++; $KEYDELETE{$i} = "Expression Information";
}
$sth = $dbh->prepare("select sampleid from VarSummary where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
if ($found) {
$i++; $KEYDELETE{$i} = "Variant Information";
}
unless ($log) {
print "--------------------------------------------------------------------------\n";
print "The following details match the sampleid '$delete' provided\n";
foreach (sort {$a <=> $b} keys %KEYDELETE) { print " ", uc($_),"\. $KEYDELETE{$_}\n";}
}
$KEYDELETE{0} = "ALL information relating to '$delete'";
unless ($log) {
print " 0\. ALL information relating to '$delete'\n";
print "--------------------------------------------------------------------------\n";
print "Choose which information you want remove (multiple options separated by comma) or press ENTER to leave ? ";
chomp ($decision = (<>)); print "\n";
} else {$decision = $log; }
if (length $decision >0) {
my @allverdict = split(",",$decision);
foreach my $verdict (sort {$b<=>$a} @allverdict) {
if (exists $KEYDELETE{$verdict}) {
printerr "NOTICE:\t Deleting $KEYDELETE{$verdict}\n";
if ($verdict == 0) {$alldelete = 1;}
if ($KEYDELETE{$verdict} =~ /^Variant/ || $alldelete == 1) {
if ($alldelete == 1){
if ($KEYDELETE{$i} =~ /^Variant/) { $i--;
my $ffastbit = fastbit($all_details{'FastBit-path'}, $all_details{'FastBit-foldername'}); #connect to fastbit
my $vfastbit = $ffastbit."/variant-information"; # specifying the variant section.
printerr "NOTICE:\t Deleting records for $delete in Variant tables ";
$sth = $dbh->prepare("delete from VarAnnotation where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from VarResult where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from VarSummary where sampleid = '$delete'"); $sth->execute(); printerr ".";
my $execute = "$ibis -d $vfastbit -y \"sampleid = '$delete'\" -z";
`$execute 2>> $efile`; printerr ".";
`rm -rf $vfastbit/*sp $vfastbit/*old $vfastbit/*idx $vfastbit/*dic $vfastbit/*int `; #removing old indexes
`ibis -d $vfastbit -query "select genename, geneid, genetype, transcript, feature, codonchange, aachange, sampleid, chrom, tissue, organism, consequence, dbsnpvariant, source" 2>> $efile`; #create a new index based on genename
printerr " Done\n";
}
} else {
my $ffastbit = fastbit($all_details{'FastBit-path'}, $all_details{'FastBit-foldername'}); #connect to fastbit
my $vfastbit = $ffastbit."/variant-information"; # specifying the variant section.
printerr "NOTICE:\t Deleting records for $delete in Variant tables ";
$sth = $dbh->prepare("delete from VarAnnotation where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from VarResult where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from VarSummary where sampleid = '$delete'"); $sth->execute(); printerr ".";
my $execute = "$ibis -d $vfastbit -y \"sampleid = '$delete'\" -z";
`$execute 2>> $efile`; printerr ".";
`rm -rf $vfastbit/*sp $vfastbit/*old $vfastbit/*idx $vfastbit/*dic $vfastbit/*int `; #removing old indexes
`ibis -d $vfastbit -query "select genename, geneid, genetype, transcript, feature, codonchange, aachange, sampleid, chrom, tissue, organism, consequence, dbsnpvariant, source" 2>> $efile`; #create a new index
printerr " Done\n";
}
}
if ($KEYDELETE{$verdict} =~ /^Expression/ || $alldelete ==1 ) {
if ($alldelete == 1){
if ($KEYDELETE{$i} =~ /^Expression/) { $i--;
my $ffastbit = fastbit($all_details{'FastBit-path'}, $all_details{'FastBit-foldername'}); #connect to fastbit
my $gfastbit = $ffastbit."/gene-information"; # specifying the gene section.
my $cfastbit = $ffastbit."/gene_count-information"; #specifying the gene count section
printerr "NOTICE:\t Deleting records for $delete in Gene tables ";
$sth = $dbh->prepare("delete from GeneStats where sampleid = '$delete'"); $sth->execute(); printerr ".";
#deleting gene-information from fastbit
my $execute = "$ibis -d -v $gfastbit -y \"sampleid = '$delete'\" -z";
`$execute 2>> $efile`; printerr ".";
`rm -rf $gfastbit/*sp $gfastbit/*old $gfastbit/*idx $gfastbit/*dic $gfastbit/*int `; #removing old indexes
`ibis -d $gfastbit -query "select genename, geneid, sampleid, chrom, tissue, organism" 2>> $efile`; #create a new index based on genename
#deleting gene_counts information from fastbit
$execute = "$ibis -d -v $cfastbit -y \"sampleid = '$delete'\" -z";
`$execute 2>> $efile`; printerr ".";
`rm -rf $cfastbit/*sp $cfastbit/*old $cfastbit/*idx $cfastbit/*dic $cfastbit/*int `; #removing old indexes
`ibis -d $cfastbit -query "select genename, sampleid, tissue, organism" 2>> $efile`; #create a new index based on genename
printerr " Done\n";
}
} else {
my $ffastbit = fastbit($all_details{'FastBit-path'}, $all_details{'FastBit-foldername'}); #connect to fastbit
my $gfastbit = $ffastbit."/gene-information"; # specifying the gene section.
my $cfastbit = $ffastbit."/gene_count-information"; #specifying the gene count section
printerr "NOTICE:\t Deleting records for $delete in Gene tables ";
$sth = $dbh->prepare("delete from GeneStats where sampleid = '$delete'"); $sth->execute(); printerr ".";
#deleting gene-information from fastbit
my $execute = "$ibis -d -v $gfastbit -y \"sampleid = '$delete'\" -z";
`$execute 2>> $efile`; printerr ".";
`rm -rf $gfastbit/*sp $gfastbit/*old $gfastbit/*idx $gfastbit/*dic $gfastbit/*int `; #removing old indexes
`ibis -d $gfastbit -query "select genename, geneid, sampleid, chrom, tissue, organism" 2>> $efile`; #create a new index based on genename
#deleting gene_counts information from fastbit
$execute = "$ibis -d -v $cfastbit -y \"sampleid = '$delete'\" -z";
`$execute 2>> $efile`; printerr ".";
`rm -rf $cfastbit/*sp $cfastbit/*old $cfastbit/*idx $cfastbit/*dic $cfastbit/*int `; #removing old indexes
`ibis -d $cfastbit -query "select genename, sampleid, tissue, organism" 2>> $efile`; #create a new index based on genename
printerr " Done\n";
}
}
if ($KEYDELETE{$verdict} =~ /^Alignment/ || $alldelete ==1 ) {
if ($alldelete == 1){
if ($KEYDELETE{$i} =~ /^Alignment/) { $i--;
$sth = $dbh->prepare("select sampleid from GeneStats where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
$sth = $dbh->prepare("select sampleid from VarSummary where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
printerr "NOTICE:\t Deleting records for $delete in Mapping tables .";
$sth = $dbh->prepare("delete from Metadata where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from CommandSyntax where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from MapStats where sampleid = '$delete'"); $sth->execute(); printerr ".";
printerr " Done\n";
} else { printerr "ERROR:\t Variant Information relating to '$delete' is in the database. Delete Variant Information first\n";}
} else { printerr "ERROR:\t Expression Information Relating to '$delete' still present in the database. Delete Expression Information first\n";}
}
} else {
$sth = $dbh->prepare("select sampleid from GeneStats where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
$sth = $dbh->prepare("select sampleid from VarSummary where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
printerr "NOTICE:\t Deleting records for $delete in Mapping tables .";
$sth = $dbh->prepare("delete from Metadata where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from CommandSyntax where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from MapStats where sampleid = '$delete'"); $sth->execute(); printerr ".";
printerr " Done\n";
} else { printerr "ERROR:\t Variant Information relating to '$delete' is in the database. Delete Variant Information first\n";}
} else { printerr "ERROR:\t Expression Information Relating to '$delete' still present in the database. Delete Expression Information first\n";}
}
}
if ($KEYDELETE{$verdict} =~ /^Sample/ || $alldelete ==1 ) {
if ($alldelete == 1){
if ($KEYDELETE{$i} =~ /^Sample/) { $i--;
$sth = $dbh->prepare("select sampleid from MapStats where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
$sth = $dbh->prepare("select sampleid from GeneStats where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
$sth = $dbh->prepare("select sampleid from VarSummary where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
printerr "NOTICE:\t Deleting records for $delete in Sample tables ";
$sth = $dbh->prepare("delete from SampleStats where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from SampleOrganization where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from SamplePerson where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from Sample where sampleid = '$delete'"); $sth->execute(); printerr ".";
printerr " Done\n";
} else { printerr "ERROR:\t Variant Information for '$delete' is in the database. Delete Variant Information first\n"; }
} else { printerr "ERROR:\t Expression Information for '$delete' still present in the database. Delete Expression Information first\n"; }
} else { printerr "ERROR:\t Alignment Information for '$delete' is in the database. Delete Alignment Information first\n"; }
}
} else {
$sth = $dbh->prepare("select sampleid from MapStats where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
$sth = $dbh->prepare("select sampleid from GeneStats where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
$sth = $dbh->prepare("select sampleid from VarSummary where sampleid = '$delete'"); $sth->execute(); $found = $sth->fetch();
unless ($found) {
printerr "NOTICE:\t Deleting records for $delete in Sample tables ";
$sth = $dbh->prepare("delete from SampleStats where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from SampleOrganization where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from SamplePerson where sampleid = '$delete'"); $sth->execute(); printerr ".";
$sth = $dbh->prepare("delete from Sample where sampleid = '$delete'"); $sth->execute(); printerr ".";
printerr " Done\n";
} else { printerr "ERROR:\t Variant Information for '$delete' is in the database. Delete Variant Information first\n"; }
} else { printerr "ERROR:\t Expression Information for '$delete' still present in the database. Delete Expression Information first\n"; }
} else { printerr "ERROR:\t Alignment Information for '$delete' is in the database. Delete Alignment Information first\n"; }
}
}
} else { printerr "ERROR:\t $verdict is an INVALID OPTION\n"; }
}
} else {printerr "NOTICE:\t No Option selected\n";}
} else {
printerr "NOTICE:\t Information relating to '$delete' is not in the database. Good-bye\n";
}
}
#output: the end
unless ($log) {
printerr "-----------------------------------------------------------------\n";
if ($metadata){
printerr ("SUCCESS: Import of Sample Information in \"$file2consider\"\n");
} #end if complete RNASeq metadata
if ($datadb){
printerr ("SUCCESS: Import of RNA Seq analysis information in \"$file2consider\"\n");
} #end if completed RNASeq data2db
printerr $additional;
$transaction = "data to database import" if $datadb;
$transaction = "METADATA IMPORT(s)" if $metadata;
$transaction = "DELETE '$delete' activity" if $delete;
printerr ("NOTICE:\t Summary of $transaction in log file $efile\n");
printerr "-----------------------------------------------------------------\n";
print LOG "TransAtlasDB Completed:\t", scalar(localtime),"\n";
close (LOG);
} else { `rm -rf $efile`; }
#--------------------------------------------------------------------------------
sub processArguments {
my @commandline = @ARGV;
GetOptions('verbose|v'=>\$verbose, 'help|h'=>\$help, 'man|m'=>\$man, 'metadata'=>\$metadata,
'data2db'=>\$datadb, 'gene'=>\$gene, 'variant'=>\$variant, 'all'=>\$all, 'vep'=>\$vep,
'annovar'=>\$annovar, 't|tab'=>\$tab, 'x|excel'=>\$excel, 'w=s'=>\$log, 'delete=s'=>\$delete ) or pod2usage ();
$help and pod2usage (-verbose=>1, -exitval=>1, -output=>\*STDOUT);
$man and pod2usage (-verbose=>2, -exitval=>1, -output=>\*STDOUT);
pod2usage(-msg=>"ERROR:\t Invalid syntax specified, choose -metadata or -data2db or -delete.") unless ( $metadata || $datadb|| $delete);
pod2usage(-msg=>"ERROR:\t Invalid syntax specified for @commandline.") if (($metadata && $datadb)||($vep && $annovar) || ($gene && $vep) || ($gene && $annovar) || ($gene && $variant));
if ($vep || $annovar) {
pod2usage(-msg=>"ERROR:\t Invalid syntax specified for @commandline, specify -variant.") unless (($variant && $annovar)||($variant && $vep) || ($all && $annovar) || ($all && $vep));
}
@ARGV<=1 or pod2usage("Syntax error");
$file2consider = $ARGV[0];
$verbose ||=0;
my $get = dirname(abs_path $0); #get source path
$connect = $get.'/.connect.txt';
#setup log file
$efile = @{ open_unique("db.tad_status.log") }[1]; `rm -rf $efile`;
$nosql = @{ open_unique(".nosqlimport.txt") }[1]; `rm -rf $nosql`;
$vnosql = @{ open_unique(".nosqlvimport.txt") }[1]; `rm -rf $vnosql`;
$gnosql = @{ open_unique(".nosqlgimport.txt") }[1]; `rm -rf $gnosql`;
$cnosql = @{ open_unique(".nosqlcimport.txt") }[1]; `rm -rf $cnosql`;
unless ($log) {
open(LOG, ">>", $efile) or die "\nERROR:\t cannot write LOG information to log file $efile $!\n";
print LOG "TransAtlasDB Version:\t",$VERSION,"\n";
print LOG "TransAtlasDB Information:\tFor questions, comments, documentation, bug reports and program update, please visit $default \n";
print LOG "TransAtlasDB Command:\t $0 @commandline\n";
print LOG "TransAtlasDB Started:\t", scalar(localtime),"\n";
}
}
sub LOGFILE { #subroutine for getting metadata
if ($fastqcfolder) { #if the fastqcfolder exist
my ($fastqcfilename, $parentfastqc);
if ($fastqcfolder =~ /zip$/) { #making sure if it's a zipped file
`unzip $fastqcfolder`;
$parentfastqc = fileparse($fastqcfolder, qr/\.[^.]*(\.zip)?$/);
$fastqcfilename = fileparse($fastqcfolder, qr/\.[^.]*(\.zip)?$/)."/fastqc_data.txt";
} else { $fastqcfilename = $fastqcfolder; } #else it will be the actual file
$totalreads = `grep "Total Sequences" $fastqcfilename | awk -F" " '{print \$3}'`;
if ($fastqcfolder =~ /zip$/) { `rm -rf $parentfastqc`; } #removing the unzipped folder
}
if ($bamfile){
my $headerdetails = `samtools view -H $bamfile | grep -m 1 "\@PG" | head -1`;
if ($headerdetails =~ /\sCL\:/) { #making sure mapping tool has the tool information or not
$headerdetails =~ /\@PG\s*ID\:(\S*).*VN\:(\S*)\s*CL\:(.*)/; $mappingtool = $1." v".$2; $mparameters = $3;
}
else { $headerdetails =~ /\@PG\s*ID\:(\S*).*VN\:(\S*)/; $mappingtool = $1." v".$2; }
if ($mappingtool =~ /hisat/i) {
$mparameters =~ /\-x\s(\S+)\s/;
$refgenome = $1; #reference genome name
$refgenomename = (split('\/', $refgenome))[-1];
if ($mparameters =~ /-1/){ #paired-end reads
$mparameters =~ /\-1\s(\S+)\s-2\s(\S+)"$/;
my @nseq = split(",",$1); my @pseq = split(",",$2);
foreach (@nseq){ $sequences .= ( (split('\/', $_))[-1] ).",";}
foreach (@pseq){ $sequences .= ( (split('\/', $_))[-1] ).",";}
chop $sequences;
}
elsif ($mparameters =~ /-U/){ #single-end reads
$mparameters =~ /\-U\s(\S+)"$/;
my @nseq = split(",",$1);
foreach (@nseq){ $sequences .= ( (split('\/', $_))[-1] ).",";}
chop $sequences;
} #end if toggle for sequences
$stranded = undef;
$annotationfile = undef;
} # end if working with hisat.
elsif ($mappingtool =~ /tophat/i) {
my $annotation; undef %ALL; my $no = 0;
my @newgeninfo = split('\s', $mparameters);
my $number = 1;
while ($number <= $#newgeninfo) {
unless ($newgeninfo[$number] =~ /-no-coverage-search/){
if ($newgeninfo[$number] =~ /^\-/){
my $old = $number++;
$ALL{$newgeninfo[$old]} = $newgeninfo[$number];
} else {
unless (exists $ALL{$no}){
$ALL{$no} = $newgeninfo[$number];
$no++;
}
}
}
$number++;
}
unless ((exists $ALL{"-G"}) || (exists $ALL{"--GTF"})) {
$annotationfile = undef;
} else {
if (exists $ALL{"-G"}){ $annotation = $ALL{"-G"} ; } else { $annotation = $ALL{"--GTF"};}
$annotationfile = uc ( (split('\.',((split("\/", $annotation))[-1])))[-1] ); #(annotation file)
}
unless (exists $ALL{"--library-type"}) { $stranded = undef; } else { $stranded = $ALL{"--library-type"}; }
$refgenome = $ALL{0}; my $seq = $ALL{1}; my $otherseq = $ALL{2};
$refgenomename = (split('\/', $ALL{0}))[-1];
unless(length($otherseq)<1){ #sequences
$sequences = ( ( split('\/', $seq) ) [-1]).",". ( ( split('\/', $otherseq) ) [-1]);
} else {
$sequences = ( ( split('\/', $seq) ) [-1]);
} #end if seq
} # end if working with tophat
elsif ($mappingtool =~ /star/i) {
my ($annotation, $otherseq); undef %ALL; my $no = 0; $mparameters =~ s/\s+/ /g;
my @newgeninfo = split('\s', $mparameters);
my $number = 1;
while ($number <= $#newgeninfo) {
unless ($newgeninfo[$number] =~ /-readFilesIn/){
if ($newgeninfo[$number] =~ /^\-/){
my $old = $number++;
$ALL{$newgeninfo[$old]} = $newgeninfo[$number];
}
} else {
#my $old = $number++;
my $seq = $newgeninfo[++$number];
my $new = $number+1;
unless ($newgeninfo[$new] =~ /^\-\-/) {
$otherseq = $newgeninfo[$new]; #if paired reads
$sequences = ( ( split('\/', $seq) ) [-1]).",". ( ( split('\/', $otherseq) ) [-1]);
$number++;
} else {
$sequences = ( ( split('\/', $seq) ) [-1]);
}
} #working with sequence names
$number++;
}
$annotationfile = undef;
$stranded = undef;
$refgenome = $ALL{"--genomeDir"};
$refgenomename = (split('\/', $ALL{"--genomeDir"}))[-1];
} # end if working with star
else {
$annotationfile = undef;
$stranded = undef; $sequences = undef;
}