forked from chaolinzhanglab/ctk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getCIMSMismatchAlleleFreq.pl
executable file
·159 lines (119 loc) · 3.99 KB
/
getCIMSMismatchAlleleFreq.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
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use File::Basename;
use Carp;
use MyConfig;
my $prog = basename ($0);
my $cmdDir = dirname ($0);
my $verbose = 0;
my $cache = getDefaultCache ($prog);
GetOptions ("cache:s"=>\$cache,
"v"=>\$verbose);
if (@ARGV != 3)
{
print "get CIMS allele frequency\n";
print "Usage: $prog [options] <mutation.txt> <CIMS.bed> <out.txt>\n";
print " -cache: cache dir ($cache)\n";
print " -v : verbose\n";
exit (1);
}
my ($inMutationFile, $inCIMSFile, $outFile) = @ARGV;
Carp::croak "$cache already exists\n" if -d $cache;
system("mkdir $cache");
my %CIMStags;
my %CIMSHash;
my $cmd;
# 1. get the number of tags at each mutation position
print "Counting mutations ...\n" if $verbose;
open (INFILE, $inCIMSFile) || Carp::croak "can not open file $inCIMSFile to read\n";
while (my $line = <INFILE>)
{
chomp $line;
my @cols = split ("\t", $line);
my $clusterId = $cols[3];
$clusterId =~ m/\[k=(\d+)\]/;
$CIMStags{$clusterId} = $1;
}
close(INFILE);
# 2. get allele frequency from mutation file
# 2a. reformat mutation file into bed (and reverse complement)
print "Process mutations ...\n" if $verbose;
my $formatMutationFile = "$cache/mutation.sub.bed";
open (OUTFILE, ">$formatMutationFile") || Carp::croak "can not open file $formatMutationFile to write\n";
open (INFILE, "<$inMutationFile") || Carp::croak "can not open file $inMutationFile to read\n";
while (my $line = <INFILE>)
{
my @cols = split ("\t", $line);
next if $cols[8] ne ">"; # check that it is a substitution
my $refBase = $cols[7];
my $subBase = $cols[9];
if ($cols[5] eq "-") # reverse complement minus strand
{
$refBase =~ tr/ATGC/TACG/;
$subBase =~ tr/ATGC/TACG/;
}
print OUTFILE "$cols[0]\t$cols[1]\t$cols[2]\t$cols[3]^^^$refBase^$subBase\t$cols[4]\t$cols[5]\n"; # print with ref/mutation bases added to tag name
}
close (INFILE);
close (OUTFILE);
# 3. overlap with CIMS
print "compare mutations and CIMS ...\n" if $verbose;
my $mutationVsCIMS = "$cache/mutation.vs.CIMS";
$cmd = "perl $cmdDir/tagoverlap.pl -region $inCIMSFile -ss $formatMutationFile $mutationVsCIMS";
$cmd .= " -v" if $verbose;
system ($cmd);
# 4. count alleles
# 4a. retrieve mutations
print "parse tag/cluster comparison ...\n" if $verbose;
open (INFILE, "<$mutationVsCIMS") || Carp::croak "can not open file $mutationVsCIMS to read\n";
while (my $line = <INFILE>)
{
next if $line =~/^\s*$/;
my @cols = split ("\t", $line);
my $name = $cols[3];
my $strand = $cols[5];
my ($tagId, $clusterId) = split (/\/\//, $name);
#$clusterId =~/\/(\d+)\]/;
#my $total = $1;
#$CIMSHash{$clusterId}->{'total'} = $total;
my $refAllele = substr ($tagId, -3, 1);
my $subAllele = substr ($tagId, -1, 1);
$subAllele = uc ($subAllele);
#$subAllele =~tr/ACGT/TGCA/ if $strand eq '-';
$CIMSHash{$clusterId}->{$subAllele}++;
$CIMSHash{$clusterId}->{"ref"} = uc($refAllele);
}
close (INFILE);
# 4b. count mutations
print "Count mutations ...\n" if $verbose;
my $outAlleleFile = "$cache/allele.txt";
open (OUTFILE, ">$outAlleleFile") || Carp::croak "cannot open file $outFile to write\n";
print OUTFILE join ("\t", "CIMS", "A", "C", "G", "T"), "\n";
foreach my $clusterId (sort keys %CIMSHash)
{
my $cluster = $CIMSHash{$clusterId};
my $refAllele = $cluster->{"ref"};
if (!exists $cluster->{'A'}) {$cluster->{'A'} = 0};
if (!exists $cluster->{'C'}) {$cluster->{'C'} = 0};
if (!exists $cluster->{'G'}) {$cluster->{'G'} = 0};
if (!exists $cluster->{'T'}) {$cluster->{'T'} = 0};
if ($cluster->{$refAllele} != 0 )
{
Carp::croak "for cluster $cluster, counts for reference allele $refAllele are not 0\n";
}
else
{
$cluster->{$refAllele} = $CIMStags{$clusterId} - $cluster->{'A'} - $cluster->{'T'} - $cluster->{'G'} - $cluster->{'C'};
}
my $A = $cluster->{'A'};
my $C = $cluster->{'C'};
my $G = $cluster->{'G'};
my $T = $cluster->{'T'};
print OUTFILE join ("\t", $clusterId, $A, $C, $G, $T), "\n";
}
close (OUTFILE);
$cmd = "perl $cmdDir/selectRow.pl -f 3 $outAlleleFile $inCIMSFile > $outFile";
system ($cmd);
print "Done.\n";