comparison [APliBio]Nebula tools suite/Nebula/CreateControlSubSet/createControlSubSet.pl @ 0:2ec3ba0e9e70 draft

Uploaded
author alermine
date Thu, 25 Oct 2012 08:18:25 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2ec3ba0e9e70
1 #:t:::::::::::::::::g@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
2 #:t::::::::::::::;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
3 #:::::::::::::z;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
4 #::::::::::::i@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
5 #::::::::::::@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@$@@@@
6 #:::::::::::3@@@@@@@@@@@@@@@@@@@@@@@@@B@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
7 #::::::::::3@@@@@@@@@@@@@@@@@@@@@BEEESSE5EEEEBBM@@@@@@@@@@@@@@@@@@@@@@@@@@
8 #::::::::::3@@@@@@@@@@@@@@@@@@@@BEEEEEE35EE55E2355E5SBMB@@@@@@@@@@@@@@@@@$
9 #::::::::::@@@@@@@@@@@@@@@@@@@EEEE55533t3tttt::::::!!!!7755E755SBBMMM@@@MM
10 #::::::::::3@@@@@@@@@@@@@@@@@@EEEE2t3ttttt:::::::::::::::::::::::!7?5225EE
11 #::::::::::3@@@@@@@@@@@@@@@@@@EEEEE31t::::::::::::::::::::::::::::::::3E5@
12 #::::::::::3@@@@@@@@@@@@@@@@@@EEEEEEtt:::::::::::::::::::::::::::::::::353
13 #::::::::::3@@@@@@@@@@@@@@@@@@EEEEEE1ttz::::::::::::::::::::::::::::::::35
14 #:::::::::::@@@@@@@@@@@@@@@@@@EEEEEEEtz1::::::::::::::::::::::::::::::::t:
15 #:::::::::!3@@@@@@@@@@@@@@@@@@@EEEEEttt::::::::::::::::::::::::::::::::;zz
16 #::::::::::@@@@@@@@@@@@@@@@@@@@EEEEEttt:::::z;z:::::::::::::::::::::::::13
17 #::::::::::3B@@@@@@@@@@@@@@@@@@EEEEEEE3tt:czzztti;:::::::::::::::::::::::3
18 #::::ttt::::3@@@@@@@@@@@@@@@@EEEEE5EE25Ezt1EEEz5Etzzz;;;;:::::::::::::::::
19 #:::::::::::I9@@@@@@@@@@@@@@@@@@@@@@@@@@EEEEEE@@@@@@@@@@@@@@Ez;:::::::::::
20 #:::::::::::::E@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Ez::::::
21 #::::::::::::::E@@@@@@@@@@@@@@@@@@@@@@@@@@@@@BE5EBB@@@@@@@@@@@@@@@EEE:::::
22 #:::::::::::::::@@@@@@@@@@@@@@@@@@@@@@@@@@@@E1::35@@@@@@@@@@ME3MMME2::::::
23 #:::::::::::::::?@@@@@@@@@@@@@@@@@@M@@@@@@@EE:::::3SB@@BBESEEt::::::::::::
24 #::::::::::::::::J$@@@@@@@B@@@@@@@@@@@@@@@@EE:::::::!35E33t:::::::::::::::
25 #:::::::::::::::::3@E@@@EE5EESE5EESE@@@@@@@Et::::::::::::tz:::::::::::::::
26 #:::::::::::::::::J@E$@EEE5133555SE@@@@@@@@Et:::::::::::::::::::::::::::::
27 #::::::::::::::::::E@E@EEEEtt3523EEE@@@@@@@E::::::::::::::::::::::::::::::
28 #:t::::::::::::::::JEE3@@@EEEEEEEEEE@@@@@@@E:::::::::t;:::::::::::::::::::
29 #:t:::::::::::::::::!5ES@EEEEEEEEES@@@@@@@@@E;:::;;;:3Ez::::::::::::::::::
30 #:t::::::::::::::::::::JE@@EEEEEEE@@@@@@@@@@@@@@@@ME!:::;:::::::::::::::::
31 #:tz::::::::::::::::::::JE@@@EEEE@@@@@@@@@@@@@@EE!:::::::t::::::::::::::::
32 #:t::::::::::::::::::::::3@@@@@@@@@@@@@@@@@@ESBE::::::::::::::::::::::::::
33 #:::::::::::::::::::::::::Q@@@@@@@@@@@@@@@@EE3EE;:::::zzzz::::::::::::::::
34 #:::::::::::::::::::::::::3@@@@@@@@@@@@@@@@@@@@@@NN@@@@@@Ez:::::::::::::::
35 #:zt:::::::::::::::::::::::3@@@@EE@@@@@@@@@@EEEEt::;z113E5t:::::::::::::::
36 #::tt:::::::::::::::::::::::3@@@E@@@@@@@@@@@@@@@@BEt::::::::::::::::t:::::
37 #:tt:t:::::::::::::::::::::::?S@@@@@@@@@@@BBEEE51!::::::::::::::zzzEt:::::
38 #::::::::::::::::::::::::::::::3Q@@@@@@@BEEEEEt:::::::::::::;zz@@@EE::::::
39 #::::::::::::::::::::::::::::::::75B@@@@@EEEtt;:::::::::;zz@@@@BEEEtz:::::
40 #::::::::::::::::::::::::::::::::::::?9@@@@@@@@@@@E2Ezg@@@@@B@@@EEEE1t::::
41 #:::::::::::::::::::::::::::::::::::::::3@@@@@@@@@@@@@@@@@@@E@EEEEEEEzzz::
42 #::::::::::::::::::::::::::::::::::::;@@@@@@@@@@@@@@@@@@@@@@@EEEEEEE5ttttt
43 #:::::::::::::::::::::::::::::::;g@@@@@@@@@@@@@@@@@@@@@@@@@@EEEEEEEEEEEtzt
44 #::::::::::::::::::::::::::::;@@@@@@@@@@@@@@@@@@@@@@@@@@E@@EEEEEEEEEEEE@@@
45 #::::::::::::::::::::::::::g@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@EEEE3EEEE@@@@@@@
46 #:::::::::::::::::::::;;g@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@EEEt33@@@@@@@@@@
47 #:::::::::::::::::;g@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@E@@@@@@EEEtg@@@@@@@@@@@@
48 #::::::::::::::;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@EEEE@@@@@@@@@@@@@@@@@@@@@@@@
49 #:::::::::::::@@@@@@@@@@@@@@@@@$@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
50 #::::::::::;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
51 #
52 # Copyleft ↄ⃝ 2012 Institut Curie
53 # Author(s): Valentina Boeva, Alban Lermine (Institut Curie) 2012
54 # Contact: valentina.boeva@curie.fr, alban.lermine@curie.fr
55 # This software is distributed under the terms of the GNU General
56 # Public License, either Version 2, June 1991 or Version 3, June 2007.
57
58 #!/usr/bin/perl
59
60 #filter out dulpicates from SAMPLE (optional) and create a control dataset w/o duplicates with the same number of reads as in the SAMPLE
61
62 use strict;
63 use warnings;
64 use diagnostics;
65
66 my $usage = qq{
67 $0
68
69 -----------------------------
70 mandatory parameters:
71
72 -f CHiP_file
73 -c control_file
74 -t type [bam, sam, eland]
75 -o output file
76 -----------------------------
77 optional parameters:
78
79 none
80 };
81
82 if(scalar(@ARGV) == 0){
83 print $usage;
84 exit(0);
85 }
86
87 ## mandatory arguments
88
89 my $filename = "";
90 my $output_fname = "";
91
92 my $controlFilename = "";
93 my $type = "";
94 my $sampleOutput = "";
95 my $samtools_bin_dir=;
96
97
98 ## optional arguments
99
100 ## parse command line arguments
101
102 while(scalar(@ARGV) > 0){
103 my $this_arg = shift @ARGV;
104 if ( $this_arg eq '-h') {print "$usage\n"; exit; }
105
106 elsif ( $this_arg eq '-f') {$filename = shift @ARGV;}
107 elsif ( $this_arg eq '-c') {$controlFilename = shift @ARGV;}
108 elsif ( $this_arg eq '-t') {$type = shift @ARGV;}
109 elsif ( $this_arg eq '-o') {$output_fname = shift @ARGV;}
110 elsif ( $this_arg eq '-s') {$sampleOutput = shift @ARGV;}
111
112
113 elsif ( $this_arg =~ m/^-/ ) { print "unknown flag: $this_arg\n";}
114 }
115
116 if ( $filename eq ""){
117 die "you should specify chip file\n";
118 }
119 if( $controlFilename eq ""){
120 die "you should specify control file\n";
121 }
122 if( $type eq ""){
123 die "you should specify file type (bam, sam or eland)\n";
124 }
125 if( $output_fname eq ""){
126 die "you should specify output filename\n";
127 }
128
129
130 print "\n-----------------\n\n";
131
132 my %hash;
133 my $chipCount = 0;
134 my @header;
135
136
137 if ($type eq "eland") {
138 open FILE, "< $filename " || die "$filename : $!\n";
139 while(<FILE>){
140 my @fields = split(/\t/,$_);
141 my $entry = $fields[6].":".$fields[7]."-".$fields[8];
142 unless (exists($hash{$entry})) {
143 $hash{$entry} = $_;
144 $chipCount++;
145 }
146 }
147 } elsif ($type eq "sam") {
148 open FILE, "< $filename " || die "$filename : $!\n";
149 while(<FILE>){
150 if (m/^@/) {
151 push(@header,$_);
152 next;
153 }
154 my @fields = split(/\t/,$_);
155 next if (scalar(@fields)<10);
156 my $entry = $fields[2].":".$fields[3]."-".$fields[1];
157 unless (exists($hash{$entry})) {
158 $hash{$entry} = $_;
159 $chipCount++;
160 }
161 }
162 } elsif ($type eq "bam") {
163 open(FILE, "$samtools_bin_dir/samtools view -h $filename |") or die "$0: can't open ".$filename.":$!\n";
164 while(<FILE>){
165 if (m/^@/) {
166 push(@header,$_);
167 next;
168 }
169 my @fields = split(/\t/,$_);
170 next if (scalar(@fields)<10);
171 my $entry = $fields[2].":".$fields[3]."-".$fields[1];
172 unless (exists($hash{$entry})) {
173 $hash{$entry} = $_;
174 $chipCount++;
175 }
176 }
177 }
178 close FILE;
179 print "ChIP: $chipCount\n";
180
181 if ($sampleOutput ne "") {
182
183 open OUT, "> $sampleOutput" || die "$sampleOutput: $!\n";
184
185 if ($type eq "bam" || $type eq "sam") { #print header
186 for my $headerLine (@header) {
187 print OUT $headerLine;
188 }
189 }
190 for my $line (values %hash) {
191 print OUT $line;
192 }
193 close OUT;
194 }
195
196 delete @hash{keys %hash};
197 @header = ();
198
199 my $controlCount = 0;
200 if ($type eq "eland") {
201 open FILE, "< $controlFilename " || die "$controlFilename : $!\n";
202 while(<FILE>){
203 my @fields = split(/\t/,$_);
204 my $entry = $fields[6].":".$fields[7]."-".$fields[8];
205 unless (exists($hash{$entry})) {
206 $hash{$entry} = $_;
207 $controlCount++;
208 }
209 }
210 } elsif ($type eq "sam") {
211 open FILE, "< $controlFilename " || die "$controlFilename : $!\n";
212 while(<FILE>){
213 if (m/^@/) {
214 push(@header,$_);
215 next;
216 }
217 my @fields = split(/\t/,$_);
218 my $entry = $fields[2].":".$fields[3]."-".$fields[1];
219 unless (exists($hash{$entry})) {
220 $hash{$entry} = $_;
221 $controlCount++;
222 }
223 }
224 } elsif ($type eq "bam") {
225 open(FILE, "$samtools_bin_dir/samtools view -h $controlFilename |") or die "$0: can't open ".$controlFilename.":$!\n";
226 while(<FILE>){
227 if (m/^@/) {
228 push(@header,$_);
229 next;
230 }
231 my @fields = split(/\t/,$_);
232 my $entry = $fields[2].":".$fields[3]."-".$fields[1];
233 unless (exists($hash{$entry})) {
234 $hash{$entry} = $_;
235 $controlCount++;
236 }
237 }
238 }
239 close FILE;
240 print "Control: $controlCount\n";
241 my $prob = $chipCount/$controlCount;
242
243 open OUT, "> $output_fname" || die "$output_fname: $!\n";
244
245 if ($type eq "bam" || $type eq "sam") { #print header
246 for my $headerLine (@header) {
247 print OUT $headerLine;
248 }
249 }
250 my $count = 0;
251
252 for my $line (values %hash) {
253 my $rand = rand();
254
255 if ($rand < $prob) {
256 print OUT $line;
257 $count ++;
258 }
259 last if ($count == $chipCount);
260 }
261
262
263 if ($count < $chipCount) {
264
265 $prob = ($chipCount-$count)/$controlCount*1.1;
266
267 for my $line (values %hash) {
268 my $rand = rand();
269
270 if ($rand < $prob) {
271 print OUT $line;
272 $count ++;
273 }
274 last if ($count == $chipCount);
275 }
276 }
277
278 print "count = $count\n";
279 close OUT;
280