/usr/share/circos/tools/connectogram/parsemap is in circos-tools 0.22-2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 | #!/usr/bin/env perl
=pod
=head1 NAME
parsemap - create Circos segment and color files for a cortical map from brain region map files
=head1 SYNOPSIS
cat map.txt | ./parsemap [-links map.links.txt] [-confdir etc/] [-datadir data/]
./parsemap -map map.txt -links map.links.txt
# help
./parsemap -h
# man page
./parsemap -man
=head1 DESCRIPTION
Using a map of brain regions, codes and colors, this script generates Circos files to draw a connectome diagram.
The format of the map input (passed via -map or STDIN) is
# region parcelated_structure_code r g b
Fro TrFPoG/S 255 153 153
Fro FMarG/S 204 0 51
...
For details and examples, see
http://www.circos.ca/documentation/tutorials/recipes/cortical_maps/
=head1 OPTIONS
=head2 -map FILE
Definitions of regions, parcelation structures and colors.
Fro TrFPoG/S 255 153 153
Fro FMarG/S 204 0 51
Fro MFS 255 153 51
...
Optionally, you can add any number of measures to each structure - these will be rendered as heatmaps, symmetrically for both hemispheres.
Fro TrFPoG/S 255 153 153 0.910094 0.265257 0.893188 0.220351 0.810623
Fro FMarG/S 204 0 51 0.631798 0.571077 0.332158 0.104455 0.173531
Fro MFS 255 153 51 0.502931 0.567394 0.854165 0.0401409 0.484983
...
If your measures are not symmetric with respect to the hemisphere, specify the left/right values as follows
# e.g. left 0.9 0.3 0.2
# right 0.8 0.5 0.8
Fro TrFPoG/S 255 153 153 0.9/0.8 0.3/0.5 0.2/0.8
=head2 -links FILE
Links between structures. The start and end of the link are defined by the structure name along with "r" or "l" to indicate the hemisphere. The values will be associated with the 'type' and 'score' parameters for each link, which can be referenced in the configuration file using var(type) and var(score).
r InfFGOrp l PosCS 1 0.0229917613607071
l BSt l SbOrS 1 0.213414893099078
l TPl r Pu 0 0.26688626172767
=head2 -structuresize 250
Change the size of each parcelation unit in the image.
You should not have to adjust this value, since all the regions have
the same size and the absolute value of this number is not relevant in
how the figure appears.
=head1 HISTORY
=over
=item * 27 Nov 2014
Added the ability to specify asymmetric measure values using left_value/right_value in the map file.
=item * 17 May 2013
Measures for parcelation regions no longer required.
Segment labels are created in C<DATADIR/structure.label.txt>.
=item * 14 Jun 2012
First version.
=back
=head1 REFERENCES
Irimia, A., M. C. Chambers, et al. (2012). "Circular representation of human cortical networks for subject and population-level connectomic visualization." NeuroImage.
Irimia, A., M. C. Chambers, et al. (2012). "Patient-tailored connectomics visualization for the assessment of white matter atrophy in traumatic brain injury." Frontiers in Neurology 3.
Van Horn, J. D., A. Irimia, et al. (2012). "Mapping connectivity damage in the case of phineas gage." PLoS One 7(5): e37454.
=head1 AUTHOR
Martin Krzywinski
=head1 CONTACT
Martin Krzywinski
Genome Sciences Center
BC Cancer Research Center
100-570 W 7th Ave
Vancouver BC V5Z 4S6
mkweb.bcgsc.ca
martink@bcgsc.ca
=cut
use strict;
use warnings FATAL=>"all";
use Carp;
use Config::General;
use Cwd qw(getcwd abs_path);
use Data::Dumper;
use File::Basename;
use FindBin;
use Getopt::Long;
use Pod::Usage;
use lib "$FindBin::RealBin";
use lib "$FindBin::RealBin/../lib";
use lib "$FindBin::RealBin/lib";
our (%OPT,%CONF,$conf);
our @COMMAND_LINE = ("map=s",
"links=s",
"configfile=s",
"datadir=s",
"confdir=s",
"structuresize=i",
"help",
"man",
"debug+");
our $VERSION = 0.01;
# common and custom module imports below
#use Regexp::Common;
#use IO::File;
#use List::Util;
use List::MoreUtils qw(uniq);
#use Set::IntSpan;
#use Statistics::Descriptive;
# read and parse configuration file
_parse_config();
#_dump_config();
my $ih;
if (my $file = $CONF{map}) {
die "No such file $file" unless -e $file;
open(FILE,$file);
$ih = \*FILE;
} else {
$ih = \*STDIN;
}
my @brain = parse_map($ih);
report_colors(@brain);
write_karyotype(@brain);
write_seg_order(@brain);
write_label(@brain);
write_heatmap(@brain);
write_links(@brain);
exit;
for my $i (0..100) {
my ($n1,$n2) = sort {rand()<=>rand()} (0..@brain-1);
my $s1 = rand() < 0.5 ? "l" : "r";
my $s2 = rand() < 0.5 ? "l" : "r";
my $type = rand() < 0.5 ? 0 : 1;
my $score = rand();
printinfo(
$s1,
$brain[$n1]{structure},
$s2,
$brain[$n2]{structure},
$type,
$score
);
}
sub write_links {
my @brain = @_;
return unless $CONF{links};
open(L,$CONF{links});
my $file = make_filename($CONF{datadir},"links.txt");
open(F,">$file");
while (<L>) {
print $_;
chomp;
next if /^\s*\#/;
next if /^\s*$/;
my ($side1,$s1,
$side2,$s2,
$type,$score) = split;
$score ||= 0;
my $s1obj = get_structure($s1,@brain);
my $s2obj = get_structure($s2,@brain);
my $link = sprintf("%s %d %d %s %d %d type=%d,score=%f",
make_region_name($s1obj->{region},$side1) ||
make_region_name($s1obj->{region},"l"),
$s1obj->{start},
$s1obj->{end},
make_region_name($s2obj->{region},$side2) ||
make_region_name($s2obj->{region},"l"),
$s2obj->{start},
$s2obj->{end},
$type,$score);
printf F ("%s\n",$link);
printdebug(1,"writing link",$link);
}
close(L);
close(F);
printdebug(1,"wrote file",$file);
}
sub write_heatmap {
my @brain = @_;
my @regions = get_regions(@brain);
unlink <make_filename($CONF{datadir},"measure.*.txt")>;
my $seenfile;
for my $side (qw(l r)) {
for my $region (@regions) {
my $region_name = make_region_name($region,$side);
next unless $region_name;
my @structures = get_structures($region,@brain);
for my $structure (@structures) {
my $sobj = get_structure($structure,@brain);
next unless int @{$sobj->{heatmap}};
for my $i (0 .. @{$sobj->{heatmap}}-1) {
my $file = make_filename($CONF{datadir},"measure.$i.txt");
open(F,">>$file");
my @values = split("/",$sobj->{heatmap}[$i]);
$values[1] = $values[0] if ! defined $values[1];
my $value = $values[ $side eq "r" ];
printf F ("%s %d %d %f\n",
$region_name,
@{$sobj}{qw(start end)},
$value);
close(F);
printdebug(1,"wrote file",$file) if ! $seenfile->{$file}++;
}
}
}
}
close(F);
}
sub report_colors {
my @brain = @_;
my $file = "color.brain.conf";
$file = make_filename($CONF{confdir},$file);
open(F,">$file");
my %seen;
for my $s (@brain) {
my $color_name = $s->{color};
next if $seen{ $color_name } ++;
printdebug(1,"writing color",$color_name,@{$s->{rgb}});
printf F ("%s = %d,%d,%d\n",
$color_name,
@{$s->{rgb}});
}
close(F);
printdebug(1,"wrote file",$file);
}
sub write_seg_order {
my @brain = @_;
my @r = grep($_ ne "BSt", uniq (map {$_->{region}} @brain));
my $file = "segment.order.conf";
$file = make_filename($CONF{confdir},$file);
open(F,">$file");
printdebug(1,"writing segment order");
if(grep($_->{region} eq "BSt", @brain)) {
printf F ("chromosomes_order = %s,%s,%s\n",
join(",",map { lc $_ . "-r" } @r ),
"bst",
join(",",map { lc $_ . "-l" } reverse @r ));
} else {
printf F ("chromosomes_order = %s,%s\n",
join(",",map { lc $_ . "-r" } @r ),
join(",",map { lc $_ . "-l" } reverse @r ));
}
close(F);
printdebug(1,"wrote file",$file);
}
sub write_label {
my @brain = @_;
my $file = make_filename($CONF{datadir},"structure.label.txt");
open(L,">$file");
printdebug(1,"writing structure labels");
for my $region (@brain) {
for my $side (qw(l r)) {
printf L ("%s-%s %d %d %s\n",
lc $region->{region},
$side,
@{$region}{qw(start end structure)});
}
}
close(L);
printdebug(1,"wrote file",$file);
}
sub make_region_name {
my ($region,$side) = @_;
croak if ! defined $region;
croak if ! defined $side;
if ($region eq "BSt") {
if ($side eq "l") {
return sprintf("%s",lc $region);
} else {
return;
}
} else {
return sprintf("%s-%s",lc $region,$side);
}
}
sub write_karyotype {
my @brain = @_;
my $file = make_filename($CONF{datadir},"segments.txt");
open(K,">$file");
my @regions = get_regions(@brain);
for my $side (qw(l r)) {
for my $region (@regions) {
my $region_name = make_region_name($region,$side);
next unless $region_name;
my @structures = get_structures($region,@brain);
printdebug(1,"writing structure to karyotype file",$region_name,$region,$CONF{structure_size}*int(@structures)-1);
printf K ("chr - %s %s 0 %d black\n",
$region_name,
$region,
$CONF{structure_size} * int(@structures) - 1);
for my $structure (@structures) {
my $sobj = get_structure($structure,@brain);
printdebug(1,"writing substructure to karyotype file",$region_name,@{$sobj}{qw(structure start end color)});
printf K ("band %s %s %s %d %d %s\n",
$region_name,
$sobj->{structure},
$sobj->{structure},
$sobj->{start},
$sobj->{end},
$sobj->{color});
}
}
}
printdebug(1,"wrote file",$file);
}
sub parse_map {
my $ih = shift;
my @brain;
my $region_count;
while (<$ih>) {
chomp;
next if /^\s*\#/;
next if /^\s*$/;
my ($region,$structure,$r,$g,$b,@hmap) = split;
if (! defined $b) {
confess "Could not parse the line [$_]. It must have 5 fields: region, structure, r, g, b.";
}
my $color_name = lc $structure;
$color_name =~ s/\W//g;
printdebug(1,$region,$structure,$color_name,$r,$g,$b);
$region_count->{$region} ||= 0;
push @brain, {region=>$region,
rgb=>[$r,$g,$b],
structure=>$structure,
start=> $CONF{structure_size} * $region_count->{$region},
end=> $CONF{structure_size} * (1+$region_count->{$region}) - 1,
heatmap=>\@hmap,
color=>$color_name};
$region_count->{$region}++;
}
return @brain;
}
sub get_regions {
my @brain = @_;
return uniq(map {$_->{region}} @brain);
}
sub get_structure {
my ($str,@brain) = @_;
my ($s) = grep($_->{structure} eq $str, @brain);
return $s;
}
sub get_structures {
my ($region,@brain) = @_;
return map {$_->{structure}} grep($_->{region} eq $region, @brain);
}
sub make_filename {
my ($dir,$name) = @_;
if (defined $dir) {
my $path = "$dir/$name";
$path =~ s/\/+/\//g;
return $path;
} else {
return $name;
}
}
sub validateconfiguration {
$CONF{confdir} ||= "etc/";
$CONF{datadir} ||= "data/";
$CONF{structure_size} ||= 100;
if(! -d $CONF{confdir}) {
die "You asked for configuration files to be written to [$CONF{confdir}] but this directory is not found. Use -confdir DIR to specify the directory.";
}
if(! -d $CONF{datadir}) {
die "You asked for data files to be written to [$CONF{datadir}] but this directory is not found. Use -datadir DIR to specify the directory.";
}
}
# HOUSEKEEPING ###############################################################
sub _dump_config {
printdumper(\%OPT,\%CONF);
}
sub _parse_config {
my $dump_debug_level = 3;
GetOptions(\%OPT,@COMMAND_LINE);
pod2usage() if $OPT{help};
pod2usage(-verbose=>2) if $OPT{man};
loadconfiguration($OPT{configfile});
populateconfiguration(); # copy command line options to config hash
validateconfiguration();
if (defined $CONF{debug} && $CONF{debug} == $dump_debug_level) {
$Data::Dumper::Indent = 2;
$Data::Dumper::Quotekeys = 0;
$Data::Dumper::Terse = 0;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Varname = "OPT";
printdumper(\%OPT);
$Data::Dumper::Varname = "CONF";
printdumper(\%CONF);
exit;
}
}
sub populateconfiguration {
for my $var (keys %OPT) {
$CONF{$var} = $OPT{$var};
}
repopulateconfiguration(\%CONF);
}
sub repopulateconfiguration {
my $root = shift;
return unless ref($root) eq "HASH";
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);
}
} elsif (defined $value) {
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;
if (defined $file) {
if (-e $file && -r _) {
# provided configuration file exists and can be read
$file = abs_path($file);
} else {
confess "The configuration file [$file] passed with -configfile does not exist or cannot be read.";
}
} else {
# otherwise, try to automatically find a configuration file
my ($scriptname,$path,$suffix) = fileparse($0);
my $cwd = getcwd();
my $bindir = $FindBin::RealBin;
my $userdir = $ENV{HOME};
my @candidate_files = (
"$cwd/$scriptname.conf",
"$cwd/etc/$scriptname.conf",
"$cwd/../etc/$scriptname.conf",
"$bindir/$scriptname.conf",
"$bindir/etc/$scriptname.conf",
"$bindir/../etc/$scriptname.conf",
"$userdir/.$scriptname.conf",
);
my @additional_files = (
);
for my $candidate_file (@additional_files,@candidate_files) {
#printinfo("configsearch",$candidate_file);
if (-e $candidate_file && -r _) {
$file = $candidate_file;
#printinfo("configfound",$candidate_file);
last;
}
}
}
if (defined $file) {
$OPT{configfile} = $file;
$conf = new Config::General(
-ConfigFile=>$file,
-IncludeRelative=>1,
-IncludeAgain=>1,
-ExtendedAccess=>1,
-AllowMultiOptions=>"yes",
-LowerCaseNames=>1,
-AutoTrue=>1
);
%CONF = $conf->getall;
}
}
sub printdebug {
my ($level,@msg) = @_;
my $prefix = "debug";
if (defined $CONF{debug} && $CONF{debug} >= $level) {
printinfo(sprintf("%s[%d]",$prefix,$level),@msg);
}
}
sub printinfo {
print join(" ",@_),"\n";
}
sub printdumper {
print Dumper(@_);
}
|