#!/usr/bin/perl

use strict;
use warnings;

use GD; # required for image creation
use GD::Text::Align; # required for formatting 
use Number::Format qw(:subs); # required for adding commas to numbers

use lib "$ENV{HOME}/scripts/jaiswallab/interactome_scripts";

my $venn_src_dir = "$ENV{HOME}/jaiswallab_svn/Personnel/elserj/venn_sources";

if($#ARGV != 0) {
	print "Usage: create_venn_diagram.pl species_list_file\n";
	exit;
}

use DbiFloret;

print "User credentials for the supercluster database\n";
my $dbh = DbiFloret::dbconnect;

# create the database handler to get the total number of gene models in the fasta file
#   Assumes the fasta has been loaded to the protein sequences database
print "User credentials for the protein sequences database\n";
my $dbh_seq = DbiFloret::dbconnect;

# read species from file
my @species_array;
my $infile = $ARGV[0];
open(INFILE, $infile)  || die "Error: File $infile can not be opened";
while(<INFILE>) {
	my $line = $_;
	chomp $line;
	push(@species_array, $line);
}
close(INFILE);

# human_mouse_kangaroo_rat_platypus_beaver
#my @species_array = ("Arabidopsis_thaliana", "Cannabis_sativa.CRBRx", "Humulus_lupulus.haplotig", "Humulus_lupulus.primary", "Vitis_vinifera");
# beaver_human_mouse_rat_platypus
#my @species_array = ("Castor_canadensis.brent.proteins", "Homo_sapiens", "Mus_musculus", "Ornithorhynchus_anatinus", "Rattus_norvegicus");
# human_mouse_beaver_rabbit_ferret
#my @species_array = ("Castor_canadensis.brent.proteins", "Homo_sapiens", "Mus_musculus", "Mustela_putorius.furo" , "Oryctolagus_cuniculus");
#my @species_array = ("Aegilops_tauschii", "Hordeum_vulgare", "TmDV92", "TmG3116");
#my @species_array = ("Arabidopsis_thaliana", "Homo_sapiens", "Oryza_sativa.japonica.IRGSP");
my $species_array_size = @species_array;

my %gene_hash;
my %super_hash;

my $sth;

if ($species_array_size == 3) {
$sth=$dbh->prepare("select * from super_clust where `species` = '$species_array[0]' or `species` = '$species_array[1]' or `species` = '$species_array[2]'");
}elsif ($species_array_size == 4) {
$sth=$dbh->prepare("select * from super_clust where `species` = '$species_array[0]' or `species` = '$species_array[1]' or `species` = '$species_array[2]' or `species` = '$species_array[3]'");
}elsif ($species_array_size == 5) {
$sth=$dbh->prepare("select * from super_clust where `species` = '$species_array[0]' or `species` = '$species_array[1]' or `species` = '$species_array[2]' or `species` = '$species_array[3]' or `species` = '$species_array[4]'");
}elsif ($species_array_size == 6) {
$sth=$dbh->prepare("select * from super_clust where `species` = '$species_array[0]' or `species` = '$species_array[1]' or `species` = '$species_array[2]' or `species` = '$species_array[3]' or `species` = '$species_array[4]' or `species` = '$species_array[5]'");
}elsif ($species_array_size == 7) {
$sth=$dbh->prepare("select * from super_clust where `species` = '$species_array[0]' or `species` = '$species_array[1]' or `species` = '$species_array[2]' or `species` = '$species_array[3]' or `species` = '$species_array[4]' or `species` = '$species_array[5]' or `species` = '$species_array[6]'");
}

my $rv = $sth->execute();

if (!$rv) {
	next;
}

# build the hash from the db
while (my @line = $sth->fetchrow_array()) {
	my ($super_id, $species, $gene) = @line;

	if(defined($super_hash{$super_id})){
		my $oldhash = $super_hash{$super_id};
		$oldhash->{'species'} = "$oldhash->{'species'}\t$species";;
		$oldhash->{'gene'} = "$oldhash->{'gene'}\t$gene";;
		$super_hash{$super_id} = $oldhash;
	}else{
		my %hash;
		$hash{'species'} = $species;
		$hash{'gene'} = $gene;
		$super_hash{$super_id} = \%hash;
	}
	
	# block to add the hash keyed by gene and value $super_id
	#  should not have the same gene with multiple ids, so error if found
	if(defined($gene_hash{$gene})) {
		die "gene found with multiple id's\t$gene";
	}else{
		$gene_hash{$gene}->{'id'} = $super_id;
		$gene_hash{$gene}->{'species'} = $species;
	}
}

# create the hash that is keyed by the cluster ids and has values given by the Venn set eg. Grape-Fragaria
my %clust_hash;
foreach my $super_id (keys %super_hash) {
	if(defined($clust_hash{$super_id})) {
		my $oldhash = $clust_hash{$super_id};
		for (my $i=0;$i<$species_array_size;$i++) {
			if($super_hash{$super_id}->{'species'} =~ /$species_array[$i]/) {
				if($clust_hash{$super_id} !~ /$species_array[$i]/) {
					$oldhash = "$oldhash\t$species_array[$i]";
				}
			}
		}
		
		$clust_hash{$super_id} = $oldhash;
		
		
	}else{
		for (my $i=0;$i<$species_array_size;$i++) {
			if($super_hash{$super_id}->{'species'} =~ /$species_array[$i]/) {
				if(defined($clust_hash{$super_id})) {
					$clust_hash{$super_id} = "$clust_hash{$super_id}\t$species_array[$i]";
				}else{$clust_hash{$super_id} = $species_array[$i];
				}
			}
		}
		
	}
}

# create a new hash that is keyed by the Venn set and with values for the genes and cluster ids
	# kind of a reverse hash of %clust_hash
my %count_hash;
foreach my $clust_id (keys %clust_hash) {
	if(defined($count_hash{$clust_hash{$clust_id}})) {
		$count_hash{$clust_hash{$clust_id}}->{'genes'} = "$count_hash{$clust_hash{$clust_id}}->{'genes'}\t$super_hash{$clust_id}->{'gene'}";
		$count_hash{$clust_hash{$clust_id}}->{'ids'} = "$count_hash{$clust_hash{$clust_id}}->{'ids'}\t$clust_id";
	}else{
		$count_hash{$clust_hash{$clust_id}}->{'genes'} = $super_hash{$clust_id}->{'gene'};
		$count_hash{$clust_hash{$clust_id}}->{'ids'} = $clust_id;
	}
}

# variables needed for venn diagram printing
my %venn_hash;

open(SUMMFILE, ">summary.txt");

print "\n\n";
foreach my $key (keys %count_hash) {
	my @gene_array = split ("\t", $count_hash{$key}->{'genes'});
	my $gene_array_size = @gene_array;
	#foreach my $gene (@gene_array) {
	#	print "$key\t$gene\n";
	#}
	my @id_array = split("\t", $count_hash{$key}->{'ids'});
	my $id_array_size = @id_array;
	#print "\n\n\n\n";
	#foreach my $id (@id_array) {
	#	print "$key\t$id\n";
	#}
	print "$key\t$id_array_size\t$gene_array_size\n";
	print SUMMFILE "$key\t$id_array_size\t$gene_array_size\n";

	# create the file to output the genes involved in each Venn set
	$key =~ s/\t/__/g; # replace the \t in the filename with two working spaces
	my $fh = "genes_$key";
	open(output_file, ">$fh");
	foreach my $gene (@gene_array) {
		print output_file "$gene\t$gene_hash{$gene}->{'species'}\t$gene_hash{$gene}->{'id'}\n";
	}
	close(output_file);
	
	# try to set the variable for printing to the venn diagram
	my $string = "spec_";
	my $sub_count_string = "";
	for (my $i = 0; $i < $species_array_size; $i++) {
			if ($key =~ $species_array[$i]) {
				$sub_count_string = $sub_count_string . $i+1;
			}
	}
	my $count_string = $string . $sub_count_string . "_count";
	if(!defined($id_array_size)) {$id_array_size = 0;}
	if(!defined($gene_array_size)) {$gene_array_size = 0;}
	my $formatted_id_size = format_number($id_array_size);
	my $formatted_gene_size = format_number($gene_array_size);
	$formatted_gene_size = "($formatted_gene_size)";
	# pad the numbers so that they appear center aligned
	my $pad_size = ((length $formatted_gene_size) - (length $formatted_id_size))/2;
	my $spaces = "";
	for (my $i = 0; $i < $pad_size; $i++) {
			$spaces = $spaces . " ";
	}
	
	$venn_hash{$count_string} = "$spaces$formatted_id_size\n$formatted_gene_size";
}



my $sth_get_species_cluster_count=$dbh->prepare("select count(distinct super_id) from super_clust where species like ?");
my $sth_get_species_gene_count=$dbh->prepare("select count(gene) from super_clust where species like ?");


for (my $i = 0; $i < $species_array_size; $i++) {
		$sth_get_species_cluster_count->execute($species_array[$i]);
		my $species_count = $sth_get_species_cluster_count->fetchrow_array();
		$sth_get_species_gene_count->execute($species_array[$i]);
		my $species_gene_count = $sth_get_species_gene_count->fetchrow_array();
		
		my $safe_species_table = $dbh_seq->quote_identifier("$species_array[$i]");
		my $sth_get_gene_model_count=$dbh_seq->prepare("select canonical_genes from isoform_info where species like ?");
		$sth_get_gene_model_count->execute($species_array[$i]);
		my $species_gene_model_count = $sth_get_gene_model_count->fetchrow_array();
		
		
		print "\nFor species $species_array[$i], there were $species_count unique clusters with $species_gene_count genes from $species_gene_model_count genes in the fasta file\n";
		print SUMMFILE "\nFor species $species_array[$i], there were $species_count unique clusters with $species_gene_count genes from $species_gene_model_count genes in the fasta file\n";
		
		# make the text to put on the venn diagram
		my $j = $i + 1;
		my $det_string_line_1 = "spec_". $j . "_det_line_1";
		my $det_string = "spec_" . $j . "_det";
		if(!defined($species_count)) {$species_count =0};
		if(!defined($species_gene_count)) {$species_gene_count =0};
		
		my $formatted_species = format_number($species_count);
		my $formatted_genes = format_number($species_gene_count);
		my $formatted_gene_model_count = format_number($species_gene_model_count);
		
		my $species;
		# this will change the species name to be genus initial followed by a dot then species
		($species = $species_array[$i]) =~ s/(\w)\w*_(\w*)/$1\. $2/;
		
		if ($i == 1) {
				my $det_text = "$formatted_gene_model_count gene models in fasta\n$formatted_species clusters\n$formatted_genes gene models in clusters";
				# pad the text so that it appears center aligned
				my $pad_size = ((length "$formatted_genes gene models in clusters") - (length $species))/2;
				my $spaces = "";
				for (my $k = 0; $k < $pad_size; $k++) {
						$spaces = $spaces . " ";
				}
				
				$venn_hash{$det_string_line_1} = "$spaces$species";
				$venn_hash{$det_string} = $det_text;
		}else{
				# pad the text so that it appears center aligned
				my $pad_size = ((length $species) - (length $formatted_gene_model_count))/2;
				my $spaces = "";
				for (my $k = 0; $k < $pad_size; $k++) {
						$spaces = $spaces . " ";
				}
				my $det_text = "$spaces$formatted_gene_model_count\n$spaces$formatted_species\n$spaces$formatted_genes";
				
				$venn_hash{$det_string_line_1} = $species;
				$venn_hash{$det_string} = $det_text;
		}
}

my $sth_get_total_cluster_count=$dbh->prepare("select count(distinct super_id) from super_clust");
$sth_get_total_cluster_count->execute();
my $tot_count = $sth_get_total_cluster_count->fetchrow_array();

my $sth_get_total_species_count=$dbh->prepare("select count(distinct species) from super_clust");
$sth_get_total_species_count->execute();
my $tot_species_count = $sth_get_total_species_count->fetchrow_array();
print "\nTotal number of clusters across all species was $tot_count across $tot_species_count species\n";
print SUMMFILE "\nTotal number of clusters across all species was $tot_count across $tot_species_count species\n";
		

my $tot_count_clusters = keys %clust_hash;
print "\nTotal number of clusters among the $species_array_size species = $tot_count_clusters\n";
print SUMMFILE "\nTotal number of clusters among the $species_array_size species = $tot_count_clusters\n";

# make the strings to store this info to put on the figure
my $formatted_tot_count = format_number($tot_count);
my $formatted_tot_count_clusters = format_number($tot_count_clusters);

my $tot_clust_string = "$formatted_tot_count total unique\ngene clusters identified\nin sequenced genomes\nof $tot_species_count species";
my $tot_string = "$formatted_tot_count_clusters clusters had a\nmember in at least one\nof these $species_array_size species";



###############################################################################
#  The below portion will draw the venn diagram                               #
###############################################################################
my $img;

if($species_array_size == 3) {
		
		# Open the venn diagram png file that already has the shapes
		$img = newFromPng GD::Image("$venn_src_dir/3_set_venn_source.png",1);
		
		# make some colors
		my $white = $img->colorAllocateAlpha(255,255,255,127);
		my $black = $img->colorAllocate(0,0,0);
		my $red = $img->colorAllocate(255,0,0);
		my $blue = $img->colorAllocate(0,0,255);
		my $green = $img->colorAllocate(0,212,0);
		
		#$img->transparent($white);
		$img->interlaced('true');
		$img->alphaBlending(1);
		$img->saveAlpha(1);
		
		#define the locations for text to go
		# species details
		my @spec_1_det = (60,375);
		my @spec_2_det = (370,22);
		my @spec_3_det = (800,375);
		
		my $spec_1_det = "$venn_hash{spec_1_det_line_1}\n$venn_hash{spec_1_det}";
		my $spec_2_det = "$venn_hash{spec_2_det_line_1}\n$venn_hash{spec_2_det}";
		my $spec_3_det = "$venn_hash{spec_3_det_line_1}\n$venn_hash{spec_3_det}";
		
		
		$img->stringFT($red,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_1_det[0],$spec_1_det[1],"$spec_1_det");
		$img->stringFT($green,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_2_det[0],$spec_2_det[1],"$spec_2_det");
		$img->stringFT($blue,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_3_det[0],$spec_3_det[1],"$spec_3_det");
		
		my @tot_clust = (100,600);
		my @tot = (600,650);
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$tot_clust[0],$tot_clust[1],"$tot_clust_string");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$tot[0],$tot[1],"$tot_string");
		
		# species individual counts
		my @spec_1_pos = (320,440);
		my @spec_2_pos = (480,200);
		my @spec_3_pos = (650,440);
		
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_1_pos[0],$spec_1_pos[1],"$venn_hash{spec_1_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_2_pos[0],$spec_2_pos[1],"$venn_hash{spec_2_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_3_pos[0],$spec_3_pos[1],"$venn_hash{spec_3_count}");
		
		# 2 species combinations
		my @spec_12_pos = (390,310);
		my @spec_13_pos = (490,465);
		my @spec_23_pos = (580,310);
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_12_pos[0],$spec_12_pos[1],"$venn_hash{spec_12_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_13_pos[0],$spec_13_pos[1],"$venn_hash{spec_13_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_23_pos[0],$spec_23_pos[1],"$venn_hash{spec_23_count}");
		
		# 3 species combination
		my @spec_123_pos = (490,375);
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_123_pos[0],$spec_123_pos[1],"$venn_hash{spec_123_count}");

}

if($species_array_size == 4) {
		
		# Open the venn diagram png file that already has the shapes
		$img = newFromPng GD::Image("$venn_src_dir/4_set_venn_source.png",1);
		
		# make some colors
		my $white = $img->colorAllocateAlpha(255,255,255,127);
		my $black = $img->colorAllocate(0,0,0);
		my $red = $img->colorAllocate(255,0,0);
		my $blue = $img->colorAllocate(0,0,255);
		my $green = $img->colorAllocate(0,212,0);
		my $yellow = $img->colorAllocate(255,255,0);
		my $gray = $img->colorAllocate(128,128,128);
		my $salmon = $img->colorAllocate(255,103,103);
		
		#$img->transparent($white);
		$img->interlaced('true');
		$img->alphaBlending(1);
		$img->saveAlpha(1);
		
		# define the locations for text to go
		# species details
		my @spec_1_det = (90,475);
		my @spec_2_det = (400,125);
		my @spec_3_det = (850,150);
		my @spec_4_det = (1100,375);
		
		my $spec_1_det = "$venn_hash{spec_1_det_line_1}\n$venn_hash{spec_1_det}";
		my $spec_2_det = "$venn_hash{spec_2_det_line_1}\n$venn_hash{spec_2_det}";
		my $spec_3_det = "$venn_hash{spec_3_det_line_1}\n$venn_hash{spec_3_det}";
		my $spec_4_det = "$venn_hash{spec_4_det_line_1}\n$venn_hash{spec_4_det}";
		
		$img->stringFT($gray,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_1_det[0],$spec_1_det[1],"$spec_1_det");
		$img->stringFT($green,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_2_det[0],$spec_2_det[1],"$spec_2_det");
		$img->stringFT($blue,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_3_det[0],$spec_3_det[1],"$spec_3_det");
		$img->stringFT($salmon,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_4_det[0],$spec_4_det[1],"$spec_4_det");
		
		my @tot_clust = (170,800);
		my @tot = (950,850);
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$tot_clust[0],$tot_clust[1],"$tot_clust_string");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$tot[0],$tot[1],"$tot_string");
		 
		# species individual counts
		my @spec_1_pos = (390,540);
		my @spec_2_pos = (590,360);
		my @spec_3_pos = (820,360);
		my @spec_4_pos = (1030,540);
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_1_pos[0],$spec_1_pos[1],"$venn_hash{spec_1_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_2_pos[0],$spec_2_pos[1],"$venn_hash{spec_2_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_3_pos[0],$spec_3_pos[1],"$venn_hash{spec_3_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_4_pos[0],$spec_4_pos[1],"$venn_hash{spec_4_count}");
		 
		# 2 species combinations
		my @spec_12_pos = (490,450);
		my @spec_13_pos = (520,660);
		my @spec_14_pos = (720,850);
		my @spec_23_pos = (710,450);
		my @spec_24_pos = (900,650);
		my @spec_34_pos = (920,450);
		
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_12_pos[0],$spec_12_pos[1],"$venn_hash{spec_12_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_13_pos[0],$spec_13_pos[1],"$venn_hash{spec_13_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_14_pos[0],$spec_14_pos[1],"$venn_hash{spec_14_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_23_pos[0],$spec_23_pos[1],"$venn_hash{spec_23_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_24_pos[0],$spec_24_pos[1],"$venn_hash{spec_24_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_34_pos[0],$spec_34_pos[1],"$venn_hash{spec_34_count}");
		# 
		# 3 species combinations
		my @spec_123_pos = (620,570);
		my @spec_124_pos = (795,750);
		my @spec_134_pos = (815,570);
		my @spec_234_pos = (590,750);
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_123_pos[0],$spec_123_pos[1],"$venn_hash{spec_123_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_124_pos[0],$spec_124_pos[1],"$venn_hash{spec_124_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_134_pos[0],$spec_134_pos[1],"$venn_hash{spec_134_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_234_pos[0],$spec_234_pos[1],"$venn_hash{spec_234_count}");
		
		# 4 species combination
		my @spec_1234_pos = (700,650);
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_1234_pos[0],$spec_1234_pos[1],"$venn_hash{spec_1234_count}");
}


if($species_array_size == 5) {
		
		# Open the venn diagram png file that already has the shapes
		$img = newFromPng GD::Image("$venn_src_dir/5_set_venn_source.png",1);
		
		# make some colors
		my $white = $img->colorAllocateAlpha(255,255,255,127);
		my $black = $img->colorAllocate(0,0,0);
		my $blue = $img->colorAllocate(0,0,205);
		my $green = $img->colorAllocate(0,212,0);
		my $yellow = $img->colorAllocate(192,192,0);
		my $orange = $img->colorAllocate(255,153,51);
		my $maroon = $img->colorAllocate(102,0,102);
		
		
		#$img->transparent($white);
		$img->interlaced('true');
		$img->alphaBlending(1);
		$img->saveAlpha(1);
		
		# define the locations for text to go
		# species details
		my @spec_1_det = (100,280);
		my @spec_2_det = (400,30);
		my @spec_3_det = (800,250);
		my @spec_4_det = (770,575);
		my @spec_5_det = (120,600);
		
		my $spec_1_det = "$venn_hash{spec_1_det_line_1}\n$venn_hash{spec_1_det}";
		my $spec_2_det = "$venn_hash{spec_2_det_line_1}\n$venn_hash{spec_2_det}";
		my $spec_3_det = "$venn_hash{spec_3_det_line_1}\n$venn_hash{spec_3_det}";
		my $spec_4_det = "$venn_hash{spec_4_det_line_1}\n$venn_hash{spec_4_det}";
		my $spec_5_det = "$venn_hash{spec_5_det_line_1}\n$venn_hash{spec_5_det}";
		
		$img->stringFT($blue,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_1_det[0],$spec_1_det[1],"$spec_1_det");
		$img->stringFT($green,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_2_det[0],$spec_2_det[1],"$spec_2_det");
		$img->stringFT($maroon,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_3_det[0],$spec_3_det[1],"$spec_3_det");
		$img->stringFT($yellow,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_4_det[0],$spec_4_det[1],"$spec_4_det");
		$img->stringFT($orange,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$spec_5_det[0],$spec_5_det[1],"$spec_5_det");
		
		my @tot_clust = (50,50);
		my @tot = (430,700);
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$tot_clust[0],$tot_clust[1],"$tot_clust_string");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',22,0,$tot[0],$tot[1],"$tot_string");
		 
		# species individual counts
		my @spec_1_pos = (260,380);
		my @spec_2_pos = (460,170);
		my @spec_3_pos = (660,290);
		my @spec_4_pos = (660,540);
		my @spec_5_pos = (365,600);
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_1_pos[0],$spec_1_pos[1],"$venn_hash{spec_1_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_2_pos[0],$spec_2_pos[1],"$venn_hash{spec_2_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_3_pos[0],$spec_3_pos[1],"$venn_hash{spec_3_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_4_pos[0],$spec_4_pos[1],"$venn_hash{spec_4_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_5_pos[0],$spec_5_pos[1],"$venn_hash{spec_5_count}");
		
		 
		# 2 species combinations
		my @spec_12_pos = (400,285);
		my @spec_13_pos = (635,365);
		my @spec_14_pos = (345,360);
		my @spec_15_pos = (340,460);
		my @spec_23_pos = (580,285);
		my @spec_24_pos = (575,532);
		my @spec_25_pos = (485,260);
		my @spec_34_pos = (640,455);
		my @spec_35_pos = (400,530);
		my @spec_45_pos = (500,560);
		
		 
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_12_pos[0],$spec_12_pos[1],"$venn_hash{spec_12_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_13_pos[0],$spec_13_pos[1],"$venn_hash{spec_13_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_14_pos[0],$spec_14_pos[1],"$venn_hash{spec_14_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_15_pos[0],$spec_15_pos[1],"$venn_hash{spec_15_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_23_pos[0],$spec_23_pos[1],"$venn_hash{spec_23_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_24_pos[0],$spec_24_pos[1],"$venn_hash{spec_24_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_25_pos[0],$spec_25_pos[1],"$venn_hash{spec_25_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_34_pos[0],$spec_34_pos[1],"$venn_hash{spec_34_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_35_pos[0],$spec_35_pos[1],"$venn_hash{spec_35_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_45_pos[0],$spec_45_pos[1],"$venn_hash{spec_45_count}");
		
		# 3 species combinations
		my @spec_123_pos = (575,350);
		my @spec_124_pos = (400,330);
		my @spec_125_pos = (465,320);
		my @spec_134_pos = (630,415);
		my @spec_135_pos = (405,485);
		my @spec_145_pos = (405,405);
		my @spec_234_pos = (580,468);
		my @spec_235_pos = (540,300);
		my @spec_245_pos = (530,525);
		my @spec_345_pos = (470,505);
		 
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_123_pos[0],$spec_123_pos[1],"$venn_hash{spec_123_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_124_pos[0],$spec_124_pos[1],"$venn_hash{spec_124_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_125_pos[0],$spec_125_pos[1],"$venn_hash{spec_125_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_134_pos[0],$spec_134_pos[1],"$venn_hash{spec_134_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_135_pos[0],$spec_135_pos[1],"$venn_hash{spec_135_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_145_pos[0],$spec_145_pos[1],"$venn_hash{spec_145_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_234_pos[0],$spec_234_pos[1],"$venn_hash{spec_234_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_235_pos[0],$spec_235_pos[1],"$venn_hash{spec_235_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_245_pos[0],$spec_245_pos[1],"$venn_hash{spec_245_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_345_pos[0],$spec_345_pos[1],"$venn_hash{spec_345_count}");
		 
		# 4 species combinations
		my @spec_1234_pos = (570,415);
		my @spec_1235_pos = (515,345);
		my @spec_1245_pos = (445,365);
		my @spec_1345_pos = (440,450);
		my @spec_2345_pos = (520,480);
		 
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_1234_pos[0],$spec_1234_pos[1],"$venn_hash{spec_1234_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_1235_pos[0],$spec_1235_pos[1],"$venn_hash{spec_1235_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_1245_pos[0],$spec_1245_pos[1],"$venn_hash{spec_1245_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_1345_pos[0],$spec_1345_pos[1],"$venn_hash{spec_1345_count}");
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_2345_pos[0],$spec_2345_pos[1],"$venn_hash{spec_2345_count}");
		
		# 5 species combination
		my @spec_12345_pos = (495,410);
		
		$img->stringFT($black,'/usr/share/fonts/liberation/LiberationSans-Regular.ttf',14,0,$spec_12345_pos[0],$spec_12345_pos[1],"$venn_hash{spec_12345_count}");
}

# convert the image to PNG and print it

open (TESTOUT, ">venn_diagram.png") || die;
my $png_data = $img->png;
binmode TESTOUT;
print TESTOUT $png_data;
close TESTOUT;
