-
Notifications
You must be signed in to change notification settings - Fork 1
/
targetscan_70_context_scores.pl
executable file
·2227 lines (1879 loc) · 66.9 KB
/
targetscan_70_context_scores.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;
#######################################################################
#
# Copyright(c) 2007-2016 Whitehead Institute for Biomedical Research.
# All Rights Reserved
#
# Author: Joe Rodriguez, Robin Ge, Kim Walker, and George W. Bell
# Bioinformatics and Research Computing
# wibr-bioinformatics@wi.mit.edu
#
# Comment: This program calculates TargetScan context scores.
# It produces output as displayed in TargetScan Release 7.0
#
# This code is available from http://www.targetscan.org/cgi-bin/targetscan/data_download.cgi?db=vert_70
#
# Version: 5.3 (October 2010)
# - Correct bugs in 5.0: AU score (A1 for 8mer; <30nt denominator; <30nt $utrUp)
# - Add code for three site types
# Nevertheless, we need this code to produce miRNA/UTR alignments
#
# Version: 6.0 (July 2011)
# - Update to calculate context+ scores (Garcia et al.)
#
# Version: 7.0 (July 2014 - July 2015)
# - Update to calculate context++ scores (Agarwal et al.)
#
# Version: 7.01 (25 March 2016)
# - Update to correct errors in the calculations of the ORF 8mer and offset 6mer contributions.
#
#######################################################################
# Basic ideas:
#
# 1 - Get names of all input files:
# - predicted targets (output from targetscan_70.pl)
# - UTRs (used by targetscan_70.pl to predicted targets)
# - mature miRNAs with miRNA name, family name, seed region, species ID, and mature sequence
# 2 - Get mature miRNAs and UTRs into memory
# 3 - Read through predicted targets file, and for each site
# - get all miRNAs in that miRNA family and species [if there are none, we can't calculate context score]
# - extract short subsequence from UTR to use for predicted consequential pairing
# - do and score alignment
# - extract long subsequence from UTR and get local AU contribution
# - get position contribution
# - sum 4 contributions to get total context score
# - and print out these data
# 4 - Once all context scores have been calculated, go back and calculate context score percentiles.
# 5 - Print out all data, including percentile ranks, into final file
# Sample command
# ./targetscan_70_context_scores.pl test/input/miR_for_context_scores.sample.txt test/input/UTR_Sequences_sample.txt test/output/targetscan_70_output.BL_PCT.txt test/output/ORF_Sequences_sample.lengths.txt test/output/ORF_8mer_counts_sample.txt test/input/All_cell_lines.AIRs.txt Targets.BL_PCT.context_scores.txt
use warnings;
# Needed for percentile rank
use POSIX qw(floor ceil);
use FindBin;
our ($PAIRING_SCORE);
# Required!!! RNAplfold (from the ViennaRNA Package 2 -- http://www.tbi.univie.ac.at/RNA/documentation.html)
######################## Constants ########################
# Link human-readable site types to site type code
$siteTypeDescription2num{'7mer-1a'} = 1;
$siteTypeDescription2num{'7mer-m8'} = 2;
$siteTypeDescription2num{'8mer-1a'} = 3;
$siteTypeDescription2num{'6mer'} = 4;
$siteTypeNum2description{1} = "7mer-1a";
$siteTypeNum2description{2} = "7mer-m8";
$siteTypeNum2description{3} = "8mer-1a";
$siteTypeNum2description{4} = "6mer";
# Required file of TA and SPS values (from Garcia et al., Supp Data 5)
$TA_SPS_FILE = "$FindBin::RealBin/TA_SPS_by_seed_region.txt";
# Agarwal coefficients file
$AgarwalParamFile = "$FindBin::RealBin/Agarwal_2015_parameters.txt";
# Minimum distance to end of CDS. If site is closer, context scores are not calculated
$MIN_DIST_TO_CDS = 15;
$TOO_CLOSE_TO_CDS = "too_close";
$DESIRED_UTR_ALIGNMENT_LENGTH = 23;
# Number of digits after decimal point for context scores
$DIGITS_AFTER_DECIMAL = 3;
# Print a dot every this number of input lines (so we can see progress)
$DOT_EVERY_THIS_NUM_LINES = 50;
# Maximum values for each site type
$maxContextScore{1} = -0.01; # 7mer-1a
$maxContextScore{2} = -0.02; # 7mer-m8
$maxContextScore{3} = -0.03; # 8mer-1a
$maxContextScore{4} = 0; # 6mer
# Produce all contributions to context++ score? (0=no; 1=yes)
$PRINT_CS_CONTRIBUTIONS = 1;
# Should we set all AIRs to 1 [1] or leave them as they are [0]?
$setAIRs_to_1 = 0;
# AIRs of 0 (even due to rounding errors) cause problems so set a min value
$minAIR = 0.0001;
# Directory with RNAplfold input and output
# Needs to contain one dir per species ID, with RNAplfold for that species' UTRs inside
$RNAplfold_IN_OUT = "RNAplfold_in_out";
# Reference species (of which affected isoform ratios (AIRs) were determined
$REF_SPECIES = 9606;
# Only calculate context scores for these species
@SPECIES = qw(10090 10116 13616 8364 9031 9544 9598 9606 9615 9913);
######################## Beginning of real code ########################
getUsage();
getFileFormats();
checkArguments();
# Read coefficients from Agarwal et al. Supp Table 3
readAgarwalParameters($AgarwalParamFile);
print STDERR "Using $REF_SPECIES as the reference species (as set by \$REF_SPECIES).\n";
foreach $species (@SPECIES)
{
$useSpecies{$species} = 1;
}
# Get mature miRNA data
readMiRNAs();
# Read file of aligned UTRs
our %utrLengthScalingFactor;
readUTRs();
# Get AIRs (UTR profiles)
readIsoformRatios($AIRsFile);
# Run RNAplfold for all UTRs
runRNAplfold_all_UTRs();
# Read files of other data
my %orf2length;
readORFdataFiles($orfLengthsFile, $orf8mersFile);
# Get mature miRNA data
read_TA_SPS($TA_SPS_FILE);
# Get a set of empty strings needed for correction of pairing
getReplaceLength(20);
# Open file for output
open (CONTEXT_SCORES_OUTPUT_TEMP, ">$contextScoresOutputTemp") || die "Cannot open $contextScoresOutputTemp for writing: $!";
if ($PRINT_CS_CONTRIBUTIONS)
{
$contextScoreFieldNum = 27; # 0-based
$percentileRankFieldNum = 28; # 0-based
$weightedContextScoreFieldNum = 30; # 0-based
$weightedPercentileRankFieldNum = 31; # 0-based
}
else
{
$contextScoreFieldNum = 6; # 0-based
$percentileRankFieldNum = 7; # 0-based
$weightedContextScoreFieldNum = 9; # 0-based
$weightedPercentileRankFieldNum = 10; # 0-based
}
# Read file of targets predicted by targetscan_70.pl
# and get context score(s) for sites line by line
readTargets();
# Read all context scores we calculated and get percentile ranks
readContextScoresFromFiles("$contextScoresOutputTemp");
getPercentileRanks();
# Read all data again, add percentile ranks, and print data to final file
addPercentileRanksToOtherData($contextScoresOutputTemp, $contextScoreFileOutput);
# Delete old RNAplfold files
unlink $contextScoresOutputTemp ;
print STDERR "Deleted temporary file ($contextScoresOutputTemp)\n";
# Report again if we're missing RNAplfold files
@missingRNAplfoldFiles = keys %missingRNAplfoldFile;
$numMissingRNAplfoldFiles = $#missingRNAplfoldFiles + 1;
if ($numMissingRNAplfoldFiles)
{
print STDERR "\n! Note that we couldn't run RNAplfold (or find the output files) for $numMissingRNAplfoldFiles sequences,\nso these will have no SA contributions!\n";
}
print STDERR "\nAll done! - See $contextScoreFileOutput for output.\n\n";
######################## Subroutines ########################
sub read_TA_SPS
{
my $TA_SPS_file = $_[0];
open (TA_SPS_FILE, $TA_SPS_file) || die "Cannot open file of TA and SPS values by seed ($TA_SPS_file): $!";
while (<TA_SPS_FILE>)
{
# Seed region SPS (8mer and 7mer-m8) SPS (7mer-1a) TA
# GAGGUAG -9.25 -6.72 3.393
chomp;
if ($. > 1) # Skip header line
{
my ($seedRegion, $SPS_1, $SPS_2, $TA) = split (/\t/, $_);
$garcia{3}{$seedRegion}{"SPS"} = $SPS_1;
$garcia{2}{$seedRegion}{"SPS"} = $SPS_1;
$garcia{1}{$seedRegion}{"SPS"} = $SPS_2;
$garcia{4}{$seedRegion}{"SPS"} = $SPS_2;
$garcia{$seedRegion}{"TA"} = $TA;
}
}
}
sub readTargets
{
# Read file with predicted targets and their BLs and PCTs, so
# two previous steps are required: target prediction, BL+PCT calculation
my $lineNum = 0;
print STDERR "Reading and processing targets file ($predictedTargetsFile)...";
open (PREDICTED_TARGETS, $predictedTargetsFile) || die "Cannot open $predictedTargetsFile for reading: $!";
while (<PREDICTED_TARGETS>)
{
# Gene_ID miRNA_family_ID species_ID MSA_start MSA_end UTR_start UTR_end Group_num Site_type miRNA in this species Group_type Branch length Pct Conserved
# ENST00000002829 CACAGUG 9913 1380 1399 674 681 18852 8mer-1a x 8mer-1a 1.23658845 0.4668 x
# ENST00000002829 CACAGUG 9925 1380 1399 670 677 18852 8mer-1a 8mer-1a 1.23658845 0.4668 x
# ENST00000001008 GAGGUAG 13616 49 76 31 37 7812 7mer-m8 x 7mer-m8 1.2446078 0.4806
# ENST00000000233 CAGCAGG 10029 19 25 18 24 958 7mer-m8 7mer-m8 5.14948759 0.4835 x
chomp;
my @f = split (/\t/, $_);
my $transcriptID = $f[0];
my $miRNA_familyID = $f[1];
my $speciesID = $f[2];
if ($useSpecies{$speciesID})
{
my $utrStart = $f[5]; # UTR start
my $utrEnd = $f[6]; # UTR end
my $groupNum = $f[7];
my $pct = $f[12];
# print "$transcriptID :: $miRNA_familyID :: $f[8]\n";
# Type of individual site (not always the same as the group type)
my $siteType = $siteTypeDescription2num{$f[8]};
my $familySpecies = "$miRNA_familyID\t$speciesID";
if ( $mature_seq{$familySpecies} )
{
###
### Get AIR (affected isoform ratio for this site)
###
$air = getAirThisSite($transcriptID, $speciesID, $utrEnd);
## If there are annotated miRNAs in this miRNA family in this species,
## extract piece of UTR that we'll need to align with the mature miRNA
my $subseqForAlignment = extractSubseqForAlignment($transcriptID, $miRNA_familyID, $speciesID, $utrStart, $utrEnd, $siteType);
###
### Extract long subsequence from UTR and get local AU contribution
###
my $localAUcontribution = getLocalAU_contribution($transcriptID, $speciesID, $siteType, $utrStart, $utrEnd);
###
### Get 3' UTR length contribution (new for TargetScan 8)
###
$len3UTR_contribution = get_len3UTR_weighted_contribution($transcriptID, $speciesID, $siteType, $utrStart, $utrEnd, $air);
###
### Get min_dist (shortest distance to stop codon or polyA site) contribution (new for TargetScan 8)
###
$minDist_contribution = getMinDist_weighted_contribution($transcriptID, $speciesID, $siteType, $utrStart, $utrEnd);
###
### Get site accessibility use ViennaRNA's RNAplfold (new for TargetScan 8)
###
$SA_contribution = getSA_contribution($transcriptID, $speciesID, $utrStart, $siteType);
###
### Get the length of this gene's ORF (new for TargetScan 8)
###
$orfLength_contribution = getORFlength_contribution($transcriptID, $speciesID, $siteType);
###
### Get the number of 8mers in this gene's ORF (new for TargetScan 8)
###
$orf8mer_contribution = getORF8mer_contribution($transcriptID, $miRNA_familyID, $speciesID, $siteType);
###
### Get the number of offset 6mers in this gene's UTR (new for TargetScan 8)
###
$offset6mer_contribution = getOffset6mer_weighted_contribution($transcriptID, $miRNA_familyID, $speciesID, $siteType, $utrEnd);
###
### Get the PCT contribution (for highly conserved miRNA families) (new for TargetScan 8)
### so PCT needs to have been calculated in a previous step
###
if ($pct eq "NA")
{ $PCT_contribution = 0; }
else
{
$PCT_contribution = getPCT_contribution($siteType, $pct);
}
##
## Get list of miRNAs for this miRNA family + species
##
##
## Get all miRNAs (if any) for this family and this species
##
for (my $i = 0; $i < $#{$mature_seq{$familySpecies}} + 1; $i++)
{
my $thisMiRNA = @{$mature_seq{$familySpecies}}[$i];
# print "$familySpecies ==> $thisMiRNA + UTR ($subseqForAlignment)\n";
my ($matureMiRNAid, $matureMiRNA) = split /\t/, $thisMiRNA;
# Modify $subseqForAlignment based on length of mature miRNA
my ($finalSubseqForAlignment, $matureMiRNAForAlignment) =
modifySubseqForAlignment($matureMiRNA, $matureMiRNAid, $subseqForAlignment, $siteType);
###
### Get sRNA1 and sRNA8 contributions (new for TargetScan 8)
###
($sRNA1A_contribution, $sRNA1C_contribution, $sRNA1G_contribution, $sRNA8A_contribution, $sRNA8C_contribution, $sRNA8G_contribution) = get_sRNA1_8_contributions($matureMiRNA, $siteType);
###
### Get Site8 contributions (new for TargetScan 8)
###
if ($siteType == 1 || $siteType == 4) # Only relevant for 7mer-1a and 6mer sites
{
($site8A_contribution, $site8C_contribution, $site8G_contribution) = getSite8_contribution($subseqForAlignment, $siteType);
}
else
{
$site8A_contribution = $site8C_contribution = $site8G_contribution = 0;
}
###
### Predict consequential pairing (alignment) and get 3' pairing contribution
###
my ($threePrimePairing_contribution, $alignedUTR, $alignmentBars, $alignedMatureMiRNA, $threePrimePairingScore) = get3primePairingContribution($siteType, $finalSubseqForAlignment, $matureMiRNAForAlignment);
# This site is too close to the CDS: don't take context scores for real
if ( $utrStart < $MIN_DIST_TO_CDS )
{
$threePrimePairing_contribution =
$localAUcontribution =
$totalcontextScore =
$TA_contribution =
$SPS_contribution =
$TOO_CLOSE_TO_CDS;
}
else
{
###
### Sum contributions to get total context score
###
# Get seed region for TA and SPS
$seedRegion = substr($matureMiRNA, 1, 7);
if ($garcia{$seedRegion}{"TA"} && $garcia{$siteType}{$seedRegion}{"SPS"})
{
$TA_contribution = getAgarwalContribution($siteType, "TA_3UTR", $garcia{$seedRegion}{"TA"});
$SPS_contribution = getAgarwalContribution($siteType, "SPS", $garcia{$siteType}{$seedRegion}{"SPS"});
}
else # Set these NAs to 0 for now
{
$TA_contribution = 0;
$SPS_contribution = 0;
}
$totalcontextScore =
$siteType2siteTypeContribution{$siteType} +
$threePrimePairing_contribution + $localAUcontribution + $minDist_contribution +
$sRNA1A_contribution + $sRNA1C_contribution + $sRNA1G_contribution +
$sRNA8A_contribution + $sRNA8C_contribution + $sRNA8G_contribution +
$site8A_contribution + $site8C_contribution + $site8G_contribution +
$len3UTR_contribution + $SA_contribution + $orfLength_contribution + $orf8mer_contribution + $offset6mer_contribution +
$TA_contribution + $SPS_contribution + $PCT_contribution;
$totalcontextScore = sprintf("%.${DIGITS_AFTER_DECIMAL}f", $totalcontextScore);
# Set ceiling of total score (new for TS7)
if ($totalcontextScore > $maxContextScore{$siteType})
{
$totalcontextScore = "$maxContextScore{$siteType}";
}
}
###
### Use context score and AIR to calculate weighted context score
###
$weightedTotalContextScore = getWeightedContextScore($totalcontextScore, $air);
###
### Print out results for this miRNA
###
print CONTEXT_SCORES_OUTPUT_TEMP "$transcriptID";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$speciesID";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$matureMiRNAid";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$siteTypeNum2description{$siteType}";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$utrStart";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$utrEnd";
if ($PRINT_CS_CONTRIBUTIONS)
{
print CONTEXT_SCORES_OUTPUT_TEMP "\t$siteType2siteTypeContribution{$siteType}";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$threePrimePairing_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$localAUcontribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$minDist_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$sRNA1A_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$sRNA1C_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$sRNA1G_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$sRNA8A_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$sRNA8C_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$sRNA8G_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$site8A_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$site8C_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$site8G_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$len3UTR_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$SA_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$orfLength_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$orf8mer_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$offset6mer_contribution";
if ($garcia{$seedRegion}{"TA"} && $garcia{$siteType}{$seedRegion}{"SPS"})
{
print CONTEXT_SCORES_OUTPUT_TEMP "\t$TA_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$SPS_contribution";
}
else
{
print CONTEXT_SCORES_OUTPUT_TEMP "\tNA";
print CONTEXT_SCORES_OUTPUT_TEMP "\tNA";
}
print CONTEXT_SCORES_OUTPUT_TEMP "\t$PCT_contribution";
}
# $scoreList = "$siteType2siteTypeContribution{$siteType} $threePrimePairing_contribution $localAUcontribution $positionContribution $sRNA1A_contribution $sRNA1C_contribution $sRNA1G_contribution $sRNA8A_contribution $sRNA8C_contribution $sRNA8G_contribution $site8A_contribution $site8C_contribution $site8G_contribution $len3UTR_contribution $minDist_contribution $SA_contribution $orfLength_contribution $orf8mer_contribution $offset6mer_contribution $TA_contribution $SPS_contribution";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$totalcontextScore";
print CONTEXT_SCORES_OUTPUT_TEMP "\tNA";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$air";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$weightedTotalContextScore";
print CONTEXT_SCORES_OUTPUT_TEMP "\tNA";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$alignedUTR";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$alignmentBars";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$alignedMatureMiRNA";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$miRNA_familyID";
print CONTEXT_SCORES_OUTPUT_TEMP "\t$groupNum";
print CONTEXT_SCORES_OUTPUT_TEMP "\n";
}
}
}
else
{
# This is a site predicted by comparative genomics and there is no annotated miRNA in this family in this species
# Ignore this site, as context score is irrelevant
}
}
# Print a dot every $DOT_EVERY_THIS_NUM_LINES input lines
if ($lineNum % $DOT_EVERY_THIS_NUM_LINES == 0)
{
print STDERR ".";
}
close (PREDICTED_TARGETS);
close (CONTEXT_SCORES_OUTPUT_TEMP);
print STDERR " done\n";
}
sub readAgarwalParameters
{
my $AgarwalParamFile = $_[0];
open (COEFFS, "$AgarwalParamFile") || die "Cannot open $AgarwalParamFile for reading: $!";
while (<COEFFS>)
{
chomp;
if ($. > 1) # Skip header line
{
my ($feature, $type3Coeff, $type2Coeff, $type1Coeff, $type4Coeff, $type3Min, $type2Min, $type1Min, $type4Min, $type3Max, $type2Max, $type1Max, $type4Max) = split /\t/, $_;
if ($. == 2) # Get intercept terms
{
$siteType2siteTypeContribution{3} = $type3Coeff;
$siteType2siteTypeContribution{2} = $type2Coeff;
$siteType2siteTypeContribution{1} = $type1Coeff;
$siteType2siteTypeContribution{4} = $type4Coeff;
}
else # Get parameters
{
$agarwal{3}{$feature}{"coeff"} = $type3Coeff;
$agarwal{2}{$feature}{"coeff"} = $type2Coeff;
$agarwal{1}{$feature}{"coeff"} = $type1Coeff;
$agarwal{4}{$feature}{"coeff"} = $type4Coeff;
$agarwal{3}{$feature}{"min"} = $type3Min;
$agarwal{2}{$feature}{"min"} = $type2Min;
$agarwal{1}{$feature}{"min"} = $type1Min;
$agarwal{4}{$feature}{"min"} = $type4Min;
$agarwal{3}{$feature}{"max"} = $type3Max;
$agarwal{2}{$feature}{"max"} = $type2Max;
$agarwal{1}{$feature}{"max"} = $type1Max;
$agarwal{4}{$feature}{"max"} = $type4Max;
}
}
}
}
sub readUTRs
{
# Read file with info about mature miRNAs, and get this in memory
print STDERR "Reading UTRs file ($UTRfile)...";
open (ALIGNED_UTRS, $UTRfile) || die "Cannot open $UTRfile for reading: $!";
while (<ALIGNED_UTRS>)
{
# BMP8B 9606 GUCCACCCGCCCGGC
# BMP8B 9615 -GUG--CUGCCCACC
chomp;
my @f = split (/\t/, $_);
my $transcriptID = $f[0];
my $speciesID = $f[1];
$haveUTRsthisSpecies{$speciesID} = 1;
if(! $useSpecies{$speciesID}) { next; } # Only get species that we want
my $seq = $f[2];
# Remove gaps from alignment
$seq =~ s/-//g;
if ($seq) # Skip any sequences that are all gaps
{
# Convert T's to U's
$seq =~ s/T/U/g;
$seq =~ s/t/u/g;
$utr_seq{$transcriptID}{$speciesID} = $seq;
}
if ($. % 1000 == 0)
{
print STDERR ".";
}
}
close (ALIGNED_UTRS);
# Get scaling factor for each UTR (its length vs that of the ref species)
foreach $transcriptID ( keys %utr_seq )
{
foreach $speciesID ( keys %{ $utr_seq{$transcriptID}})
{
if (length($utr_seq{$transcriptID}{$speciesID}))
{
$utrLengthScalingFactor{$transcriptID}{$speciesID} = length($utr_seq{$transcriptID}{$REF_SPECIES}) / length($utr_seq{$transcriptID}{$speciesID});
# print STDERR "utrLengthScalingFactor for *$transcriptID $speciesID* => $utrLengthScalingFactor{$transcriptID}{$speciesID}\n";
}
}
}
print STDERR " done\n";
}
sub readMiRNAs
{
# Read file with info about mature miRNAs, and get this in memory
print STDERR "Reading miRNA file ($miRNAfile)...\n";
open (MATURE_MIRNAS, $miRNAfile) || die "Cannot open $miRNAfile for reading: $!";
while (<MATURE_MIRNAS>)
{
# let-7/98 9606 hsa-let-7a UGAGGUAGUAGGUUGUAUAGUU
# miR-1/206 10090 mmu-miR-1 UGGAAUGUAAAGAAGUAUGUAU
chomp;
my @f = split (/\t/, $_);
# Make key of miRNA family seed region and species ID
my $miRNA_familyID = $f[0];
my $speciesID = $f[1];
my $familySpecies = "$miRNA_familyID\t$speciesID";
# Make hash linking $familySpecies to mature miRNA sequence + mature miRNA name
my $matureMiRNAname = $f[2];
my $matureMiRNAseq = $f[3];
my $matureMiRNAnameSeq = "$matureMiRNAname\t$matureMiRNAseq";
# Get seed region for family
$seedRegion = substr($matureMiRNAseq, 1, 7);
# my $sRNA1_nt = substr($matureMiRNAseq, 0, 1);
# my $sRNA8_nt = substr($matureMiRNAseq, 7, 1);
# $miRNA_familyID_to_sRNA1{$miRNA_familyID} = $sRNA1_nt;
# $miRNA_familyID_to_sRNA8{$miRNA_familyID} = $sRNA8_nt;
# Assign family => seed region (if we haven't seen it before)
if (! $miRNA_familyID_to_seedRegion{$miRNA_familyID})
{
$miRNA_familyID_to_seedRegion{$miRNA_familyID} = $seedRegion;
}
# Check that family has only 1 seed region
elsif ($seedRegion ne $miRNA_familyID_to_seedRegion{$miRNA_familyID})
{
print STDERR "ERROR: miRNA family $miRNA_familyID seems to have more than 1 seed region: $miRNA_familyID_to_seedRegion{$miRNA_familyID}, $seedRegion\n";
print STDERR "Please correct family definitions and re-run analysis\n";
exit(1);
}
if ( $mature_seq{$familySpecies} )
{
push (@{$mature_seq{$familySpecies}}, $matureMiRNAnameSeq);
}
else
{
@{$mature_seq{$familySpecies}} = $matureMiRNAnameSeq;
}
}
close (MATURE_MIRNAS);
}
sub readIsoformRatios
{
my $AIRsFile = shift;
open (ISOFORM_RATIOS, $AIRsFile) || die "Cannot open $AIRsFile for reading: $!";
print STDERR "Reading affected isoform ratios (AIRs) file ($AIRsFile)...\n";
while (<ISOFORM_RATIOS>)
{
chomp;
my ($utr, $start, $end, $air) = split /\t/, $_;
my $scaledEnd;
if ($setAIRs_to_1) { $air = 100; }
# Scale these ratios according to UTR lengths in different species
foreach $speciesID (keys %haveUTRsthisSpecies)
{
# Look only at UTRs in our current analysis set
if (defined $utrLengthScalingFactor{$utr}{$speciesID})
{
# Need to divide by scaling factor
$scaledEnd = sprintf("%.0f", $end / $utrLengthScalingFactor{$utr}{$speciesID});
if ($start > 1)
{
$scaledStart = sprintf("%.0f", $start / $utrLengthScalingFactor{$utr}{$speciesID});
}
else
{
$scaledStart = 1;
}
$utrScaledEndToAIR{$utr}{$speciesID}{$scaledEnd} = $air;
$utrScaledEndToStart{$utr}{$speciesID}{$scaledEnd} = $scaledStart;
}
}
}
}
sub readORFdataFiles
{
# Get names of other 3 files and then read them
my ($orfLengthsFile, $orf8mersFile) = @_;
open (ORF_LENGTHS, $orfLengthsFile) || die "Cannot open $orfLengthsFile for reading: $!";
print STDERR "Reading ORF lengths file ($orfLengthsFile)...\n";
while (<ORF_LENGTHS>)
{
chomp;
my ($orf, $species, $length) = split /\t/, $_;
if($useSpecies{$species})
{
$orf2length{$orf}{$species} = $length;
}
}
open (ORF_8MERS, $orf8mersFile) || die "Cannot open $orf8mersFile for reading: $!";
print STDERR "Reading ORF 8mer counts file ($orf8mersFile)...\n";
while (<ORF_8MERS>)
{
chomp;
my ($orf, $species, $miRNAfamily, $count) = split /\t/, $_;
if($useSpecies{$species})
{
$orfTo8merCounts{$orf}{$species}{$miRNAfamily} = $count;
}
}
}
sub getAirThisSite
{
my ($transcriptID, $speciesID, $siteEnd) = @_;
$closestAirRegion = 10000000;
if (! $utrScaledEndToAIR{$transcriptID}{$speciesID})
{
print STDERR "No isoform ratios (AIRs) for UTR $transcriptID of species $speciesID. Setting AIR to 1.\n";
$airThisRegion = 1;
}
else
{
# Go through all UTR ends in UTR profile
foreach $AIR_end ( keys %{ $utrScaledEndToAIR{$transcriptID}{$speciesID} } )
{
$AIR_start = $utrScaledEndToStart{$transcriptID}{$speciesID}{$AIR_end};
if ( $AIR_end >= $siteEnd && $AIR_start <= $siteEnd) # This UTR is long enough to include this site
{
# print STDERR "Site in $transcriptID ($speciesID) at $siteEnd within AIR region $AIR_start - $AIR_end (AIR=$air)\n";
$air = $utrScaledEndToAIR{$transcriptID}{$speciesID}{$AIR_end};
# Divide by 100 to change 100-point scale to 0-1 fractional scale
return (1.0*$air/100);
}
else
{
# Need to do this to take care of rounding errors in AIR scaling
$distToNearestEnd = min(abs($AIR_end - $siteEnd), abs($AIR_start - $siteEnd));
if ($distToNearestEnd < $closestAirRegion)
{
$closestAirRegion = $distToNearestEnd;
$airClosestRegion = $utrScaledEndToAIR{$transcriptID}{$speciesID}{$AIR_end};
}
}
}
if (! $airClosestRegion) { $airClosestRegion = 100; }
$airThisRegion = 1.0*$airClosestRegion/100;
if ($airThisRegion < $minAIR) { $airThisRegion = $minAIR; }
}
return $airThisRegion;
}
sub getWeightedContextScore
{
my ($contextScore, $air) = @_;
$weightedTotalContextScore = log((2**($contextScore) - 1) * $air + 1) / log(2);
$weightedTotalContextScore = sprintf("%.4f", $weightedTotalContextScore);
return $weightedTotalContextScore;
}
sub getUsage
{
$usage = <<EODOCS;
Description: Calculate context scores predicted miRNA targets
using TargetScan methods.
USAGE:
$0 miRNA_file UTR_file PredictedTargetsBL_PCT_file ORF_lengths_file ORF_8mer_counts_file UTR_profiles ContextScoresOutput_file
EXAMPLE:
$0 test/input/miR_for_context_scores.sample.txt test/input/UTR_Sequences_sample.txt test/output/targetscan_70_output.BL_PCT.txt test/output/ORF_Sequences_sample.lengths.txt test/output/ORF_8mer_counts_sample.txt test/input/All_cell_lines.AIRs.txt Targets.BL_PCT.context_scores.txt
Required input files:
miRNA_file => mature miRNA data [not used by targetscan_70.pl]
UTR_file => aligned UTRs (same as for targetscan_70.pl)
PredictedTargets => output from targetscan_70_BL_PCT.pl
ORF lengths => length of each ORF corresponding to aligned 3\' UTRs
ORF 8mer counts => number of 8mer sites in ORFs of previous file
UTR profiles => Affected isoform ratios (AIRs) by 3\' UTR region
TA_SPS_FILE => TA and SPS parameters (same as for targetscan_70_BL_PCT.pl)
called "TA_SPS_by_seed_region.txt"
CS++ parameters => Parameters for context++ score model (Agarwal et al., 2015)
called "Agarwal_2015_parameters.txt"
Output file:
ContextScoresOutput => Lists context scores and contributions
For a description of input file formats, type
$0 -h
Authors: Joe Rodriguez, Robin Ge, Kim Walker, and George W. Bell,
Bioinformatics and Research Computing
Version: 7.0
Copyright (c) The Whitehead Institute of Biomedical Research
EODOCS
}
sub checkArguments
{
# Check for input and output file arguments
if ($ARGV[0] && $ARGV[0] eq "-h")
{
print STDERR "$usage";
print STDERR "$fileFormats";
exit(0);
}
elsif (scalar(@ARGV) != 7)
{
print STDERR "\nYou need to supply exactly 7 arguments\n";
print STDERR "$usage";
exit(1);
}
elsif (! -e $ARGV[0]) # miRNA file not present
{
print STDERR "\nI can't find the file $ARGV[0]\n";
print STDERR "which should contain the miRNA families by species.\n";
exit(1);
}
elsif (! -e $ARGV[1]) # UTR file not present
{
print STDERR "\nI can't find the file $ARGV[1]\n";
print STDERR "which should contain the aligned UTRs.\n";
exit(1);
}
elsif (! -e $ARGV[2]) # PredictedTargets file not present
{
print STDERR "\nI can't find the file $ARGV[2]\n";
print STDERR "which should contain the predicted targets/BL/PCT file from targetscan_70_BL_PCT.pl .\n";
exit(1);
}
elsif (! -e $ARGV[3]) # ORF lengths file not present
{
print STDERR "\nI can't find the file $ARGV[3]\n";
print STDERR "which should contain the ORF lengths.\n";
exit(1);
}
elsif (! -e $ARGV[4]) # ORF 8mer counts file not present
{
print STDERR "\nI can't find the file $ARGV[4]\n";
print STDERR "which should contain the ORF 8mer counts.\n";
exit(1);
}
elsif (! -e $ARGV[5]) # Affected isoform ratios (AIRs) by 3' UTR region file not present
{
print STDERR "\nI can't find the file $ARGV[4]\n";
print STDERR "which should contain the affected isoform ratios (AIRs) by 3' UTR region.\n";
exit(1);
}
# Get the file names
$miRNAfile = $ARGV[0];
$UTRfile = $ARGV[1];
$predictedTargetsFile = $ARGV[2];
$orfLengthsFile = $ARGV[3];
$orf8mersFile = $ARGV[4];
$AIRsFile = $ARGV[5];
$contextScoreFileOutput = $ARGV[6];
$contextScoresOutputTemp = "$ARGV[6].tmp";
if (-e $contextScoreFileOutput)
{
print STDERR "Should I over-write $contextScoreFileOutput [yes/no]? ";
$answer = <STDIN>;
if ($answer !~ /^y/i) { exit; }
}
}
sub getFileFormats
{
$fileFormats = <<EODOCS;
** Required input files:
1 - miRNA_file => mature miRNA information
contains four fields (tab-delimited):
Family members Seed+m8 Species ID miRNA_ID Mature sequence
a. miRNA family ID/name
b. species ID in which this miRNA has been annotated
c. ID for this mature miRNA
d. sequence of this mature miRNA
ex:
miR-1/206 9606 hsa-miR-1 UGGAAUGUAAAGAAGUAUGUAU
let-7/98 10090 mmu-let-7a UGAGGUAGUAGGUUGUAUAGUU
2 - UTR_file => Aligned UTRs [same format as for targetscan_70.pl]
contains three fields (tab-delimited):
a. Gene/UTR ID or name
b. Species ID for this gene/UTR (must match ID in miRNA file)
c. Aligned UTR or gene (with gaps from alignment)
ex:
BMP8B 9606 GUCCACCCGCCCGGC
BMP8B 9615 -GUG--CUGCCCACC
A gene will typically be represented on multiple adjacent lines.
3 - PredictedTargets_file => targets (with BL and PCT) from targetscan_70_BL_PCT.pl
contains 14 fields, although some fields are ignored
See test/output/targetscan_70_output.BL_PCT.txt for sample
4 - ORF lengths (for ORFs matching 3\' UTRs in UTR_file): file with Gene/UTR ID, species ID, length
created by targetscan_count_8mers.pl
5 - ORF 8mer counts (for ORFs matching 3\' UTRs in UTR_file): file with Gene/UTR ID, species ID, miRNA family ID/name, count of sites
6 - UTR_profiles => Affected isoform ratios (AIRs) by 3' UTR region file. Contains 4 tab-separated fields.
See test/input/All_cell_lines.AIRs.txt for sample
EODOCS
}
sub extractSubseqForAlignment
{
# Extract a subsequence of a UTR to use for prediced consequential pairing
my ($transcriptID, $miRNA_familyID, $speciesID, $utrStart, $utrEnd, $siteType) = @_;
my ($real_start, $real_end);
my $startEnd = "$utrStart\t$utrEnd";
#####
# Step 1 - Calculate coordinates needed to extract utr_seq
#####
# depending on the seed_type
# you want to do different things to the start and end coordinates
# these start and end coordinates refer to which piece of msa_sequence you want for the RNAfold process
### 4 Nov 2010 -- Add 3 new site types
if ($siteType < 5)
{
$real_start = $utrStart - 16;
}
elsif ($siteType >= 5)
{
$real_start = $utrStart - 19;
}
else
{ print STDERR "Unrecognized site type\n"; }
if ($real_start < 0) { $real_start = 0; }
# if ($siteType == 1 || $siteType == 2 || $siteType == 6)
if ($siteType == 1 || $siteType == 2 || $siteType == 4)
{
$real_end = $utrEnd + 1;
}
else
{
$real_end = $utrEnd;
}
if ($real_start >= $real_end) { $real_start = 0; }
#####
# Step 2 - Extract utr_seq
#####
# Now that we have the right coordinates
# lets get the length of the seq to be extracted (substr needs length)
# use $key to pull out the right seq from %utr_seq $key = symbol_tax_id
my $length = $real_end - $real_start;
my $seq = $utr_seq{$transcriptID}{$speciesID};
my $subseqForAlignment = substr($seq, $real_start, $length);
# one last thing, we need to double check the length of the sequence
# If the sequence doesn't equal $length ($length is how long the sequence should be)
# that means it was either at the very end or the very beginning of the utr_seq
# so the rest of the script runs correctly, add the correct number of N's till it is $length
my $subseqForAlignmentLength = length($subseqForAlignment);
$subseqSpacer = "";
if ($DESIRED_UTR_ALIGNMENT_LENGTH > $subseqForAlignmentLength)
{
for($i = $subseqForAlignmentLength; $i<= $DESIRED_UTR_ALIGNMENT_LENGTH; $i++)