forked from pinetree1/crispr-dav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.pl
executable file
·310 lines (278 loc) · 10.6 KB
/
report.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
#!/usr/bin/env perl
# Creates html page
# Author: X. Wang
use 5.010;
use strict;
use File::Basename;
use Getopt::Long;
use File::Path qw(make_path);
use FindBin qw($Bin);
my $usage = "Usage: $0 [option] indir outdir
--ref <str> reference name, e.g. hg19. Required.
--gene <str> Gene name, e.g. FPR2. Required.
--region <str> a bed file of amplicon. Required.
--crispr <str> a bed file containing sgRNA region. Required.
--cname <str> Name of CRISPR site. Optional.
--nocx Do not to create canvasXpress alignment view
--high_res High resolution tiff image was created.
--min_qual_mean <int> minimum mean quality score of a read
--min_len <int> minimum length of a read
--ns_max_p <int> max percentage of Ns in read
--realign Flag to turn on realignment with ABRA
--min_mapq <int> Minimum mapping quality score
--wing_length <int> Number of bases on each side of sgRNA to view SNP
indir input directory where intermediate result files (e.g.
plot image files) are, e.g ./align.
outdir output directory, e.g. ./deliberables
";
my %h;
GetOptions( \%h, 'ref=s', 'region=s', 'crispr=s', 'gene=s', 'cname=s', 'nocx',
'high_res', 'min_qual_mean=i', 'min_len=i', 'ns_max_p=i', 'realign',
'min_mapq=i', 'wing_length=i' );
die $usage if ( @ARGV != 2 );
my ( $indir, $outdir ) = @ARGV;
die "Missing required argument."
if ( !$h{ref}
or !$h{region}
or !$h{gene}
or !$h{crispr} );
my $ref = $h{ref};
my $gene = $h{gene};
my ( $amp_chr, $amp_start, $amp_end, $amp_name, $amp_seq, $amp_strand ) =
getTarget( $h{region} );
my ( $chr, $start, $end, $site_name, $seq, $strand, $hdr ) =
getTarget( $h{crispr}, $h{cname} );
die "Incorrect crispr name $h{cname}.\n"
if ( $h{cname} && $h{cname} ne $site_name );
$amp_start++;
$start++;
## set up Assets
my $sitedir = "$outdir/$site_name";
make_path($sitedir);
qx(cp $Bin/Assets/* $sitedir/Assets);
## find samples
my @samples = getSamples( $indir, $site_name );
my $rowsep = scalar(@samples) > 10 ? "</tr><tr>" : "";
## find processing errors
my $err_rows;
foreach my $f ( sort glob("$indir/*.failed") ) {
basename($f) =~ /(.*)\.failed$/;
my $s = $1;
open(my $erf, $f);
my $err = <$erf>; chomp $err;
close $erf;
$err //= "Check log in $indir";
$err_rows .= "<tr><td>$s</td><td>$err</td></tr>";
}
# create web page
my $page = "$sitedir/index.html";
open( my $fh, ">$page" ) or die $!;
my $rc_tog = getToggleLink("rc"); ## read count
my $pp_tog = getToggleLink( "pp", 1 ); ## preprocessing
my $plot_ext = $h{high_res} ? "tif" : "png";
my $hdr_snp_flag = $hdr ? 1 : 0;
print $fh "<html lang='en'>
<head>
<meta charset='utf-8'>
<script src=Assets/crispr.js></script>
<body>
<h2>CRISPR Analysis Results</h2><p>
<p><b>Gene: $gene</b><p>
<table border=0><tr><td width=30></td><td>
<table border=1 cellpadding=3 cellspacing=0 style='border-collapse:collapse' width=800>
<tr align=center><th>Region</th><th>Reference</th><th>Chr</th>
<th>Start</th><th>End</th><th>Strand</th><th>CRISPR Sequence</th>
<th>Expected HDR Base Changes on + Strand</th></tr>
<tr align=center><td>Amplicon</td><td>$ref</td><td>$amp_chr</td>
<td>$amp_start</td><td>$amp_end</td><td>$amp_strand</td><td></td>
<td></td></tr>
<tr align=center><td>$site_name</td><td>$ref</td><td>$chr</td>
<td>$start</td> <td>$end</td><td>$strand</td><td>$seq</td>
<td>$hdr</td></tr>
</table>
</td></tr></table>
<div id='high_res' style='display:none'>$h{high_res}</div>
<p><b>Read Counts and Percentages at CRISPR Site:</b>$rc_tog
<table><tr>
<td><img src=Assets/$site_name.indelcnt.$plot_ext /></td>$rowsep
<td><img src=Assets/$site_name.indelpct.$plot_ext /></td>
</tr></table>
</div>
<!--select box for individual sample-->
<p><b>Charts for Individual Sample</b>
<select id='select1' onchange='showCharts(\"$site_name\",$hdr_snp_flag)'>
<option value=''>Select a sample to view</option>
";
foreach my $s (@samples) {
print $fh "<option value=\'$s\'>$s</option>";
}
print $fh "</select>
<p id='charts'></p>
<p><b>Preprocessing of Reads: </b>$pp_tog
<table><tr>
<td><img src=Assets/$site_name.readcnt.$plot_ext></td>$rowsep
<td><img src=Assets/$site_name.readchr.$plot_ext></td>
</tr></table>
";
if ( $err_rows ) {
print $fh "<br>
<table border=0><tr><td width=30></td><td>
<table border=1 cellpadding=3 cellspacing=0
style='border-collapse:collapse' width=400>
<tr><th>Sample</th><th>Processing Error</th></tr>
$err_rows
</table>
</td></tr></table>
";
}
print $fh "</div>";
## categories with each plot for individual sample
my %category_header = (
"len" => "Allele Frequency at CRISPR Site",
"cov" => "Amplicon Coverage",
"ins" => "Insertion Distribution in Amplicon",
"del" => "Deletion Distribution in Amplicon",
"snp" => "SNP Frequency at CRISPR Site (Not all positions are on same read)",
"hdr.snp" =>"SNP Frequency in HDR region (All positions are on same read)"
);
my @cats = ( "cov", "ins", "del", "len", "snp" );
push(@cats, "hdr.snp") if $hdr;
foreach my $cat (@cats) {
my $tab = getCategoryTable( $cat, $plot_ext );
my $tog = getToggleLink( $cat, 1 );
print $fh "<p><b>$category_header{$cat}:</b>$tog\n$tab</div>\n\n";
}
## HDR chart
if ($hdr) {
my $hdr_tog = getToggleLink( "hdr", 1 );
print $fh "
<p><b>Homology Directed Repair Rates: </b>$hdr_tog
<table border=0><tr><td width=30></td><td>
<table border=0>
<tr><tr><td>When categorizing oligo types, the region from the first to the last base
change was examined.</td></tr>
<tr><td>Non-Oligo: None of the intended base changes occurs, regardless of indel.</td></tr>
<tr><td>Partial Oligo: Some but not all intended base changes occur, and no indel.</td></tr>
<tr><td>Edited Oligo: One or more intended base changes occur, and there is indel(s).</td></tr>
<tr><td>Perfect Oligo: All intended base changes occur, but no indel. Its value is labeled.</td></tr>
<tr>
<td><img src=Assets/$site_name.hdr.$plot_ext /></td>$rowsep
</tr>
</table>
</td></tr></table>
</div>
";
}
## Allele alignment view
my $tog = getToggleLink( "cvxp", 1 );
if ( !$h{nocx} ) {
print $fh "<p><b>Alignment of Indel Alleles:</b>$tog
<table border=0><tr><td width=30></td><td>
<table border=0 cellpadding=3 cellspacing=0 style='border-collapse:collapse'>
<tr><td><a href=${site_name}_cx1.html>Alleles with indel rate ≥ 1%</a></td></tr>
<tr><td><a href=${site_name}_cx0.html>All alleles</a></td></tr>
</table>
</td></tr></table>
</div>
";
}
## Analysis parameters
$tog = getToggleLink( "param", 1 );
my $realign = $h{realign}? "Y" : "N";
print $fh "<p><b>Parameters Used in Analysis:</b>$tog
<table border=0><tr><td width=30></td><td>
<table border=1 cellpadding=3 cellspacing=0 style='border-collapse:collapse'>
<tr><th>Parameter</th><th>Value</th></tr>
<tr><td>Minimum mean quality score of read</td><td align=center>$h{min_qual_mean}</td></tr>
<tr><td>Minimum length of read</td><td align=center>$h{min_len}</td></tr>
<tr><td>Maximum percentage of non-called base N in read</td><td align=center>$h{ns_max_p}</td></tr>
<tr><td>Minimum mapping quality score of read</td><td align=center>$h{min_mapq}</td></tr>
<tr><td>Perform ABRA realignment after initial BWA alignment</td><td align=center>$realign</td></tr>
<tr><td>Number of bases on each side of guide sequence to view SNP</td><td align=center>$h{wing_length}</td></tr>
</table>
</td></tr></table>
</div>
";
## Spreadsheet data
$tog = getToggleLink( "data", 1 );
print $fh "<p><b>Spreadsheet Data:</b>$tog
<table border=0><tr><td width=30></td><td>
<table border=0 cellpadding=3 cellspacing=0 style='border-collapse:collapse'>
<tr><td><a href=Assets/${site_name}_cnt.xlsx>Read stats</a></td></tr>
<tr><td><a href=Assets/${site_name}_pct.xlsx>Indel summary</a></td></tr>
<tr><td><a href=Assets/${site_name}_len.xlsx>Allele data</a></td></tr>
<tr><td><a href=Assets/${site_name}_snp.xlsx>SNP data (Not all positions are on same read)</a></td></tr>
";
if ($hdr) {
print $fh
"<tr><td><a href=Assets/${site_name}_hdr.snp.xlsx>SNP data (All positions are on same read)</a></td></tr>
<tr><td><a href=Assets/${site_name}_hdr.xlsx>HDR data</a></td></tr>";
}
print $fh "</table>
</td></tr></table>
</div>
</body></html>";
close $fh;
## return a category's html table of all samples
# input: extension of plots, like len.png for length distribution plot
sub getCategoryTable {
my ( $cat, $plot_ext ) = @_;
my $i = 0;
my $CELLS = $cat eq "snp" ? 1 : 2; # number of cells per row
my $tab = "<table border=0>";
foreach my $s (@samples) {
my $img = "$sitedir/Assets/$s.$site_name.$cat.$plot_ext";
next if !-f $img;
if ( $i % $CELLS == 0 ) {
$tab .= "</tr>" if ($i);
$tab .= "<tr>";
}
$tab .= "<td><img src=Assets/$s.$site_name.$cat.$plot_ext></td>";
$i++;
}
$tab .= "</tr>" if ( $tab !~ /<\/tr>$/ );
$tab .= "</table>";
return $tab;
}
# name is in 4th column
sub getTarget {
my ( $bedfile, $name ) = @_;
die "Cannot find $bedfile\n" if !-f $bedfile;
### Only the first coordinate entry is used.
open( F, $bedfile ) or die "cannot open target file $bedfile\n";
my $line;
while ( $line = <F> ) {
next if ( $line =~ /^\#/ or $line !~ /\w/ );
chomp $line;
my @a = split( /\s+/, $line );
if ( !$name or $a[3] eq $name ) {
last;
}
}
close F;
return split( /\s+/, $line ); # (chr, start, end, name, score, strand, hdr)
}
## find samples of this CRISPR site
sub getSamples {
my ( $indir, $site_name ) = @_;
my @samples;
foreach my $f ( glob("$indir/*.$site_name.pct") ) {
basename($f) =~ /(.*)\.${site_name}\.pct$/;
push( @samples, $1 );
}
die "No sample data for $site_name\n" if !@samples;
return sort @samples;
}
sub getToggleLink {
my ( $sn, $hide ) = @_;
# section name, like: pp --preprocessing
# hide: 1 or 0
my $fname = $hide ? "plus.jpg" : "minus.jpg";
my $style = $hide ? "none" : "block";
# In IE, we have to specify no border.
my $str =
"<a href=\"javascript:hideshow(\'$sn.div\');toggleImg(\'$sn.img\')\">";
$str .= "<img style='border:0;' src=Assets/$fname width=15 height=15 id=\'$sn.img\'></a>";
$str .= "\n<div id=\"$sn.div\" style=\"display:$style\">";
return $str;
}