-
Notifications
You must be signed in to change notification settings - Fork 4
/
rna_seq_utils.pl
executable file
·1058 lines (974 loc) · 47.6 KB
/
rna_seq_utils.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
# Perl core modules
use strict;
use warnings;
use diagnostics;
use FindBin;
use lib "$FindBin::Bin/.."; # Get lib path for Utils.pm
use Utils;
# Frederic Bastian, created November 2012
# Julien Roux, updated March 2016, October 2016
# list of subs useful for analyzing RNA-Seq data
#############################################################
use List::MoreUtils qw(all any);
use LWP::Simple;
use File::Slurp;
use Digest::SHA;
$| = 1;
my $floatingPointRegex = '^[-+]?\d*\.?\d+([eE][-+]?\d+)?$';
my $integerRegex = '^[-+]?\d+$';
my $positiveIntegerRegex = '^\d+$';
sub bgeeTrim {
my ($stringToTrim) = @_;
if ( defined $stringToTrim ){
$stringToTrim =~ s/^\s+|\s+$//g ;
}
return $stringToTrim;
}
# Retrieve the library ID (SRX...) from the sample ID (GSMxxx) by requesting the ncbi website
# and parsing the webpage
sub retrieveLibraryId {
my ($sampleId) = @_;
# to not overload the server
sleep 10;
my $fileName = 'temp_ncbi_download';
# var to check that the downloaded file has the expected format:
# line where the "Relations" title is reached
my $relationsLine = 0;
# line where the "SRA" column is reached
my $sraLine = 0;
my $libId = undef;
my $statusCode = getstore('https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc='.$sampleId, $fileName);
if ( $statusCode != 200 ){
die 'Warning: could not get https://www.ncbi.nlm.nih.gov/geo/query/acc.cgi?acc=', $sampleId, ': error ', $statusCode, "\n";
}
my $lineCount = 0;
for my $line ( read_file($fileName, chomp => 1) ){
if ( $line =~ /<strong>Relations<\/strong>/ ){
$relationsLine = $lineCount;
}
elsif ( $line =~ /<td>SRA<\/td>/ ){
$sraLine = $lineCount;
}
# OK, now it should be the line from which to extract the SRX ID
elsif ( $lineCount == $relationsLine + 2 && $lineCount == $sraLine + 1 && $line =~ /<a.+?>([SEDC]RX.+?)<\/a>/ ){
$libId = bgeeTrim($1);
last;
}
$lineCount++;
}
unlink($fileName) or die("Could not remove [$fileName]\n");
if ( !defined $libId || $libId !~ /^[SEDC]RX/ ){
print("Could not retrieve the library ID for sampleId [$sampleId]\n");
}
return $libId;
}
# TODO is this function still used in pipeline? Isn't it redundant with get_SRA.pl?
# Generate SHA512 checksum for a given file
sub generateCheckSum {
my ($file, $removeFirstLine) = @_;
# $removeFirstLine is optional argument
# to define whether the first line of the file should be removed before generating the checksum
# it is the case for filtered processed mas5, where lines are ordered by probeset IDs, but header is not standardized.
# better chance to detect duplicates by removing it
my $sha = Digest::SHA->new(512);
if ( defined $removeFirstLine && $removeFirstLine ){
open(my $TEMPOUT, '>', 'firtLineRemovedTempFile') or die "could not open temp file\n";
open(my $IN, '<', $file) or die "could not read [$file]\n";
my $line = <$IN>;#first line removed
while ( defined ($line = <$IN>) ){
print {$TEMPOUT} $line;
}
close $IN;
close $TEMPOUT;
$sha->addfile('firtLineRemovedTempFile', 'p');
unlink('firtLineRemovedTempFile');
}
else {
$sha->addfile($file, 'p');
}
return $sha->hexdigest();
}
# TODO still used in pipeline? Keep?
# Extract experiments information from the experiment annotation file
sub getAllAnnotatedExperiments2 {
my ($TSV) = @_;
my %tsv = %$TSV;
# $experiments{experimentId}->{'name'}
# $experiments{experimentId}->{'description'}
# $experiments{experimentId}->{'source'}
# $experiments{experimentId}->{'status'}
# $experiments{experimentId}->{'commented'}
my %experiments = ();
my $totalLineCount = 0;
for my $line ( 0..$#{$tsv{'experimentId'}} ){
my $commented = 0;
my $experimentId = $tsv{'experimentId'}[$line];
if ( ($tsv{'experimentId'}[$line] =~ /^#(.+)/) or ($tsv{'experimentId'}[$line] =~ /^\"#(.+)/) ){
$tsv{'experimentId'}[$line] = $1;
$commented = 1;
}
if ( length($tsv{'experimentId'}[$line]) < 255 && $tsv{'experimentId'}[$line] !~ /\s/ ){
# Initialize the hash for this expId
$experiments{$experimentId}->{'name'} = '';
$experiments{$experimentId}->{'description'} = '';
$experiments{$experimentId}->{'source'} = '';
$experiments{$experimentId}->{'status'} = '';
$experiments{$experimentId}->{'commented'} = '';
}
else {
warn "Badly formatted RNAseqExperiment file at line [$line]: [invalid experimentId]\n";
next;
}
if ($tsv{'experimentName'}[$line] eq '') {
warn "experiment Name is emtpy for $experimentId";
}
if ($tsv{'experimentDescription'}[$line] eq '') {
warn "experiment description is emtpy for $experimentId";
}
if ($tsv{'experimentSource'}[$line] eq '') {
warn "experiment source is emtpy for $experimentId";
}
if ($tsv{'experimentStatus'}[$line] eq '') {
warn "experiment status is emtpy for $experimentId";
}
$experiments{$experimentId}->{'name'} = $tsv{'experimentName'}[$line];
$experiments{$experimentId}->{'description'} = $tsv{'experimentDescription'}[$line];
$experiments{$experimentId}->{'source'} = $tsv{'experimentSource'}[$line];
$experiments{$experimentId}->{'status'} = $tsv{'experimentStatus'}[$line];
$experiments{$experimentId}->{'commented'} = $commented;
}
return %experiments;
}
## TODO remove? This is the old format for RNASeqExperiment.tsv
sub getAllAnnotatedExperiments {
my ($rnaSeqExpAnnotationFile) = @_;
# $experiments{experimentId}->{'name'}
# $experiments{experimentId}->{'description'}
# $experiments{experimentId}->{'source'}
# $experiments{experimentId}->{'status'}
my %experiments = ();
my $lineCount = 0;
my $totalLineCount = 0;
my $error = '';
my $expId = undef;
for my $line ( read_file($rnaSeqExpAnnotationFile, chomp => 1) ){
# file format:
# first line: experimentId\texpId
# second line: experimentName\texpName
# third line: experimentDescription\tdescr.
# fourth line: experimentSource\tsource
# fifth line: experimentStatus\status
# 6th line: either optional COMMENT, or experiment separator //
my @tmp;
($tmp[0], $tmp[1]) = map { bgeeTrim($_) } map { s/^\"//; s/\"$//; $_ } split(/\t/, $line);
# first line describing an experiment
if ( $lineCount == 0 && !defined $expId ){
if ( $tmp[0] eq 'experimentId' && length($tmp[1]) < 255 && $tmp[1] !~ /\s/ ){
$expId = $tmp[1];
# Initialize the hash for this expId
$experiments{$expId}->{'name'} = '';
$experiments{$expId}->{'description'} = '';
$experiments{$expId}->{'source'} = '';
$experiments{$expId}->{'status'} = '';
}
else {
$error = 'missing experimentId';
}
}
elsif ( $lineCount == 1 && defined $expId ){
if ( $tmp[0] eq 'experimentName' ){
$experiments{$expId}->{'name'} = $tmp[1];
}
else {
$error = 'missing experimentName';
}
}
elsif ( $lineCount == 2 && defined $expId ){
if ( $tmp[0] eq 'experimentDescription' ){
$experiments{$expId}->{'description'} = $tmp[1];
}
else {
$error = 'missing experimentDescription';
}
}
elsif ( $lineCount == 3 && defined $expId ){
if ( $tmp[0] eq 'experimentSource' ){
$experiments{$expId}->{'source'} = $tmp[1];
}
else {
$error = 'missing experimentSource';
}
}
elsif ( $lineCount == 4 && defined $expId ){
if ( $tmp[0] eq 'experimentStatus' ){
$experiments{$expId}->{'status'} = $tmp[1];
}
else {
$error = 'missing experimentStatus';
}
} elsif ( $lineCount == 5 && defined $expId ){
if ( $tmp[0] eq 'COMMENT' ){
# optionnal comment line, everything's OK
# $line-- to match "//" during next iteration
$lineCount--;
}
elsif ( $line =~ /^\/\/\s*$/ ){
#everything's OK
}
else {
$error = 'missing COMMENT or end of experiment //';
}
}
else {
warn "Error, no experiment ID already defined when reaching line [$totalLineCount]\n";
}
$totalLineCount++;
if ( $error ne '' ){
warn "Badly formatted rnaSeaExperiment file at line [$totalLineCount]: [$error]\n";
$error = '';
}
if ( $line =~ /^\/\/\s*$/ ){
$expId = undef;
$lineCount = -1;
$error = '';
}
$lineCount++;
}
return %experiments;
}
## Problem with this: the # at beginning of lines marks header by also commented libraries
sub getAllRnaSeqAnnotations2 {
my ($TSV) = @_;
my %tsv = %$TSV;
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'platform'} = platform
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'anatId'} = anatId
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'stageId'} = stageId
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'sex'} = sex
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'strain'} = strain
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'commented'} = commented
my %rnaSeqAnnotations;
for my $line ( 0..$#{$tsv{'libraryId'}} ){
my $commented = 0;
if ( ($tsv{'libraryId'}[$line] =~ /^#(.+)/) or ($tsv{'libraryId'}[$line] =~ /^\"#(.+)/)){
$tsv{'libraryId'}[$line] = $1;
$commented = 1;
}
# file format: libraryId experimentId platform ... organId stageId
# #libraryId experimentId platform organId organName anatId anatName stageId stageName infoOrgan infoStage libraryTitle librarySource libraryDescription libraryCharacteristics organAnnotationStatus organBiologicalStatus stageAnnotationStatus stageBiologicalStatus sex strain speciesId comment annotatorId lastModificationDate
# file format: libraryId experimentId platform ... organId stageId
my $libraryId = $tsv{'libraryId'}[$line];
my $experimentId = $tsv{'experimentId'}[$line];
my $platform = $tsv{'platform'}[$line];
my $anatId = $tsv{'anatId'}[$line];
my $stageId = $tsv{'stageId'}[$line];
my $sex = $tsv{'sex'}[$line];
my $strain = $tsv{'strain'}[$line];
my $speciesId = $tsv{'speciesId'}[$line];
if ( !defined $rnaSeqAnnotations{$experimentId}->{$libraryId} ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'commented'} = $commented;
# platform
if ( $platform ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'platform'} = $platform;
}
else {
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'platform'} = '';
warn "Warning: no platform specified for [$experimentId--$libraryId]. Commented: $commented\n";
}
# anatId
if ( $anatId ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'anatId'} = $anatId;
}
else {
warn "Warning: no anatId specified for [$experimentId--$libraryId]. Commented: $commented\n";
}
# stageId
if ( $stageId ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'stageId'} = $stageId;
}
else {
warn "Warning: no stageId specified for [$experimentId--$libraryId]. Commented: $commented\n";
}
# sex
if ( $sex ne '' ){
# Normalize sex info
my $norm_sex = $sex eq 'F' ? 'female'
: $sex eq 'M' ? 'male'
: $sex eq 'H' ? 'hermaphrodite'
: $sex eq 'U' ? 'not annotated'
: $sex eq 'mixed' ? 'mixed'
: $sex =~ /^[Mm]ixed/ ? 'mixed' # Mixed 1:1
: $sex eq 'NA' ? 'NA'
: 'NA';
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'sex'} = $norm_sex;
}
else {
warn "Warning: no sex specified for [$experimentId--$libraryId]. Commented: $commented\n";
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'sex'} = 'NA';
}
# strain
if ( $strain ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'strain'} = $strain;
}
else {
warn "Warning: no strain specified for [$experimentId--$libraryId]. Commented: $commented\n";
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'strain'} = 'NA';
}
# species
if ( $speciesId ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'speciesId'} = $speciesId;
}
else {
warn "Warning: no species specified for [$experimentId--$libraryId]. Commented: $commented\n";
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'speciesId'} = 'NA';
}
}
else {
warn 'Warning: library present several times in the annotation file: experiment: ',
$experimentId, ' - library: ', $libraryId, ". Commented: $commented\n";
}
}
return %rnaSeqAnnotations;
}
## TODO remove? This doesn't take care of removing the quotes
sub getAllRnaSeqAnnotations {
my ($rnaSeqAnnotationFile) = @_;
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'platform'} = platform
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'anatId'} = anatId
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'stageId'} = stageId
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'sex'} = sex
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'strain'} = strain
# $rnaSeqAnnotations{expId}->{libraryId (SRXxxx)}->{'commented'} = commented
my %rnaSeqAnnotations;
open(my $IN, '<', $rnaSeqAnnotationFile) or die "Could not read file [$rnaSeqAnnotationFile]\n";
my $line = <$IN>; #header
while ( defined ($line = <$IN>) ){
chomp $line;
my $commented = 0;
if ( ($line =~ /^#(.+)/) or ($line =~ /^\"#(.+)/) ){
$line = $1;
$commented = 1;
}
# file format:
# #libraryId experimentId platform organId organName anatId uberonName stageId stageName infoOrgan infoStage sampleTitle sampleSource sampleDescription sampleCharacteristics organAnnotationStatus organBiologicalStatus stageAnnotationStatus stageBiologicalStatus sex strain speciesId comment annotatorId lastModificationDate replicate infoReplicate SRSId tags RNASeqProtocol physiological status globin_reduction PATOid PATOname
my @tmp = map { bgeeTrim($_) }
map { s/^\"//; s/\"$//; $_ } # remove quotes
split(/\t/, $line);
my $libraryId = $tmp[0];
my $experimentId = $tmp[1];
my $platform = $tmp[2];
my $anatId = $tmp[4];
my $stageId = $tmp[6];
my $freeTextOrgan = $tmp[9];
my $freeTextStage = $tmp[10];
my $sex = $tmp[14];
my $strain = $tmp[15];
my $genotype = $tmp[16];
my $speciesId = $tmp[17];
my $protocol = $tmp[18];
my $protocolType = $tmp[19];
my $popCapture = $tmp[20];
my $physiologicalStatus = $tmp[31];
if ( !defined $rnaSeqAnnotations{$experimentId}->{$libraryId} ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'commented'} = $commented;
# platform
if ( $platform ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'platform'} = $platform;
}
else {
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'platform'} = '';
warn "Warning: no platform specified for [$experimentId--$libraryId]. Commented: $commented\n";
}
# anatId
if ( $anatId ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'anatId'} = $anatId;
}
else {
warn "Warning: no anatId specified for [$experimentId--$libraryId]. Commented: $commented\n";
}
# stageId
if ( $stageId ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'stageId'} = $stageId;
}
else {
warn "Warning: no stageId specified for [$experimentId--$libraryId]. Commented: $commented\n";
}
# free text author annotation. no warning throws if empty
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'freeTextOrgan'} = $freeTextOrgan;
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'freeTextStage'} = $freeTextStage;
# sex
if ( $sex ne '' ){
# Normalize sex info
my $norm_sex = $sex eq 'F' ? 'female'
: $sex eq 'M' ? 'male'
: $sex eq 'H' ? 'hermaphrodite'
: $sex eq 'U' ? 'not annotated'
: $sex eq 'mixed' ? 'mixed'
: $sex =~ /^[Mm]ixed/ ? 'mixed' # Mixed 1:1
: $sex eq 'NA' ? 'NA'
: 'NA';
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'sex'} = $norm_sex;
}
else {
warn "Warning: no sex specified for [$experimentId--$libraryId]. Commented: $commented\n";
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'sex'} = 'NA';
}
# strain
if ( $strain ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'strain'} = $strain;
}
else {
warn "Warning: no strain specified for [$experimentId--$libraryId]. Commented: $commented\n";
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'strain'} = 'NA';
}
# genotype information. Do not throw a warning if empty
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'genotype'} = $genotype;
# species
if ( $speciesId ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'speciesId'} = $speciesId;
}
else {
warn "Warning: no species specified for [$experimentId--$libraryId]. Commented: $commented\n";
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'speciesId'} = 'NA';
}
# protocol and protocol type are often empty. Do not throw a warning if empty
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'protocol'} = $protocol;
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'protocolType'} = $protocolType;
# population capture
if ( !defined($popCapture) || $popCapture ne '' ){
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'populationCapture'} = $popCapture;
}
else {
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'populationCapture'} = '';
warn "Warning: no RNA population captured specified for [$experimentId--$libraryId]. Commented: $commented\n";
}
$rnaSeqAnnotations{$experimentId}->{$libraryId}->{'physiologicalStatus'} = $physiologicalStatus;
}
else {
warn 'Warning: library present several times in the annotation file: experiment: ',
$experimentId, ' - library: ', $libraryId, ". Commented: $commented\n";
}
}
close $IN;
return %rnaSeqAnnotations;
}
# Retrieve information on libraries
sub getAllRnaSeqLibrariesInfo {
# Updated to be compatible with new Bgee v15 RNA-seq pipeline. This is reading rna_seq_sample_info.txt file
my ($rnaSeqLibraryFile) = @_;
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'speciesId'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'organism'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'genomeFilePath'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'database'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'platform'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'libraryType'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'libraryInfo'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'readLength'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'runIds'}->{$runId} = ()
my @valid_platforms = ('Illumina', 'NextSeq', 'HiSeq X Ten');
my %rnaSeqLibrary;
for my $line ( read_file("$rnaSeqLibraryFile", chomp=>1) ){
next if ( $line =~ /^#/ or $line =~ /^\"#/ );
#libraryId experimentId speciesId organism genomeFilePath database platform libraryType libraryInfo readLength runIds
my @tmp = map { bgeeTrim($_) } map { s/^\"//; s/\"$//; $_ } split(/\t/, $line);
my $experimentId = $tmp[1];
my $libraryId = $tmp[0];
my $speciesId = $tmp[2];
my $organism = $tmp[3];
my $genomeFilePath = $tmp[4];
my $database = $tmp[5];
my $platform = $tmp[6];
my $libraryType = $tmp[7];
my $libraryInfo = $tmp[8];
my $readLength = $tmp[9];
my $runIds = $tmp[10];
die "tsv field number problem [$line]\n" if ( scalar @tmp != 11 );
if ( !defined $rnaSeqLibrary{$experimentId}->{$libraryId} ){
# Perform format checks
my $discarded = 0;
if ( $experimentId eq '' ){
warn "Warning, wrong format for experimentId [$experimentId]\n";
$discarded = 1;
}
if ( $libraryId eq '' ){
warn "Warning, wrong format for libraryId [$libraryId]\n";
$discarded = 1;
}
if ( $speciesId eq '' ){
warn "Warning, wrong format for speciesId [$speciesId]\n";
$discarded = 1;
}
if ( $organism eq '' ){
warn "Warning, wrong format for organism [$organism]\n";
$discarded = 1;
}
if ( $genomeFilePath eq '' ){
warn "Warning, wrong format for genomeFilePath [$genomeFilePath]\n";
$discarded = 1;
}
if ( $database ne 'Ensembl' && $database ne 'EnsemblMetazoa' && $database ne 'RefSeq'){
warn "Warning, wrong format for database [$database]\n";
$discarded = 1;
}
if ( $platform eq '' || all { $platform !~ /^$_/ } @valid_platforms ){
warn "Warning, wrong format for platform [$platform]\n";
$discarded = 1;
}
if ( $libraryType ne 'SINGLE' && $libraryType ne 'PAIRED' ){
warn "Warning, wrong format for libraryType [$libraryType]\n";
$discarded = 1;
}
#NOTE SRA may return float for read length because of read length variability
if ( $readLength ne '' && ($readLength !~ /$floatingPointRegex/ || $readLength < 0 ) ){
warn "Warning, wrong format for readLength [$readLength]\n";
$discarded = 1;
}
my @runs = split(/,/, $runIds);
my %runs;
foreach my $runId ( @runs ){
if ( $runId eq '' ){
warn "Warning, wrong format for one runId [$runId]\n";
$discarded = 1;
}
$runs{$runId} = ();
}
if ( $discarded ){
warn ' for experiment: ', $experimentId, ' - sample: ', $libraryId, ", sample discarded!\n";
}
else {
$rnaSeqLibrary{$experimentId}->{$libraryId}->{'speciesId'} = $speciesId;
$rnaSeqLibrary{$experimentId}->{$libraryId}->{'organism'} = $organism;
$rnaSeqLibrary{$experimentId}->{$libraryId}->{'genomeFilePath'} = $genomeFilePath;
$rnaSeqLibrary{$experimentId}->{$libraryId}->{'database'} = $database;
$rnaSeqLibrary{$experimentId}->{$libraryId}->{'platform'} = $platform;
$rnaSeqLibrary{$experimentId}->{$libraryId}->{'libraryType'} = lc($libraryType);
$rnaSeqLibrary{$experimentId}->{$libraryId}->{'libraryInfo'} = $libraryInfo;
$rnaSeqLibrary{$experimentId}->{$libraryId}->{'readLength'} = $readLength;
foreach my $runId ( keys %runs ){
$rnaSeqLibrary{$experimentId}->{$libraryId}->{'runIds'}->{$runId} = ();
}
}
}
else {
warn 'Warning: sample present several times in the library file: experiment: ', $experimentId, ' - sample: ', $libraryId, "\n";
}
}
return %rnaSeqLibrary;
}
sub getExcludedLibraries {
# record libraries excluded because of low mapping quality or problem during mapping
my ($excludedLibraryFile) = @_;
my %excludedLibraries;
for my $line ( read_file("$excludedLibraryFile", chomp=>1) ){
next if ( $line =~ /^#/ or $line =~ /^\"#/ or $line =~ /^$/ );
my @tmp = map { bgeeTrim($_) } map { s/^\"//; s/\"$//; $_ } split(/\t/, $line);
if ( $tmp[1] eq 'TRUE' ){
$excludedLibraries{$tmp[0]} = $tmp[2];
}
}
return %excludedLibraries;
}
## function to read cutoff and percent present infos
sub getAllRnaSeqLibrariesStats {
# Updated to be compatible with new Bgee v15 RNA-seq pipeline. This is reading presence_absence_all_samples.txt file
my ($rnaSeqLibraryFile) = @_;
my %rnaSeqLibraries;
for my $line ( read_file("$rnaSeqLibraryFile", chomp=>1) ){
next if ( $line =~ /^#/ or $line =~ /^\"#/ or $line =~ /^libraryId/);
#libraryId cutoffTPM proportionGenicPresent numberGenicPresent numberGenic proportionCodingPresent numberPresentCoding numberCoding proportionIntergenicPresent numberIntergenicPresent numberIntergenic pValueCutoff meanIntergenic sdIntergenic speciesId
my @tmp = map { bgeeTrim($_) } map { s/^\"//; s/\"$//; $_ } split(/\t/, $line);
my $libraryId = $tmp[0];
my $cutoffTPM = $tmp[1];
my $allGenesPercentPresent = $tmp[2];
my $proteinCodingPercentPresent = $tmp[5];
my $intergenicRegionsPercentPresent = $tmp[8];
my $pValueThreshold = $tmp[11];
my $meanIntergenic = $tmp[12];
my $sdIntergenic = $tmp[13];
my $speciesId = $tmp[14];
die "tsv field number problem [$line]\n" if ( scalar @tmp != 15 );
if ( !defined $rnaSeqLibraries{$libraryId} ){
# Perform format checks
my $discarded = 0;
if ( $libraryId eq '' ){
warn "Warning, wrong format for libraryId [$libraryId]\n";
$discarded = 1;
}
if ( $cutoffTPM !~ /$floatingPointRegex/ ){
warn "Warning, wrong format for cutoffTPM [$cutoffTPM]\n";
$discarded = 1;
}
if ( $allGenesPercentPresent !~ /$floatingPointRegex/ || $allGenesPercentPresent < 0 || $allGenesPercentPresent > 100 ){
warn "Warning, wrong format for allGenesPercentPresent [$allGenesPercentPresent]\n";
$discarded = 1;
}
if ( $proteinCodingPercentPresent !~ /$floatingPointRegex/ || $proteinCodingPercentPresent < 0 || $proteinCodingPercentPresent > 100 ){
warn "Warning, wrong format for proteinCodingPercentPresent [$proteinCodingPercentPresent]\n";
$discarded = 1;
}
if ( $intergenicRegionsPercentPresent !~ /$floatingPointRegex/ || $intergenicRegionsPercentPresent < 0 || $intergenicRegionsPercentPresent > 100 ){
warn "Warning, wrong format for intergenicRegionsPercentPresent [$intergenicRegionsPercentPresent]\n";
$discarded = 1;
}
if ( $pValueThreshold !~ /$floatingPointRegex/ || $pValueThreshold < 0 || $pValueThreshold > 1 ){
warn "Warning, wrong format for pValueThreshold [$pValueThreshold]\n";
$discarded = 1;
}
if ( $meanIntergenic !~ /$floatingPointRegex/ || $meanIntergenic < 0){
warn "Warning, wrong format for meanIntergenic [$meanIntergenic]\n";
$discarded = 1;
}
if ( $sdIntergenic !~ /$floatingPointRegex/ || $sdIntergenic < 0){
warn "Warning, wrong format for sdIntergenic [$sdIntergenic]\n";
$discarded = 1;
}
if ( $speciesId eq '' ){
warn "Warning, wrong format for speciesId [$speciesId]\n";
$discarded = 1;
}
if ( $discarded ){
warn 'Sample ', $libraryId, " discarded!\n";
}
else {
$rnaSeqLibraries{$libraryId}->{'cutoffTPM'} = $cutoffTPM;
$rnaSeqLibraries{$libraryId}->{'allGenesPercentPresent'} = $allGenesPercentPresent;
$rnaSeqLibraries{$libraryId}->{'proteinCodingPercentPresent'} = $proteinCodingPercentPresent;
$rnaSeqLibraries{$libraryId}->{'intergenicRegionsPercentPresent'} = $intergenicRegionsPercentPresent;
$rnaSeqLibraries{$libraryId}->{'pValueThreshold'} = $pValueThreshold;
$rnaSeqLibraries{$libraryId}->{'meanIntergenic'} = $meanIntergenic;
$rnaSeqLibraries{$libraryId}->{'sdIntergenic'} = $sdIntergenic;
$rnaSeqLibraries{$libraryId}->{'speciesId'} = $speciesId;
}
}
else {
warn 'Warning: sample present several times in the file: ', $libraryId, "\n";
}
}
return %rnaSeqLibraries;
}
## function to read percent mapping and read length infos
sub getAllRnaSeqReportInfo {
my ($rnaSeqReportFile) = @_;
my %rnaSeqLibraries;
for my $line ( read_file("$rnaSeqReportFile", chomp=>1) ){
next if ( $line =~ /^#/ or $line =~ /^\"#/ or $line =~ /^libraryId/); # this includes the header
my @tmp = map { bgeeTrim($_) } map { s/^\"//; s/\"$//; $_ } split(/\t/, $line);
# columns: libraryId reads min_read_size max_read_size number_aligned number_unique_aligned prop_aligned prop_unique_aligned number_targets start_time kallisto_version
my $libraryId = $tmp[0];
my $allReadsCount = $tmp[1];
#transform from potential scientific notation to integer
if ( $allReadsCount =~ /e[+|-][0-9]+$/ ) {
$allReadsCount = sprintf("%.0f", $tmp[1]);
}
my $minReadLength = $tmp[2];
my $maxReadLength = $tmp[3];
my $mappedReadsCount = $tmp[4];
#transform from potential scientific notation to integer
if ( $allReadsCount =~ /^[0-9]+e[+|-][0-9]+$/ ) {
$allReadsCount = sprintf("%.0f", $tmp[4]);
}
die "tsv field number problem [$line]\n" if ( scalar @tmp != 11 );
if ( !defined $rnaSeqLibraries{$libraryId} ){
# Perform format checks
my $discarded = 0;
if ( $libraryId eq '' ){
warn "Warning, wrong format for libraryId [$libraryId]\n";
$discarded = 1;
}
if ( $allReadsCount !~ /$positiveIntegerRegex/ and $allReadsCount ne 'NULL' ){
warn "Warning, wrong format for allReadsCount [$allReadsCount]\n";
$discarded = 1;
}
if ( $mappedReadsCount !~ /$positiveIntegerRegex/ and $mappedReadsCount ne 'NULL' ){
warn "Warning, wrong format for mappedReadsCount [$mappedReadsCount]\n";
$discarded = 1;
}
if ( $minReadLength !~ /$positiveIntegerRegex/ and $minReadLength ne 'NULL' ){
warn "Warning, wrong format for minReadLength [$minReadLength]\n";
$discarded = 1;
}
if ( $maxReadLength !~ /$positiveIntegerRegex/ and $maxReadLength ne 'NULL' ){
warn "Warning, wrong format for maxReadLength [$maxReadLength]\n";
$discarded = 1;
}
if ( $discarded ){
warn 'Sample ', $libraryId, " discarded!\n";
}
else {
$rnaSeqLibraries{$libraryId}->{'allReadsCount'} = $allReadsCount;
$rnaSeqLibraries{$libraryId}->{'mappedReadsCount'} = $mappedReadsCount;
if ( $minReadLength eq 'NULL' ){
$rnaSeqLibraries{$libraryId}->{'minReadLength'} = 0;
}
else {
$rnaSeqLibraries{$libraryId}->{'minReadLength'} = $minReadLength;
}
if ( $maxReadLength eq 'NULL' ){
$rnaSeqLibraries{$libraryId}->{'maxReadLength'} = 0;
}
else {
$rnaSeqLibraries{$libraryId}->{'maxReadLength'} = $maxReadLength;
}
}
}
else {
warn 'Warning: sample present several times in the file: ', $libraryId, "\n";
}
}
return %rnaSeqLibraries;
}
# Retrieve for a sample the expression calls, TPM values for all genes
sub getGenesResults {
my ($sampleExpCallsFile) = @_;
# $expressionCalls{geneId}->{'estimatedCount'} = ...
# $expressionCalls{geneId}->{'FPKM'} = ...
# $expressionCalls{geneId}->{'TPM'} = ...
# $expressionCalls{$geneId}->{'zscore'} = ...
# $expressionCalls{$geneId}->{'pValue'} = ...
# $expressionCalls{geneId}->{'biotype'} = ...
# $expressionCalls{geneId}->{'expressionCall'} = ...
my %expressionCalls;
open(my $IN, '<', $sampleExpCallsFile) or die "Could not read file [$sampleExpCallsFile]\n";
my $line = <$IN>; #header
while ( defined ($line = <$IN>) ){
chomp $line;
# file format: geneId, tpm, counts, length, biotype, type, zscore, pvalue, expression call, fpkm
my @tmp = map { bgeeTrim($_) } map { s/^\"//; s/\"$//; $_ } split(/\t/, $line);
my $geneId = $tmp[0];
my $TPM = $tmp[1];
my $estimatedCount = $tmp[2];
my $biotype = $tmp[4];
my $zscore = $tmp[6];
my $pValue = $tmp[7];
my $expressionCall = $tmp[8];
if ( !defined $expressionCalls{$geneId} ){
# Perform format checks
my $discarded = 0;
if ( $geneId eq '' || $geneId =~ /\s/ ){
warn "Warning, wrong format for geneId [$geneId]\n";
$discarded = 1;
}
if ( $TPM !~ /$floatingPointRegex/ || $TPM < 0 || $TPM > 1000000){
warn "Warning, wrong format for TPM [$TPM]\n";
$discarded = 1;
}
if ( $estimatedCount !~ /$floatingPointRegex/ || $estimatedCount < 0 ){
warn "Warning, wrong format for estimatedCount [$estimatedCount]\n";
$discarded = 1;
}
if ( $biotype eq '' ){
warn "Warning, wrong format for biotype [$biotype]\n";
$discarded = 1;
}
if ( !($zscore =~ /$floatingPointRegex/ || $zscore eq 'NA') ){
warn "Warning, wrong format for zscore [$zscore]\n";
$discarded = 1;
}
if ( !( $pValue eq 'NA' || ($pValue =~ /$floatingPointRegex/ && $pValue >= 0 && $pValue <= 1) ) ){
warn "Warning, wrong format for pValue [$pValue]\n";
$discarded = 1;
}
if ( $expressionCall ne 'absent' && $expressionCall ne 'present' ){
warn "Warning, wrong format for expressionCall [$expressionCall]\n";
$discarded = 1;
}
if ( $discarded ){
warn ' for gene: ', $geneId, "\n";
}
else {
$expressionCalls{$geneId}->{'TPM'} = $TPM;
$expressionCalls{$geneId}->{'estimatedCount'} = $estimatedCount;
$expressionCalls{$geneId}->{'biotype'} = $biotype;
$expressionCalls{$geneId}->{'zscore'} = $zscore;
$expressionCalls{$geneId}->{'pValue'} = $pValue;
$expressionCalls{$geneId}->{'expressionCall'} = $expressionCall;
}
}
else {
warn "Warning: expression call defined several times for geneId [$geneId]\n";
}
}
close $IN;
return %expressionCalls;
}
## TODO keep this?
# Julien Roux, Oct 2015
# Getting feature length used for FPKM calculation
# We retrieve feature length for genes of any species and any biotype
sub getFeatureLength {
my ($featureLengthFolder, $ensRelease) = @_;
my %featureLength;
for my $file ( glob($featureLengthFolder.'/*'.$ensRelease.'.gtf_all_length') ){
$file = basename($file);
open(my $IN, '<', $featureLengthFolder.'/'.$file) or die "Could not read file [$file]\n";
while ( defined (my $line = <$IN>) ) {
chomp $line;
# file format: geneId, featureLength, biotype
my @tmp = map { bgeeTrim($_) } map { s/^\"//; s/\"$//; $_ } split(/\t/, $line);
my $geneId = $tmp[0];
my $featureLength = $tmp[1];
my $biotype = $tmp[2];
$featureLength{$geneId}->{'featureLength'} = $featureLength;
$featureLength{$geneId}->{'biotype'} = $biotype;
}
close $IN;
}
return %featureLength;
}
# Julien Wollbrett, Feb. 2021
# Read file containing mapping between RNA-Seq protocols and gene biotypes
# for which absent calls does not have to be created
# return a hash with a protocol name as key and an array of biotypes as value
sub retrieveProtocolsToBiotypeExcludeAbsentCalls {
my %protocolToBiotypes = ();
my ($mappingProtocolToBiotypesFile) = @_;
for my $line ( read_file("$mappingProtocolToBiotypesFile", chomp=>1) ){
#skip the header
next if ( $line =~ /^RNASeqProtocol/ or $line =~ /^\"RNASeqProtocol/ );
# RNASeqProtocol biotypes_excluded_for_absent_calls
my @columns = map { bgeeTrim($_) } map { s/^\"//; s/\"$//; $_ } split(/\t/, $line);
my @biotypes = split(/,/, $columns[1]);
$protocolToBiotypes{$columns[0]} = \@biotypes;
}
return %protocolToBiotypes;
}
######################################################################################################
######################################################################################################
################################### FULL LENGTH scRNASEQ FUNCTIONS ###################################
######################################################################################################
######################################################################################################
# TODO: homogenize columns of sample_info file from bulk and single cell in order to get libraries info from the same function
# Retrieve information on libraries of full length RNASeq
sub getAllFullLengthScRnaSeqLibrariesInfo {
my ($fullLengthScRnaSeqLibraryFile) = @_;
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'speciesId'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'organism'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'platform'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'libraryType'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'libraryInfo'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'readLength'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'anatId'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'stageId'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'cellTypeId'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'sex'} = ...
# $rnaSeqLibrary{experimentId}->{libraryId (SRX...)}->{'strain'} = ...
my @valid_platforms = ('Illumina HiSeq 2500', 'Illumina HiSeq 2000', 'Illumina MiSeq', 'NextSeq 500');
my @valid_protocols = ('Adapted_SMART_seq2', 'Fluidigm C1 instrument and Nextera XT protocol',
'NEBNext® Ultra™ DNA Library Prep Kit for Illumina®', 'SMARTer Ultra Low', 'SMART_seq', 'SMART_seq2');
my %fullLengthScRnaSeqLibrary;
for my $line ( read_file("$fullLengthScRnaSeqLibraryFile", chomp=>1) ){
next if ( $line =~ /^#/ or $line =~ /^libraryId/ );
#libraryId experimentId cellTypeName cellTypeId speciesId platform protocol protocolType libraryType infoOrgan stageId uberonId sex strain readLength organism
my @tmp = map { bgeeTrim($_) } map { s/^\"//; s/\"$//; $_ } split(/\t/, $line);
my $libraryId = $tmp[0];
my $experimentId = $tmp[1];
my $cellTypeId = $tmp[3];
my $speciesId = $tmp[4];
my $platform = $tmp[5];
my $protocol = $tmp[6];
my $libraryType = $tmp[8];
my $stageId = $tmp[10];
my $uberonId = $tmp[11];
my $sex = $tmp[12];
my $strain = $tmp[13];
my $readLength = $tmp[14];
my $organism = $tmp[15];
my $genotype = $tmp[16];
#TODO: change the annotation to fit authorized sex values in the DB ('not annotated','hermaphrodite','female','male','mixed','NA')
# it is ugly to have to manually modify the values in this script
if($sex eq "(Missing)") {
$sex = $Utils::NA_SEX;
}
if($sex eq "M") {
$sex = $Utils::MALE_SEX;
}
die "tsv field number problem [$line]\n" if ( scalar @tmp != 16 );
if ( !defined $fullLengthScRnaSeqLibrary{$experimentId}->{$libraryId} ){
# Perform format checks
my $discarded = 0;
if ( $libraryId eq '' ){
warn "Warning, wrong format for libraryId [$libraryId]\n";
$discarded = 1;
}
if ( $experimentId eq '' ){
warn "Warning, wrong format for experimentId [$experimentId]\n";
$discarded = 1;
}
if ( $speciesId eq '' ){
warn "Warning, wrong format for speciesId [$speciesId]\n";
$discarded = 1;
}
if ( $platform eq '' || all { $platform !~ /^$_/ } @valid_platforms ){
warn "Warning, wrong format for platform [$platform]\n";
$discarded = 1;
}
if ( $protocol eq '' || all { $protocol !~ /^$_/ } @valid_protocols ){
warn "Warning, wrong format for protocol [$protocol]\n";
$discarded = 1;
}
if ( $libraryType ne 'SINGLE' && $libraryType ne 'PAIRED' ){
warn "Warning, wrong format for libraryType [$libraryType]\n";
$discarded = 1;
}
#NOTE SRA may return float for read length because of read length variability
if ( $readLength ne '' && ($readLength !~ /$floatingPointRegex/ || $readLength < 0 ) ){
warn "Warning, wrong format for readLength [$readLength]\n";
$discarded = 1;
}
if ( $organism eq '' ){
warn "Warning, wrong format for organism [$organism]\n";
$discarded = 1;