-
Notifications
You must be signed in to change notification settings - Fork 4
/
Calculate_Pvalue_CisElements.pl
151 lines (130 loc) · 5.11 KB
/
Calculate_Pvalue_CisElements.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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Std;
use List::Util qw(sum);
use Statistics::Descriptive;
getopt('mnsdo');
my$usage="\n\n---------------------------------------------------------------------------------------
perl script {options.....}
-s 'real sequences SignalScan results'
-d 'All the promoters signal scan results(formatted by Process Webscan script)'
-m 'number of datasets to test' [1000]
-n 'number of sequences in each dataset'
-o 'output file to store results'
---------------------------------------------------------------------------------------\n\n";
print "$usage\n";
our($opt_m,$opt_n,$opt_s,$opt_d,$opt_o,%count,%header,%hit);
open OUT,">$opt_o" or die "Cannot open file $opt_o\n$usage";
printf OUT"%-15s %-10s %-10s %-10s %-10s %-10s\n","Cis_element","True_Hits","Expec_Hit","Var","Std","Z-score";
#Assign default value to $opt_m if not provided by the user
$opt_m=1000 if !defined $opt_m;
# Read sequence signal scan to get values of motifs and their frequencies.
if(defined $opt_s){open RSCAN,"$opt_s";}
else{die "\nCannot find file containing Signal Scan results to test\n$usage";}
$/="\n________________________";
my @section=();
while(<RSCAN>){push(@section,$_);}
close RSCAN; # close file to reduce stress on computer.
#push(@filenames,$opt_s);
print "Reading file and creating list of motifs:$opt_s....";
#------------------------------------------------------------------------
#extract sequence name from file.
my@sequenceheader=split(/\n/,$section[0]);
my$seqname=();
foreach(@sequenceheader){if(/>/){$_=~s/>//;($seqname)=split(/\s+/,$_);$seqname=~s/\s*//;last;}}
#push(@seqnames,$seqname);
$section[1]=~s/_+||-----+[\w\s\W\S]+//g;
my@motif_line=split(/\n/,$section[1]);
my%motifs;
foreach(@motif_line){
next if $_=~/^\s*$/;
$_=~s/^\s+//;
my@motif_info=();
@motif_info=split(/\s+/,$_);
$motif_info[0]=~s/\s+//;
my$motif_name=$motif_info[0];
next if ($motif_name=~/^\s*$/);
$count{$motif_name}{$opt_s}++ if ($motif_name=~/$motif_info[0]/);
#$count{$motif_name}{$seqname}++ if ($motif_name=~/$motif_info[0]/); # use this for real seq name rather than filenames.
$motifs{$motif_name}=();
}
print "...Done\nCreating headers of motifs read.....";
push(my @motif_in_seq,keys%motifs);
print"\nNumber of Motifs found in sequence file:";
print scalar@motif_in_seq;
print"\n";
$/="\n";
open DATASET,"$opt_d";
my$first_line=<DATASET>;
my @header=split(/\t/,$first_line);
#shift @headers;
#print "@header\n";
for(my$i=0;$i<=scalar@header-1;$i++){$header{$header[$i]}=$i;};
#print "\nheader: @header\n";
close DATASET;
print "Done\nCreating table from formatted dataset file......";
#--------------------------------------------------------------------------
my%file_value;
my$i=1;
$/="\n";
open DATASET,"$opt_d";
while(<DATASET>){
my@hit=split(/\t/,$_);
my$hit=join("\t",@hit);
$file_value{$hit[0]}=$hit;
$i++;
#print "reading $i line in dataset\n";
}
close DATASET;
print "...Done\n";
push(my@dataset_range,keys%file_value);
my$range=scalar@dataset_range;
$range--;
print "Total number of datasets:$range\n";
#--------------------------------------------------------------------------
# my $range=33518; # manual entry of random number generator limit.
# pick values for each motifs in true file from n random sequences;
foreach (keys %motifs){
my$motif=$_;
#print "Collecting frequencies for $motif\n";
my@hit_values=();
for(my$i=1;$i<=$opt_m;$i++){
loop: my$random= int(rand($range)); # generate a random number
goto loop if $random ==0; # discard if random number is zero. refind random number.
my$filename=$random.'.txt';
my $hit_value=read_hitValues($filename,$header{$motif});
push(@hit_values,$hit_value);
#print"Reading $i th time\n";
}
my $stat = Statistics::Descriptive::Full->new();
$stat->add_data(@hit_values);
my $mean = $stat->mean();
my $var = $stat->variance();
my $std = $stat->standard_deviation();
$Statistics::Descriptive::Tolerance = 1e-180;
my $Zscore=($count{$motif}{$opt_s} - $mean)/$std if ($std!=0);
if(defined $Zscore){
printf OUT"%-15s%10.2f%10.2f%10.2f%10.2f%10.2f\n",$motif,$count{$motif}{$opt_s},$mean,$var,$std,$Zscore;
printf "%-15s%10.2f%10.2f%10.2f%10.2f%10.2f\n",$motif,$count{$motif}{$opt_s},$mean,$var,$std,$Zscore;
}
else{
$Zscore=' NA';
printf OUT"%-15s%10.2f%10.2f%10.2f%10.2f%10.2f%10.2f\n",$motif,$count{$motif}{$opt_s},$mean,$var,$std,$Zscore;
printf "%-15s%10.2f%10.2f%10.2f%10.2f%10.2f%10.2f\n",$motif,$count{$motif}{$opt_s},$mean,$var,$std,$Zscore;
}
}
#--------------------------------------------------------------------------------
sub read_hitValues{
(my$filename,my$position)=@_;
my %hit=();
$/="\n";
if (exists$file_value{$filename}){
my@hit=split(/\t/,$file_value{$filename});
for(my$i=0;$i<=scalar@hit;$i++){$hit{$hit[0]}{$i}=$hit[$i];}
return($hit{$filename}{$position});
@hit=();
}
else{print"\nCould not find $filename\n";}
}
#---------------------------------------------------------------------------------