-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayplotter.pl
executable file
·344 lines (264 loc) · 9.71 KB
/
arrayplotter.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
#!/usr/bin/env perl
# CPAN packages
use Data::Dumper;
use File::Basename;
use POSIX 'strftime';
use strict;
use Term::ProgressBar;
use JSON::XS;
=podmd
This is a utility script for plotting positional genome data:
- probes (log value and b-allele fraction)
- segments (segmented log value and b-allele fractions)
- labels
Examples:
* perl arrayplotter.pl -in ../../in/GSM325151/ -chr2plot 2 -plotregions 2:0-180000000 -markers 2:29332467-29575397:ALK:#ff3300 -factor_probedots 2 -marker_text_px 14
=cut
# local packages
my $path_of_this_module;
BEGIN {
$path_of_this_module = File::Basename::dirname( eval { ( caller() )[1] } );
push @INC, $path_of_this_module.'/..';
}
use PGX;
# command line input
our %args = @ARGV;
$args{'-plottype'} = 'array'; # fixed
$args{'-genome'} ||= 'GRCh38';
$args{'-do_allchros'} ||= 'y';
$args{'-plotregions'} ||= q{};
# (possibly) derived
if ($args{'-chr2plot'} =~ /\w/) { $args{'-do_allchros'} = 'n' }
$args{'-chr2plot'} ||= join(',', 1..22, 'X');
# file names and paths
$args{'-probefilename'} ||= $args{'-pfn'} ||= 'probes,cn.tsv';
$args{'-segfilename'} ||= $args{'-sfn'} ||= 'segments,cn.tsv';
$args{'-fracbprobefilename'}||= $args{'-fbpfn'} ||= 'probes,fracb.tsv';
$args{'-fracbsegfilename'} ||= $args{'-fbsfn'} ||= 'segments,fracb.tsv';
$args{'-defaultsfilename'} ||= $args{'-dfn'} ||= 'plotdefaults.yaml';
$args{'-callsetfilename'} ||= $args{'-csfn'} ||= 'callset.json';
$args{'-arraypath'} ||= $args{'-in'} ||= q{};
$args{'-arraypath'} =~ s/\/$//;
foreach (grep{ /filename$/} keys %args) {
my $file = $args{'-arraypath'}.'/'.$args{$_};
my $pathK = $_;
$pathK =~ s/name//;
$args{$pathK} = $file;
}
# the output files will be named later, if not given here as a single SVG
$args{'-out'} ||= $args{'-arraypath'};
$args{'-svgfilename'} ||= $args{'-svgfn'} ||= q{};
# check or feedback
_checkArgs();
my $outdir = $args{'-out'};
$outdir =~ s/\/$//;
# preconfigured objects
our $pgx = new PGX(\%args);
$pgx->{debug} = 1;
_printFeedback();
######## #### #### #### #### #### #### #### #### ####
######## main #### #### #### #### #### #### #### ####
######## #### #### #### #### #### #### #### #### ####
$pgx->pgx_add_fracbprobes_from_file($args{'-fracbprobefile'});
$pgx->pgx_add_fracbsegments_from_file($args{'-fracbsegfile'});
$pgx->pgx_add_probes_from_file($args{'-probefile'});
$pgx->pgx_add_segments_from_file($args{'-segfile'});
if (defined $pgx->{pgxfileheader}->{plotpars}) {
foreach (keys %{ $pgx->{pgxfileheader}->{plotpars} }) {
$args{$_} = $pgx->{pgxfileheader}->{plotpars}->{$_};
$pgx->{parameters}->{$_} = $pgx->{pgxfileheader}->{plotpars}->{$_};
}
}
$pgx->plot_adjust_random_probevalues();
$pgx->return_arrayplot_svg();
my $plotfile;
if ($args{'-plotregions'} =~ /\w\:\d+?\-\d+?(?:\,|$)/) {
$plotfile = 'arrayplot,chr'.$args{'-plotregions'}.'.svg';
$plotfile =~ s/[\:]/_/g;
$args{'-do_allchros'} = 'n';
}
elsif (scalar(@{$pgx->{parameters}->{chr2plot}}) < 22) {
$plotfile = 'arrayplot,chr'.$args{'-chr2plot'}.'.svg' }
else {
$plotfile = 'arrayplot.svg' }
if ($args{'-svgfilename'} =~ /^[\w\,\-]+?\.svg/i) {
$plotfile = $args{'-svgfilename'};
$args{'-do_allchros'} = 'n';
}
print "\nFiles are written to $outdir:\n\n";
print "$plotfile\n";
open (FILE, ">", $outdir.'/'.$plotfile);
binmode(FILE, ":utf8");
print FILE $pgx->{svg};
close FILE;
if ($args{'-do_allchros'} !~ /y/i) { exit }
my $i = 0;
# my $progBar = Term::ProgressBar->new(scalar @{$pgx->{parameters}->{chr2plot}});
my $chr2plot = $pgx->{parameters}->{chr2plot};
foreach my $chro (@$chr2plot) {
# re-initializing some values for multiple plots ...
$pgx->{parameters}->{chr2plot} = [$chro];
$pgx->{svg} = q{};
$pgx->{genomesize} = get_genome_basecount(
$pgx->{cytobands},
$pgx->{parameters}->{chr2plot},
);
$pgx = return_arrayplot_svg($pgx);
$plotfile = 'arrayplot,chr'.$chro.'.svg';
open (FILE, ">", $outdir.'/'.$plotfile);
binmode(FILE, ":utf8");
print FILE $pgx->{svg};
close FILE;
print "$plotfile\n";
$pgx->segments_add_statusmaps();
# TODO: This just assumes that the last part of the file pathe is the array ID
my $arrayName = $args{'-out'};
$arrayName =~ s/^.*\/?//;
my $callset = {
id => $arrayName,
variants => [ grep{ $_->{variant_type} =~ /^D(?:UP)|(?:EL)$/ } @{ $pgx->{segmentdata} }],
cnv_statusmaps => $pgx->{cnv_statusmaps},
};
open (FILE, ">", $outdir.'/'.$args{'-callsetfilename'});
print FILE JSON::XS->new->encode($callset);
close FILE;
# $progBar->update($i++);
}
# $progBar->update(scalar @$chr2plot);
################################################################################
# / main #######################################################################
################################################################################
################################################################################
# subs #########################################################################
################################################################################
sub _checkArgs {
# terminating if arraypath doesn't exist
if (
(! -d $args{'-arraypath'})
||
(! -d $args{'-out'})
) {
print <<END;
Script parameters:
-arraypath Path to the directory containging probe nad segment files.
(or -in) Required
-out Path to the output directory for the SVG files.
Defaults to -arraypath value if not specified
-format_inputfiles
Allows modification for different column order and file
content. The only alternative option to the "progenetix"
default is currently "TCGA", which will adjust the column order
of probe number and segment value.
-genome Genome edition, for setting the correct coordinate space.
Default: GRCh38
Both "hg" and "GRCh" styles can be used.
-cna_loss_threshold
Theshold for calling loss segments.
Default: -0.15
Alternative values provided by local defaults file or through
command line parameters.
-cna_gain_threshold
Theshold for calling gain segments.
Default: 0.15
... as above ...
-plot_adjust_baseline
Value is added to both probes and segment values, before
applying thresholds to the segments
Default: 0
-plot_adjust_baseline
Value is added to both probes and segment values, before
applying thresholds to the segments
Default: 0
-chr2plot Chromosomes to be plotted (comma separated)
Default: 1 => Y
Example: "7,8,18"
-plotregions
For generating a single plot, with one or more plot regions
Syntax: 9:20000000-22000000,17:0-25000000
The filename will be adjusted accordingly (here
`arrayplot,chr9_20000000-22000000,17_0-25000000.svg`).
This overrides the "-chr2plot" parameter.
-markers For adding colored overlay block on one or more specified
regions, with an optional text label. Colors can be added, or
will be ranomised.
Example:
11:2000000-3000000:marker:#ffcc00,11:2900000-3400000:another
-value_plot_y_max
Maximum Y value of the plot
Default: 3.2
Can be adjusted depending on probe dynamics.
-factor_probedots
Relative size of the probe dots
Default: 3
Can be lowered or increased, depending on probe number
-size_chromosome_w_px
pixel width of the chromosomal bands
Default: 12
Cytoband plotting can be ski[pped by setting this to "0".
-do_chromosomes_proportional
When giving a single chromosome in "-chr2plot", the size of the
plot is adjusted relative to chromosome 1.
Default: y
Setting this to "n" will keep the full plot size.
END
exit;
}
# title
if ($args{'-title'} !~ /^[\w \,\-]+?$/) {
if ($args{'-arraypath'} =~ /(?:^|\/)([\w\,\-]+?)$/) {
$args{'-title'} = $1 }
}
# adjusting for special cases
# opendir DIR, $args{'-arraypath'};
# my @arrayDirF = grep{ /^\w[\w\.\,\-]+?\.\w\w\w\w?\w?\w?$/ } -f, readdir(DIR);
# close DIR;
#
# # TCGA
# if ($args{'-format_inputfiles'} =~ /tcga/i) {
# if (! -f $args{'-segfile'} ) {
# my @segGuess = grep{ /seg\.txt/ } @arrayDirF;
# if (@segGuess == 1) { $args{'-segfile'} = $args{'-arraypath'}.'/'.$segGuess[0] }
# }
# $args{'-probefile'} = $path_of_this_module.'/../rsrc/probemaps/'.genome_names_to_hg($args{'-genome'}).'/GPL6801,probes,cn.tsv';
# $args{'-simulated_probes'} = 'y';
# $args{'-text_bottom_left'} = 'TCGA (simulated probes)';
# }
#
# # / TCGA
# / adjusting for special cases
}
################################################################################
sub _printFeedback {
my @showArgs = qw(
title
text_bottom_left
genome
format_inputfiles
simulated_probes
cna_gain_threshold
cna_loss_threshold
segment_probecount_min
value_plot_y_max
factor_probedots
colorschema
size_plotimage_w_px
size_chromosome_w_px
size_segments_stroke_px
);
print <<END;
################################################################################
You are processing the array files from
$args{-arraypath}
Some of the current parameters are shown below. Those may be changed by
providing them as command line arguments.
END
foreach (@showArgs) {
my $name = ' -'.$_.(" " x 40);
$name =~ s/^(.{40}).*?$/$1/;
print $name.$pgx->{parameters}->{$_}."\n";
}
print <<END;
################################################################################
END
}
1;