<commit_before>use 5.010;
use strict;
use warnings FATAL => 'all';
use File::Temp;
use File::Spec;
use File::Basename;
use autodie qw(open);
use Test::More tests => 30;

#TODO: Add tests that sequences and IDs are correct between tests 
my $cmd     = File::Spec->catfile('bin', 'pairfq');
my $fq_data = _build_fq_data();
my $fa_data = _build_fa_data();

joinpairs_inmemory($cmd, $fq_data, $fa_data);
joinpairs_stdout($cmd, $fq_data, $fa_data);
joinpairs_ondisk($cmd, $fq_data, $fa_data);

#
# methods
#
sub joinpairs_inmemory {
    my ($cmd, $fq_data, $fa_data) = @_;
    my $tmpfq_out = File::Temp->new( TEMPLATE => "pairfq_fq_XXXX",
				     DIR      => 't',
				     SUFFIX   => ".fastq",
				     UNLINK   => 0 );
    
    my $tmpfa_out = File::Temp->new( TEMPLATE => "pairfq_fa_XXXX",
				     DIR      => 't',
				     SUFFIX   => ".fasta",
				     UNLINK   => 0 );
    
    system("$cmd joinpairs -f $fq_data->[0] -r $fq_data->[1] -o $tmpfq_out") == 0 
	or die "system failed: $?";
    system("$cmd joinpairs -f $fa_data->[0] -r $fa_data->[1] -o $tmpfa_out") == 0 
	or die "system failed: $?";

    open my $fqo, '<', $tmpfq_out;
    open my $fao, '<', $tmpfa_out;
    
    my ($fq_fct, $fq_rct, $fa_fct, $fa_rct) = (0, 0, 0, 0);
    my (%fqpairs, %fapairs);
    while (<$fqo>) {
	if (/^\@(\S+)\/1$/) {
	    $fqpairs{$1}++;
	    $fq_fct++;
	}
	elsif (/^\@(\S+)\/2$/) {
	    $fqpairs{$1}++;
	    $fq_rct++;
	}
    }
    close $fqo;
    
    my $fqpaired = (keys %fqpairs);
    my @fqpairnum = (values %fqpairs);
    my $allfqpaired = all_the_same(\@fqpairnum);
    ok($allfqpaired == 1, 'All fastq reads were paired');

    is($fqpaired,          6, 'Correct number of fastq pairs');
    is($fq_fct,            6, 'Correct number of forward fastq reads paired');
    is($fq_rct,            6, 'Correct number of reverse fastq reads paired');
    is($fq_fct + $fq_fct, 12, 'Correct number of total fastq reads paired');
    unlink $tmpfq_out;
    
    while (<$fao>) {
	if (/^\>(\S+)\/1$/) {
	    $fapairs{$1}++;
	    $fa_fct++;
	}
	elsif (/^\>(\S+)\/2$/) {
	    $fapairs{$1}++;
	    $fa_rct++;
	}
    }
    close $fao;
    
    my $fapaired = (keys %fapairs);
    my @fapairnum = (values %fapairs);
    my $allfapaired = all_the_same(\@fapairnum);
    ok($allfapaired == 1, 'All fasta reads were paired');

    is($fapaired,          6, 'Correct number of fasta pairs');
    is($fa_fct,            6, 'Correct number of forward fasta reads paired');
    is($fa_rct,            6, 'Correct number of reverse fasta reads paired');
    is($fa_fct + $fa_fct, 12, 'Correct number of total fasta reads paired');
    unlink $tmpfa_out;
}

sub joinpairs_stdout {
    my ($cmd, $fq_data, $fa_data) = @_;
    my $tmpfq_out = File::Temp->new( TEMPLATE => "pairfq_fq_XXXX",
				     DIR      => 't',
				     SUFFIX   => ".fastq",
				     UNLINK   => 0 );
    
    my $tmpfa_out = File::Temp->new( TEMPLATE => "pairfq_fa_XXXX",
				     DIR      => 't',
				     SUFFIX   => ".fasta",
				     UNLINK   => 0 );

    {
        open STDOUT, '>', $tmpfq_out;
        system("$cmd joinpairs -f $fq_data->[0] -r $fq_data->[1] -o -") == 0 
	    or die "system failed: $?";
	close STDOUT;
    }
    {
        open STDOUT, '>', $tmpfa_out;
 	system("$cmd joinpairs -f $fa_data->[0] -r $fa_data->[1] -o -") == 0 
	    or die "system failed: $?";
	close STDOUT;
    }
<commit_msg>use lexical refs to glob<commit_after>use 5.010;
use strict;
use warnings FATAL => 'all';
use File::Temp;
use File::Spec;
use File::Basename;
use autodie qw(open);
use Test::More tests => 30;

#TODO: Add tests that sequences and IDs are correct between tests 
my $cmd     = File::Spec->catfile('bin', 'pairfq');
my $fq_data = _build_fq_data();
my $fa_data = _build_fa_data();

joinpairs_inmemory($cmd, $fq_data, $fa_data);
joinpairs_stdout($cmd, $fq_data, $fa_data);
joinpairs_ondisk($cmd, $fq_data, $fa_data);

#
# methods
#
sub joinpairs_inmemory {
    my ($cmd, $fq_data, $fa_data) = @_;
    my $tmpfq_out = File::Temp->new( TEMPLATE => "pairfq_fq_XXXX",
				     DIR      => 't',
				     SUFFIX   => ".fastq",
				     UNLINK   => 0 );
    
    my $tmpfa_out = File::Temp->new( TEMPLATE => "pairfq_fa_XXXX",
				     DIR      => 't',
				     SUFFIX   => ".fasta",
				     UNLINK   => 0 );
    
    system("$cmd joinpairs -f $fq_data->[0] -r $fq_data->[1] -o $tmpfq_out") == 0 
	or die "system failed: $?";
    system("$cmd joinpairs -f $fa_data->[0] -r $fa_data->[1] -o $tmpfa_out") == 0 
	or die "system failed: $?";

    open my $fqo, '<', $tmpfq_out;
    open my $fao, '<', $tmpfa_out;
    
    my ($fq_fct, $fq_rct, $fa_fct, $fa_rct) = (0, 0, 0, 0);
    my (%fqpairs, %fapairs);
    while (<$fqo>) {
	if (/^\@(\S+)\/1$/) {
	    $fqpairs{$1}++;
	    $fq_fct++;
	}
	elsif (/^\@(\S+)\/2$/) {
	    $fqpairs{$1}++;
	    $fq_rct++;
	}
    }
    close $fqo;
    
    my $fqpaired = (keys %fqpairs);
    my @fqpairnum = (values %fqpairs);
    my $allfqpaired = all_the_same(\@fqpairnum);
    ok($allfqpaired == 1, 'All fastq reads were paired');

    is($fqpaired,          6, 'Correct number of fastq pairs');
    is($fq_fct,            6, 'Correct number of forward fastq reads paired');
    is($fq_rct,            6, 'Correct number of reverse fastq reads paired');
    is($fq_fct + $fq_fct, 12, 'Correct number of total fastq reads paired');
    unlink $tmpfq_out;
    
    while (<$fao>) {
	if (/^\>(\S+)\/1$/) {
	    $fapairs{$1}++;
	    $fa_fct++;
	}
	elsif (/^\>(\S+)\/2$/) {
	    $fapairs{$1}++;
	    $fa_rct++;
	}
    }
    close $fao;
    
    my $fapaired = (keys %fapairs);
    my @fapairnum = (values %fapairs);
    my $allfapaired = all_the_same(\@fapairnum);
    ok($allfapaired == 1, 'All fasta reads were paired');

    is($fapaired,          6, 'Correct number of fasta pairs');
    is($fa_fct,            6, 'Correct number of forward fasta reads paired');
    is($fa_rct,            6, 'Correct number of reverse fasta reads paired');
    is($fa_fct + $fa_fct, 12, 'Correct number of total fasta reads paired');
    unlink $tmpfa_out;
}

sub joinpairs_stdout {
    my ($cmd, $fq_data, $fa_data) = @_;
    my $tmpfq_out = File::Temp->new( TEMPLATE => "pairfq_fq_XXXX",
				     DIR      => 't',
				     SUFFIX   => ".fastq",
				     UNLINK   => 0 );
    
    my $tmpfa_out = File::Temp->new( TEMPLATE => "pairfq_fa_XXXX",
				     DIR      => 't',
				     SUFFIX   => ".fasta",
				     UNLINK   => 0 );

    {
        my $fqstdo = \*STDOUT;
        open $fqstdo, '>', $tmpfq_out;
        system("$cmd joinpairs -f $fq_data->[0] -r $fq_data->[1] -o -") == 0 
	    or die "system failed: $?";
	#close $fqstdo;
    }
    {
        my $fastdo = \*STDOUT;
        open $fastdo, '>', $tmpfa_out;
 	system("$cmd joinpairs -f $fa_data->[0] -r $fa_data->[1] -o -") == 0 
	    or die "system failed: $?";
	#close $fastdo;
    }

{'fraction_non_alphanumeric': 0.18793128762296285, 'fraction_numerical': 0.013507561297900456, 'mean_word_length': 2.4701986754966887, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '1254917', 'n_tokens_mistral': 3044, 'n_tokens_neox': 2792, 'n_words': 654}