<commit_before>#!/usr/bin/perl -w

use strict;
use GD;
use Getopt::Long;


use CoGeX;
use CoGe::Accessory::Web;
use CoGe::Accessory::SynMap_report;
use DBI;
use Data::Dumper;
use DBI;
use POSIX;

use vars qw($P $dagfile $alignfile $width $link $min_chr_size $dsgid1 $dsgid2 $help $coge $graphics_context $CHR1 $CHR2 $basename $link_type $flip $grid $ks_db $ks_type $log $MAX $MIN $assemble $axis_metric $color_type $box_diags $fid1 $fid2 $selfself $labels $color_scheme $chr_sort_order $font $GZIP $GUNZIP $URL $conffile $skip_random $force_box $chr_order $dotsize);
#17:1:14:5:7:18:3:4:11:9:13:6:8:12:10:19:2:15:16


GetOptions(
	   "dagfile|d=s"=>\$dagfile, #all dots
	   "alignfile|a=s"=>\$alignfile, #syntenic dots
	   "width|w=i"=>\$width,
	   "link|l=s"=>\$link,
	   "link_type|lt=i"=>\$link_type,
	   "min_chr_size|mcs=i"=>\$min_chr_size,
	   "dsgid1|dsg1=i"=>\$dsgid1,
	   "dsgid2|dsg2=i"=>\$dsgid2,
	   "help|h" =>\$help,
	   "chr1|c1=s"=>\$CHR1,
	   "chr2|c2=s"=>\$CHR2,
	   "basename|b=s"=>\$basename,
	   "flip|f=i"=>\$flip,
	   "grid|g=i"=>\$grid,
	   "ksdb|ks_db=s"=>\$ks_db,
	   "ks_type|kst=s"=>\$ks_type, 
	   "color_diags_type|cdt=s"=>\$color_type, #how to color diagonals
	                                         #NOTE this is trumped by ksdb and the diag dots will be colored according to ks_type
	   "max=s"=>\$MAX,
	   "min=s"=>\$MIN,
	   "log=s"=>\$log,
	   "assemble=s"=>\$assemble, #syntenic path assembly option
	   "axis_metrix|am=s"=>\$axis_metric,
	   "box_diags|bd=i"=>\$box_diags,
	   "fid1|f1=i"=>\$fid1,
	   "fid2|f2=i"=>\$fid2,
	   "selfself" => \$selfself, #draw diag for self self comparison
	   "labels=s" => \$labels, #do we draw labels for chromosomes, on my default
	   "color_scheme=s"=>\$color_scheme,
	   "font=s"=>\$font,
	   "chr_sort_order|cso=s"=>\$chr_sort_order,#display sort order for chromosomes, "Name|N" || "Size|S";
	   "config_file|cf=s"=>\$conffile,
	   "skip_random|sr=i"=>\$skip_random, #flag or skipping chromosome containing the wod 'random' in their name
	   "force_box|fb" => \$force_box, #flag to make dotplot dimensions a box instead of relative on genomic content
	   "chr_order|co=s" => \$chr_order, #string of ":" delimted chromosome name order to display for the genome with fewer chromosomes
	   "dotsize|ds=i"=>\$dotsize, #size of dots in dotplot
	   );
$selfself = 1 unless defined $selfself;
$labels = 1 unless defined $labels;
$chr_sort_order = "size" unless defined $chr_sort_order;

$P = CoGe::Accessory::Web::get_defaults($conffile);
$font = $P->{FONT} unless $font && -r $font;
$GZIP = $P->{GZIP};
$GUNZIP = $P->{GUNZIP};
$URL = $P->{URL};

usage() if $help;
usage() unless (defined $dagfile &&-r $dagfile) || -r $alignfile || -r "$alignfile.gz";

if (defined $dagfile and ! (-r $dagfile || -r $dagfile.".gz"))
  {
    warn "dagfile specified but not present or readable: $!";
  }

$dagfile = CoGe::Accessory::Web::gunzip($dagfile, $conffile) if $dagfile;# && $dagfile =~ /\.gz$/;
$alignfile = CoGe::Accessory::Web::gunzip($alignfile, $conffile) if $alignfile;# && $alignfile =~ /\.gz$/;

if ($alignfile && -r $alignfile && $alignfile =~ /\.gz$/)
  {
    print "Problem decompressing $alignfile.\n";
    exit;
  }

if ($dagfile && -r $dagfile && $dagfile =~ /\.gz$/)
  {
    print "Problem decompressing $dagfile.\n";
    exit;
  }


#set a default for this, make sure it is uppercase
$ks_type = "kS" unless $ks_type;
$ks_type = uc($ks_type) if $ks_type;

my $ks_hist = $P->{KS_HISTOGRAM};

$basename = "test" unless $basename;
$width = 1024 unless $width;
my $DBNAME = $P->{DBNAME};
my $DBHOST = $P->{DBHOST};
my $DBPORT = $P->{DBPORT};
my $DBUSER = $P->{DBUSER};
my $DBPASS = $P->{DBPASS};
my $connstr = "dbi:mysql:dbname=".$DBNAME.";host=".$DBHOST.";port=".$DBPORT;
$coge = CoGeX->dbconnect(db_connection_string=>$connstr, db_name=>$DBUSER, db_passwd=>$DBPASS );
my $synmap_report = new CoGe::Accessory::SynMap_report;

my ($dsg1) = $coge->resultset('Genome')->find($dsgid1);
my ($dsg2) = $coge->resultset('Genome')->find($dsgid2);
unless ($dsg1)
  {
    warn "No genome found with dbid $dsgid1\n";
    return;
  }
unless ($dsg2)
  {
    warn "No genome found with dbid $dsgid2\n";
    return;
  }

#get display order of chromosomes, get genome information
my ($org1_order, $org1info) = get_dsg_order(dsg=>$dsg1, chr=>$CHR1, minsize=>$min_chr_size, chr_sort_order=>$chr_sort_order, skip_random=>$skip_random);
my ($org2_order, $org2info) = get_dsg_order(dsg=>$dsg2, chr=>$CHR2, minsize=>$min_chr_size, chr_sort_order=>$chr_sort_order, skip_random=>$skip_random);

add_user_order (order1=>$org1_order, order2=>$org2_order, user_order=>$chr_order) if $chr_order;

if ($axis_metric && $axis_metric =~ /gene/) #add in gene information
  {
    get_gene_info(dsgid=>$dsgid1, info=>$org1info);
    get_gene_info(dsgid=>$dsgid2, info=>$org2info);
  }
print STDERR Dumper $org1info,"\n";
print STDERR Dumper $org2info,"\n";
#print STDERR Dumper $org1info, $org2info;

#will need to reorder whichever genome has more chromosomes/contigs

my $spa_info_file = $basename.".spa_info.txt";
if ($assemble) 
  {
    my $skip_non_aligned_contigs = $assemble && $assemble =~ /2/ ? 1 : 0;
    my ($org1_association, $org2_association) = $synmap_report->parse_syn_blocks(file=>$alignfile) if $assemble;
    #print STDERR Dumper $synmap_report;
    my $output;
    if (@$org1_association > @$org2_association && $assemble > 0 || @$org1_association < @$org2_association && $assemble < 0 ) 
      {
	$output = reord(reorder=>$org1_order, order=>$org2_order, assoc=>$org2_association, skip=>$skip_non_aligned_contigs, info=>$org1_association, skip_random=>$skip_random);
      }
    else 
      {
	$output = reord (reorder=>$org2_order, order=>$org1_order, assoc=>$org1_association, skip=>$skip_non_aligned_contigs, info=>$org2_association, skip_random=>$skip_random);
      }

    open OUT, ">$spa_info_file";
    print OUT $output;
    close OUT;
    add_rev_info(info=>$org1info, assoc=>$org1_association);
    add_rev_info(info=>$org2info, assoc=>$org2_association);
  }<commit_msg>removed debugging messages<commit_after>#!/usr/bin/perl -w

use strict;
use GD;
use Getopt::Long;


use CoGeX;
use CoGe::Accessory::Web;
use CoGe::Accessory::SynMap_report;
use DBI;
use Data::Dumper;
use DBI;
use POSIX;

use vars qw($P $dagfile $alignfile $width $link $min_chr_size $dsgid1 $dsgid2 $help $coge $graphics_context $CHR1 $CHR2 $basename $link_type $flip $grid $ks_db $ks_type $log $MAX $MIN $assemble $axis_metric $color_type $box_diags $fid1 $fid2 $selfself $labels $color_scheme $chr_sort_order $font $GZIP $GUNZIP $URL $conffile $skip_random $force_box $chr_order $dotsize);
#17:1:14:5:7:18:3:4:11:9:13:6:8:12:10:19:2:15:16


GetOptions(
	   "dagfile|d=s"=>\$dagfile, #all dots
	   "alignfile|a=s"=>\$alignfile, #syntenic dots
	   "width|w=i"=>\$width,
	   "link|l=s"=>\$link,
	   "link_type|lt=i"=>\$link_type,
	   "min_chr_size|mcs=i"=>\$min_chr_size,
	   "dsgid1|dsg1=i"=>\$dsgid1,
	   "dsgid2|dsg2=i"=>\$dsgid2,
	   "help|h" =>\$help,
	   "chr1|c1=s"=>\$CHR1,
	   "chr2|c2=s"=>\$CHR2,
	   "basename|b=s"=>\$basename,
	   "flip|f=i"=>\$flip,
	   "grid|g=i"=>\$grid,
	   "ksdb|ks_db=s"=>\$ks_db,
	   "ks_type|kst=s"=>\$ks_type, 
	   "color_diags_type|cdt=s"=>\$color_type, #how to color diagonals
	                                         #NOTE this is trumped by ksdb and the diag dots will be colored according to ks_type
	   "max=s"=>\$MAX,
	   "min=s"=>\$MIN,
	   "log=s"=>\$log,
	   "assemble=s"=>\$assemble, #syntenic path assembly option
	   "axis_metrix|am=s"=>\$axis_metric,
	   "box_diags|bd=i"=>\$box_diags,
	   "fid1|f1=i"=>\$fid1,
	   "fid2|f2=i"=>\$fid2,
	   "selfself" => \$selfself, #draw diag for self self comparison
	   "labels=s" => \$labels, #do we draw labels for chromosomes, on my default
	   "color_scheme=s"=>\$color_scheme,
	   "font=s"=>\$font,
	   "chr_sort_order|cso=s"=>\$chr_sort_order,#display sort order for chromosomes, "Name|N" || "Size|S";
	   "config_file|cf=s"=>\$conffile,
	   "skip_random|sr=i"=>\$skip_random, #flag or skipping chromosome containing the wod 'random' in their name
	   "force_box|fb" => \$force_box, #flag to make dotplot dimensions a box instead of relative on genomic content
	   "chr_order|co=s" => \$chr_order, #string of ":" delimted chromosome name order to display for the genome with fewer chromosomes
	   "dotsize|ds=i"=>\$dotsize, #size of dots in dotplot
	   );
$selfself = 1 unless defined $selfself;
$labels = 1 unless defined $labels;
$chr_sort_order = "size" unless defined $chr_sort_order;

$P = CoGe::Accessory::Web::get_defaults($conffile);
$font = $P->{FONT} unless $font && -r $font;
$GZIP = $P->{GZIP};
$GUNZIP = $P->{GUNZIP};
$URL = $P->{URL};

usage() if $help;
usage() unless (defined $dagfile &&-r $dagfile) || -r $alignfile || -r "$alignfile.gz";

if (defined $dagfile and ! (-r $dagfile || -r $dagfile.".gz"))
  {
    warn "dagfile specified but not present or readable: $!";
  }

$dagfile = CoGe::Accessory::Web::gunzip($dagfile, $conffile) if $dagfile;# && $dagfile =~ /\.gz$/;
$alignfile = CoGe::Accessory::Web::gunzip($alignfile, $conffile) if $alignfile;# && $alignfile =~ /\.gz$/;

if ($alignfile && -r $alignfile && $alignfile =~ /\.gz$/)
  {
    print "Problem decompressing $alignfile.\n";
    exit;
  }

if ($dagfile && -r $dagfile && $dagfile =~ /\.gz$/)
  {
    print "Problem decompressing $dagfile.\n";
    exit;
  }


#set a default for this, make sure it is uppercase
$ks_type = "kS" unless $ks_type;
$ks_type = uc($ks_type) if $ks_type;

my $ks_hist = $P->{KS_HISTOGRAM};

$basename = "test" unless $basename;
$width = 1024 unless $width;
my $DBNAME = $P->{DBNAME};
my $DBHOST = $P->{DBHOST};
my $DBPORT = $P->{DBPORT};
my $DBUSER = $P->{DBUSER};
my $DBPASS = $P->{DBPASS};
my $connstr = "dbi:mysql:dbname=".$DBNAME.";host=".$DBHOST.";port=".$DBPORT;
$coge = CoGeX->dbconnect(db_connection_string=>$connstr, db_name=>$DBUSER, db_passwd=>$<PASSWORD> );
my $synmap_report = new CoGe::Accessory::SynMap_report;

my ($dsg1) = $coge->resultset('Genome')->find($dsgid1);
my ($dsg2) = $coge->resultset('Genome')->find($dsgid2);
unless ($dsg1)
  {
    warn "No genome found with dbid $dsgid1\n";
    return;
  }
unless ($dsg2)
  {
    warn "No genome found with dbid $dsgid2\n";
    return;
  }

#get display order of chromosomes, get genome information
my ($org1_order, $org1info) = get_dsg_order(dsg=>$dsg1, chr=>$CHR1, minsize=>$min_chr_size, chr_sort_order=>$chr_sort_order, skip_random=>$skip_random);
my ($org2_order, $org2info) = get_dsg_order(dsg=>$dsg2, chr=>$CHR2, minsize=>$min_chr_size, chr_sort_order=>$chr_sort_order, skip_random=>$skip_random);

add_user_order (order1=>$org1_order, order2=>$org2_order, user_order=>$chr_order) if $chr_order;

if ($axis_metric && $axis_metric =~ /gene/) #add in gene information
  {
    get_gene_info(dsgid=>$dsgid1, info=>$org1info);
    get_gene_info(dsgid=>$dsgid2, info=>$org2info);
  }
#print STDERR Dumper $org1info,"\n";
#print STDERR Dumper $org2info,"\n";
#print STDERR Dumper $org1info, $org2info;

#will need to reorder whichever genome has more chromosomes/contigs

my $spa_info_file = $basename.".spa_info.txt";
if ($assemble) 
  {
    my $skip_non_aligned_contigs = $assemble && $assemble =~ /2/ ? 1 : 0;
    my ($org1_association, $org2_association) = $synmap_report->parse_syn_blocks(file=>$alignfile) if $assemble;
    #print STDERR Dumper $synmap_report;
    my $output;
    if (@$org1_association > @$org2_association && $assemble > 0 || @$org1_association < @$org2_association && $assemble < 0 ) 
      {
	$output = reord(reorder=>$org1_order, order=>$org2_order, assoc=>$org2_association, skip=>$skip_non_aligned_contigs, info=>$org1_association, skip_random=>$skip_random);
      }
    else 
      {
	$output = reord (reorder=>$org2_order, order=>$org1_order, assoc=>$org1_association, skip=>$skip_non_aligned_contigs, info=>$org2_association, skip_random=>$skip_random);
      }

    open OUT, ">$spa_info_file";
    print OUT $output;
    close OUT;
    add_rev_info(info=>$org1info, assoc=>$org1_association);
    add_rev_info(info=>$org2info, assoc=>$org2_association);
  }
{'fraction_non_alphanumeric': 0.1835908452449806, 'fraction_numerical': 0.018177311410394116, 'mean_word_length': 4.788617886178862, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 186, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4582203', 'n_tokens_mistral': 5216, 'n_tokens_neox': 4933, 'n_words': 1000}