<commit_before>use strict;
use Template;
use Getopt::Long;

# vars
my $tsv = "data/PCAWG_Data_Freeze_Train_2.0_Pilot-58.tsv";
my $output_dir = "results";
my $defaults = "config/settings.conf";
my $template = "template/workflow_config.ini.tt";

my $srv_hash = {};
my $def = {};
my $tt = Template->new;

GetOptions(
  "tsv=s" => \$tsv,
  "output_dir=s" => \$output_dir,
  "defaults=s" => \$defaults,
  "template=s" => \$template,
) or die ("Getopt error");

# program

# read in the defaults
open DEF, "<$defaults" or die;
while(<DEF>) {
  chomp;
  if(/(\S+)\s*=\s*(\S+)/) {
    my $key = $1;
    my $val = $2;
    $def->{$key} = $value;
  }
}
# now deal with hash values
my @down_srv = split /,/, $def->{'gnosServer'};
my @upload_srv = split /,/, $def->{'uploadServer'};
my @down_pem = split /,/,  $def->{'pemFile'};
my @upload_pem = split /,/,  $def->{'uploadPemFile'};
for (my $i=0; $i<scalar(@down_srv); $i++) {
  $srv_hash->{$down_srv[$i]}{'upload'} = $upload_srv[$i];
  $srv_hash->{$down_srv[$i]}{'down_pem'} = $down_pem[$i];
  $srv_hash->{$down_srv[$i]}{'upload_pem'} = $upload_pem[$i];
}
close DEF;

# scripts to run
my @scripts;

# loop over the contents of the data file
open IN, "<$tsv" or die;
while(<IN>) {
  chomp;
  push @scripts, process($_, $def);
}
close IN;

# write out a script file for triggering these
open OUT, ">$output_dir/run_scripts.sh" or die;
foreach my $script (@scripts) {
  print OUT "$script\n";
}
close OUT;


# subs
sub process {

  my ($line, $def) = @_;

  my @a = split /\t/, $line;
  my $tumourAliquotIds = $a[13];
  my $tumourAnalysisIds = $a[15];
  my $tumourBams = $a[16];
  my $controlAnalysisId = $a[8];
  my $controlBam = $a[9];

  # make a working dir
  system("mkdir -p $output_dir/$tumourAliquotIds");

  my $values = fill_values($def, \@a);

  # make an ini file
  $tt->process($template, $values, "$output_dir/$tumourAliquotIds/config.ini") || die $tt->error;

  # make a submission script
  my $command = "cd $output_dir/$tumourAliquotIds; seqware_1.1.0-alpha.5 bundle launch --dir /glusterfs/netapp/homes1/BOCONNOR/provisioned-bundles/Workflow_Bundle_SangerPancancerCgpCnIndelSnvStr_1.0.0_SeqWare_1.1.0-alpha.5 --ini config.ini";

  # add to master list of scripts
  return($command);
}

sub fill_values {
  my ($def, $a) = @_;

  # read defaults
  my $r = {};
  foreach my $key (keys %{$def}) {
    $r->{$key} = $def->{$key};
  }

  # now process
  my $tumourAliquotIds = $a->[13];
  my $tumourAnalysisIds = $a->[15];
  my $tumourBams = $a->[16];
  my $controlAnalysisId = $a->[8];
  my $controlBam = $a->[9];
  my $norm_gnos_download_srv = $a->[7];
  my $tumour_gnos_download_srv = $a->[14];
  die "$norm_gnos_download_srv ne $tumour_gnos_download_srv\n" if ($norm_gnos_download_srv ne $tumour_gnos_download_srv);

  # correctly deal with download/upload server
  # the pem keys may change depending on where
  my $upload_srv = $srv_hash->{$norm_gnos_download_srv}{'upload'};
  my $upload_pem = $srv_hash->{$norm_gnos_download_srv}{'upload_pem'};
  my $down_pem = $srv_hash->{$norm_gnos_download_srv}{'down_pem'};
  $r->{"pemFile"} = $down_pem;
  $r->{"uploadPemFile"} = $upload_pem;
  $r->{"gnosServer"} = $norm_gnos_download_srv;
  $r->{"uploadServer"} = $upload_srv;

  # now deal with file UUIDs
  $r->{"tumourAliquotIds"} = $tumourAliquotIds;
  $r->{"tumourAnalysisIds"} = $tumourAnalysisIds;
  $r->{"tumourBams"} = $tumourBams;
  $r->{"controlAnalysisId"} = $controlAnalysisId;
  $r->{"controlBam"} = $controlBam;

  return($r);
}


pemFile=/mnt/seqware-build/ebi.pem
gnosServer=https://gtrepo-ebi.annailabs.com
## comment out upload server to block vcfUpload
uploadServer=https://gtrepo-ebi.annailabs.com
uploadPemFile=/mnt/seqware-build/ebi.pem

study-refname-override=icgc_pancancer_vcf_test
#analysis-center-override=
#upload-test=true
#upload-skip=true

assembly=GRCh37
species=human
seqType=WGS
gender=L


# PD4116a 30x vs PD4116b 30x
tumourAliquotIds=f393bb07-270c-2c93-e040-11ac0d484533
tumourAnalysisIds=ef26d046-e88a-4f21-a232-16ccb43637f2
tumourBams=7723a85b59ebce340fe43fc1df504b35.bam
controlAnalysisId=1b9215ab-3634-4108-9db7-7e63139ef7e9
controlBam=8f957ddae66343269cb9b854c02eee2f.bam<commit_msg>Working on script to make ini files<commit_after>use strict;
use Template;
use Getopt::Long;

# vars
my $tsv = "data/PCAWG_Data_Freeze_Train_2.0_Pilot-58.tsv";
my $output_dir = "results";
my $defaults = "config/settings.conf";
my $template = "template/workflow_config.ini.tt";

my $srv_hash = {};
my $def = {};
my $tt = Template->new;

GetOptions(
  "tsv=s" => \$tsv,
  "output_dir=s" => \$output_dir,
  "defaults=s" => \$defaults,
  "template=s" => \$template,
) or die ("Getopt error");

# program

# read in the defaults
open DEF, "<$defaults" or die;
while(<DEF>) {
  chomp;
  if(/(\S+)\s*=\s*(\S+)/) {
    my $key = $1;
    my $val = $2;
    $def->{$key} = $value;
  }
}
# now deal with hash values
my @down_srv = split /,/, $def->{'gnosServer'};
my @upload_srv = split /,/, $def->{'uploadServer'};
my @down_pem = split /,/,  $def->{'pemFile'};
my @upload_pem = split /,/,  $def->{'uploadPemFile'};
for (my $i=0; $i<scalar(@down_srv); $i++) {
  $srv_hash->{$down_srv[$i]}{'upload'} = $upload_srv[$i];
  $srv_hash->{$down_srv[$i]}{'down_pem'} = $down_pem[$i];
  $srv_hash->{$down_srv[$i]}{'upload_pem'} = $upload_pem[$i];
}
close DEF;

# scripts to run
my @scripts;

# loop over the contents of the data file
open IN, "<$tsv" or die;
while(<IN>) {
  chomp;
  push @scripts, process($_, $def);
}
close IN;

# write out a script file for triggering these
open OUT, ">$output_dir/run_scripts.sh" or die;
foreach my $script (@scripts) {
  print OUT "$script\n";
}
close OUT;


# subs
sub process {

  my ($line, $def) = @_;

  my @a = split /\t/, $line;
  my $tumourAliquotIds = $a[13];
  my $tumourAnalysisIds = $a[15];
  my $tumourBams = $a[16];
  my $controlAnalysisId = $a[8];
  my $controlBam = $a[9];

  # make a working dir
  system("mkdir -p $output_dir/$tumourAliquotIds");

  my $values = fill_values($def, \@a);

  # make an ini file
  $tt->process($template, $values, "$output_dir/$tumourAliquotIds/config.ini") || die $tt->error;

  # make a submission script
  my $command = "cd $output_dir/$tumourAliquotIds; seqware_1.1.0-alpha.5 bundle launch --dir /glusterfs/netapp/homes1/BOCONNOR/provisioned-bundles/Workflow_Bundle_SangerPancancerCgpCnIndelSnvStr_1.0.0_SeqWare_1.1.0-alpha.5 --ini config.ini";

  # add to master list of scripts
  return($command);
}

sub fill_values {
  my ($def, $a) = @_;

  # read defaults
  my $r = {};
  foreach my $key (keys %{$def}) {
    $r->{$key} = $def->{$key};
  }

  # now process
  my $tumourAliquotIds = $a->[13];
  my $tumourAnalysisIds = $a->[15];
  my $tumourBams = $a->[16];
  my $controlAnalysisId = $a->[8];
  my $controlBam = $a->[9];
  my $norm_gnos_download_srv = $a->[7];
  my $tumour_gnos_download_srv = $a->[14];
  die "$norm_gnos_download_srv ne $tumour_gnos_download_srv\n" if ($norm_gnos_download_srv ne $tumour_gnos_download_srv);

  # correctly deal with download/upload server
  # the pem keys may change depending on where
  my $upload_srv = $srv_hash->{$norm_gnos_download_srv}{'upload'};
  my $upload_pem = $srv_hash->{$norm_gnos_download_srv}{'upload_pem'};
  my $down_pem = $srv_hash->{$norm_gnos_download_srv}{'down_pem'};
  $r->{"pemFile"} = $down_pem;
  $r->{"uploadPemFile"} = $upload_pem;
  $r->{"gnosServer"} = $norm_gnos_download_srv;
  $r->{"uploadServer"} = $upload_srv;

  # now deal with file UUIDs
  $r->{"tumourAliquotIds"} = $tumourAliquotIds;
  $r->{"tumourAnalysisIds"} = $tumourAnalysisIds;
  $r->{"tumourBams"} = $tumourBams;
  $r->{"controlAnalysisId"} = $controlAnalysisId;
  $r->{"controlBam"} = $controlBam;

  return($r);
}
{'fraction_non_alphanumeric': 0.18832856771510267, 'fraction_numerical': 0.02534442422667013, 'mean_word_length': 4.5359712230215825, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 81, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5411163', 'n_tokens_mistral': 3381, 'n_tokens_neox': 3208, 'n_words': 640}