<commit_before>#
#
# Cared for by <NAME>  <<EMAIL>>
#
# Copyright <NAME>
#
# You may distribute this module under the same terms as perl itself
#
# POD documentation - main docs before the code

=pod 

=head1 NAME

Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE

=head1 SYNOPSIS

#create and fill Bio::Seq object
my $seqfile = '/nfs/disk65/mq2/temp/bA151E14.seq';
my $seq = Bio::Seq->new();
my $seqstream = Bio::SeqIO->new(-file => $seqfile, -fmt => 'Fasta');
$seq = $seqstream->next_seq();
#create Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE object
my $trna = Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE->new (-QUERY => $seq);
$trna->workdir($workdir);
$trna->run();
my @results = $trna->output();

=head1 DESCRIPTION

tRNAscan_SE takes a Bio::Seq (or Bio::PrimarySeq) object and runs tRNAscan-SE on it. 
The resulting output file is parsed to produce a set of features.

=head1 CONTACT

<NAME>: <EMAIL>

=head1 APPENDIX

The rest of the documentation details each of the object methods. 
Internal methods are usually preceded with a _

=cut

package Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE;

use vars qw(@ISA);
use strict;
# Object preamble - inherits from Bio::EnsEMBL::Root;

use Bio::EnsEMBL::Pipeline::RunnableI;
use Bio::EnsEMBL::SimpleFeature;
use Bio::EnsEMBL::Analysis;
use Bio::Seq;
use Bio::SeqIO;
use Bio::EnsEMBL::Root;

@ISA = qw(Bio::EnsEMBL::Pipeline::RunnableI Bio::EnsEMBL::Root );

=head2 new

    Title   :   new
    Usage   :   my obj =  Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE->new (-QUERY => $seq);
    Function:   Initialises tRNAscan_SE object
    Returns :   a tRNAscan_SE Object
    Args    :   A Bio::Seq object (-QUERY) 

=cut

sub new {
  my ($class,@args) = @_;

  my $self = $class->SUPER::new(@_);    
  
  $self->{'_flist'} = [];           #an array of Bio::SimpleFeatures
  $self->{'_sequence'}  = undef;    #location of Bio::Seq object
  $self->{'_tRNAscan_SE'} = undef;  #location of tRNAscan_SE executable
  $self->{'_workdir'}   = undef;    #location of temp directory
  $self->{'_filename'}  = undef;    #file to store Bio::Seq object
  $self->{'_results'}   = undef;    #file to store results of tRNAscan_SE
  $self->{'_protected'} = [];       #a list of files protected from deletion
  
  my( $sequence, $tRNAscan_SE) = $self->_rearrange([qw(QUERY 
						       TRNASCAN_SE)], 
						   @args);


  $tRNAscan_SE = 'tRNAscan-SE' unless $tRNAscan_SE;

  $self->query($sequence) if ($sequence);       
  $self->tRNAscan_SE($self->find_executable($tRNAscan_SE));

  return $self;
}

#################
# get/set methods 
#################

sub query {
    my ($self, $seq) = @_;
    if ($seq)
    {
	unless ($seq->isa("Bio::PrimarySeqI") || $seq->isa("Bio::SeqI")) 
        {
	    $self->throw("Input isn't a Bio::SeqI or Bio::PrimarySeqI");
        }
	$self->{'_sequence'} = $seq ;

	$self->filename($self->query->id.".$$.seq");
	$self->results($self->filename.".out");
	$self->file($self->results);
    }
    return $self->{'_sequence'};
}

=head2 tRNAscan_SE

    Title   :   tRNAscan_SE
    Usage   :   $obj->tRNAscan_SE('/usr/local/pubseq/bin/tRNAscan-SE');
    Function:   Get/set method for the location of tRNAscan-SE script
    Args    :   File path (optional)

=cut

sub tRNAscan_SE {
    my ($self, $location) = @_;
    if ($location)
    {
        $self->throw("tRNAscan-SE not found at $location: $!\n") 
	    unless (-e $location);
        $self->{'_tRNAscan_SE'} = $location ;
    }
    return $self->{'_tRNAscan_SE'};
}

###########
# Analysis methods
##########

=head2 run

    Title   :  run
    Usage   :   $obj->run($workdir, $args)
    Function:   Runs cpg script and creates array of features
    Returns :   none
    Args    :   optional $workdir and $args (e.g. '-ace' for ace file output)

=cut

sub run {
    my ($self) = @_;
    #set arguments for tRNAscan_SE
    #check seq
    my $seq = $self->query() || $self->throw("Seq required for tRNAscan_SE\n");
    #set directory if provided
    $self->workdir('/tmp') unless $self->workdir();
    $self->checkdir();

    # reset filename and results as necessary
    my $tmp = $self->workdir();
    my $input = $tmp."/".$self->filename();
    $self->filename($input);
    $tmp .= "/".$self->results();
    $self->results($tmp);

    #write sequence to file
    $self->writefile();        
    $self->run_tRNAscan_SE();
    #parse output of tRNAscan_SE
    $self->parse_results();
    $self->deletefiles();
    1;
}

=head2 run_tRNAscan_SE

    Title   :  run_tRNAscan_SE
    Usage   :   $obj->run_tRNAscan_SE($filename)
    Function:   makes the system call to tRNAscan_SE
    Returns :   none
    Args    :   optional filename

=cut

sub run_tRNAscan_SE {
    my ($self) = @_;
    #run tRNAscan_SE
    print "Running tRNAscan_SE\n";
    $self->throw("Error running tRNAscan_SE on ".$self->filename."\n") 
        if (system ($self->tRNAscan_SE.' -q '.$self->filename." > ".$self->results)); 
}

=head2 parse_results

    Title   :  parse_results
    Usage   :   $obj->parse_results($filename)
    Function:   Parses tRNAscan_SE output to give a set of features
                parsefile can accept filenames, filehandles or pipes (\*STDIN)
    Returns :   none
    Args    :   optional filename

=cut

sub parse_results {<commit_msg>*** empty log message ***<commit_after>#
#
# Cared for by <NAME>  <<EMAIL>>
#
# Copyright <NAME>
#
# You may distribute this module under the same terms as perl itself
#
# POD documentation - main docs before the code

=pod 

=head1 NAME

Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE

=head1 SYNOPSIS

#create and fill Bio::Seq object
my $seqfile = '/nfs/disk65/mq2/temp/bA151E14.seq';
my $seq = Bio::Seq->new();
my $seqstream = Bio::SeqIO->new(-file => $seqfile, -fmt => 'Fasta');
$seq = $seqstream->next_seq();
#create Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE object
my $trna = Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE->new (-QUERY => $seq);
$trna->workdir($workdir);
$trna->run();
my @results = $trna->output();

=head1 DESCRIPTION

tRNAscan_SE takes a Bio::Seq (or Bio::PrimarySeq) object and runs tRNAscan-SE on it. 
The resulting output file is parsed to produce a set of features.

=head1 CONTACT

<NAME>: <EMAIL>

=head1 APPENDIX

The rest of the documentation details each of the object methods. 
Internal methods are usually preceded with a _

=cut

package Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE;

use vars qw(@ISA);
use strict;
# Object preamble - inherits from Bio::EnsEMBL::Root;

use Bio::EnsEMBL::Pipeline::RunnableI;
use Bio::EnsEMBL::SimpleFeature;
use Bio::EnsEMBL::Analysis;
use Bio::Seq;
use Bio::SeqIO;
use Bio::EnsEMBL::Root;

@ISA = qw(Bio::EnsEMBL::Pipeline::RunnableI Bio::EnsEMBL::Root );

=head2 new

    Title   :   new
    Usage   :   my obj =  Bio::EnsEMBL::Pipeline::Runnable::tRNAscan_SE->new (-QUERY => $seq);
    Function:   Initialises tRNAscan_SE object
    Returns :   a tRNAscan_SE Object
    Args    :   A Bio::Seq object (-QUERY) 

=cut

sub new {
  my ($class,@args) = @_;

  my $self = $class->SUPER::new(@_);    
  
  $self->{'_flist'} = [];           #an array of Bio::SimpleFeatures
  $self->{'_sequence'}  = undef;    #location of Bio::Seq object
  $self->{'_tRNAscan_SE'} = undef;  #location of tRNAscan_SE executable
  $self->{'_workdir'}   = undef;    #location of temp directory
  $self->{'_filename'}  = undef;    #file to store Bio::Seq object
  $self->{'_results'}   = undef;    #file to store results of tRNAscan_SE
  $self->{'_protected'} = [];       #a list of files protected from deletion
  
  my( $sequence, $tRNAscan_SE) = $self->_rearrange([qw(QUERY 
						       TRNASCAN_SE)], 
						   @args);


  $tRNAscan_SE = 'tRNAscan-SE' unless $tRNAscan_SE;

  $self->query($sequence) if ($sequence);       
  $self->tRNAscan_SE($self->find_executable($tRNAscan_SE));

  return $self;
}

#################
# get/set methods 
#################

sub query {
    my ($self, $seq) = @_;
    if ($seq)
    {
	unless ($seq->isa("Bio::PrimarySeqI") || $seq->isa("Bio::SeqI")) 
        {
	    $self->throw("Input isn't a Bio::SeqI or Bio::PrimarySeqI");
        }
	$self->{'_sequence'} = $seq ;

	$self->filename($self->query->id.".$$.seq");
	$self->results($self->filename.".out");
	$self->file($self->results);
    }
    return $self->{'_sequence'};
}

=head2 tRNAscan_SE

    Title   :   tRNAscan_SE
    Usage   :   $obj->tRNAscan_SE('/usr/local/pubseq/bin/tRNAscan-SE');
    Function:   Get/set method for the location of tRNAscan-SE script
    Args    :   File path (optional)

=cut

sub tRNAscan_SE {
    my ($self, $location) = @_;
    if ($location)
    {
        $self->throw("tRNAscan-SE not found at $location: $!\n") 
	    unless (-e $location);
        $self->{'_tRNAscan_SE'} = $location ;
    }
    return $self->{'_tRNAscan_SE'};
}

###########
# Analysis methods
##########

=head2 run

    Title   :  run
    Usage   :   $obj->run($workdir, $args)
    Function:   Runs cpg script and creates array of features
    Returns :   none
    Args    :   optional $workdir and $args (e.g. '-ace' for ace file output)

=cut

sub run {
    my ($self) = @_;
    #set arguments for tRNAscan_SE
    #check seq
    my $seq = $self->query() || $self->throw("Seq required for tRNAscan_SE\n");
    #set directory if provided
    $self->workdir('/tmp') unless $self->workdir();
    $self->checkdir();

    # reset filename and results as necessary
    my $tmp = $self->workdir();
    my $input = $tmp."/".$self->filename();
    $self->filename($input);
    $tmp .= "/".$self->results();
    $self->results($tmp);

    #write sequence to file
    $self->writefile();        
    $self->run_tRNAscan_SE();
    #parse output of tRNAscan_SE
    $self->parse_results();
    $self->deletefiles();
    1;
}

=head2 run_tRNAscan_SE

    Title   :  run_tRNAscan_SE
    Usage   :   $obj->run_tRNAscan_SE($filename)
    Function:   makes the system call to tRNAscan_SE
    Returns :   none
    Args    :   optional filename

=cut

sub run_tRNAscan_SE {
    my ($self) = @_;
    #run tRNAscan_SE
    print "Running tRNAscan_SE cmd:<".$self->tRNAscan_SE.' -q '.$self->filename." > ".$self->results.">\n";
    $self->throw("Error running tRNAscan_SE on ".$self->filename."\n") 
        if (system ($self->tRNAscan_SE.' -q '.$self->filename." > ".$self->results)); 
}

=head2 parse_results

    Title   :  parse_results
    Usage   :   $obj->parse_results($filename)
    Function:   Parses tRNAscan_SE output to give a set of features
                parsefile can accept filenames, filehandles or pipes (\*STDIN)
    Returns :   none
    Args    :   optional filename

=cut

sub parse_results {
{'fraction_non_alphanumeric': 0.16727958510136728, 'fraction_numerical': 0.003583215464403583, 'mean_word_length': 3.2939271255060727, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 146, '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': '2554725', 'n_tokens_mistral': 3978, 'n_tokens_neox': 3688, 'n_words': 924}