annotate bin/ppp.pm @ 14:8b7000eac6a0 draft

Deleted selected files
author romaingred
date Mon, 16 Oct 2017 03:46:15 -0400
parents 198009598544
children 8031792a6e2c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
198009598544 Uploaded
romaingred
parents:
diff changeset
1 package ppp;
198009598544 Uploaded
romaingred
parents:
diff changeset
2
198009598544 Uploaded
romaingred
parents:
diff changeset
3 use strict;
198009598544 Uploaded
romaingred
parents:
diff changeset
4 use warnings;
198009598544 Uploaded
romaingred
parents:
diff changeset
5 use FindBin;
198009598544 Uploaded
romaingred
parents:
diff changeset
6 use lib $FindBin::Bin;
198009598544 Uploaded
romaingred
parents:
diff changeset
7 use Rcall qw ( histogram );
198009598544 Uploaded
romaingred
parents:
diff changeset
8 use Math::CDF;
198009598544 Uploaded
romaingred
parents:
diff changeset
9
198009598544 Uploaded
romaingred
parents:
diff changeset
10 use Exporter;
198009598544 Uploaded
romaingred
parents:
diff changeset
11 our @ISA = qw( Exporter );
198009598544 Uploaded
romaingred
parents:
diff changeset
12 our @EXPORT_OK = qw( &ping_pong_partners );
198009598544 Uploaded
romaingred
parents:
diff changeset
13
198009598544 Uploaded
romaingred
parents:
diff changeset
14 sub ping_pong_partners
198009598544 Uploaded
romaingred
parents:
diff changeset
15 {
198009598544 Uploaded
romaingred
parents:
diff changeset
16 my ( $TE_fai, $sam, $dir, $max ) = @_;
198009598544 Uploaded
romaingred
parents:
diff changeset
17 my ( $hashRef, $dupRef ) = count_mapped ( $TE_fai, $sam );
198009598544 Uploaded
romaingred
parents:
diff changeset
18 my ( %num_per_overlap_size, $overlap_number, $reverseR, $begRev, $endRev, $sensR, $begSens, $endSens, $snum, $rnum, $overlap );
198009598544 Uploaded
romaingred
parents:
diff changeset
19 my ( $SP, $AP, $SN, $AN, $txt);
198009598544 Uploaded
romaingred
parents:
diff changeset
20 my $flag = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
21 my @distri_overlap = (); my @overlaps_names = ();
198009598544 Uploaded
romaingred
parents:
diff changeset
22
198009598544 Uploaded
romaingred
parents:
diff changeset
23 open my $ppp_f, '>', $dir."ppp.txt" || die "cannot create ppp.txt $!\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
24 foreach my $k ( sort keys %{$hashRef} )
198009598544 Uploaded
romaingred
parents:
diff changeset
25 {
198009598544 Uploaded
romaingred
parents:
diff changeset
26 my $v = $hashRef->{$k};
198009598544 Uploaded
romaingred
parents:
diff changeset
27 my $TE_dir = $dir.$k.'/';
198009598544 Uploaded
romaingred
parents:
diff changeset
28
198009598544 Uploaded
romaingred
parents:
diff changeset
29 %num_per_overlap_size = (); $overlap_number = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
30 $flag = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
31 for ( my $i = 0; $i <= $#{$v->[1]} ; $i++ )
198009598544 Uploaded
romaingred
parents:
diff changeset
32 {
198009598544 Uploaded
romaingred
parents:
diff changeset
33 $reverseR = ${$v->[1]}[$i] ;
198009598544 Uploaded
romaingred
parents:
diff changeset
34 $begRev = $reverseR->[0];
198009598544 Uploaded
romaingred
parents:
diff changeset
35 $endRev = $begRev + length($reverseR->[1]) - 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
36
198009598544 Uploaded
romaingred
parents:
diff changeset
37 my $revR = reverse($reverseR->[1]);
198009598544 Uploaded
romaingred
parents:
diff changeset
38 $revR =~ tr/atgcuATGCU/tacgaTACGA/;
198009598544 Uploaded
romaingred
parents:
diff changeset
39
198009598544 Uploaded
romaingred
parents:
diff changeset
40 for ( my $j = 0; $j <= $#{$v->[0]}; $j++ )
198009598544 Uploaded
romaingred
parents:
diff changeset
41 {
198009598544 Uploaded
romaingred
parents:
diff changeset
42 $sensR = ${$v->[0]}[$j];
198009598544 Uploaded
romaingred
parents:
diff changeset
43 $begSens = $sensR->[0];
198009598544 Uploaded
romaingred
parents:
diff changeset
44
198009598544 Uploaded
romaingred
parents:
diff changeset
45 $endSens = $begSens + length($sensR->[1]) - 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
46
198009598544 Uploaded
romaingred
parents:
diff changeset
47 if ( $begSens <= $endRev && $endSens > $endRev )
198009598544 Uploaded
romaingred
parents:
diff changeset
48 {
198009598544 Uploaded
romaingred
parents:
diff changeset
49 $flag = 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
50
198009598544 Uploaded
romaingred
parents:
diff changeset
51 mkdir $TE_dir;
198009598544 Uploaded
romaingred
parents:
diff changeset
52 open $SP, '>>', $TE_dir."sensPPP.txt" || die "cannot create sensPPP\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
53 open $AP, '>>', $TE_dir."antisensPPP.txt" || die "cannot create antisensPPP\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
54 open $SN, '>>', $TE_dir."sens.txt" || die "cannot create sens\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
55 open $AN, '>>', $TE_dir."antisens.txt" || die "cannot create antisens\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
56 open $txt, '>', $TE_dir.'overlap_size.txt' || die "cannot open repartition\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
57
198009598544 Uploaded
romaingred
parents:
diff changeset
58 $overlap = $endRev - $begSens + 1;
198009598544 Uploaded
romaingred
parents:
diff changeset
59 $snum = $dupRef->{$sensR->[0].$sensR->[1].$sensR->[2].$sensR->[3]};
198009598544 Uploaded
romaingred
parents:
diff changeset
60 $rnum = $dupRef->{$reverseR->[0].$reverseR->[1].$reverseR->[2].$reverseR->[3]};
198009598544 Uploaded
romaingred
parents:
diff changeset
61
198009598544 Uploaded
romaingred
parents:
diff changeset
62 if ( $overlap == 10 )
198009598544 Uploaded
romaingred
parents:
diff changeset
63 {
198009598544 Uploaded
romaingred
parents:
diff changeset
64 print $SP ">$sensR->[0]|$sensR->[2]|$sensR->[3]|$snum\n$sensR->[1]\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
65 print $AP ">$reverseR->[0]|$reverseR->[2]|$reverseR->[3]|$rnum\n$revR\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
66 }
198009598544 Uploaded
romaingred
parents:
diff changeset
67 else
198009598544 Uploaded
romaingred
parents:
diff changeset
68 {
198009598544 Uploaded
romaingred
parents:
diff changeset
69 print $SN ">$sensR->[0]|$sensR->[2]|$sensR->[3]|$snum\n$sensR->[1]\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
70 print $AN ">$reverseR->[0]|$reverseR->[2]|$reverseR->[3]|$rnum\n$revR\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
71 }
198009598544 Uploaded
romaingred
parents:
diff changeset
72 next if $overlap > $max;
198009598544 Uploaded
romaingred
parents:
diff changeset
73 if ( $snum < $rnum )
198009598544 Uploaded
romaingred
parents:
diff changeset
74 {
198009598544 Uploaded
romaingred
parents:
diff changeset
75 $num_per_overlap_size{$overlap} += $snum;
198009598544 Uploaded
romaingred
parents:
diff changeset
76 $overlap_number += $snum;
198009598544 Uploaded
romaingred
parents:
diff changeset
77 }
198009598544 Uploaded
romaingred
parents:
diff changeset
78 else
198009598544 Uploaded
romaingred
parents:
diff changeset
79 {
198009598544 Uploaded
romaingred
parents:
diff changeset
80 $num_per_overlap_size{$overlap} += $rnum ;
198009598544 Uploaded
romaingred
parents:
diff changeset
81 $overlap_number += $rnum ;
198009598544 Uploaded
romaingred
parents:
diff changeset
82 }
198009598544 Uploaded
romaingred
parents:
diff changeset
83 }
198009598544 Uploaded
romaingred
parents:
diff changeset
84 }
198009598544 Uploaded
romaingred
parents:
diff changeset
85 }
198009598544 Uploaded
romaingred
parents:
diff changeset
86
198009598544 Uploaded
romaingred
parents:
diff changeset
87 if ( $max != 0 )
198009598544 Uploaded
romaingred
parents:
diff changeset
88 {
198009598544 Uploaded
romaingred
parents:
diff changeset
89 my @overlaps = ();
198009598544 Uploaded
romaingred
parents:
diff changeset
90 push @overlaps_names, $k;
198009598544 Uploaded
romaingred
parents:
diff changeset
91 for my $i (1..$max)
198009598544 Uploaded
romaingred
parents:
diff changeset
92 {
198009598544 Uploaded
romaingred
parents:
diff changeset
93 $num_per_overlap_size{$i} = 0 unless exists( $num_per_overlap_size{$i} );
198009598544 Uploaded
romaingred
parents:
diff changeset
94 push @overlaps, $num_per_overlap_size{$i};
198009598544 Uploaded
romaingred
parents:
diff changeset
95 }
198009598544 Uploaded
romaingred
parents:
diff changeset
96 push @distri_overlap, \@overlaps;
198009598544 Uploaded
romaingred
parents:
diff changeset
97 }
198009598544 Uploaded
romaingred
parents:
diff changeset
98
198009598544 Uploaded
romaingred
parents:
diff changeset
99 if ( $flag == 1 )
198009598544 Uploaded
romaingred
parents:
diff changeset
100 {
198009598544 Uploaded
romaingred
parents:
diff changeset
101 my $histo_png = $TE_dir.'histogram.png';
198009598544 Uploaded
romaingred
parents:
diff changeset
102 histogram( \%num_per_overlap_size, $histo_png, $overlap_number );
198009598544 Uploaded
romaingred
parents:
diff changeset
103 print $txt "size\tnumber\tpercentage of the total overlap number\n";
198009598544 Uploaded
romaingred
parents:
diff changeset
104 foreach my $k ( sort {$a <=> $b} keys %num_per_overlap_size )
198009598544 Uploaded
romaingred
parents:
diff changeset
105 {
198009598544 Uploaded
romaingred
parents:
diff changeset
106 my $percentage = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
107 $percentage = $num_per_overlap_size{$k} * 100 / $overlap_number unless $overlap_number == 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
108 print $txt "$k\t$num_per_overlap_size{$k}\t"; printf $txt "%.2f\n",$percentage;
198009598544 Uploaded
romaingred
parents:
diff changeset
109
198009598544 Uploaded
romaingred
parents:
diff changeset
110 }
198009598544 Uploaded
romaingred
parents:
diff changeset
111 close $txt;
198009598544 Uploaded
romaingred
parents:
diff changeset
112 }
198009598544 Uploaded
romaingred
parents:
diff changeset
113
198009598544 Uploaded
romaingred
parents:
diff changeset
114 }
198009598544 Uploaded
romaingred
parents:
diff changeset
115
198009598544 Uploaded
romaingred
parents:
diff changeset
116 foreach my $tabP ( @distri_overlap )
198009598544 Uploaded
romaingred
parents:
diff changeset
117 {
198009598544 Uploaded
romaingred
parents:
diff changeset
118 my $sum = sum($tabP);
198009598544 Uploaded
romaingred
parents:
diff changeset
119 my $ten = $tabP->[9];
198009598544 Uploaded
romaingred
parents:
diff changeset
120 my $mean = mean($tabP);
198009598544 Uploaded
romaingred
parents:
diff changeset
121 my $std = standard_deviation($tabP, $mean);
198009598544 Uploaded
romaingred
parents:
diff changeset
122 my $zsc = z_significance($ten, $mean, $std);
198009598544 Uploaded
romaingred
parents:
diff changeset
123 my $name = shift @overlaps_names;
198009598544 Uploaded
romaingred
parents:
diff changeset
124 my $prob = 'NA';
198009598544 Uploaded
romaingred
parents:
diff changeset
125 $prob = 1 - &Math::CDF::pnorm( $zsc ) if $zsc ne 'NA';
198009598544 Uploaded
romaingred
parents:
diff changeset
126 print $ppp_f (join ("\t", $name, $sum, $ten, $mean, $std, $zsc, $prob ),"\n" );
198009598544 Uploaded
romaingred
parents:
diff changeset
127 }
198009598544 Uploaded
romaingred
parents:
diff changeset
128 close $ppp_f;
198009598544 Uploaded
romaingred
parents:
diff changeset
129 }
198009598544 Uploaded
romaingred
parents:
diff changeset
130
198009598544 Uploaded
romaingred
parents:
diff changeset
131 sub count_mapped
198009598544 Uploaded
romaingred
parents:
diff changeset
132 {
198009598544 Uploaded
romaingred
parents:
diff changeset
133 my ( $fai, $in_file ) = @_;
198009598544 Uploaded
romaingred
parents:
diff changeset
134 my ( %mapped, %dup );
198009598544 Uploaded
romaingred
parents:
diff changeset
135
198009598544 Uploaded
romaingred
parents:
diff changeset
136 open my $f, '<', $fai || die "cannot open $fai $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
137 while(<$f>)
198009598544 Uploaded
romaingred
parents:
diff changeset
138 {
198009598544 Uploaded
romaingred
parents:
diff changeset
139 if ($_ =~ /(.*)\t(\d+)\n/)
198009598544 Uploaded
romaingred
parents:
diff changeset
140 {
198009598544 Uploaded
romaingred
parents:
diff changeset
141 $mapped{$1} = [];
198009598544 Uploaded
romaingred
parents:
diff changeset
142 $mapped{$1}->[0] = []; $mapped{$1}->[1] = [];
198009598544 Uploaded
romaingred
parents:
diff changeset
143 }
198009598544 Uploaded
romaingred
parents:
diff changeset
144 }
198009598544 Uploaded
romaingred
parents:
diff changeset
145 close $f;
198009598544 Uploaded
romaingred
parents:
diff changeset
146
198009598544 Uploaded
romaingred
parents:
diff changeset
147 open my $infile, "samtools view $in_file |"|| die "cannot open input file $! \n";
198009598544 Uploaded
romaingred
parents:
diff changeset
148 while(<$infile>)
198009598544 Uploaded
romaingred
parents:
diff changeset
149 {
198009598544 Uploaded
romaingred
parents:
diff changeset
150 unless ($_ =~ /^\@[A-Za-z][A-Za-z](\t[A-Za-z][A-Za-z0-9]:[ -~]+)+$/ || $_ =~ /^\@CO\t.*/ )
198009598544 Uploaded
romaingred
parents:
diff changeset
151 {
198009598544 Uploaded
romaingred
parents:
diff changeset
152 my @line = split (/\t/,$_);
198009598544 Uploaded
romaingred
parents:
diff changeset
153 if ($line[1] == 0)
198009598544 Uploaded
romaingred
parents:
diff changeset
154 {
198009598544 Uploaded
romaingred
parents:
diff changeset
155 push @{$mapped{$line[2]}->[0]} , [$line[3], $line[9], $line[1], $line[2]] unless exists ($dup{$line[3].$line[9].$line[1].$line[2]});
198009598544 Uploaded
romaingred
parents:
diff changeset
156 $dup{$line[3].$line[9].$line[1].$line[2]}+=1;
198009598544 Uploaded
romaingred
parents:
diff changeset
157 }
198009598544 Uploaded
romaingred
parents:
diff changeset
158 elsif ($line[1] == 16)
198009598544 Uploaded
romaingred
parents:
diff changeset
159 {
198009598544 Uploaded
romaingred
parents:
diff changeset
160 push @{$mapped{$line[2]}->[1]} , [$line[3], $line[9], $line[1], $line[2]] unless exists ($dup{$line[3].$line[9].$line[1].$line[2]});
198009598544 Uploaded
romaingred
parents:
diff changeset
161 $dup{$line[3].$line[9].$line[1].$line[2]}+=1;
198009598544 Uploaded
romaingred
parents:
diff changeset
162 }
198009598544 Uploaded
romaingred
parents:
diff changeset
163 }
198009598544 Uploaded
romaingred
parents:
diff changeset
164 }
198009598544 Uploaded
romaingred
parents:
diff changeset
165 close $infile;
198009598544 Uploaded
romaingred
parents:
diff changeset
166 return (\%mapped, \%dup );
198009598544 Uploaded
romaingred
parents:
diff changeset
167 }
198009598544 Uploaded
romaingred
parents:
diff changeset
168
198009598544 Uploaded
romaingred
parents:
diff changeset
169 sub sum
198009598544 Uploaded
romaingred
parents:
diff changeset
170 {
198009598544 Uploaded
romaingred
parents:
diff changeset
171 my $arrayref = shift;
198009598544 Uploaded
romaingred
parents:
diff changeset
172 my $result = 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
173 foreach (@$arrayref) {$result += $_}
198009598544 Uploaded
romaingred
parents:
diff changeset
174 return $result;
198009598544 Uploaded
romaingred
parents:
diff changeset
175 }
198009598544 Uploaded
romaingred
parents:
diff changeset
176
198009598544 Uploaded
romaingred
parents:
diff changeset
177 sub mean
198009598544 Uploaded
romaingred
parents:
diff changeset
178 {
198009598544 Uploaded
romaingred
parents:
diff changeset
179 my $arrayref = shift;
198009598544 Uploaded
romaingred
parents:
diff changeset
180 my $result;
198009598544 Uploaded
romaingred
parents:
diff changeset
181 foreach (@$arrayref) {$result += $_}
198009598544 Uploaded
romaingred
parents:
diff changeset
182 return $result / scalar(@$arrayref);
198009598544 Uploaded
romaingred
parents:
diff changeset
183 }
198009598544 Uploaded
romaingred
parents:
diff changeset
184
198009598544 Uploaded
romaingred
parents:
diff changeset
185 sub standard_deviation
198009598544 Uploaded
romaingred
parents:
diff changeset
186 {
198009598544 Uploaded
romaingred
parents:
diff changeset
187 my ($arrayref, $mean) = @_;
198009598544 Uploaded
romaingred
parents:
diff changeset
188 return sqrt ( mean ( [map $_**2 , @$arrayref ]) - ($mean**2));
198009598544 Uploaded
romaingred
parents:
diff changeset
189 }
198009598544 Uploaded
romaingred
parents:
diff changeset
190
198009598544 Uploaded
romaingred
parents:
diff changeset
191 sub z_significance
198009598544 Uploaded
romaingred
parents:
diff changeset
192 {
198009598544 Uploaded
romaingred
parents:
diff changeset
193 my ($ten, $mean, $std) = @_;
198009598544 Uploaded
romaingred
parents:
diff changeset
194 my $z = 'NA';
198009598544 Uploaded
romaingred
parents:
diff changeset
195 $z = (($ten - $mean) / $std) if $std != 0;
198009598544 Uploaded
romaingred
parents:
diff changeset
196 return $z;
198009598544 Uploaded
romaingred
parents:
diff changeset
197 }
198009598544 Uploaded
romaingred
parents:
diff changeset
198
198009598544 Uploaded
romaingred
parents:
diff changeset
199 1;