forked from z0on/2bRAD_denovo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergeUniq.pl
executable file
·167 lines (147 loc) · 3.4 KB
/
mergeUniq.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
#!/usr/bin/perl
my $usage="
mergeUniq.pl :
Merges individual uniqued files produced by uniquerOne.pl into one table.
arg1: common extension of uniqued files
minDP=[integer] minimum sequencing depth to consider a uniqie tag. Default 5.
prints to STDOUT
Example:
mergeUniq.pl uni >mydataMerged.uniq
";
# use Statistics::ChiSquare;
sub rcom {
my $rcs=scalar reverse ("$_[0]");
$rcs=lc $rcs;
$rcs=~s/a/T/g;
$rcs=~s/t/A/g;
$rcs=~s/g/C/g;
$rcs=~s/c/G/g;
# $rcs=~s/![atgcATGC]/N/g;
return $rcs;
}
my $mincount=5;
if ("@ARGV"=~/minDP=(\d+)/) { $mincount=$1;}
$glob=shift or die $usage;
opendir THIS, ".";
@ins=grep /\.$glob$/,readdir THIS;
my $tag;
my $tot1;
my $rc1;
my $ind1;
my $tot2;
my $rc2;
my $chi1;
my $chi2;
my $counts1;
my $counts2;
my $ind2;
my %seen={};
my $now;
my %dstring={};
my @rest;
my @ins=sort @ins;
my @allins=();
my $frst=1;
$reff=shift(@ins);
open INP, $reff or die "cannot open first file $reff\n";
warn "\tfile $reff\n";
$now=localtime;
warn "reading: $now\n";
while (<INP>) {
next if ($_=~/^seq/);
chop;
($tag,@rest)=split("\t",$_);
@{$dstring{$tag}}=@rest;
if ($frst) {
push @allins,$rest[2];
$frst=0;
}
}
foreach $gmap (@ins) {
warn "\tfile $gmap\n";
$frst=1;
$now=localtime;
warn "reading: $now\n";
open INP, $gmap or die "cannot open file $gmap\n";
while (<INP>) {
next if ($_=~/^seq/);
chop;
my $line=$_;
($tag,$tot2,$rc2,$ind2,$counts2)=split("\t",$line);
if ($frst) {
push @allins,$ind2;
$frst=0;
}
if (!$dstring{$tag}) {
my $rc=rcom($tag);
if ($dstring{$rc}){
$tag=$rc;
$rc2=$tot2-$rc2;
}
}
if (!$dstring{$tag}) { @{$dstring{$tag}}=($tot2,$rc2,$ind2,$counts2);}
else {
$tot1=$tot2+${$dstring{$tag}}[0];
$rc1=$rc2+${$dstring{$tag}}[1];
#warn "$tag|@{$dstring{$tag}}\n";
# $chi1=chisquare($rc1,($tot1-$rc1));
#warn "$chi1\n\n";
# if ($chi1=~/<(\d+)/) { $chi1=$1;}
# else { die "chisquare error: $chi\n";}
$ind1=${$dstring{$tag}}[2].",".$ind2;
$counts1=${$dstring{$tag}}[3].",".$counts2;
@{$dstring{$tag}}=($tot1,$rc1,$ind1,$counts1);
}
}
}
#warn "ALL allins: @allins\n";
$now=localtime;
warn "processing: $now\n";
sub bycount {
${$dstring{$b}}[0] <=> ${$dstring{$a}}[0]
}
my @seqs=keys %dstring;
@seqs= sort bycount @seqs;
foreach $s (@allins){
$s=~s/\..+//;
}
#@allins
open FAS, ">mergedUniqTags.fasta" or die "cannot create file mergedUniqTags.fasta";
print "tag\tseq\tcount\trevcom\t",join("\t",@allins),"\n";
my $globalcount=0;
my $indexx=0;
foreach $seq (@seqs) {
next if ($seq=~/HASH/ || $seq!~/[ATGC]+/);
last if (${$dstring{$seq}}[0]<$mincount);
$globalcount+=${$dstring{$seq}}[0];
$indexx++;
my $fahead="tag".$indexx." count=".${$dstring{$seq}}[0];
print FAS ">$fahead\n$seq\n";
my @cts=split(",",pop @{$dstring{$seq}});
my @inds=split(",", pop @{$dstring{$seq}});
#warn "$seq inds: @inds\n";
#warn "$seq counts: @cts\n";
my @ctall=();
my $skip=0;
for ($i=0;$ind=$allins[$i];$i++) {
$inds[$i-$skip]=~s/\..+//;
#warn "\ti $i, skip $skip. is ind $inds[$i-$skip] the same as $ind?\n";
if ($inds[$i-$skip] eq $ind) {
#warn "\tyes\n\n";
push @ctall, $cts[$i-$skip];
}
else {
push @ctall, 0;
$skip++;
#warn "\tno\n";
}
}
#warn "\t\tcounts: @ctall\n\n";
push @{$dstring{$seq}}, @ctall;
print "tag$indexx\t$seq\t",join("\t",@{$dstring{$seq}}),"\n";
undef $dstring{$seq};
}
close FAS;
warn "\nconsidering reads seen at least $mincount times:\n$globalcount reads processed\n";
$now=localtime;
warn "$now\n";