File: parse-category

package info (click to toggle)
circos-tools 0.23-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 39,188 kB
  • sloc: perl: 6,551; sh: 56; makefile: 16
file content (384 lines) | stat: -rwxr-xr-x 11,298 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl

=pod

=head1 NAME

parse-category - parse categorial data into a Circos plot

=head1 SYNOPSIS

  cat data.txt | parse-category
  parse-category -input data.txt

=head1 DESCRIPTION

This script formats data into a plot that encodes the relationship
between two categorical variables, such as salary and level of
training.

Each category within the variable is assigned to a segment on the
circle.

Ribbons represent the number of data points (e.g. indidividuals) for
which a given variable pair has been observed. Ribbon format can be
coded by the value of a specific column. Radial position of the ribbon
can be coded by the value of a specific column.

=head1 CONFIGURATION

All inputs are controlled through the configuration file.

=head1 HISTORY

=over

=item * 21 Apr 2014 v0.11

Updated documentation.

=item * 16 Feb 2009 v0.1

Expanded and versiond.

=back 

=head1 BUGS

=head1 AUTHOR

Martin Krzywinski

=head1 CONTACT

  Martin Krzywinski
  Genome Sciences Centre
  Vancouver BC Canada
  www.bcgsc.ca
  martink@bcgsc.ca

=cut

use strict;
use Config::General;
use Data::Dumper;
use File::Basename;
use FindBin;
use Getopt::Long;
use IO::File;
use List::Util;
use List::MoreUtils qw(uniq);
use Math::VecStat qw(sum min max average);
use Pod::Usage;
use Set::IntSpan;
use Statistics::Descriptive;
use Storable;
use Time::HiRes qw(gettimeofday tv_interval);
use lib "$FindBin::RealBin";
use lib "$FindBin::RealBin/../lib";
use lib "$FindBin::RealBin/lib";
use vars qw(%OPT %CONF);

################################################################
#
# *** YOUR MODULE IMPORTS HERE
#
################################################################

GetOptions(\%OPT,
					 "input=s",
					 "configfile=s",
					 "help",
					 "man",
					 "debug+");

pod2usage() if $OPT{help};
pod2usage(-verbose=>2) if $OPT{man};
loadconfiguration($OPT{configfile});
populateconfiguration(); # copy command line options to config hash
validateconfiguration(); 
if($CONF{debug} > 1) {
  $Data::Dumper::Pad = "debug parameters";
  $Data::Dumper::Indent = 1;
  $Data::Dumper::Quotekeys = 0;
  $Data::Dumper::Terse = 1;
  print Dumper(\%CONF);
}

my $inputhandle = get_handle();

my @links;
while(<>) {

  chomp;
	next if /^\s*\#/;

  my @tok = split(/\t/,$_);

  my $link_start = $tok[$CONF{link_start}{col}];
  my $link_end   = $tok[$CONF{link_end}{col}];

  next unless $link_start =~ /$CONF{link_start}{rx}/;
  next unless $link_end   =~ /$CONF{link_end}{rx}/;

  next unless pass_min_max($link_start,$CONF{link_start}{min},$CONF{link_start}{max});
  next unless pass_min_max($link_end,$CONF{link_end}{min},$CONF{link_end}{max});

  my $link_format = {%{$CONF{link_default}}};
  my $coding_code;
  for my $coding_name (sort {$CONF{link_coding}{$a}{order} <=> $CONF{link_coding}{$b}{order}} keys %{$CONF{link_coding}}) {
    my $coding_branch = $CONF{link_coding}{$coding_name};
    next unless $coding_branch->{use};
    if(! defined $tok[$coding_branch->{col}]) {
      if($coding_branch->{values}{missing}) {
				for my $var_datum (split(",",$coding_branch->{values}{missing})) {
					my ($var,$value) = split("=",$var_datum);
					$link_format->{$var} = $value;
				}
      }
    } else {
      for my $value (keys %{$coding_branch->{values}}) {
				if($tok[$coding_branch->{col}] eq $value) {
					my $var_data = $coding_branch->{values}{$value};
					$coding_code .= $value;
					for my $var_datum (split(",",$var_data)) {
						my ($var,$value) = split("=",$var_datum);
						$link_format->{$var} = $value;
					}
				}
      }
    }
  }
  my $link = {id1=>$CONF{link_start}{id},
							pos1=>$link_start,
							id2=>$CONF{link_end}{id},
							pos2=>$link_end,
							code=>$coding_code,
							format=>$link_format};
  # assign a radius to the ends of the link, if requested
  for my $type (qw(start end)) {
    if($CONF{"link_".$type}{radius}{use}) {
      my $value = $tok[ $CONF{"link_".$type}{radius}{col}];
      if($CONF{"link_".$type}{radius}{remap}) {
				my $remap_value = $CONF{"link_".$type}{radius}{remap};
				$remap_value =~ s/x/$value/g;
				$value = eval $remap_value;
      }
      my $leaf = $CONF{"link_".$type}{radius};
      my $r = $leaf->{rmin} + ($leaf->{rmax}-$leaf->{rmin})*($value-$leaf->{min})/($leaf->{max}-$leaf->{min});
      $link->{ $type eq "start" ? "radius1" : "radius2" } = $r;
    }
  }
  # idX  - name of the category for the start/end of the link
  # poSX - value of the variable in the category (this determines which segment the link is on)
  push @links, $link;
}

# calculate statistics for links associated with each category/value on both ideograms
# - number of links out/to a given category/value (for each different format)
# - number of links between a given combination of category/values (for each different format)

my $stats;
for my $link (@links) {
  my $format = make_format_string( $link->{format} );
  $stats->{ from }{ $link->{id1} }{ $link->{pos1} }{ n }{ $format }++;
  $stats->{  to  }{ $link->{id2} }{ $link->{pos2} }{ n }{ $format }++;
  $stats->{ from }{ $link->{id1} }{ $link->{pos1} }{ to }{ $link->{id2} }{ $link->{pos2} }{ $format }++;
}

# Each link will contribute to a ribbon. Here, assign links to ribbons, creating ribbons
# where necessary. A new ribbon is created for a new combination of
# - category/value
# - radial position
# - format (color, thickness, etc)

my @ribbons = ();
for my $link (@links) {
  my $ribbon;
  if(@ribbons) {
    ($ribbon) = grep(
										 $_->{start}{id}     eq $link->{id1}     &&
										 $_->{start}{pos}    eq $link->{pos1}    &&
										 $_->{start}{radius} eq $link->{radius1} &&
										 $_->{end}{id}       eq $link->{id2}     &&
										 $_->{end}{pos}      eq $link->{pos2}    &&
										 $_->{end}{radius}   eq $link->{radius2} &&
										 make_format_string($_->{format}) eq make_format_string($link->{format}), @ribbons);
  }
  if(! $ribbon) {
    $ribbon = { start=>{id=>$link->{id1},pos=>$link->{pos1},radius=>$link->{radius1}},
								end=>{id=>$link->{id2},pos=>$link->{pos2},radius=>$link->{radius2}},
								format=>$link->{format},
								size=>1,
								code=>$link->{code} };
    push @ribbons, $ribbon;
  } else {
    $ribbon->{size}++;
  }
}

# Determine the position of the ribbons within each category/value segment. This is done based
# on the values in the <link_order> block.

for my $type ($CONF{link_order}{start} eq "otherend" ? qw(end start) : qw(start end)) {
  for my $id (uniq( map {$_->{$type}{id}} @ribbons)) {
    for my $pos (uniq( map {$_->{$type}{pos}} grep($_->{$type}{id} eq $id, @ribbons))) {
      my @r = grep($_->{$type}{id} eq $id && $_->{$type}{pos} eq $pos, @ribbons);
      if($CONF{link_order}{$type} eq "size") {
				@r = sort { $b->{size} <=> $a->{size} } @r;
      } elsif ($CONF{link_order}{$type} eq "coding") {
				@r = sort { ($a->{code} cmp $b->{code}) || ($b->{size} <=> $a->{size}) } @r;
      } elsif ($CONF{link_order}{$type} eq "radius") {
				@r = sort { $a->{$type}{radius} <=> $b->{$type}{radius} } @r;
      } elsif ($CONF{link_order}{$type} eq "otherend") {
				my $otype = $type eq "start" ? "end" : "start";
				@r = sort { ($a->{$otype}{pos} <=> $b->{$otype}{pos}) || ($a->{$otype}{u} <=> $b->{$otype}{v}) } @r;
      }
      # determine the start/end positions (u/v) of the ribbon on the category/value segment
      my $cumul_pos = 0;
      for my $r (@r) {
				#printinfo($type,$id,$pos,$r->{start}{radius});
				$r->{$type}{u} = $cumul_pos;
				$r->{$type}{v} = $cumul_pos + $r->{size};
				$cumul_pos += $r->{size};
      }
    }
  }
}

my $linkid=0;
for my $ribbon (@ribbons) {
  my @format;
  push @format, sprintf("%s",make_format_string($ribbon->{format}));
  push @format, sprintf("radius1=%.3fr",$ribbon->{start}{radius}) if defined $ribbon->{start}{radius};
  push @format, sprintf("radius2=%.3fr",$ribbon->{end}{radius}) if defined $ribbon->{end}{radius};
  push @format, sprintf("z=%d",$ribbon->{size});
  printinfo(sprintf("link%d %s-%d %d %d %s",
										$linkid,
										@{$ribbon->{start}}{qw(id pos u v)},
										join(",",@format)));
  printinfo(sprintf("link%d %s-%d %d %d %s",
										$linkid,
										@{$ribbon->{end}}{qw(id pos u v)},
										join(",",@format)));
  $linkid++;
}

for my $end (qw(start end)) {
  my $dir = $end eq "start" ? "from" : "to";
  for my $id (sort keys %{$stats->{$dir}}) {
    for my $pos (sort {$end eq "start" ? $a <=> $b : $b <=> $a} keys %{$stats->{$dir}{$id}}) {
      my $size = sum( values %{$stats->{$dir}{$id}{$pos}{n}} );
      my $idname  = sprintf("%s-%s",$id,$pos);
      my $idlabel = sprintf("%s-%s",$id,$pos);
      my $idcolor = sprintf("%s_a%d",$CONF{"link_".$end}{color}, ($pos%10)||1 );
      print STDERR sprintf("chr - %s %s 0 %d %s\n",
													 $idname,$idlabel,
													 $size,
													 $idcolor);
    }
  }
}

sub pass_min_max {
  my ($value,$min,$max) = @_;
  my $pass = 1;
  $pass = 0 if defined $min && $value < $min;
  $pass = 0 if defined $max && $value > $max;
  return $pass;
}

sub make_format_string {
  my $format = shift;
  my @vars;
  for my $var (sort keys %$format) {
    push @vars, sprintf("%s=%s",$var,$format->{$var});
  }
  return join(",",@vars);
}

sub get_handle {
  if(my $file = $CONF{input}) {
    die "No such file $file" unless -e $file;
    open(FILE,$file);
    $inputhandle = \*FILE;
  } else {
    $inputhandle = \*STDIN;
  }
}

sub validateconfiguration {

}

################################################################
#
# *** DO NOT EDIT BELOW THIS LINE ***
#
################################################################

sub populateconfiguration {
  foreach my $key (keys %OPT) {
    $CONF{$key} = $OPT{$key};
  }
  repopulateconfiguration(\%CONF);
}

sub repopulateconfiguration {
  my $root     = shift;
  for my $key (keys %$root) {
    my $value = $root->{$key};
    if(ref($value) eq "HASH") {
      repopulateconfiguration($value);
    } elsif (ref($value) eq "ARRAY") {
      for my $item (@$value) {
        repopulateconfiguration($item);
      }
    } else {
      while($value =~ /__([^_].+?)__/g) {
        my $source = "__" . $1 . "__";
        my $target = eval $1;
        $value =~ s/\Q$source\E/$target/g;
      }
      $root->{$key} = $value;
    }
  }
}

sub loadconfiguration {
  my $file = shift;
  my ($scriptname) = fileparse($0);
  if(-e $file && -r _) {
    # great the file exists
  } elsif (-e "/home/$ENV{LOGNAME}/.$scriptname.conf" && -r _) {
    $file = "/home/$ENV{LOGNAME}/.$scriptname.conf";
  } elsif (-e "$FindBin::RealBin/$scriptname.conf" && -r _) {
    $file = "$FindBin::RealBin/$scriptname.conf";
  } elsif (-e "$FindBin::RealBin/etc/$scriptname.conf" && -r _) {
    $file = "$FindBin::RealBin/etc/$scriptname.conf";
  } elsif (-e "$FindBin::RealBin/../etc/$scriptname.conf" && -r _) {
    $file = "$FindBin::RealBin/../etc/$scriptname.conf";
  } else {
    return undef;
  }
  $OPT{configfile} = $file;
  my $conf = new Config::General(-ConfigFile=>$file,
																 -IncludeAgain=>1,
																 -AllowMultiOptions=>"yes",
																 -LowerCaseNames=>0,
																 -AutoTrue=>1);
  %CONF = $conf->getall;
}

sub printdebug {
  printinfo("debug",@_)  if $CONF{debug};
}

sub printdumper {
  printinfo(Dumper(@_));
}

sub printinfo {
  printf("%s\n",join(" ",@_));
}