<commit_before>package Bio::BioVeL::AsynchronousService::TNRS;
use strict;
use warnings;
use Bio::BioVeL::AsynchronousService ':status';
use Bio::Phylo::Util::Logger 'WARN';
use base 'Bio::BioVeL::AsynchronousService';
use Env::C;
	
    
=head1 NAME

Bio::BioVeL::AsynchronousService::TNRS - wrapper for the SUPERSMART TNRS service

=head1 DESCRIPTION

B<NOTE>: this service is untested, it is a work in progress. It is meant to show
how the scripts of the L<http://www.supersmart-project.org> could be executed as
asynchronous web services.

=head1 METHODS

=over

=item new

The constructor specifies one object property: the location of the input C<names>
list.

=cut

sub new {
	shift->SUPER::new( 'parameters' => [ 'names' ], @_ );
}


=item launch

Launches the TNRS script. This will require the SUPERSMART_HOME environment 
variable to be defined, which when running under mod_perl needs to be done by
adding something like the following to httpd.conf:

 PerlSetEnv SUPERSMART_HOME /Library/WebServer/Perl/supersmart

=cut

sub launch {
	my $self = shift;
	my $log = $self->logger;
	$log->VERBOSE( '-level' => WARN, '-class' => __PACKAGE__ );
	
	# contents in this results dir may be made visible to the user
	my $outfile = $self->outdir . '/taxa.tsv';
	my $infile  = $self->outdir . '/names.txt';
	my $logfile = $self->outdir . '/TNRS.log';
	
	# SUPERSMART_HOME needs to be known and accessible to the httpd process
	die ("need environment variable SUPERSMART_HOME") if not $ENV{'SUPERSMART_HOME'};
	
	# with mod_perl, environment variables are not passed to the child script; use Env::C to set globally
	Env::C::setenv("SUPERSMART_HOME", $ENV{SUPERSMART_HOME});
	
	my $script = $ENV{'SUPERSMART_HOME'} . '/script/supersmart/parallel_write_taxa_table.pl';
	$log->error("no such file: $script") if not -e $script;
	
	# fetch the input file
	$log->info("going to fetch input names from ".$self->names);
	my $readfh  = $self->get_handle( $self->names );	
	open my $writefh, '>', $infile;
	print $writefh $_ while <$readfh>; 
	
	# run the job
	my $command = "perl $script -i $infile 2>$logfile";
	my $out = qx($command);
	
	# there was an error from the OS
	if ( $? ) {
		$self->status( ERROR );
		$self->lasterr( "unexpected problem, exit code: " . ( $? >> 8 ) );
		$log->error( "unexpected problem, exit code: " . ( $? >> 8 ) ); 
	}
	else {
		# write output file<commit_msg>updated comment on using Env::C, because in some settings (for instance on Macos), environment variables are not passed to child processes!<commit_after>package Bio::BioVeL::AsynchronousService::TNRS;
use strict;
use warnings;
use Bio::BioVeL::AsynchronousService ':status';
use Bio::Phylo::Util::Logger 'WARN';
use base 'Bio::BioVeL::AsynchronousService';
use Env::C;
	
    
=head1 NAME

Bio::BioVeL::AsynchronousService::TNRS - wrapper for the SUPERSMART TNRS service

=head1 DESCRIPTION

B<NOTE>: this service is untested, it is a work in progress. It is meant to show
how the scripts of the L<http://www.supersmart-project.org> could be executed as
asynchronous web services.

=head1 METHODS

=over

=item new

The constructor specifies one object property: the location of the input C<names>
list.

=cut

sub new {
	shift->SUPER::new( 'parameters' => [ 'names' ], @_ );
}


=item launch

Launches the TNRS script. This will require the SUPERSMART_HOME environment 
variable to be defined, which when running under mod_perl needs to be done by
adding something like the following to httpd.conf:

 PerlSetEnv SUPERSMART_HOME /Library/WebServer/Perl/supersmart

=cut

sub launch {
	my $self = shift;
	my $log = $self->logger;
	$log->VERBOSE( '-level' => WARN, '-class' => __PACKAGE__ );
	
	# contents in this results dir may be made visible to the user
	my $outfile = $self->outdir . '/taxa.tsv';
	my $infile  = $self->outdir . '/names.txt';
	my $logfile = $self->outdir . '/TNRS.log';
	
	# SUPERSMART_HOME needs to be known and accessible to the httpd process
	die ("need environment variable SUPERSMART_HOME") if not $ENV{'SUPERSMART_HOME'};
	
	# with mod_perl, environment variables might not be passed to the child script (depending of OS and apache version); use Env::C to set globally
	Env::C::setenv("SUPERSMART_HOME", $ENV{SUPERSMART_HOME});
	
	my $script = $ENV{'SUPERSMART_HOME'} . '/script/supersmart/parallel_write_taxa_table.pl';
	$log->error("no such file: $script") if not -e $script;
	
	# fetch the input file
	$log->info("going to fetch input names from ".$self->names);
	my $readfh  = $self->get_handle( $self->names );	
	open my $writefh, '>', $infile;
	print $writefh $_ while <$readfh>; 
	
	# run the job
	my $command = "perl $script -i $infile 2>$logfile";
	my $out = qx($command);
	
	# there was an error from the OS
	if ( $? ) {
		$self->status( ERROR );
		$self->lasterr( "unexpected problem, exit code: " . ( $? >> 8 ) );
		$log->error( "unexpected problem, exit code: " . ( $? >> 8 ) ); 
	}
	else {
		# write output file
{'fraction_non_alphanumeric': 0.13164300202839757, 'fraction_numerical': 0.002434077079107505, 'mean_word_length': 4.336580086580087, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '401433', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1664, 'n_words': 558}