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

use strict;
use warnings;

use FindBin::Real;
use lib FindBin::Real::Bin() . '/../lib';

use AliTV;

use Getopt::Long;
use Pod::Usage;

use Log::Log4perl;

use File::Temp;
use File::Basename;

use YAML;

my $man = 0;
my $help = 0;

my ($project, $logfile, $output);
my $overwrite = 0; # keeping existing files is default

GetOptions(
    'help|?' => \$help,
    man => \$man,
    'project=s' => \$project,
    'logfile=s' => \$logfile,
    'output=s' => \$output,
    'overwrite|force!' => \$overwrite,
    ) or pod2usage(2);

pod2usage(-exitval => 0, -verbose => 2) if $man;
pod2usage(1) if ($help || @ARGV== 0);

my $yml = "";

# Check if we have a single parameter left, which needs to be a yml file
if (@ARGV == 1)
{
    $yml = shift @ARGV;

    # yml specified, therefor use the ymls basename (without suffix
    # .yml) as project name

    if ($project)
    {
	$project = fileparse($yml, qr/\Q.yml\E/i);
	warn "YML file specified, therefore the project name was overwritten by '$project'!\n";
    }
}

# generate a uniq project name if not specified and a log file name
# accordingly if also not specified

($project, $output, $logfile) = generate_filenames($project, $output, $logfile);

# Log4Perl configuration
my $conf = q(
    log4perl.category                  = INFO, Logfile, Screen

    log4perl.appender.Logfile          = Log::Log4perl::Appender::File
    log4perl.appender.Logfile.filename = sub { logfile(); };
    log4perl.appender.Logfile.layout   = Log::Log4perl::Layout::PatternLayout
    log4perl.appender.Logfile.layout.ConversionPattern = [%r] %F %L %m%n

    log4perl.appender.Screen         = Log::Log4perl::Appender::Screen
    log4perl.appender.Screen.stderr  = 1
    log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
  );

Log::Log4perl::init( \$conf );
my $logger = Log::Log4perl->get_logger();

# print a status message including a version information
printf STDERR "
***********************************************************************
*                                                                     *
*  AliTV perl interface                                               *
*                                                                     *
***********************************************************************

You are using version %s.
", $AliTV::VERSION;

if (@ARGV > 1)
{
    my $config = AliTV::get_default_settings();
    $config->{genomes} = [];

    foreach my $infile (@ARGV)
    {
	push(@{$config->{genomes}}, {name => $infile, sequence_files => [ $infile ]});
    }

    $yml = $project.".yml";

    YAML::DumpFile($yml, $config);

    $logger->info("Wrote temporary YAML file '$yml'");
}

my $obj = AliTV->new(-file => $yml, -project => $project);

my $outputfh;

if ($output eq "-" || $output eq "")
{
    $outputfh = *STDOUT;
} else {
    open($outputfh, ">", $output) || die "Unable to open file '$output' for writing: $!";
}

print $outputfh $obj->run();

close($outputfh) || die "Unable to close file '$output' after writing: $!";

sub generate_filenames
{
    my ($project, $output, $logfile) = @_;

    unless (defined $project)
    {
	my ($fh, $fn) = File::Temp::tempfile("autogen_XXXXXXX");
	close($fh) || die "Unable to close file '$fn': !!\n";

	# unlink the temporary file
	unlink($fn) || die "Unable to delete file '$fn': $!\n";

	$project = $fn;

	if (-e $project.".yml")
	{
	    if ($overwrite)
	    {
		warn "File '".$project.".yml' exists... But due to overwrite parameter is specified the file will be overwritten!\n";
	    } else {
		die "File '".$project.".yml' exists... Unless you specify --overwrite the file will not be overwritten!\n";
	    }
	}

    }

    unless (defined $output)
    {
	$output = $project.".json";

	if (-e $output)
	{
	    if ($overwrite)
	    {
		warn "File '$output' exists... But due to overwrite parameter is specified the file will be overwritten!\n";
	    } else {
		die "File '$output' exists... Unless you specify --overwrite the file will not be overwritten!\n";
	    }
	}
    }

    unless (defined $logfile)
    {
	$logfile = $project.".log";

	if (-e $logfile)
	{
	    warn "Log File '$logfile' exists... Log messages will be appended\n";
	}
    }

    return ($project, $output, $logfile);
}

sub logfile()
{
    if (defined $logfile)
    {
	return $logfile;
    } else {
	return "logfile.log";
    }
}

=pod

=encoding utf8

=head1 NAME

AliTV perl interface - generates the required JSON file for AliTV

=head1 SYNOPSIS

    # complex configuration via yml file
    alitv.pl [OPTIONS] options.yml

    # OR

    # easy alternative including the generation of a yml file
    alitv.pl [OPTIONS] *.fasta

=head1 DESCRIPTION<commit_msg>Changing alitv.pl into a modulino for easy testing<commit_after>#!/usr/bin/env perl
package AliTV::Script;

use strict;
use warnings;

use FindBin::Real;
use lib FindBin::Real::Bin() . '/../lib';

use AliTV;

use Getopt::Long;
use Pod::Usage;

use Log::Log4perl;

use File::Temp;
use File::Basename;

use YAML;

__PACKAGE__->run( @ARGV ) unless caller();

sub run
{
    my $class = shift;

    my @params = @_;

    my $man = 0;
    my $help = 0;

    my ($project, $logfile, $output);
    my $overwrite = 0; # keeping existing files is default

    Getopt::Long::GetOptionsFromArray(\@params,
	'help|?' => \$help,
	man => \$man,
	'project=s' => \$project,
	'logfile=s' => \$logfile,
	'output=s' => \$output,
	'overwrite|force!' => \$overwrite,
	) or pod2usage(2);

    pod2usage(-exitval => 0, -verbose => 2) if $man;
    pod2usage(1) if ($help || @params== 0);

    my $yml = "";

    # Check if we have a single parameter left, which needs to be a yml file
    if (@params == 1)
    {
	$yml = shift @params;

	# yml specified, therefor use the ymls basename (without suffix
	# .yml) as project name

	if ($project)
	{
	    $project = fileparse($yml, qr/\Q.yml\E/i);
	    warn "YML file specified, therefore the project name was overwritten by '$project'!\n";
	}
    }

    # generate a uniq project name if not specified and a log file name
    # accordingly if also not specified

    ($project, $output, $logfile) = generate_filenames($project, $output, $logfile, $overwrite);

    # Log4Perl configuration
    my $conf = q(
    log4perl.category                  = INFO, Logfile, Screen

    log4perl.appender.Logfile          = Log::Log4perl::Appender::File
    log4perl.appender.Logfile.filename = sub { logfile($logfile); };
    log4perl.appender.Logfile.layout   = Log::Log4perl::Layout::PatternLayout
    log4perl.appender.Logfile.layout.ConversionPattern = [%r] %F %L %m%n

    log4perl.appender.Screen         = Log::Log4perl::Appender::Screen
    log4perl.appender.Screen.stderr  = 1
    log4perl.appender.Screen.layout = Log::Log4perl::Layout::SimpleLayout
  );

    Log::Log4perl::init( \$conf );
    my $logger = Log::Log4perl->get_logger();

    # print a status message including a version information
    printf STDERR "
***********************************************************************
*                                                                     *
*  AliTV perl interface                                               *
*                                                                     *
***********************************************************************

You are using version %s.
", $AliTV::VERSION;

    if (@params > 1)
    {
	my $config = AliTV::get_default_settings();
	$config->{genomes} = [];

	foreach my $infile (@params)
	{
	    push(@{$config->{genomes}}, {name => $infile, sequence_files => [ $infile ]});
	}

	$yml = $project.".yml";

	YAML::DumpFile($yml, $config);

	$logger->info("Wrote temporary YAML file '$yml'");
    }

    my $obj = AliTV->new(-file => $yml, -project => $project);

    my $outputfh;

    if ($output eq "-" || $output eq "")
    {
	$outputfh = *STDOUT;
    } else {
	open($outputfh, ">", $output) || die "Unable to open file '$output' for writing: $!";
    }

    print $outputfh $obj->run();

    close($outputfh) || die "Unable to close file '$output' after writing: $!";

}

sub generate_filenames
{
    my ($project, $output, $logfile, $overwrite) = @_;

    unless (defined $project)
    {
	my ($fh, $fn) = File::Temp::tempfile("autogen_XXXXXXX");
	close($fh) || die "Unable to close file '$fn': !!\n";

	# unlink the temporary file
	unlink($fn) || die "Unable to delete file '$fn': $!\n";

	$project = $fn;

	if (-e $project.".yml")
	{
	    if ($overwrite)
	    {
		warn "File '".$project.".yml' exists... But due to overwrite parameter is specified the file will be overwritten!\n";
	    } else {
		die "File '".$project.".yml' exists... Unless you specify --overwrite the file will not be overwritten!\n";
	    }
	}

    }

    unless (defined $output)
    {
	$output = $project.".json";

	if (-e $output)
	{
	    if ($overwrite)
	    {
		warn "File '$output' exists... But due to overwrite parameter is specified the file will be overwritten!\n";
	    } else {
		die "File '$output' exists... Unless you specify --overwrite the file will not be overwritten!\n";
	    }
	}
    }

    unless (defined $logfile)
    {
	$logfile = $project.".log";

	if (-e $logfile)
	{
	    warn "Log File '$logfile' exists... Log messages will be appended\n";
	}
    }

    return ($project, $output, $logfile);
}

sub logfile()
{
    my $logfile = shift;

    if (defined $logfile)
    {
	return $logfile;
    } else {
	return "logfile.log";
    }
}

=pod

=encoding utf8

=head1 NAME

AliTV perl interface - generates the required JSON file for AliTV

=head1 SYNOPSIS

    # complex configuration via yml file
    alitv.pl [OPTIONS] options.yml

    # OR

    # easy alternative including the generation of a yml file
    alitv.pl [OPTIONS] *.fasta

=head1 DESCRIPTION
{'fraction_non_alphanumeric': 0.17790530846484937, 'fraction_numerical': 0.006968641114982578, 'mean_word_length': 2.7825581395348835, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '287565', 'n_tokens_mistral': 3371, 'n_tokens_neox': 3135, 'n_words': 914}