<commit_before>#!/usr/bin/env perl

##Assumes you have a working conda installation

use strict;
use warnings;

use Getopt::Long;
use IO::File;
use Cwd;
use FindBin qw($Bin); #Find directory of script
use vars qw($USAGE);

BEGIN {
    $USAGE =
	qq{install.pl [options]
           -env/--condaEnvironment Conda environment (Default: "mip")
           -bvc/--bioConda Set the module version of the programs that can be installed with bioConda (i.e. 'bwa=0.7.12')
           -pm/perlModules Set the perl modules to be installed via cpanm (comma sep)
           -pip/--pip Set the module version of the programs that can be installed with pip (i.e. 'genmod=3.3.3')
           -sbb/sambamba Set the sambamba version (Default: "0.5.9")
           -vct/--vcfTools Set the vcftools version (Default: "0.1.14")
           -vt/--vt Set the vt version (Default: "0.57")
           -plk/--plink  Set the plink version (Default: "1.07")
           -vep/--VariantEffectPredictor Set the VEP version (Default: "82")
	   -vepc/--vepDirectoryCache Specify the cache directory to use (whole path; defaults to "~/miniconda/envs/condaEnvironment/ensembl-tools-release-VariantEffectPredictorVersion/cache")
           -ppd/--printParameterDefaults Print the parameter defaults
           -h/--help Display this help message   
           -v/--version Display version
        };    
}
my ($installDirectory) = (".MIP");

my %parameter; 

### Set parameter default

##Conda
$parameter{'condaEnvironment'} = "mip";
$parameter{'bioConda'}{'bwa'} = "0.7.12";
$parameter{'bioConda'}{'fastqc'} = "0.11.2";
$parameter{'bioConda'}{'samtools'} = "1.2";
$parameter{'bioConda'}{'bcftools'} = "1.2";
$parameter{'bioConda'}{'snpeff'} = "4.1";
$parameter{'bioConda'}{'picard'} = "1.139";
$parameter{'bioConda'}{'bedtools'} = "2.25";
$parameter{'bioConda'}{'mosaik'} = "2.2.26";

##Perl Modules
$parameter{'perlModules'} = ["YAML",
			     "Log::Log4perl",
			     "List::MoreUtils",
			     "DateTime",
			     "DateTime::Format::ISO8601",
			     "DateTime::Format::HTTP",
			     "DateTime::Format::Mail",
			     "Set::IntervalTree",  # vcfParser
			     "LWP::Simple",  # VEP
			     "LWP::Protocol::https",  # VEP
			     "Archive::Zip",  # VEP
			     "DBI",  # VEP
			     "JSON",  # VEP
			     "DBD::mysql",  # VEP
    ];

## PIP
$parameter{'pip'}{'genmod'} = "3.3.3";
$parameter{'pip'}{'chanjo'} = "3.0.1";
$parameter{'pip'}{'cosmid'} = "0.4.9.1";
$parameter{'pip'}{'python-Levenshtein'} = "0.12.0";

## Programs currently not supported by conda or other packet manager
$parameter{'sambamba'} = "0.5.9";
$parameter{'vcfTools'} = "0.1.14";
$parameter{'vt'} = "0.57";
$parameter{'plink'} = "1.07";
$parameter{'VariantEffectPredictor'} = "82";
$parameter{'vepDirectoryCache'} = q?~/miniconda/envs/?.$parameter{'condaEnvironment'}.q?/ensembl-tools-release-?.$parameter{'VariantEffectPredictor'}.q?/cache?;  #Cache directory;

my $installVersion = "0.0.1";

###User Options
GetOptions('env|condaEnvironment:s'  => \$parameter{'condaEnvironment'},
	   'bcv|bioConda=s'  => \%{$parameter{'bioConda'}},
	   'pm|perlModules:s'  => \@{$parameter{'perlModules'}},  #Comma separated list
	   'pip|pip=s'  => \%{$parameter{'pip'}},
	   'sbb|sambamba:s'  => \$parameter{'sambamba'},
	   'vct|vcfTools:s'  => \$parameter{'vcfTools'},
	   'vt|vt:s'  => \$parameter{'vt'},
	   'plk|plink:s'  => \$parameter{'plink'},
	   'vep|VariantEffectPredictor:s'  => \$parameter{'VariantEffectPredictor'},
	   'vepc|vepDirectoryCache:s'  => \$parameter{'vepDirectoryCache'},  #path to vep cache dir
	   'ppd|printParameterDefaults'  => sub { &PrintParameters(\%parameter); exit;},  #Display parameter defaults
	   'h|help' => sub { print STDOUT $USAGE, "\n"; exit;},  #Display help text
	   'v|version' => sub { print STDOUT "\ninstall.pl ".$installVersion, "\n\n"; exit;},  #Display version number
    );

###MAIN###


#my $LOGFILEHANDLE = &OpenLogFile("MIP_installation.log");

my $BASHFILEHANDLE = &CreateBashFile("mip.sh");

&CreateCondaEnvironment(\%parameter, $BASHFILEHANDLE);

&InstallCpanmAndModules(\%parameter, $BASHFILEHANDLE);

&PipInstall(\%parameter, $BASHFILEHANDLE);

&Sambamba(\%parameter, $BASHFILEHANDLE);

&VcfTools(\%parameter, $BASHFILEHANDLE);

&VT(\%parameter, $BASHFILEHANDLE);

&Plink(\%parameter, $BASHFILEHANDLE);

&VariantEffectPredictor(\%parameter, $BASHFILEHANDLE);

close($BASHFILEHANDLE);
#close($LOGFILEHANDLE);

###SubRoutines###

sub CreateBashFile {

    my $fileName = $_[0];

    my $FILEHANDLE = IO::Handle->new();  #Create anonymous filehandle
    
    ## Open batch file
    open ($FILEHANDLE, ">",$fileName) or die("Can't write to '".$fileName."' :".$!."\n");

    print $FILEHANDLE "#!/usr/bin/env bash", "\n\n";
    return $FILEHANDLE;
}

sub OpenLogFile {

    my $fileName = $_[0];

    my $FILEHANDLE = IO::Handle->new();  #Create anonymous filehandle
    
    ## Open batch file
    open ($FILEHANDLE, ">",$fileName) or die("Can't write to '".$fileName."' :".$!."\n");

    return $FILEHANDLE;
}

sub PrintParameters {
    
    my $parameterHashRef = $_[0];
    
    foreach my $key (keys %{$parameterHashRef}) {
	
	if (ref(${$parameterHashRef}{$key})!~/ARRAY|HASH/) {

	    print STDOUT $key." ".${$parameterHashRef}{$key}, "\n";
	}
	elsif (ref(${$parameterHashRef}{$key})=~/HASH/) {
	    
	    foreach my $program (keys %{${$parameterHashRef}{$key}}) {
		
		print STDOUT $key." ".$program.": ".${$parameterHashRef}{$key}{$program}, "\n";
	    }
	}
	elsif (ref(${$parameterHashRef}{$key})=~/ARRAY/)  {

	    print STDOUT $key.": ".join(" ", @{${$parameterHashRef}{$key}}), "\n";
	}
    }
}


sub CreateCondaEnvironment {

    my $parameterHashRef = $_[0];
    my $FILEHANDLE = $_[1];

    ## Create MIP environment
    print $FILEHANDLE "### Creating Conda Environment: ".${$parameterHashRef}{'condaEnvironment'}."\n";<commit_msg>Added conda update prior to creating env<commit_after>#!/usr/bin/env perl

##Assumes you have a working conda installation

use strict;
use warnings;

use Getopt::Long;
use IO::File;
use Cwd;
use FindBin qw($Bin); #Find directory of script
use vars qw($USAGE);

BEGIN {
    $USAGE =
	qq{install.pl [options]
           -env/--condaEnvironment Conda environment (Default: "mip")
           -bvc/--bioConda Set the module version of the programs that can be installed with bioConda (i.e. 'bwa=0.7.12')
           -pm/perlModules Set the perl modules to be installed via cpanm (comma sep)
           -pip/--pip Set the module version of the programs that can be installed with pip (i.e. 'genmod=3.3.3')
           -sbb/sambamba Set the sambamba version (Default: "0.5.9")
           -vct/--vcfTools Set the vcftools version (Default: "0.1.14")
           -vt/--vt Set the vt version (Default: "0.57")
           -plk/--plink  Set the plink version (Default: "1.07")
           -vep/--VariantEffectPredictor Set the VEP version (Default: "82")
	   -vepc/--vepDirectoryCache Specify the cache directory to use (whole path; defaults to "~/miniconda/envs/condaEnvironment/ensembl-tools-release-VariantEffectPredictorVersion/cache")
           -ppd/--printParameterDefaults Print the parameter defaults
           -h/--help Display this help message   
           -v/--version Display version
        };    
}
my ($installDirectory) = (".MIP");

my %parameter; 

### Set parameter default

##Conda
$parameter{'condaEnvironment'} = "mip";
$parameter{'bioConda'}{'bwa'} = "0.7.12";
$parameter{'bioConda'}{'fastqc'} = "0.11.2";
$parameter{'bioConda'}{'samtools'} = "1.2";
$parameter{'bioConda'}{'bcftools'} = "1.2";
$parameter{'bioConda'}{'snpeff'} = "4.1";
$parameter{'bioConda'}{'picard'} = "1.139";
$parameter{'bioConda'}{'bedtools'} = "2.25";
$parameter{'bioConda'}{'mosaik'} = "2.2.26";

##Perl Modules
$parameter{'perlModules'} = ["YAML",
			     "Log::Log4perl",
			     "List::MoreUtils",
			     "DateTime",
			     "DateTime::Format::ISO8601",
			     "DateTime::Format::HTTP",
			     "DateTime::Format::Mail",
			     "Set::IntervalTree",  # vcfParser
			     "LWP::Simple",  # VEP
			     "LWP::Protocol::https",  # VEP
			     "Archive::Zip",  # VEP
			     "DBI",  # VEP
			     "JSON",  # VEP
			     "DBD::mysql",  # VEP
    ];

## PIP
$parameter{'pip'}{'genmod'} = "3.3.3";
$parameter{'pip'}{'chanjo'} = "3.0.1";
$parameter{'pip'}{'cosmid'} = "0.4.9.1";
$parameter{'pip'}{'python-Levenshtein'} = "0.12.0";

## Programs currently not supported by conda or other packet manager
$parameter{'sambamba'} = "0.5.9";
$parameter{'vcfTools'} = "0.1.14";
$parameter{'vt'} = "0.57";
$parameter{'plink'} = "1.07";
$parameter{'VariantEffectPredictor'} = "82";
$parameter{'vepDirectoryCache'} = q?~/miniconda/envs/?.$parameter{'condaEnvironment'}.q?/ensembl-tools-release-?.$parameter{'VariantEffectPredictor'}.q?/cache?;  #Cache directory;

my $installVersion = "0.0.1";

###User Options
GetOptions('env|condaEnvironment:s'  => \$parameter{'condaEnvironment'},
	   'bcv|bioConda=s'  => \%{$parameter{'bioConda'}},
	   'pm|perlModules:s'  => \@{$parameter{'perlModules'}},  #Comma separated list
	   'pip|pip=s'  => \%{$parameter{'pip'}},
	   'sbb|sambamba:s'  => \$parameter{'sambamba'},
	   'vct|vcfTools:s'  => \$parameter{'vcfTools'},
	   'vt|vt:s'  => \$parameter{'vt'},
	   'plk|plink:s'  => \$parameter{'plink'},
	   'vep|VariantEffectPredictor:s'  => \$parameter{'VariantEffectPredictor'},
	   'vepc|vepDirectoryCache:s'  => \$parameter{'vepDirectoryCache'},  #path to vep cache dir
	   'ppd|printParameterDefaults'  => sub { &PrintParameters(\%parameter); exit;},  #Display parameter defaults
	   'h|help' => sub { print STDOUT $USAGE, "\n"; exit;},  #Display help text
	   'v|version' => sub { print STDOUT "\ninstall.pl ".$installVersion, "\n\n"; exit;},  #Display version number
    );

###MAIN###


#my $LOGFILEHANDLE = &OpenLogFile("MIP_installation.log");

my $BASHFILEHANDLE = &CreateBashFile("mip.sh");

&CreateCondaEnvironment(\%parameter, $BASHFILEHANDLE);

&InstallCpanmAndModules(\%parameter, $BASHFILEHANDLE);

&PipInstall(\%parameter, $BASHFILEHANDLE);

&Sambamba(\%parameter, $BASHFILEHANDLE);

&VcfTools(\%parameter, $BASHFILEHANDLE);

&VT(\%parameter, $BASHFILEHANDLE);

&Plink(\%parameter, $BASHFILEHANDLE);

&VariantEffectPredictor(\%parameter, $BASHFILEHANDLE);

close($BASHFILEHANDLE);
#close($LOGFILEHANDLE);

###SubRoutines###

sub CreateBashFile {

    my $fileName = $_[0];

    my $FILEHANDLE = IO::Handle->new();  #Create anonymous filehandle
    
    ## Open batch file
    open ($FILEHANDLE, ">",$fileName) or die("Can't write to '".$fileName."' :".$!."\n");

    print $FILEHANDLE "#!/usr/bin/env bash", "\n\n";
    return $FILEHANDLE;
}

sub OpenLogFile {

    my $fileName = $_[0];

    my $FILEHANDLE = IO::Handle->new();  #Create anonymous filehandle
    
    ## Open batch file
    open ($FILEHANDLE, ">",$fileName) or die("Can't write to '".$fileName."' :".$!."\n");

    return $FILEHANDLE;
}

sub PrintParameters {
    
    my $parameterHashRef = $_[0];
    
    foreach my $key (keys %{$parameterHashRef}) {
	
	if (ref(${$parameterHashRef}{$key})!~/ARRAY|HASH/) {

	    print STDOUT $key." ".${$parameterHashRef}{$key}, "\n";
	}
	elsif (ref(${$parameterHashRef}{$key})=~/HASH/) {
	    
	    foreach my $program (keys %{${$parameterHashRef}{$key}}) {
		
		print STDOUT $key." ".$program.": ".${$parameterHashRef}{$key}{$program}, "\n";
	    }
	}
	elsif (ref(${$parameterHashRef}{$key})=~/ARRAY/)  {

	    print STDOUT $key.": ".join(" ", @{${$parameterHashRef}{$key}}), "\n";
	}
    }
}


sub CreateCondaEnvironment {

    my $parameterHashRef = $_[0];
    my $FILEHANDLE = $_[1];

    ## Update Conda
    print $FILEHANDLE "### Update Conda\n";
    print $FILEHANDLE "conda update -y conda ";
    print $FILEHANDLE "\n\n";

    ## Create MIP environment
    print $FILEHANDLE "### Creating Conda Environment: ".${$parameterHashRef}{'condaEnvironment'}."\n";
{'fraction_non_alphanumeric': 0.19778454253340097, 'fraction_numerical': 0.015051581261626923, 'mean_word_length': 3.819478402607987, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '163388', 'n_tokens_mistral': 4419, 'n_tokens_neox': 4137, 'n_words': 866}