-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine_rec.pl
395 lines (321 loc) · 10.1 KB
/
combine_rec.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
use warnings;
use strict;
#Thu Oct 10 08:29:46 CST 2018 by huang_dawei
use warnings;
use strict;
use Getopt::Long;
my %opts;
GetOptions(\%opts,
"readcount_dir:s",
"readcount_fileExtension:s",
"filter_libNo:s",
"filter_tissue:s",
"filter_week:s",
"readcount:s",
"readcount_percent:s",
"readcount_percent_mean:s",
"baseline_compare:s"
);
my $usage= <<"USAGE";
Program: $0
INPUT:
-readcount_dir ./readcount/
-readcount_fileExtension readcount
-filter_libNo lib01,lib31,lib32,lib33
-filter_tissue Brain,Kidney
-filter_week W1,W2
-readcount_percent 1 or 0
-readcount_percent 1 or 0
-readcount_percent_mean 1 or 0
-baseline_compare 1 or 0
FILES REQUIRED:none
OUTPUT:
USAGE
die $usage unless ($opts{readcount_dir});
die $usage unless ($opts{readcount_fileExtension});
$opts{readcount} = 0 if(!defined $opts{readcount} );
$opts{readcount_percent} = 0 if(!defined $opts{readcount_percent} );
$opts{readcount_percent_mean} = 0 if(!defined $opts{readcount_percent_mean} );
$opts{baseline_compare} = 0 if(!defined $opts{baseline_compare} );
my $startTime=localtime();
my $filter_tissue;
if(defined $opts{filter_tissue}){
my @r = split /,/,uc($opts{filter_tissue});
foreach(@r){
$filter_tissue->{$_} = 1;
}
}
my $filter_week;
if(defined $opts{filter_week}){
my @r = split /,/,uc($opts{filter_week});
foreach(@r){
$filter_week->{$_} = 1;
}
}
my $filter_libNo;
if(defined $opts{filter_week}){
my @r = split /,/,uc($opts{filter_libNo});
foreach(@r){
$filter_libNo->{$_} = 1;
}
}
open I, "< sgRNA_seq_libIndex.txt";
#defined a output order/sequence according to seq order in the result table (excel)
my @seqs;
my $lib_seq;
while(<I>){
chomp;
#Lib01 AACAATCAGAATACCAACACGGCTCCTACTGCAGGAACC
my @r = split /\t/,$_;
$lib_seq->{by_lib}->{uc($r[0])} = $r[1];
$lib_seq->{by_seq}->{$r[1]} = uc($r[0]);
}
close I;
open I, "< condition.conf.all.txt";
#read in the condition info and set a order for column in the output
my $file_group;
my $file_group_by_tissue;
my $wgc_id_info;
my $weeks;
while(<I>){
#R18055872LD01 W4-LN-807 ref
#
=cut
#time phase
baseline
Sample
W1
W2
W4
#tissue
Brain
Heart
Kidney
Lib
Liver
LN
Lung
Muscle
Spleen
=cut
next if(!/R.*/);
chomp;
my @r = split /\t/,$_;
my @time = split /-/,$r[1];
#W4-LN-807
#R18055862LD01 W4-Kidney-806 ref
my $time_tag = $time[0]. "-". uc($time[1]);
my $wgc_id = $r[0];
my $week = $time[0];
my $tissue = $time[1];
next if(defined $filter_tissue->{uc($tissue)});
next if(defined $filter_week->{uc($week)});
$weeks->{$week} = 1;
$wgc_id_info->{$wgc_id}->{week} = $week;
$wgc_id_info->{$wgc_id}->{tisu} = $tissue;
$wgc_id_info->{$wgc_id}->{file} = 0;
#print $r[0], "\t", $time_tag, "\n";
$file_group->{$time_tag}->{$wgc_id} = 1;
}
my $count;
my @rec_files;
# read and store seq count in each file.
opendir(DIR, $opts{readcount_dir});
while (defined(my $file = readdir(DIR)))
{
next if ($file!~/\.$opts{readcount_fileExtension}$/);
my $week ;
my $tissue;
if($file =~ /^(.*)\.$opts{readcount_fileExtension}$/){
my $wgc_id = $1;
if(!defined $wgc_id_info->{$wgc_id}){
# print "unknown wgc id: $wgc_id\n";
}else{
$wgc_id_info->{$wgc_id}->{file} = 1;
$week = $wgc_id_info->{$wgc_id}->{week};
$tissue = $wgc_id_info->{$wgc_id}->{tisu};
open I,"< $opts{readcount_dir}/$file";
my ($wgc_id, $extension) = split /\./,$file;
next if(!defined $wgc_id_info->{$wgc_id});
push @rec_files, $wgc_id;
while (<I>)
{
chomp;
if(/^[A|T|C|G]+.*\d+$/){
my @r = split /\t/,$_;
#AACTTGCAGCAAACCAATACAGGGCCTATTGTGGGAAAT 87268
my $seq = $r[0];
my $libNo = $lib_seq->{by_seq}->{$seq};
#reset abnormal libNo's readcount as none.
$r[1] = 0 if(defined $filter_libNo->{$libNo});
#store each data point of wgc id;
$count->{$wgc_id}->{eahr}->{$r[0]}->{cont} = $r[1];
$count->{$wgc_id}->{summ} += $r[1];
# print "$tissue\t$week\t$seq\t$wgc_id\n";
$file_group_by_tissue->{$tissue}->{$week}->{data}->{$seq}->{repe}->{$wgc_id}->{rec}->{count} = $r[1];
}
}
close I;
}
}
}
closedir DIR;
foreach my $wgc_id (keys %{$wgc_id_info}){
print $wgc_id if($wgc_id_info->{$wgc_id}->{file} == 0);
}
# cal percent for each sgRNA for each repeat.
foreach my $wgc_id (keys %{$count}){
foreach my $sgRNA (keys %{$count->{$wgc_id}->{eahr}}){
# print "$count->{$wgc_id}->{summ}\n";
$count->{$wgc_id}->{eahr}->{$sgRNA}->{pcnt} = $count->{$wgc_id}->{eahr}->{$sgRNA}->{cont} / $count->{$wgc_id}->{summ};
}}
sub Mean{
my $string = 0;
my $cnt = 1;
foreach(@_){
if($_ ne "NA"){
$string += $_;
$cnt += 1;
}
}
my $mean = $string/$cnt;
return $mean;
}
#BASELINE
#R18048294LD01 baseline-Lib-mix ref
foreach my $sgRNA (sort keys %{$file_group_by_tissue->{Lib}->{baseline}->{data} }){
my @pcnt = ();
foreach my $wgc_id (sort keys %{$file_group_by_tissue->{Lib}->{baseline}->{data}->{$sgRNA}->{repe}}){
push @pcnt, $count->{$wgc_id}->{eahr}->{$sgRNA}->{pcnt};
}
my $mpnt = Mean(@pcnt);
$file_group_by_tissue->{Lib}->{baseline}->{data}->{$sgRNA}->{stas}->{mpnt} = $mpnt;
}
foreach my $tissue (sort keys %{$file_group_by_tissue }){
foreach my $week (sort keys %{$file_group_by_tissue->{$tissue} }){
foreach my $sgRNA (sort keys %{$file_group_by_tissue->{$tissue}->{$week}->{data} }){
my @pcnt = ();
foreach my $wgc_id (sort keys %{$file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{repe}}){
push @pcnt, $count->{$wgc_id}->{eahr}->{$sgRNA}->{pcnt};
}
my $mpnt = Mean(@pcnt);
$file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{stas}->{mpnt} = $mpnt;
if(
$mpnt
>
$file_group_by_tissue->{Lib}->{baseline}->{data}->{$sgRNA}->{stas}->{mpnt}
){
$file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{stas}->{tORf} = 1;
}else{
$file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{stas}->{tORf} = 0;
}
# print $mpnt, "\n";
}
}
}
#print table column name/header
my @tissues = sort keys %{$file_group_by_tissue};
print "lib\tseq\t";
foreach my $week (sort keys %{$weeks}){
foreach my $tissue (sort keys %{$file_group_by_tissue}){
next if(defined $filter_tissue->{uc($tissue)});
next if(defined $filter_week->{uc($week)});
if($opts{readcount}==1){
foreach my $wgc_id (sort keys %{$wgc_id_info}){
if($wgc_id_info->{$wgc_id}->{week} eq $week &&
$wgc_id_info->{$wgc_id}->{tisu} eq $tissue){
if(defined $file_group_by_tissue->{$tissue}->{$week}->{data}){
print "$week-$tissue-$wgc_id\t";
}
}
}
}
print "$week-$tissue-p1\tp2\tp3\t" if($opts{readcount_percent}==1);
print "aveP\t" if($opts{readcount_percent_mean}==1);
print "baselineCheck\t" if($opts{baseline_compare}==1);
}}
print "\n";
foreach my $lib (sort keys %{$lib_seq->{by_lib}}){
my $sgRNA = $lib_seq->{by_lib}->{$lib};
print "$lib\t$sgRNA\t";
foreach my $week (sort keys %{$weeks}){
foreach my $tissue (sort keys %{$file_group_by_tissue}){
next if(defined $filter_tissue->{uc($tissue)});
next if(defined $filter_week->{uc($week)});
#output read count
if($opts{readcount}==1){
foreach my $wgc_id (sort keys %{$wgc_id_info}){
if(
$wgc_id_info->{$wgc_id}->{week} eq $week &&
$wgc_id_info->{$wgc_id}->{tisu} eq $tissue
){
if(defined $file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{repe}->{$wgc_id}){
print $file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{repe}->{$wgc_id}->{rec}->{count}, "\t";
}
}
}}
#output percent
if($opts{readcount_percent}==1){
foreach my $wgc_id (sort keys %{$wgc_id_info}){
if(
$wgc_id_info->{$wgc_id}->{week} eq $week &&
$wgc_id_info->{$wgc_id}->{tisu} eq $tissue
){
if(defined $file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{repe}->{$wgc_id}){
print $count->{$wgc_id}->{eahr}->{$sgRNA}->{pcnt}, "\t";
}
}
}
}
#output mean readcount percent and baseline comparision
if(defined $file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}){
print $file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{stas}->{mpnt}, "\t" if($opts{readcount_percent_mean}==1);
print $file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{stas}->{tORf}, "\t" if($opts{baseline_compare}==1);
}
}}
print "\n";
}
__END__
print "seq\t";
foreach my $tissue (sort keys %{$file_group_by_tissue }){
foreach my $week (sort keys %{$file_group_by_tissue->{$tissue} }){
foreach my $sgRNA (sort keys %{$file_group_by_tissue->{$tissue}->{$week}->{data} }){
foreach my $wgc_id (sort keys %{$file_group_by_tissue->{$tissue}->{$week}->{data}->{$sgRNA}->{repe}}){
}}}}
foreach my $time_tag (sort keys %{$file_group} ){
foreach my $wgc_id ( sort keys %{$file_group->{$time_tag}} ){
print "$time_tag\t";
}
}
print "\n";
print "seq\t";
foreach my $time_tag (sort keys %{$file_group} ){
foreach my $wgc_id ( sort keys %{$file_group->{$time_tag}} ){
print "$wgc_id\t";
}}
print "\n";
#print data ordered by seq for each wgc_id ordered by condition
foreach my $seq (@seqs){
print $seq, "\t";
foreach my $time_tag (sort keys %{$file_group} ){
foreach my $wgc_id ( sort keys %{$file_group->{$time_tag}} ){
print $count->{$wgc_id}->{$seq}, "\t";
}
}
print "\n";
}
#===============================================================================================================
__END__
my $options;
$options .= "-loci $opts{loci}";
$options .= "-loci $opts{loci}";
$options .= "-loci $opts{loci}";
my $endTime=localtime();
open LOG,">>$0.Program\_RLog";
print LOG "From \<$startTime\> to \<$endTime\>\tperl $0 $options\n";
close LOG;
__END__
sgRNA sequence 0
AACAATCAGAATACCAACACGGCTCCTACTGCAGGAACC 55639
AACTTACAATCGGCTAATACTGCACCCCAGACACAAACT 40805
AACCTACAGCAGCAAAACACCGCTCCTACTGTGGGGGCC 41929