<commit_before>#!/usr/bin/env perl
# Assemble genomes for SARS-CoV-2 amplicons

use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;
use File::Basename qw/fileparse basename dirname/;
use File::Temp qw/tempdir tempfile/;
use File::Copy qw/mv cp/;
use File::Spec;
use Bio::SeqIO;
use Bio::FeatureIO::gff;
use List::Util qw/min max/;

use threads;
use Thread::Queue;

use FindBin qw/$RealBin/;
use lib "$FindBin::RealBin/../lib/perl5";
use SneakerNet qw/readTsv exitOnSomeSneakernetOptions recordProperties readConfig samplesheetInfo_tsv command logmsg fullPathToExec/;
use GD; # for fonts
use GD::Graph::lines;

our $VERSION = "1.4";
our $CITATION= "SARS-CoV-2 assembly plugin by <NAME>.";

# A message to show in the report if any
my $warningMsg="";

local $0=fileparse $0;
exit(main());

sub main{
  my $settings=readConfig();
  GetOptions($settings,qw(version citation check-dependencies help tempdir=s debug numcpus=i force)) or die $!;
  exitOnSomeSneakernetOptions({
      _CITATION => $CITATION,
      _VERSION  => $VERSION,
      #'run_prediction_metrics.pl (CG-Pipeline)'     => "echo CG Pipeline version unknown",
      'run_assembly_metrics.pl (CG-Pipeline)'       => "echo CG Pipeline version unknown",
      cat                             => 'cat --version | head -n 1',
      # wget needed in SneakerNet.pm
      wget                            => 'wget --version | head -n 1',
      samtools                        => 'samtools 2>&1 | grep Version:',
      bcftools                        => 'bcftools 2>&1 | grep Version:',
      'trimmomatic'                   => 'trimmomatic -version 2>&1 | grep -v _JAVA',
      seqtk                           => 'seqtk 2>&1 | grep -m 1 Version:',
      bgzip                           => 'bgzip --version | head -n1',
      tabix                           => 'tabix --version | head -n1',
      bowtie2                         => 'bowtie2 --version | grep -m 1 version',
      'bowtie2-build'                   => 'bowtie2-build --version | grep -m 1 version',
    }, $settings,
  );

  die usage() if($$settings{help} || !@ARGV);
  $$settings{numcpus}||=1;
  $$settings{tempdir}||=File::Temp::tempdir(basename($0).".XXXXXX",TMPDIR=>1,CLEANUP=>1);
  logmsg "Temporary directory is at $$settings{tempdir}";

  my $dir=$ARGV[0];
  mkdir "$dir/SneakerNet/forEmail";

  if($$settings{force}){
    logmsg "--force was given; removing any existing assembly results.";
    system("rm -rf $dir/SneakerNet/assemblies $dir/forEmail/assemblyMetrics.tsv");
  }
 
  mkdir "$dir/SneakerNet";
  mkdir "$dir/SneakerNet/assemblies";

  my $metricsOut=assembleAll($dir,$settings);
  logmsg "Metrics can be found in $metricsOut";

  my $imagePath = makeCoverageGraph($dir, $settings);

  recordProperties($dir,{
    version  => $VERSION,
    table    => $metricsOut,
    warnings => $warningMsg,
  });

  return 0;
}

sub assembleAll{
  my($dir,$settings)=@_;
  
  # Find information about each genome
  my $sampleInfo=samplesheetInfo_tsv("$dir/samples.tsv",$settings);
  my %sampleMetrics = ();
  my $cdsOverabundance = 0; # if any sample has > 100% CDS
  while(my($sample,$info)=each(%$sampleInfo)){
    next if(ref($info) ne "HASH");
    logmsg "ASSEMBLE SAMPLE $sample";

    my $outdir="$dir/SneakerNet/assemblies/$sample";
    my $outassembly=File::Spec->rel2abs("$outdir/$sample.bowtie2.bcftools.fasta");
    my $outgbk="$outdir/$sample.bowtie2.bcftools.gbk";

    # Run the assembly
    if(!-e $outassembly){
      my $assemblyDir = assembleSample($sample,$info,$settings);
      my $assembly    = "$assemblyDir/consensus.fasta";
      next if(! -d $assemblyDir);
      next if(! -e $assembly);
      next if(! -s $assembly);

      # Save the assembly
      mkdir $outdir;<commit_msg>added image path to properties.txt<commit_after>#!/usr/bin/env perl
# Assemble genomes for SARS-CoV-2 amplicons

use strict;
use warnings;
use Getopt::Long;
use Data::Dumper;
use File::Basename qw/fileparse basename dirname/;
use File::Temp qw/tempdir tempfile/;
use File::Copy qw/mv cp/;
use File::Spec;
use Bio::SeqIO;
use Bio::FeatureIO::gff;
use List::Util qw/min max/;

use threads;
use Thread::Queue;

use FindBin qw/$RealBin/;
use lib "$FindBin::RealBin/../lib/perl5";
use SneakerNet qw/readTsv exitOnSomeSneakernetOptions recordProperties readConfig samplesheetInfo_tsv command logmsg fullPathToExec/;
use GD; # for fonts
use GD::Graph::lines;

our $VERSION = "1.5";
our $CITATION= "SARS-CoV-2 assembly plugin by <NAME>.";

# A message to show in the report if any
my $warningMsg="";

local $0=fileparse $0;
exit(main());

sub main{
  my $settings=readConfig();
  GetOptions($settings,qw(version citation check-dependencies help tempdir=s debug numcpus=i force)) or die $!;
  exitOnSomeSneakernetOptions({
      _CITATION => $CITATION,
      _VERSION  => $VERSION,
      #'run_prediction_metrics.pl (CG-Pipeline)'     => "echo CG Pipeline version unknown",
      'run_assembly_metrics.pl (CG-Pipeline)'       => "echo CG Pipeline version unknown",
      cat                             => 'cat --version | head -n 1',
      # wget needed in SneakerNet.pm
      wget                            => 'wget --version | head -n 1',
      samtools                        => 'samtools 2>&1 | grep Version:',
      bcftools                        => 'bcftools 2>&1 | grep Version:',
      'trimmomatic'                   => 'trimmomatic -version 2>&1 | grep -v _JAVA',
      seqtk                           => 'seqtk 2>&1 | grep -m 1 Version:',
      bgzip                           => 'bgzip --version | head -n1',
      tabix                           => 'tabix --version | head -n1',
      bowtie2                         => 'bowtie2 --version | grep -m 1 version',
      'bowtie2-build'                   => 'bowtie2-build --version | grep -m 1 version',
    }, $settings,
  );

  die usage() if($$settings{help} || !@ARGV);
  $$settings{numcpus}||=1;
  $$settings{tempdir}||=File::Temp::tempdir(basename($0).".XXXXXX",TMPDIR=>1,CLEANUP=>1);
  logmsg "Temporary directory is at $$settings{tempdir}";

  my $dir=$ARGV[0];
  mkdir "$dir/SneakerNet/forEmail";

  if($$settings{force}){
    logmsg "--force was given; removing any existing assembly results.";
    system("rm -rf $dir/SneakerNet/assemblies $dir/forEmail/assemblyMetrics.tsv");
  }
 
  mkdir "$dir/SneakerNet";
  mkdir "$dir/SneakerNet/assemblies";

  my $metricsOut=assembleAll($dir,$settings);
  logmsg "Metrics can be found in $metricsOut";

  my $imagePath = makeCoverageGraph($dir, $settings);

  recordProperties($dir,{
    version  => $VERSION,
    table    => $metricsOut,
    warnings => $warningMsg,
    image    => $imagePath,
  });

  return 0;
}

sub assembleAll{
  my($dir,$settings)=@_;
  
  # Find information about each genome
  my $sampleInfo=samplesheetInfo_tsv("$dir/samples.tsv",$settings);
  my %sampleMetrics = ();
  my $cdsOverabundance = 0; # if any sample has > 100% CDS
  while(my($sample,$info)=each(%$sampleInfo)){
    next if(ref($info) ne "HASH");
    logmsg "ASSEMBLE SAMPLE $sample";

    my $outdir="$dir/SneakerNet/assemblies/$sample";
    my $outassembly=File::Spec->rel2abs("$outdir/$sample.bowtie2.bcftools.fasta");
    my $outgbk="$outdir/$sample.bowtie2.bcftools.gbk";

    # Run the assembly
    if(!-e $outassembly){
      my $assemblyDir = assembleSample($sample,$info,$settings);
      my $assembly    = "$assemblyDir/consensus.fasta";
      next if(! -d $assemblyDir);
      next if(! -e $assembly);
      next if(! -s $assembly);

      # Save the assembly
      mkdir $outdir;
{'fraction_non_alphanumeric': 0.14843853820598008, 'fraction_numerical': 0.01036544850498339, 'mean_word_length': 2.875386199794027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '968321', 'n_tokens_mistral': 2600, 'n_tokens_neox': 2450, 'n_words': 619}