changeset 0:b77ab858eac1 draft

Uploaded
author morinlab
date Mon, 12 Sep 2016 16:23:26 -0400
parents
children 916c60c717a6
files bin/make.circos.data bin/parse etc/bands.conf etc/breaks.conf etc/circos.conf etc/circos.conf~ etc/cnv.type.conf etc/cnv.type.conf~ etc/color.conf etc/color.conf~ etc/histogram.conf etc/ideogram.conf etc/ideogram.conf~ etc/ideogram.label.conf etc/ideogram.position.conf etc/ideogram.position.conf~ etc/library.conf etc/library.conf~ etc/r0r1.conf etc/r0r1.conf~ etc/rule.snp.conf etc/rule.type.conf etc/rule.type.conf~ etc/sv.type.conf etc/sv.type.conf~ etc/ticks.conf etc/ticks.conf~ etc/track.counter.conf etc/track.counter.conf~ etc/white.bin.conf etc/white.bin.conf~ oncocircos.xml parse.conf tool_dependencies.xml
diffstat 34 files changed, 2142 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/make.circos.data	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,404 @@
+#!/bin/env perl
+
+=pod
+
+=head1 NAME
+
+make.circos.data - create Circos data files from summary tables of SV/CNV mutations
+
+=head1 SYNOPSIS
+
+  bin/parse > table.txt
+
+  # uses same config file as parse
+  cat table.txt | bin/make.circos.data
+
+=head1 DESCRIPTION
+
+=head1 OPTIONS
+
+=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 Math::Round qw(round nearest);
+use Math::VecStat qw(sum min max average);
+use Pod::Usage;
+use Time::HiRes qw(gettimeofday tv_interval);
+use Storable;
+use lib "$FindBin::RealBin";
+use lib "$FindBin::RealBin/../lib";
+use lib "$FindBin::RealBin/lib";
+
+our (%OPT,%CONF,$conf);
+our @COMMAND_LINE = ("file=s",
+										 "configfile=s",
+										 "help",
+										 "cdump",
+										 "man",
+										 "debug");
+our $VERSION = 0.02;
+
+# common and custom module imports below
+#use Regexp::Common;
+#use IO::File;
+#use List::Util;
+#use List::MoreUtils;
+use Set::IntSpan;
+#use Statistics::Descriptive;
+
+# read and parse configuration file
+parse_config();
+
+sub validateconfiguration {
+
+}
+
+################################################################
+# get files
+my $table = read_file();
+
+my $path = "$CONF{files}{root}/$CONF{files}{circos}";
+# karyotype
+open(F,">$path/karyotype.txt");
+for my $chr (1..22,"X","Y") {
+	my $n = grep($_->{chr} eq $chr,@$table);
+	next unless $n;
+	printf F ("chr - hs%s %s 0 %d chr%s\n",$chr,$chr,$n,lc $chr);
+}
+close(F);
+
+# number of CNV
+open(F,">$path/mutations.txt");
+open(FSV,">$path/mutations.stacked.sv.txt");
+open(FCNV,">$path/mutations.stacked.cnv.txt");
+for my $gene (@$table) {
+	my @sv;
+	my @sv_vals;
+	my @cnv;
+	my @cnv_vals;
+	# number of samples for each SV type
+	for my $type (sort { $CONF{sv}{types}{$b} <=> $CONF{sv}{types}{$a}} keys %{$CONF{sv}{types}}) {
+		push @sv, sprintf("sv_%s=%d",lc $type,$gene->{sv}{$type}||0);
+		push @sv_vals, $gene->{sv}{$type}||0;
+	}
+	# number of samples for each CNV type
+	for my $type (sort { $CONF{cnv}{types}{$b} <=> $CONF{cnv}{types}{$a}} keys %{$CONF{cnv}{types}}) {
+		next unless $CONF{cnv}{types}{$type};
+		push @cnv, sprintf("cnv_%s=%d",lc $type,$gene->{cnv}{$type}{n}||0);
+		push @cnv_vals, $gene->{cnv}{$type}{n}||0;
+	}
+	my $cnv_plus  = ($gene->{cnv}{amp}{n} ||0) + ($gene->{cnv}{gain}{n} ||0);
+	my $cnv_minus = ($gene->{cnv}{homd}{n}||0) + ($gene->{cnv}{hetd}{n} ||0);
+	printf F ("hs%s %d %d %s size=%d,sv_top_type=%s,sv_top_n=%d,sv_tot=%d,svaa_max_pos=%s,svaa_max_n=%d,cnv_top_type=%s,cnv_top_n=%d,cnv_top_avg=%f,cnv_top_med=%f,cnv_plus=%d,cnv_minus=%d,%s,%s\n",
+						$gene->{chr},
+						$gene->{pos},
+						$gene->{pos}+1,
+						$gene->{name},
+						$gene->{size},
+						keys %{$gene->{sv_top}},
+						values %{$gene->{sv_top}},
+						$gene->{sv}{"*"} || 0,
+						(keys %{$gene->{svaa_top}})||0,
+						(values %{$gene->{svaa_top}})||0,
+						$gene->{cnv_top}{class} || "-",
+						$gene->{cnv_top}{n}     || 0,
+						$gene->{cnv_top}{avg}   || 0,
+						$gene->{cnv_top}{med}   || 0,
+						$cnv_plus||0,
+						$cnv_minus||0,
+						join(",",@sv),
+						join(",",@cnv));
+	# stacked histograms of number of samples with each SV type
+	printf FSV ("hs%s %d %d %s name=%s,sv_top_type=%s,sv_top_n=%d\n",
+						$gene->{chr},
+						$gene->{pos},
+						$gene->{pos}+1,
+						join(",",@sv_vals),
+						$gene->{name},
+						keys %{$gene->{sv_top}},
+						values %{$gene->{sv_top}},
+						 );
+	printf FCNV ("hs%s %d %d %s name=%s,cnv_top_type=%s,cnv_top_n=%d,cnv_top_avg=%f,cnv_top_med=%f\n",
+						$gene->{chr},
+						$gene->{pos},
+						$gene->{pos}+1,
+						join(",",@cnv_vals),
+						$gene->{name},
+						$gene->{cnv_top}{class} || "-",
+						$gene->{cnv_top}{n}     || 0,
+						$gene->{cnv_top}{avg}   || 0,
+						$gene->{cnv_top}{med}   || 0,
+							 
+							);
+}
+close(F);
+close(FSV);
+close(FCNV);
+
+sub read_file {
+	my $fh = get_handle();
+	my @data;
+	my $chrpos;
+	while(<$fh>) {
+		chomp;
+		next if /^\#/;
+		my @tok = split;
+		my $gene = list2hash([qw(i id name chr start end size)],
+												 [splice(@tok,0,7)]);
+		$gene->{pos} = $chrpos->{ $gene->{chr} }++;
+		# remaining tokens
+		for my $tok (@tok) {
+			my @subtok = split(":",$tok);
+			my $event  = lc shift @subtok;
+			my $type   = lc shift @subtok;
+			if($event =~ /sv/) {
+				$gene->{$event}{$type} = shift @subtok;
+			} elsif($event =~ /cnv/) {
+				my $h = { class=> $type,
+									n=>     shift @subtok,
+									min=>   shift @subtok,
+									avg=>   shift @subtok,
+									med=>   shift @subtok,
+									max=>   shift @subtok};
+				if($event =~ /top/) {
+					$gene->{$event} = $h;
+				} else {
+					$gene->{$event}{$type} = $h;
+				}
+			}
+		}
+		printdumper($gene);
+		push @data, $gene;
+	}
+	return \@data;
+}
+
+sub list2hash {
+	my ($names,$list) = @_;
+	my $h;
+	my $i = 0;
+	for my $name (@$names) {
+		$h->{$name} = $list->[$i++];
+	}
+	return $h;
+}
+
+sub get_handle {
+	my $h;
+	if(my $file = $CONF{file}) {
+		die "No such file [$file]" unless -e $file;
+		open(FILE,$file);
+		$h = \*FILE;
+	} else {
+		$h = \*STDIN;
+	}
+	return $h;
+}
+
+# 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 ($CONF{cdump}) {
+    $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 ($node,$parent_node_name) = shift;
+  return unless ref($node) eq "HASH";
+  for my $key (keys %$node) {
+		my $value = $node->{$key};
+		if (ref($value) eq "HASH") {
+			repopulateconfiguration($value,$key);
+		} elsif (ref($value) eq "ARRAY") {
+			for my $item (@$value) {
+				repopulateconfiguration($item,$key);
+			}
+		} elsif (defined $value) {
+			my $new_value = parse_field($value,$key,$parent_node_name,$node);
+			$node->{$key} = $new_value;
+		}
+	}
+}
+
+sub parse_field {
+	my ($str,$key,$parent_node_name,$node) = @_;
+	# replace configuration field
+	# conf(LEAF,LEAF,...)
+	while ( $str =~ /(conf\(\s*(.+?)\s*\))/g ) {
+		my ($template,$leaf) = ($1,$2);
+		if (defined $template && defined $leaf) {
+			my @leaf         = split(/\s*,\s*/,$leaf);
+			my $new_template;
+			if (@leaf == 2 && $leaf[0] eq ".") {
+				$new_template = $node->{$leaf[1]};
+			} else {
+				$new_template = fetch_conf(@leaf);
+			}
+			$str =~ s/\Q$template\E/$new_template/g;
+		}
+	}
+	if ($str =~ /\s*eval\s*\(\s*(.+)\s*\)/) {
+		my $fn = $1;
+		$str = eval $fn;
+		if ($@) {
+			die "could not parse configuration parameter [$@]";
+		}
+	}
+	return $str;
+}
+
+sub fetch_configuration {
+	my @config_path = @_;
+	my $node        = \%CONF;
+	if(! @config_path) {
+		return \%CONF;
+	}
+	for my $path_element (@config_path) {
+		if (! exists $node->{$path_element}) {
+	    return undef;
+		} else {
+	    $node = $node->{$path_element};
+		}
+	}
+	return $node;
+}
+
+sub fetch_conf {
+	return fetch_configuration(@_);
+}
+
+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 {
+	printinfo("debug",@_) if defined $CONF{debug};
+}
+
+sub printinfo {
+	print join(" ",map { defined $_ ? $_ : "_undef_" } @_),"\n";
+}
+
+sub printfinfo {
+	my ($fmt,@args) = @_;
+	@args = map { defined $_ ? $_ : "_undef_" } @args;
+	printf("$fmt\n",@args);
+}
+
+sub printerr {
+	print STDERR join(" ",map { defined $_ ? $_ : "_undef_" } @_),"\n";
+}
+
+sub printdumper {
+	print Dumper(@_);
+}
+
+=pod 
+
+=head1 HISTORY
+
+=over
+
+=item * 30 Nov 2015
+
+Started.
+
+=back 
+
+=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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/bin/parse	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,453 @@
+#!/bin/env perl
+
+=pod
+
+=head1 NAME
+
+parse - parse Ryan's MAF and CNV files and generate a summary table of all genes and their mutations and CNV status
+
+=head1 SYNOPSIS
+
+  # automatically load etc/parse.conf
+  bin/parse 
+
+  # if config file is elsewhere
+  bin/parse -conf elsewhere/my.conf
+
+=head1 DESCRIPTION
+
+See etc/parse.conf for all settings.
+
+=head1 OPTIONS
+
+=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 Math::Round qw(round nearest);
+use Math::VecStat qw(sum min max average);
+use Pod::Usage;
+use Time::HiRes qw(gettimeofday tv_interval);
+use Statistics::Basic qw(median);
+use Storable;
+use lib "$FindBin::RealBin";
+use lib "$FindBin::RealBin/../lib";
+use lib "$FindBin::RealBin/lib";
+
+our (%OPT,%CONF,$conf);
+our @COMMAND_LINE = ("file=s",
+										 "configfile=s",
+										 "help",
+										 "cdump",
+										 "man",
+										 "debug");
+our $VERSION = 0.02;
+
+# common and custom module imports below
+#use Regexp::Common;
+#use IO::File;
+#use List::Util;
+#use List::MoreUtils;
+use Set::IntSpan;
+#use Statistics::Descriptive;
+
+# read and parse configuration file
+parse_config();
+
+sub validateconfiguration {
+}
+
+################################################################
+# get files
+my $sv    = read_file($CONF{files}{sv}  ,"sv"   );
+my $genes = read_file($CONF{files}{mart},"genes");
+my $cnv   = read_file($CONF{files}{cnv} ,"cnv"  );
+
+################################################################
+# traverse all genes from biomart and determine number
+# of SV and CNV events across samples
+for my $chr (keys %$genes) {
+	next if $CONF{filter}{chr} && $chr ne $CONF{filter}{chr};
+	printdebug("processing",$chr);
+	for my $gene (@{$genes->{$chr}}) {
+		my $id = $gene->{id};
+		# filter out by presence and number of SV events
+		next if $CONF{filter}{sv} && ! $sv->{$id};
+		# number of samples that have SV event
+		my @samples_sv  = keys %{$sv->{$id}};
+		next if $CONF{filter}{sv_num} && @samples_sv < $CONF{filter}{sv_num};
+
+		$gene->{affected} = 1;
+
+		# register SV events
+		my $pos;
+		for my $sample (@samples_sv) {
+			for my $sv (sort {$b->{weight} <=> $a->{weight}} @{$sv->{$id}{$sample}}) {
+				$gene->{sv}{ $sv->{type} }++;
+				$gene->{sv}{ "*" }++;
+				$pos->{ $sv->{aa} }++; # register the protein position of the SV
+				next if $CONF{sv}{top_damage_only};
+			}
+		}
+		# top SV event
+		if($gene->{sv}) {
+			my ($sv_top) = sort {$gene->{sv}{$b} <=> $gene->{sv}{$a}} grep($_ ne "*",keys %{$gene->{sv}});
+			$gene->{sv_top}{$sv_top} = $gene->{sv}{$sv_top};
+		}
+		for my $aa (sort {$pos->{$b} <=> $pos->{$a}} keys %$pos) {
+			#next unless $pos->{$aa} > 1;
+			my $n = $pos->{$aa};
+			$gene->{svaa_top}{$aa} = $n if ! defined $gene->{svaa_top};
+			$gene->{svaa}{"*"}      += $n;
+			$gene->{svaa}{$aa}       = $n;
+		}
+		# register CNV events
+		my @samples_cnv = keys %$cnv;
+		# lookup any CNV events -- this can take a bit of time
+		# we can bin the CNV hash later if needed
+		for my $sample (@samples_cnv) {
+			my $chr = $gene->{chr};
+			next unless $cnv->{$sample}{$chr};
+			for my $cnv (@{$cnv->{$sample}{$chr}}) {
+				my $int = $cnv->{set}->intersect($gene->{set})->cardinality;
+				next unless $int;
+				push @{$gene->{cnv}{$cnv->{category}}{$sample}}, $cnv->{avg};
+			}
+		}
+	}
+}
+
+################################################################
+# report
+my $i = 0;
+for my $chr (1..22,"X","Y") {
+	next unless $genes->{$chr};
+	for my $gene (sort {$a->{start} <=> $b->{start}} @{$genes->{$chr}}) {
+		next unless $gene->{affected};
+		my @report = ($i++,@{$gene}{qw(id name chr start end size)});
+		if($gene->{sv}) {
+			push @report, sprintf("sv_top:%s:%d",keys %{$gene->{sv_top}},values %{$gene->{sv_top}});
+			for my $type (sort keys %{$gene->{sv}}) {
+				push @report, sprintf("sv:%s:%d",$type,$gene->{sv}{$type});
+			}
+		}
+		if($gene->{svaa}) {
+			push @report, sprintf("svaa_top:%s:%d",keys %{$gene->{svaa_top}},values %{$gene->{svaa_top}});
+			for my $aa (sort {$gene->{svaa}{$b} <=> $gene->{svaa}{$a}} keys %{$gene->{svaa}}) {
+				push @report, sprintf("svaa:%s:%d",$aa,$gene->{svaa}{$aa});
+			}
+		}
+		if($gene->{cnv}) {
+			my $type_count;
+			my $delins_count;
+			my $values_by_type;
+			for my $type (sort keys %{$gene->{cnv}}) {
+				my @sample_avg;
+				for my $sample (keys %{$gene->{cnv}{$type}}) {
+					# number of samples with this kind of CNV event
+					$type_count->{$type}++;
+					my @values = @{$gene->{cnv}{$type}{$sample}};
+					push @sample_avg, average(@values);
+					push @{$values_by_type->{$type}}, @values;
+				}
+				push @report, sprintf("cnv:%s:%d:%f:%f:%f:%f",
+															$type,
+															int(@sample_avg),
+															scalar(min(@sample_avg)),
+															average(@sample_avg),
+															median(@sample_avg)->query,
+															scalar(max(@sample_avg)));
+			}
+			my ($top_type) = sort {$type_count->{$b} <=> $type_count->{$a}} keys %$type_count;
+			push @report, sprintf("cnv_top:%s:%d:%f:%f:%f:%f",
+														$top_type,
+														$type_count->{$top_type},
+														scalar(min(@{$values_by_type->{$top_type}})),
+														average(@{$values_by_type->{$top_type}}),
+														median(@{$values_by_type->{$top_type}})->query,
+														scalar(max(@{$values_by_type->{$top_type}})));
+		}
+		printinfo(@report);
+	}
+}
+
+exit;
+
+sub read_file {
+	my ($file,$type) = @_;
+	open(F,$file) || die "Could not open file [$file] for reading";
+	my $data;
+
+	my @fields  = grep(/\d/,keys %{$CONF{fields}{$type}});
+	my @keys    = split(",",$CONF{fields}{$type}{key});
+
+	my $i;
+	while(<F>) {
+		chomp;
+		next if /^\#/;
+		my @tok = split "\t";
+		my $entry = {class=>$type};
+		for my $col (@fields) {
+			my ($field_name,$field_transform) = split(":",$CONF{fields}{$type}{$col});
+			my $value = $tok[$col];
+			if($field_transform) {
+				$value = lc $value if $field_transform =~ /lc/;
+			}
+			$entry->{ $field_name } = $value;
+		}
+		# skip mutation types that are not important
+		next if $CONF{sv}{filter} && $type eq "sv" && exists $entry->{type} && ! $CONF{sv}{types}{$entry->{type}};
+		next if $CONF{cnv}{filter} && $type eq "cnv" && exists $entry->{category} && ! $CONF{cnv}{types}{$entry->{category}};
+		if($type eq "sv") {
+			$entry->{weight} = $CONF{sv}{types}{$entry->{type}};
+		}
+		$entry->{chr} = "X" if $entry->{chr} eq 23;
+		$entry->{chr} = "Y" if $entry->{chr} eq 24;
+		next unless grep($entry->{chr} eq $_, (1..22,"X","Y"));
+		$entry->{set} = span(@{$entry}{qw(start end)}) if $entry->{start};
+		$entry->{size} = $entry->{set}->cardinality;
+		#printdumper($entry);
+		$i++;
+		if(@keys == 1) {
+			push @{$data->{$entry->{$keys[0]}}}, $entry;
+		} elsif (@keys == 2) {
+			push @{$data->{$entry->{$keys[0]}}{$entry->{$keys[1]}}}, $entry;
+		}
+	}
+	printdebug("got",$i,$type);
+	return $data;
+}
+
+sub list2hash {
+	my %h;
+	map {$h{$_}=1} @_;
+	return %h;
+}
+
+sub span {
+	my ($x,$y) = @_;
+	if($x==$y) {
+		return Set::IntSpan->new($x);
+	} else {
+		return Set::IntSpan->new("$x-$y");
+	}
+}
+
+sub get_handle {
+	my $h;
+	if(my $file = $CONF{file}) {
+		die "No such file [$file]" unless -e $file;
+		open(FILE,$file);
+		$h = \*FILE;
+	} else {
+		$h = \*STDIN;
+	}
+	return $h;
+}
+
+# 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 ($CONF{cdump}) {
+    $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 ($node,$parent_node_name) = shift;
+  return unless ref($node) eq "HASH";
+  for my $key (keys %$node) {
+		my $value = $node->{$key};
+		if (ref($value) eq "HASH") {
+			repopulateconfiguration($value,$key);
+		} elsif (ref($value) eq "ARRAY") {
+			for my $item (@$value) {
+				repopulateconfiguration($item,$key);
+			}
+		} elsif (defined $value) {
+			my $new_value = parse_field($value,$key,$parent_node_name,$node);
+			$node->{$key} = $new_value;
+		}
+	}
+}
+
+sub parse_field {
+	my ($str,$key,$parent_node_name,$node) = @_;
+	# replace configuration field
+	# conf(LEAF,LEAF,...)
+	while ( $str =~ /(conf\(\s*(.+?)\s*\))/g ) {
+		my ($template,$leaf) = ($1,$2);
+		if (defined $template && defined $leaf) {
+			my @leaf         = split(/\s*,\s*/,$leaf);
+			my $new_template;
+			if (@leaf == 2 && $leaf[0] eq ".") {
+				$new_template = $node->{$leaf[1]};
+			} else {
+				$new_template = fetch_conf(@leaf);
+			}
+			$str =~ s/\Q$template\E/$new_template/g;
+		}
+	}
+	if ($str =~ /\s*eval\s*\(\s*(.+)\s*\)/) {
+		my $fn = $1;
+		$str = eval $fn;
+		if ($@) {
+			die "could not parse configuration parameter [$@]";
+		}
+	}
+	return $str;
+}
+
+sub fetch_configuration {
+	my @config_path = @_;
+	my $node        = \%CONF;
+	if(! @config_path) {
+		return \%CONF;
+	}
+	for my $path_element (@config_path) {
+		if (! exists $node->{$path_element}) {
+	    return undef;
+		} else {
+	    $node = $node->{$path_element};
+		}
+	}
+	return $node;
+}
+
+sub fetch_conf {
+	return fetch_configuration(@_);
+}
+
+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 {
+	printinfo("debug",@_) if defined $CONF{debug};
+}
+
+sub printinfo {
+	print join(" ",map { defined $_ ? $_ : "_undef_" } @_),"\n";
+}
+
+sub printfinfo {
+	my ($fmt,@args) = @_;
+	@args = map { defined $_ ? $_ : "_undef_" } @args;
+	printf("$fmt\n",@args);
+}
+
+sub printerr {
+	print STDERR join(" ",map { defined $_ ? $_ : "_undef_" } @_),"\n";
+}
+
+sub printdumper {
+	print Dumper(@_);
+}
+
+=pod 
+
+=head1 HISTORY
+
+=over
+
+=item * 30 Nov 2015
+
+Started.
+
+=back 
+
+=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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/bands.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,5 @@
+show_bands            = yes
+fill_bands            = yes
+band_stroke_thickness = 0
+band_stroke_color     = white
+band_transparency     = 4
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/breaks.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,13 @@
+<break_style 1>
+stroke_color = black
+fill_color   = blue
+thickness    = 0.25r
+stroke_thickness = 2
+</break_style>
+
+<break_style 2>
+stroke_color     = white
+stroke_thickness = 3
+thickness        = 4r
+</break_style>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/circos.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,312 @@
+
+# You can control which tracks to show. 
+# 
+# These can be set at the command line using
+#
+# circos -param show_labels=no -param show_sv_total=no ...
+#
+# You can verify settings using -cdump 
+#
+# circos -cdump | grep show_
+
+show_labels   = yes
+show_sv_total = yes
+show_svaa     = yes
+show_sv_stack = yes
+show_sv_types = yes
+
+show_cnv_stack = yes
+show_cnv_types = yes
+show_cnv_plus_minus = yes
+show_cnv_copy_number = yes
+
+sv_label_min  = 5
+
+################################################################
+# Colors are referenced using their name e.g. "amp" in 
+# parameters directly
+#
+# fill_color = amp
+
+<colors>
+amp   = vdred
+gain  = red
+neut  = dgrey 
+homd  = blue
+hetd  = vdblue
+
+cnv-1 = amp
+cnv-2 = gain
+cnv-3 = hetd
+cnv-4 = homd
+
+sv-1  = grey
+sv-2  = vdblue
+sv-3  = vdred
+sv-4  = blue
+sv-5  = red
+sv-6  = lorange
+sv-7  = vdorange
+sv-8  = vlgrey
+
+# color lists, sorted numerically in increasing order
+# using the text matched in capture brackets
+sv    = sv-(\d+)
+cnv   = cnv-(\d+)
+</colors>
+
+chromosomes_units           = 1
+chromosomes_display_default = yes
+chromosomes                 = -hsY
+
+#chromosomes_display_default = no
+#chromosomes                 = /hs[1234]$/
+
+karyotype = data/karyotype.txt
+
+################################################################
+# For auto-spacing and positioning of tracks
+
+# Start, width and spacing of tracks
+track_r0 = 0.99
+track_w  = 0.1
+track_s  = 0.025
+
+# width and spacing of individual SV type tracks
+sv_w     = 0.0   # 
+sv_s     = 0.015 # 
+
+# Order in which SV and CNV tracks are shown
+sv_types = nonsense_mutation,frame_shift_del,frame_shift_ins,in_frame_del,in_frame_ins,splice_site,nonstop_mutation,missense_mutation
+cnv_types = amp,gain,hetd,homd
+
+<plots>
+
+# All plot blocks share data from the same file
+file             = data/mutations.txt
+stroke_thickness = 0
+
+################################################################
+# Gene labels, filtered by SNV total
+<plot>
+show       = conf(show_labels)
+type       = text
+r1         = dims(ideogram,radius_outer) + 500p
+r0         = dims(ideogram,radius_outer) + 20p + conf(track_w)r
+
+label_size           = 14
+label_snuggle        = yes
+max_snuggle_distance = 3r
+snuggle_sampling     = 1
+snuggle_tolerance    = 0.25r
+
+show_links    = yes
+link_color    = black
+link_dims     = 0p,5p,5p,5p,0p
+
+<rules>
+<rule>
+condition  = var(sv_tot) < conf(sv_label_min)
+show       = no
+</rule>
+<rule>
+condition  = var(sv_tot) > 2*conf(sv_label_min)
+label_font = bold
+</rule>
+</rules>
+</plot>
+
+################################################################
+# SV total 
+
+<plot>
+show       = conf(show_sv_total)
+type       = histogram
+r1         = dims(ideogram,radius_outer) + 20p + conf(track_w)r
+r0         = dims(ideogram,radius_outer) + 20p
+min        = 0
+#max        = 20
+fill_color = black
+
+<<include white.bin.conf>>
+
+<axes>
+<axis>
+spacing = 0.1r
+</axis>
+</axes>
+
+<rules>
+<rule>
+condition = 1
+value     = eval(var(sv_tot))
+flow      = continue
+</rule>
+</rules>
+</plot>
+
+################################################################
+# SV protein position recurrence
+
+<plot>
+show      = conf(show_svaa)
+type      = heatmap
+r1        = dims(ideogram,radius_outer)+15p
+r0        = dims(ideogram,radius_outer)+5p
+min       = 2
+max       = 5
+color     = grey,orange,red,dred
+<rules>
+<rule>
+condition = 1
+value     = eval(var(svaa_max_n))
+flow      = continue
+</rule>
+<rule>
+condition = var(value) == 1
+show      = no
+</rule>
+</rules>
+</plot>
+
+
+################################################################
+# SV stacked barplot 
+
+<plot>
+show = conf(show_sv_stack)
+file = data/mutations.stacked.sv.txt
+orientation = in
+type = histogram
+r1   = eval(sprintf("%fr",conf(track_r0)))
+r0   = eval(sprintf("%fr",conf(track_r0)-conf(track_w)))
+min  = 0
+max  = 1
+fill_color           = sv
+normalize_bin_values = yes
+<<include white.bin.conf>>
+<<include track.counter.conf>>
+</plot>
+
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+
+###############################################################
+# +/- CNV histograms
+
+<plot>
+show = conf(show_cnv_plus_minus)
+type = histogram
+<<include r0r1.conf>>
+max = 20
+min = -20
+fill_color = gain
+<<include white.bin.conf>>
+<axes>
+<axis>
+spacing = .1r
+</axis>
+<axis>
+position  = 0
+thickness = 2
+</axis>
+</axes>
+<backgrounds>
+<background>
+color = vvlgrey
+</background>
+</backgrounds>
+<rules>
+<rule>
+condition  = 1
+value      = eval(var(cnv_plus))
+</rule>
+</rules>
+</plot>
+
+<plot>
+show = conf(show_cnv_plus_minus)
+type = histogram
+<<include r0r1.conf>>
+max  = 20
+min  = -20
+fill_color = homd
+<<include white.bin.conf>>
+<<include track.counter.conf>>
+<rules>
+<rule>
+condition  = 1
+value      = eval(-var(cnv_minus))
+</rule>
+</rules>
+</plot>
+
+################################################################
+# CNV stacked bar plot
+
+<plot>
+show = conf(show_cnv_stack)
+file = data/mutations.stacked.cnv.txt
+orientation = in
+type = histogram
+<<include r0r1.conf>>
+min  = 0
+max  = 1
+fill_color = cnv
+normalize_bin_values = yes
+<<include white.bin.conf>>
+<<include track.counter.conf>>
+</plot>
+
+<plot>
+show = conf(show_cnv_copy_number)
+type = histogram
+<<include r0r1.conf>>
+min  = -2
+max  = 2
+<<include white.bin.conf>>
+<<include track.counter.conf>>
+<axes>
+<axis>
+position = 0
+color    = black
+</axis>
+<axis>
+spacing  = 0.5
+y1       = 0
+color    = lred
+</axis>
+<axis>
+spacing  = 0.5
+y0       = 0
+color    = lblue
+</axis>
+</axes>
+<rules>
+<rule>
+condition = !var(cnv_top_n)
+show      = no
+</rule>
+<rule>
+condition  = 1
+value      = eval(var(cnv_top_avg))
+fill_color = eval(lc var(cnv_top_type))
+</rule>
+</rules>
+</plot>
+
+</plots>
+
+<image>
+<<include etc/image.conf>>
+</image>
+<<include etc/housekeeping.conf>>
+<<include etc/colors_fonts_patterns.conf>>
+<<include ideogram.conf>>
+#<<include ticks.conf>>
+max_points_per_track* = 50000
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/circos.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,309 @@
+
+show_labels   = yes
+show_sv_total = yes
+show_svaa     = yes
+show_sv_stack = yes
+show_sv_types = yes
+
+show_cnv_stack = yes
+show_cnv_types = yes
+show_cnv_plus_minus = yes
+show_cnv_copy_number = yes
+
+sv_label_min  = 5
+
+show_cnv = no # old
+
+<colors>
+amp   = dred
+gain  = red
+neut  = dgrey  # not used
+homd  = blue
+hetd  = dblue
+
+cnv-1 = amp
+cnv-2 = gain
+cnv-3 = hetd
+cnv-4 = homd
+
+sv-1  = black
+sv-2  = dblue
+sv-3  = dred
+sv-4  = blue
+sv-5  = red
+sv-6  = lorange
+sv-7  = orange
+sv-8  = vlgrey
+
+sv    = sv-(\d+)
+cnv   = cnv-(\d+)
+</colors>
+
+chromosomes_units           = 1
+chromosomes_display_default = yes
+chromosomes                 = -hsY # hs1;hs2;hs3
+
+karyotype = data/karyotype.txt
+
+# SV by type
+sv_r0    = 0.90  # outer track radius
+sv_w     = 0.0   # track width
+sv_s     = 0.015 # track width for smaller tracks
+# this is the order of the SV type tracks, from outside in
+sv_types = nonsense_mutation,frame_shift_del,frame_shift_ins,in_frame_del,in_frame_ins,splice_site,nonstop_mutation,missense_mutation
+
+# CNV by type
+cnv_r0    = 0.70  # outer track radius
+cnv_w     = 0.0   # track width
+cnv_s     = 0.015 # track width for smaller tracks
+cnv_types = amp,gain,hetd,homd
+
+<plots>
+
+file             = data/mutations.txt
+stroke_thickness = 0
+
+################################################################
+# SV total 
+
+<plot>
+show       = conf(show_sv_total)
+type       = histogram
+r1         = dims(ideogram,radius_outer) + 100p
+r0         = dims(ideogram,radius_outer) + 20p
+min        = 0
+max        = 20
+fill_color = black
+
+<axes>
+<axis>
+spacing   = 4
+</axis>
+</axes>
+
+<rules>
+<rule>
+condition = 1
+value     = eval(var(sv_tot))
+flow      = continue
+</rule>
+</rules>
+</plot>
+
+################################################################
+# SV protein position recurrence
+
+<plot>
+show      = conf(show_svaa)
+type      = heatmap
+r1        = dims(ideogram,radius_outer)+15p
+r0        = dims(ideogram,radius_outer)+5p
+min       = 2
+max       = 5
+color     = grey,orange,red,dred
+<rules>
+<rule>
+condition = 1
+value     = eval(var(svaa_max_n))
+flow      = continue
+</rule>
+<rule>
+condition = var(value) == 1
+show      = no
+</rule>
+</rules>
+</plot>
+
+################################################################
+# Gene labels, filtered by SNV total
+<plot>
+show       = conf(show_labels)
+type       = text
+r1         = dims(ideogram,radius_outer) + 500p
+r0         = dims(ideogram,radius_outer) + 100p
+
+label_snuggle = yes
+show_links    = yes
+
+label_size    = 14
+link_color    = black
+link_dims     = 0p,5p,5p,5p,0p
+
+<rules>
+<rule>
+condition  = var(sv_tot) < conf(sv_label_min)
+show       = no
+</rule>
+</rules>
+</plot>
+
+################################################################
+# SV stacked barplot 
+
+<plot>
+show = conf(show_sv_stack)
+file = data/mutations.stacked.sv.txt
+orientation = in
+type = histogram
+r1   = 0.995r
+r0   = 0.925r
+min  = 0
+max  = 1
+fill_color           = sv
+normalize_bin_values = yes
+</plot>
+
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+<<include sv.type.conf>>
+
+################################################################
+# CNV stacked bar plot
+
+<plot>
+show = conf(show_cnv_stack)
+file = data/mutations.stacked.cnv.txt
+orientation = in
+type = histogram
+r1   = 0.775r
+r0   = 0.725r
+min  = 0
+max  = 1
+fill_color = cnv
+normalize_bin_values = yes
+</plot>
+
+################################################################
+# CNV in samples, by event type
+
+<<include cnv.type.conf>>
+<<include cnv.type.conf>>
+<<include cnv.type.conf>>
+<<include cnv.type.conf>>
+
+###############################################################
+# +/- CNV histograms
+
+<plot>
+show = conf(show_cnv_plus_minus)
+type = histogram
+r1   = 0.625r
+r0   = 0.55r
+max  = 20
+min  = -20
+fill_color = blue
+<axes>
+<axis>
+spacing = 5
+</axis>
+<axis>
+position  = 0
+thickness = 2
+</axis>
+</axes>
+<backgrounds>
+<background>
+color = vvlgrey
+</background>
+</backgrounds>
+<rules>
+<rule>
+condition  = 1
+value      = eval(var(cnv_plus))
+</rule>
+</rules>
+</plot>
+
+<plot>
+show = conf(show_cnv_plus_minus)
+type = histogram
+r1   = 0.625r
+r0   = 0.55r
+max  = 20
+min  = -20
+fill_color = red
+<rules>
+<rule>
+condition  = 1
+value      = eval(-var(cnv_minus))
+</rule>
+</rules>
+</plot>
+
+<plot>
+show = conf(show_cnv)
+type = histogram
+r1   = 0.775r
+r0   = 0.725r
+min  = 0
+
+<axes>
+<axis>
+spacing = 10
+</axis>
+</axes>
+
+<rules>
+<rule>
+condition  = !var(cnv_tot)
+show       = no
+</rule>
+<rule>
+condition  = 1
+value      = eval(var(cnv_tot))
+fill_color = eval(lc var(cnv_type))
+</rule>
+</rules>
+
+</plot>
+
+<plot>
+show = conf(show_cnv_copy_number)
+type = histogram
+r1   = 0.525r
+r0   = 0.40r
+min  = -2
+max  = 2
+<axes>
+<axis>
+position = 0
+color    = black
+</axis>
+<axis>
+spacing  = 0.5
+y1       = 0
+color    = lred
+</axis>
+<axis>
+spacing  = 0.5
+y0       = 0
+color    = lblue
+</axis>
+</axes>
+<rules>
+<rule>
+condition = !var(cnv_top_n)
+show      = no
+</rule>
+<rule>
+condition  = 1
+value      = eval(var(cnv_top_avg))
+fill_color = eval(lc var(cnv_top_type))
+</rule>
+</rules>
+</plot>
+
+</plots>
+
+<image>
+<<include etc/image.conf>>
+</image>
+<<include etc/housekeeping.conf>>
+<<include etc/colors_fonts_patterns.conf>>
+<<include ideogram.conf>>
+#<<include ticks.conf>>
+max_points_per_track* = 50000
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/cnv.type.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,32 @@
+<plot>
+show = conf(show_cnv_types)
+init_counter           = cnv:0
+post_increment_counter = cnv:1
+type = scatter
+r1   = eval(sprintf("%fr",conf(cnv_r0) - counter(cnv)*(conf(cnv_w)+conf(cnv_s))))
+r0   = eval(sprintf("%fr",conf(cnv_r0) - counter(cnv)*(conf(cnv_w)+conf(cnv_s)) - conf(cnv_w)))
+min  = 0
+max  = 0
+color = eval(sprintf("cnv-%d",1+counter(cnv)))
+<axes>
+<axis>
+position = 0
+</axis>
+</axes>
+<rules>
+<rule>
+condition  = 1
+type       = eval(sprintf("cnv_%s",(split(",",qq{conf(cnv_types)}))[counter(cnv)]))
+flow       = continue
+</rule>
+<rule>
+condition  = !var(var(type))
+show       = no
+</rule>
+<rule>
+condition  = 1
+value      = 0
+glyph_size = eval(remap_int(var(var(type)),1,10,10,30))
+</rule>
+</rules>
+</plot>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/cnv.type.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,32 @@
+<plot>
+show = conf(show_sv_types)
+init_counter           = sv:0
+post_increment_counter = sv:1
+type = scatter
+r1   = eval(sprintf("%fr",conf(sv_r0) - counter(sv)*(conf(sv_w)+conf(sv_s))))
+r0   = eval(sprintf("%fr",conf(sv_r0) - counter(sv)*(conf(sv_w)+conf(sv_s)) - conf(sv_w)))
+min  = 0
+max  = 0
+color = eval(sprintf("dark2-8-qual-%d",1+counter(sv)))
+<axes>
+<axis>
+position = 0
+</axis>
+</axes>
+<rules>
+<rule>
+condition  = 1
+type       = eval(sprintf("sv_%s",(split(",",qq{conf(sv_types)}))[counter(sv)]))
+flow       = continue
+</rule>
+<rule>
+condition  = !var(var(type))
+show       = no
+</rule>
+<rule>
+condition  = 1
+value      = 0
+glyph_size = eval(remap_int(var(var(type)),1,10,10,30))
+</rule>
+</rules>
+</plot>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/color.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,1 @@
+color = eval((counter(plot)%3) == 0 ? "blue" : (counter(plot)%3) == 1 ? "orange,red,dred" : "black")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/color.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,1 @@
+color = eval((counter(plot)%3) == 0 ? "blue" : (counter(plot)%3) == 1 ? "orange,red,dred" : "red")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/histogram.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,86 @@
+# this file defines the histograms about system inputs and outputs
+
+<plots>
+
+<plot>
+type        = histogram
+<axes>
+<axis>
+# grey axis every 100 units 
+color     = grey_a3
+spacing   = 100
+thickness = 2p
+</axis>
+<axis>
+# black axis every 100 units 
+color     = black_a2
+spacing   = 500
+thickness = 2p
+</axis>
+</axes>
+z=0
+</plot>
+
+
+
+# datatrack type
+type        = histogram
+thickness   = 0p
+color       = black
+
+# radial position of the histogram
+r0          = 1r+150p
+r1          = 1.3r
+
+# value start and end of the drawn histogram
+min         = 0
+# the maximum should be the maximum of all primary inputs or final outputs
+# i.e. max_value(max_value(total primary inputs), max_value(total finalgoods),max_value(total emission_X))
+max         = 1246.0
+
+<plot>
+# Normal histogram for resources
+file        = ../data/histogram_data_resources.txt
+fill_color  = col_resources_a1
+z=100
+</plot>
+
+<plot>
+# Normal histogram for final_goods
+file        = ../data/histogram_data_final_goods.txt
+fill_color  = col_final_goods_a1
+z=100
+</plot>
+
+<plot>
+# Normal histogram for emission_0
+file        = ../data/histogram_data_emission_0.txt
+fill_color  = col_emission_0_a1
+z=100
+</plot>
+
+<plot>
+# text labels plot for normal or stacked histograms for resources, fd and emissions
+type  = text
+color = black
+file  = ../data/histogram_labels.txt
+#note: if label do not fit in the space r1-r0, they will  not be drawn
+r0 = 1.32r
+r1 = 1.5r
+label_parallel = yes
+#show_links     = no
+#link_dims      = 0p,2p,6p,2p,5p
+#link_thickness = 2p
+#link_color     = black
+#label_snuggle        = yes
+#max_snuggle_distance = 1r
+#snuggle_tolerance    = 0.25r
+#snuggle_sampling     = 2
+#snuggle_refine       = yes
+label_size = 20p
+label_font = default
+padding    = 0p
+rpadding   = 0p
+</plot>
+
+</plots>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/ideogram.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,14 @@
+
+<ideogram>
+
+<spacing>
+
+default = 0.001r
+
+</spacing>
+
+<<include ideogram.position.conf>>
+<<include ideogram.label.conf>>
+
+</ideogram>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/ideogram.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,21 @@
+
+<ideogram>
+
+<spacing>
+
+default = 0.002r
+break   = 0.25r
+
+axis_break         = yes
+axis_break_style   = 2
+axis_break_at_edge = yes
+
+<<include breaks.conf>>
+
+</spacing>
+
+<<include ideogram.position.conf>>
+<<include ideogram.label.conf>>
+
+</ideogram>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/ideogram.label.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,19 @@
+
+show_label       = yes
+label_font       = default
+label_radius     = dims(image,radius)-35p
+label_with_tag   = yes
+label_size       = 24
+label_parallel   = no
+label_case       = lower
+
+# you can format the label by using properties
+# of the ideogram, accessible with var(PROPERTY):
+#
+# chr, chr_with_tag, chrlength, display_idx, end, idx, 
+# label, length, reverse, scale, size, start, tag
+
+label_format     = eval(sprintf("%s%s",var(chr) =~ /hs[234]$/ ? "     " : "",var(label)))
+#label_format     = eval(sprintf("chr%s",var(label)))
+#label_format     = eval(my $x = var(label); $x = "" if $x =~ /1/; $x ? sprintf("chr%s",$x) : "")
+#label_format     = eval(my $x = var(label); my ($num) = $x =~ /(\d+)/g; $num < 10 ? $x : "")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/ideogram.position.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,6 @@
+radius           = 0.8r
+thickness        = 10p
+fill             = yes
+fill_color       = black
+stroke_thickness = 1
+stroke_color     = black
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/ideogram.position.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,6 @@
+radius           = 0.85r
+thickness        = 10p
+fill             = yes
+fill_color       = black
+stroke_thickness = 1
+stroke_color     = black
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/library.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,31 @@
+<plot>
+minsize = 5e6
+file = data/lib.eval(counter(library)).txt
+<<include color.conf>>
+<<include r0r1.conf>>
+<rules>
+<<include rule.type.conf>>
+</rules>
+</plot>
+
+<plot>
+file = data/lib.eval(counter(library)).txt
+<<include color.conf>>
+<<include r0r1.conf>>
+<rules>
+<<include rule.type.conf>>
+</rules>
+</plot>
+
+<plot>
+minsize = 5e6
+file = data/lib.eval(counter(library)).txt
+<<include color.conf>>
+<<include r0r1.conf>>
+<rules>
+<<include rule.type.conf>>
+</rules>
+
+post_increment_counter = library:1
+
+</plot>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/library.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,29 @@
+<plot>
+file = data/lib.eval(counter(library)).txt
+<<include color.conf>>
+<<include r0r1.conf>>
+<rules>
+<<include rule.type.conf>>
+</rules>
+</plot>
+
+<plot>
+file = data/lib.eval(counter(library)).txt
+<<include color.conf>>
+<<include r0r1.conf>>
+<rules>
+<<include rule.type.conf>>
+</rules>
+</plot>
+
+<plot>
+file = data/lib.eval(counter(library)).txt
+<<include color.conf>>
+<<include r0r1.conf>>
+<rules>
+<<include rule.type.conf>>
+</rules>
+
+post_increment_counter = library:1
+
+</plot>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/r0r1.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,2 @@
+r1   = eval(sprintf("%fr",conf(track_r0)-7*conf(sv_w)-6*conf(sv_s)-conf(track_s)-counter(x)*(conf(track_w)+conf(track_s))))
+r0   = eval(sprintf("%fr",conf(track_r0)-7*conf(sv_w)-6*conf(sv_s)-conf(track_s)-counter(x)*(conf(track_w)+conf(track_s))-conf(track_w)))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/r0r1.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,2 @@
+r1 = eval(sprintf("%fr",((counter(plot)%3==2)?-conf(tw):0)      + ((counter(plot)%3==0)?conf(tws):0)          + conf(t0) - counter(library)*(conf(tw)+conf(ts))))
+r0 = eval(sprintf("%fr",((counter(plot)%3==2)?-conf(tws):0) + ((counter(plot)%3==0)?+conf(tw):0) + conf(t0) - counter(library)*(conf(tw)+conf(ts)) - conf(tw)))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/rule.snp.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,4 @@
+<rule>
+condition = var(type) ne "snp"
+show      = no
+</rule>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/rule.type.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,26 @@
+<rule>
+condition = (counter(plot)%3) == 0 && var(type) eq "ins"
+show      = yes
+</rule>
+
+<rule>
+condition = (counter(plot)%3) == 1 && var(type) eq "snp" && var(class) eq "missense_mutation"
+z         = 10
+show      = yes
+</rule>
+
+<rule>
+condition = (counter(plot)%3) == 1 && var(type) eq "snp" && var(class) eq "all"
+color     = grey
+show      = yes
+</rule>
+
+<rule>
+condition = (counter(plot)%3) == 2 && var(type) eq "del"
+show      = yes
+</rule>
+<rule>
+condition = 1
+show      = no
+</rule>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/rule.type.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,21 @@
+<rule>
+condition = var(class) ne "all"
+show      = no
+</rule>
+<rule>
+condition = (counter(plot)%3) == 0 && var(type) eq "ins"
+show      = yes
+</rule>
+<rule>
+condition = (counter(plot)%3) == 1 && var(type) eq "snp"
+show      = yes
+</rule>
+<rule>
+condition = (counter(plot)%3) == 2 && var(type) eq "del"
+show      = yes
+</rule>
+<rule>
+condition = 1
+show      = no
+</rule>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/sv.type.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,32 @@
+<plot>
+show = conf(show_sv_types)
+init_counter           = sv:0
+post_increment_counter = sv:1
+type = scatter
+r1   = eval(sprintf("%fr",conf(track_r0) - counter(x)*(conf(track_w)+conf(track_s)) - counter(sv)*(conf(sv_w)+conf(sv_s))))
+r0   = eval(sprintf("%fr",conf(track_r0) - counter(x)*(conf(track_w)+conf(track_s)) - counter(sv)*(conf(sv_w)+conf(sv_s)) - conf(sv_w)))
+min  = 0
+max  = 0
+color = eval(sprintf("sv-%d",1+counter(sv)))
+<axes>
+<axis>
+position = 0
+</axis>
+</axes>
+<rules>
+<rule>
+condition  = 1
+type       = eval(sprintf("sv_%s",(split(",",qq{conf(sv_types)}))[counter(sv)]))
+flow       = continue
+</rule>
+<rule>
+condition  = !var(var(type))
+show       = no
+</rule>
+<rule>
+condition  = 1
+value      = 0
+glyph_size = eval(remap_int(var(var(type)),1,10,10,30))
+</rule>
+</rules>
+</plot>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/sv.type.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,32 @@
+<plot>
+show = conf(show_sv_types)
+init_counter           = sv:0
+post_increment_counter = sv:1
+type = scatter
+r1   = eval(sprintf("%fr",conf(sv_r0) - counter(sv)*(conf(sv_w)+conf(sv_s))))
+r0   = eval(sprintf("%fr",conf(sv_r0) - counter(sv)*(conf(sv_w)+conf(sv_s)) - conf(sv_w)))
+min  = 0
+max  = 0
+color = eval(sprintf("sv-%d",1+counter(sv)))
+<axes>
+<axis>
+position = 0
+</axis>
+</axes>
+<rules>
+<rule>
+condition  = 1
+type       = eval(sprintf("sv_%s",(split(",",qq{conf(sv_types)}))[counter(sv)]))
+flow       = continue
+</rule>
+<rule>
+condition  = !var(var(type))
+show       = no
+</rule>
+<rule>
+condition  = 1
+value      = 0
+glyph_size = eval(remap_int(var(var(type)),1,10,10,30))
+</rule>
+</rules>
+</plot>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/ticks.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,40 @@
+
+show_ticks          = yes
+show_tick_labels    = yes
+#show_grid           = yes
+
+<ticks>
+radius           = dims(ideogram,radius_outer)
+color            = black
+thickness        = 2p
+
+grid_start     = 0.2r
+grid_end       = 0.999r
+grid_color     = lgrey
+grid_thickness = 1p
+
+<tick>
+spacing        = 1u
+size           = 6p
+</tick>
+
+<tick>
+show = no
+spacing        = 10u
+size           = 10p
+grid           = yes
+grid_color     = vlgrey
+</tick>
+
+<tick>
+show = no
+grid           = yes
+spacing        = 20u
+size           = 15p
+show_label     = yes
+label_size     = 20p
+label_offset   = 10p
+format         = %d
+</tick>
+
+</ticks>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/ticks.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,39 @@
+
+show_ticks          = yes
+show_tick_labels    = yes
+show_grid = yes
+
+<ticks>
+radius           = dims(ideogram,radius_outer)
+multiplier       = 1e-6
+color            = black
+thickness        = 2p
+
+grid_start = 0.2r
+grid_end   = 0.999r
+grid_color     = lgrey
+grid_thickness = 1p
+
+<tick>
+spacing        = 5u
+size           = 6p
+</tick>
+
+<tick>
+spacing        = 10u
+size           = 10p
+grid = yes
+grid_color = vlgrey
+</tick>
+
+<tick>
+grid = yes
+spacing        = 20u
+size           = 15p
+show_label     = yes
+label_size     = 20p
+label_offset   = 10p
+format         = %d
+</tick>
+
+</ticks>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/track.counter.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,2 @@
+init_counter           = x:0
+post_increment_counter = x:1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/track.counter.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,2 @@
+init_counter           = track:1
+post_increment_counter = track:1
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/white.bin.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,4 @@
+extend_bin       = no
+color            = white_a4
+stroke_thickness = 1
+stroke_type      = bin
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/etc/white.bin.conf~	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,4 @@
+extend_bin = no
+color      = white
+stroke_thickness = 1
+stroke_type = bin
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/oncocircos.xml	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,77 @@
+<tool id="oncocircos" name="Oncocircos" version="0.69">
+
+  <description>
+    creates a genome-centric visualization of SNV and CNV Data
+  </description>
+
+  <requirements>
+    <requirement type="package" version="0.69">circos_perl_environment</requirement>
+    <requirement type="package" version="0.69">circos</requirement>
+  </requirements>
+
+
+  <command error_checking="aggressive">
+
+  mkdir data;
+  mkdir circos;
+  mkdir circos/data;
+  mkdir circos/etc;
+
+  cp $__tool_directory__/etc/* circos/etc/;
+
+  mkdir etc;
+  
+  ln -s $input_maf data/snv.txt;
+  ln -s $input_seg data/cnv.txt;  
+  cat $__tool_directory__/parse.conf
+
+  #if $advancedOptions.filter_snvs != 1:
+    | sed 's/^SNV_FILTER$/sv = yes/g'
+    | sed 's/^SNV_FILTER_NUM$/sv_num = $advancedOptions.filter_snvs/g'
+  #else:
+    | sed 's/^SNV_FILTER$/sv = no/g'
+    | sed 's/^SNV_FILTER_NUM$//g'
+  #end if
+
+  #if $advancedOptions.chromosome != "":
+    | sed 's/^CHR$/chr = $advancedOptions.chromosome/g'
+  #else:
+    | sed 's/^CHR$//g'
+  #end if
+
+  | sed "s#^ROOT#root = \$(pwd)#g"
+  | sed 's#^CNV$#cnv = data/cnv.txt#g'
+  | sed 's#^SNV$#sv = data/snv.txt#g'
+  | sed 's#^MART#mart = $biomart#g'
+  | sed 's#^CIRCOS#circos = circos/data#g'
+
+  > ./etc/parse.conf;
+
+  perl $__tool_directory__/bin/parse --conf ./etc/parse.conf | perl $__tool_directory__/bin/make.circos.data --conf ./etc/parse.conf;
+
+  circos --conf ./circos/etc/circos.conf;
+
+  cp circos.png $png;
+  cp circos.svg $svg;
+  cp ./etc/parse.conf $tmp;
+
+  </command>
+
+  <inputs>
+    <param type="data" format="maf" name="input_maf" label="Cohort Wide MAF File"/>
+    <param type="data" format="segs" name="input_seg" label="Cohort Wide SEGS File"/>
+    <param type="data" format="" name="biomart" label="Biomart File"/>
+    <section name="advancedOptions">
+      <param type="integer" name="filter_snvs" 
+             min="1" value="1" max="100" 
+             label="Filter genes with a cohort wide snv tally below this value"/>
+      <param type="text" name="chromosome" value="" 
+            label="Restrict plot to the following chromosome"/>
+    </section>
+  </inputs>
+  <outputs>
+    <data format="png" name="png" />
+    <data format="svg" name="svg" />
+    <data format="txt" name="tmp" />
+  </outputs>
+</tool>    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/parse.conf	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,72 @@
+<filter>
+SNV_FILTER
+SNV_FILTER_NUM
+CHR
+</filter>
+
+<files>
+ROOT
+CNV
+SNV
+MART 
+CIRCOS 
+</files>
+
+<fields>
+<cnv>
+key = sample,chr
+0   = sample
+1   = chr
+2   = start
+3   = end
+5   = avg
+6   = category:lc
+</cnv>
+
+<sv>
+key = id,sample
+4   = chr
+5   = start
+6   = end
+8   = type:lc
+15  = sample
+47  = id
+53  = aa
+</sv>
+
+<genes>
+key = chr
+0   = id
+2   = end
+3   = start
+4   = chr
+5   = name
+</genes>
+</fields>
+
+<cnv>
+filter     = yes
+<types>
+amp   = 20
+gain  = 15
+neut  = 0
+hetd  = 10
+homd  = 5
+hlamp = 0
+</types>
+</cnv>
+
+<sv>
+filter     = yes
+top_damage_only   = yes
+<types>
+nonsense_mutation = 35
+frame_shift_del   = 30
+frame_shift_ins   = 25
+in_frame_del      = 20
+in_frame_ins      = 15
+splice_site       = 10
+nonstop_mutation  = 5
+missense_mutation = 1
+</types>
+</sv>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool_dependencies.xml	Mon Sep 12 16:23:26 2016 -0400
@@ -0,0 +1,9 @@
+<?xml version="1.0"?>
+<tool_dependency>
+  <package name="circos_perl_environment" version="0.69">
+    <repository changeset_revision="a6a59681930e" name="package_circos_0_69" owner="morinlab" prior_installation_required="True" toolshed="https://testtoolshed.g2.bx.psu.edu" />
+  </package>
+  <package name="circos" version="0.69">
+    <repository changeset_revision="a6a59681930e" name="package_circos_0_69" owner="morinlab" prior_installation_required="True" toolshed="https://testtoolshed.g2.bx.psu.edu" />
+  </package>
+</tool_dependency>