forked from marvin-jens/clip_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collapse_reads.pl
executable file
·169 lines (123 loc) · 3.63 KB
/
collapse_reads.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
#!/usr/bin/perl
## libname statt @read , done
## stderr input reads, unique reads,
## count matrices 5' und 3' ende
use strict;
my %len;
for(my $i=15;$i<77;$i++){
$len{$i}{'raw'}=0;
$len{$i}{'uniq'}=0;
}
## get correct file name -> there is a perl module for this but better we do not use it
my $library=shift @ARGV or die "no library name given\n";
my $total_reads=0;
my $uniq_reads=0;
my $type='fastq';
my $seq;
my %hash;
my %h2;
my %pwm3=();
my %pwm5=();
my $rlen=20;
if($type eq 'fastq'){
my $line=0;
while(<>){
chomp;
next if(/^\s*$/);
$line++;
if($line == 1){
}elsif($line == 2){
$seq=uc($_);
$hash{$seq}++;
$len{length($seq)}{'raw'}++;
$total_reads++;
}elsif($line == 3){
}elsif($line == 4){
if(not $h2{$seq}){
$h2{$seq}=$_;
}
$line=0;
}
}
close IN;
my $count=0;
for(sort {$hash{$b} <=> $hash{$a}} keys %hash){
print "\@${library}_${count}_x$hash{$_}\n$_\n+\n$h2{$_}\n";
$len{length($_)}{'uniq'}++;
$count++;
pwm(\%pwm5,\%pwm3,\$_,\$hash{$_});
}
$uniq_reads=$count;
}
## print out stats to stderr
print STDERR ">read statistics
#property\tcounts_$library
input\t$total_reads
uniq\t$uniq_reads
";
my @nts=qw(A C G T );
for(my $i=0;$i < $rlen ;$i++){
## add pseudocount
foreach my $nt(@nts){
$pwm5{$i}{$nt}++;
$pwm3{$i}{$nt}++;
$pwm5{$i}{'tot'}++;
$pwm3{$i}{'tot'}++;
}
}
print STDERR ">nt satistics from 5'end\n## --col_stack=A,C,G,T\n#pos";
foreach my $nt(@nts){
print STDERR "\t$nt";
}
print STDERR "\n";
for(my $i=0;$i < 20;$i++){
print STDERR "$i";
foreach my $nt(@nts){
printf( STDERR "\t%.3f",$pwm5{$i}{$nt}/$pwm5{$i}{'tot'});
# printf( STDERR "\t%.2f\t%i\t%.2f",$pwm5{$i}{$nt},$pwm5{$i}{'tot'},$pwm5{$i}{$nt}/$pwm5{$i}{'tot'});
}
print STDERR "\n";
}
print STDERR "\n>nt satistics from 3'end\n## --col_stack=A,C,G,T\n#pos";
foreach my $nt(@nts){
print STDERR "\t$nt";
}
print STDERR "\n";
for(my $i=0;$i < 20;$i++){
print STDERR "-$i";
foreach my $nt(@nts){
printf( STDERR "\t%.3f",$pwm3{$i}{$nt}/$pwm3{$i}{'tot'});
}
print STDERR "\n";
}
print STDERR "\n>read_lengths_raw\n";
print STDERR "## --cumsum --steps\n";
print STDERR "#length\t$library\n";
for my $i(sort {$a <=> $b} keys %len){
print STDERR "$i\t$len{$i}{'raw'}\n";
}
print STDERR "\n>read_lengths_unique\n";
print STDERR "## --cumsum --steps\n";
print STDERR "#length\t$library\n";
for my $i(sort {$a <=> $b} keys %len){
print STDERR "$i\t$len{$i}{'uniq'}\n";
}
## pwm function
sub pwm{
my ($h5,$h3,$seq,$num) = @_;
my $c;
my $len=length($$seq);
for(my $i=0; $i<$len;$i++){
$c=uc(substr($$seq,$i,1));
# this is for unique read sequences
$$h5{$i}{$c}++;
$$h3{$len-$i-1}{$c}++;
$$h5{$i}{'tot'}++;
$$h3{$len-$i-1}{'tot'}++;
# this would be weighted
# $$h5{$i}{$c}+=$$num;
# $$h3{$len-$i-1}{$c}+=$$num;
# $$h5{$i}{'tot'}+=$$num;
# $$h3{$len-$i-1}{'tot'}+=$$num;
}
}