-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcheck_genes.pl
174 lines (149 loc) · 5.15 KB
/
check_genes.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
#!/usr/bin/perl -w
#############################################################
#
#############################################################
use strict;
use File::Basename;
use Cwd qw(abs_path);
my $directory_of_script = dirname(abs_path(__FILE__));
require $directory_of_script . '/common-utils.pl';
if (scalar @ARGV != 1) {
die "Usage: perl $0 <metafile>\n" .
"Example: perl $0 human.txt\n";
}
my $TRUE = 0;
my $FALSE = 1;
my $DEBUG = $TRUE;
my ($meta_file) = @ARGV;
my $meta_href = readMeta($meta_file);
my %meta_hash = %{$meta_href};
my @chromosomes = getChr ($meta_hash{"PARENT_DIR"} . "/". $meta_hash{"GENE_DOWNLOAD_DEST"});
my $mo_out_dir = $meta_hash{"PARENT_DIR"} . "/". $meta_hash{"ORG_VERSION"};
chomp ($mo_out_dir);
#print join ("," , @chromosomes);
get_genes_in_db ($mo_out_dir, @chromosomes);
sub count_genes_in_SIFT_db_file
{
my ($file) = @_;
my %genes = ();
my %genes_with_sift_score = ();
my $cds_pos = 0;
my $cds_pos_with_sift_score = 0;
my $sift_confident_score = 0;
my $line;
open (DB_IN, $file) || die "can't open $file";
while ($line = <DB_IN>) {
if ($line =~ /CDS/) {
my @fields = split ('\t', $line);
my $txid = $fields[3];
my $siftscore = $fields[10];
my $medianscore = $fields[11];
$genes{$txid} = 1;
if ($siftscore ne "NA" && $siftscore >= 0.0 && $siftscore ne "") {
$genes_with_sift_score{$txid} = 1;
}
if ($fields[7] ne $fields[8] && $fields[7] ne "*"
&& $fields[8] ne "*") {
$cds_pos++;
if ($siftscore ne "NA" && $siftscore >= 0.0
&& $siftscore ne "") {
$cds_pos_with_sift_score++;
if ($medianscore <= 3.5) {
$sift_confident_score++;
}
}
}
}
}
close (DB_IN);
my $num_genes = scalar keys %genes;
my $num_genes_sift = scalar keys %genes_with_sift_score;
return ($num_genes, $num_genes_sift, $cds_pos,
$cds_pos_with_sift_score, $sift_confident_score);
}
sub get_genes_in_db
{
my ($mo_out_dir, @chromosomes) = @_;
my %count_hash;
my $results_file = $mo_out_dir . "/CHECK_GENES.LOG";
open (OUT_RES, ">$results_file") || die "can't write to $results_file";
print OUT_RES "Chr\tGenes with SIFT Scores\tPos with SIFT scores\tPos with Confident Scores\n";
my $tmp_file = $meta_hash{"PARENT_DIR"} . "/SIFT_tmp233_rm.txt";
if (-e $tmp_file) {
system ("rm $tmp_file");
}
my ($total_cds_gene_count_with_sift,$total_cds_gene_count,
$total_cds_pos_with_sift_scores,
$total_cds_pos, $total_sift_confident_score)
= (0) x 5;
for (my $i = 0; $i < @chromosomes; $i++) {
my $chr = $chromosomes[$i];
chomp ($chr);
my $file = $mo_out_dir . "/" . $chr . ".gz";
system ("zcat $file > $tmp_file");
my ($cds_gene_count, $cds_gene_count_with_sift, $cds_pos,
$cds_pos_with_sift_scores, $sift_confident_score) =
count_genes_in_SIFT_db_file ($tmp_file);
my $cds_perc = 0;
if ($cds_gene_count > 0) {
$cds_perc = $cds_gene_count_with_sift/$cds_gene_count * 100;
}
my $pos_perc = 0;
if ($cds_pos > 0) {
$pos_perc = $cds_pos_with_sift_scores / $cds_pos * 100;
}
my $sift_conf_perc = 0;
if ($cds_pos_with_sift_scores > 0) {
$sift_conf_perc = $sift_confident_score/$cds_pos_with_sift_scores * 100;
}
print OUT_RES "$chr\t" . round ($cds_perc) .
" ($cds_gene_count_with_sift/$cds_gene_count)\t" . round ($pos_perc) . " ($cds_pos_with_sift_scores/$cds_pos)\t" . round ($sift_conf_perc) . "($sift_confident_score/$cds_pos_with_sift_scores)\n";
system ("rm $tmp_file");
$total_cds_gene_count_with_sift += $cds_gene_count_with_sift;
$total_cds_gene_count += $cds_gene_count;
$total_cds_pos_with_sift_scores += $cds_pos_with_sift_scores;
$total_cds_pos += $cds_pos;
$total_sift_confident_score += $sift_confident_score;
}
print OUT_RES "\n";
# do it genome-wide, get totals
my $total_cds_perc = 0;
if ($total_cds_gene_count > 0) {
$total_cds_perc = $total_cds_gene_count_with_sift/$total_cds_gene_count * 100;
}
my $total_pos_perc = 0;
if ($total_cds_pos > 0) {
$total_pos_perc = $total_cds_pos_with_sift_scores / $total_cds_pos * 100;
}
my $total_sift_conf_perc = 0;
if ($total_cds_pos_with_sift_scores > 0) {
$total_sift_conf_perc = $total_sift_confident_score/$total_cds_pos_with_sift_scores * 100;
}
print OUT_RES "ALL\t" . round ($total_cds_perc) .
" ($total_cds_gene_count_with_sift/$total_cds_gene_count)\t"
. round ($total_pos_perc) .
" ($total_cds_pos_with_sift_scores/$total_cds_pos)\t" .
round ($total_sift_conf_perc) .
"($total_sift_confident_score/$total_cds_pos_with_sift_scores)\n";
close (OUT_RES);
}
sub round
{
my ($float ) = @_;
return (int ($float+0.5));
}
sub num_genes_per_chr
{
my (%meta_hash) = @_;
my %chr_counts;
my $gene_transcripts_file = $meta_hash{"PARENT_DIR"} . "/" . $meta_hash{"GENE_DOWNLOAD_DEST"} . "/protein_coding_genes.txt";
my @counts = `cat $gene_transcripts_file | cut -f1,8 -d: | sort | uniq | cut -f1 -d: | uniq -c`;
for (my $i= 0; $i < @counts; $i++) {
chomp ($counts[$i]);
$counts[$i] =~ s/^\s+//;
my ($num, $chr) = split (/\s+/, $counts[$i]);
# print "chr $chr num $num\n";
$chr_counts{$chr} = $num;
}
return (%chr_counts);
}