-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalChiTest.pl
191 lines (166 loc) · 4.98 KB
/
finalChiTest.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
#!/usr/bin/perl
use strict;
use warnings;
use YAML;
use Carp;
use List::Util qw< sum >;
use Statistics::Distributions qw< chisqrprob >;
use GD::Graph::bars;
use GD::Graph::Data;
use Getopt::Long;
print <<'WELCOME';
-R-
[+] - RANDOMNESS - [+]
Note: Can be used for checking randomness of any values on chromosomes
License: Creative Commons Licence
Bug-reports and requests to:
jitendra.narayanATunamur.be
-------------------------------------------------------------------
perl finalChiTest.pl -c renamedAdinetaV2.fa.fai -g extractedAliensCors -l 100000 -p 0 -s 100000 -m gene -p 1 -f 15 > aaaaaa
-------------------------------------------------------------------
WELCOME
my (
$chrfile,
$genefile,
$bin,
$size,
$mode,
$finicalgene,
$plot,
$logfile,
);
my $VERSION = 0.1;
my $verbose = 0; # Verbose set to 0;
my %options = ();
my %allChiScore;
$plot=0; #default no plot
GetOptions(
\%options,
'chrfile|c=s' => \$chrfile, ## Infile
'genefile|g=s' => \$genefile, ## Outfile
'bin|l=i' => \$bin, ## Size of sub-string
'size|s=i' => \$size, ## Size of sub-string
'mode|m=s' => \$mode, ## Size of sub-string
'plot|p=i' => \$plot, ## Size of sub-string
'finicalgene|f=i' => \$finicalgene, ## minimum number of gene to consider
'help|?|h!' => sub { RWelcome($VERSION) },
'who|w!' => sub { RWho($VERSION) },
'verbose' => \$verbose,
'logfile=s' => \$logfile, ## logfile
) or die 'cannot help';
if ((!$chrfile) or (!$genefile) or (!$bin) or(!$size) or (!$mode) or (!$finicalgene)) { print "Missing some commands!!!\n"; exit; }
#Create a DIR to store all the plotted graphs
if ($plot) { mkdir("plotted"); }
my $chrFH=read_fh($chrfile);
while (<$chrFH>){
my ($chr, $chrsize) = split;
next if $chrsize <= $size;
my $geneFH=read_fh($genefile);
my %count; my $gino=1;
while (<$geneFH>) {
my ($text, $na, $gene, $count) = split;
next if $chr ne $text;
next unless $gene =~ /$mode/;
$count{int($count / $bin)}++;
$gino++;
}
#Add the black 0 if no hits for a range found
my @allKeys = sort {$a <=> $b} keys %count;
for (my $aa=0; $aa <= $chrsize/$bin; $aa++) {
$count{$aa}=0 unless grep { $aa == $_ } @allKeys;
}
my @allVal; my @k;
for my $key (sort {$a <=> $b} keys %count) {
push @allVal, $count{$key};
push @k, $key;
}
next if $gino <= $finicalgene; # Check the humber of genes
my $number=$gino/scalar(@allVal);
my $chiVal = chi_squared_test(
observed => [ @allVal],
expected => [ ($number) x scalar(@allVal) ]
);
#print "$chiVal\n";
$allChiScore{$chr}=$chiVal;
if ($plot) {printBar(\@k, \@allVal, $chr, $bin, $mode);}
close $geneFH;
undef %count; undef @allVal; undef @k;
}
for my $k (sort {$a cmp $b} keys %allChiScore) {
print "$k\t$allChiScore{$k}\n";
}
#All subs here ---
#Read the files
sub read_fh {
my $filename = shift @_;
my $filehandle;
if ($filename =~ /gz$/) {
open $filehandle, "gunzip -dc $filename |" or die $!;
}
else {
open $filehandle, "<$filename" or die $!;
}
return $filehandle;
}
#Calculate the chi test
sub chi_squared_test {
my %arg_for = @_;
my $observed = $arg_for{observed}
// croak q(Argument "observed" required);
my $expected = $arg_for{expected}
// croak q(Argument "expected" required);
@$observed == @$expected or croak q(Input arrays must have same length);
my $chi_squared = sum map {
( $observed->[$_] - $expected->[$_] )**2 / $expected->[$_]
} 0 .. $#$observed;
my $degrees_of_freedom = @$observed - 1;
my $probability = chisqrprob(
$degrees_of_freedom,
$chi_squared
);
return $probability;
}
#Print the bar graph
sub printBar {
my ($key_ref, $val_ref, $chr, $bin, $mode)=@_;
my @k=@$key_ref; my @allVal=@$val_ref;
my $data= [[@k],[@allVal]];
my $graph = GD::Graph::bars->new;
$graph->set(
x_label => "X Label bin $bin",
y_label => "Y label $mode count",
title => 'A Simple Distributation Bar Chart',
#y_max_value => 7,
#y_tick_number => 8,
#y_label_skip => 3,
#x_labels_vertical => 1,
#bar_spacing => 10,
#shadow_depth => 4,
#shadowclr => 'dred',
#transparent => 0,
) or die $graph->error;
$graph->plot($data) or die $graph->error;
my $file = "plotted/bars_$chr.png";
open(my $out, '>', $file) or die "Cannot open '$file' for write: $!";
binmode $out;
print $out $graph->gd->png;
close $out;
}
sub RWelcome {
my $VERSION = shift;
print "\nWelcome to Randomness estimation, version $VERSION\n";
print "
'chrfile|c=s' ## Indexed genome file; samtools faidx genome.fa > genome.fa.fai file
'genefile|g=s' ## Genefile in gff format; see sample 'extractedAliensCors'
'bin|l=i' ## Bin to check for randomness
'size|s=i' ## Size of sub-string
'mode|m=s' ## gene
'plot|p=i' ## 1 or 0
'finicalgene|f=i' ## minimum number of gene to consider
";
}
sub RWho {
my $VERSION = shift;
print "\nJitendra Narayan\n";
}
__END__