<commit_before># $Source$
# $Revision$
# $Date$
# $Author$
#
=head1 Bio::EnsEMBL::Healthcheck::Translation

	Protein tanslation healthcheck

	Iterates over all protein coding genes of a core database, checks every 
	gene, if it contains stop codons or comprises only of Xs. Fails, if it 
	finds such a gene. 

=cut
package Bio::EnsEMBL::Healthcheck::Translation;
use warnings;
use strict;
use Carp;
use base qw(Bio::EnsEMBL::Healthcheck);
use Data::Dumper;
use Bio::Seq;

sub new {
	my $caller = shift;
	my $class  = ref($caller) || $caller;
	my $self   = $class->SUPER::new(@_);
	
	return $self;
}

=head2 run

	Iterates over all protein coding genes, checks if there is a stop codon 
	in them or if the sequence comprises only of Xs.
	
	If there were genes with problems, returns a table with all
	transcripts and all their stop codons.

=cut
sub run {
	my ($self) = @_;
	
	# Indicates whether or not the test has passed.
	#
	my $passes = 1;

	$self->log()->debug("Getting all protein coding genes");
	
	my $genes = $self->dba()->get_GeneAdaptor()->fetch_all_by_biotype("protein_coding");
	
	my $problem_report_stop_codons_tabular_all;
	my $table_header_report_stop_codons;
	
	my $num_of_genes = @{$genes};
	my $genes_tested = 0;
	my $num_genes_until_lifesign_printed = 50;
	my $num_genes_until_printed_since_last_lifesign = 0;
	
	# Used for testing so a run of this testcase doesn't take forever.
	#
	my $max_genes_to_test  = 100;
	my $only_test_upto_max = 0; 
	
	for my $gene ( @{$genes} ) {
		
		if ($only_test_upto_max && $genes_tested>$max_genes_to_test) {
			return $passes;
		}
		
		for my $transcript ( @{ $gene->get_all_Transcripts() } ) {
			my $seq = $transcript->translate();
			if ($seq) {
				
				my $sequence = $seq->seq();
				
				if ($self->sequence_comprises_only_of_Xs($sequence)) {
					
					$self->problem( "Transcript for "
						  . "\ndbID:"                   . $transcript->dbID
						  . "\ndisplay_id:"             . $transcript->display_id
						  . "\ntranscript stable_id: "  . $transcript->stable_id<commit_msg>There was a memory issue when running the ProteinTranslation healthcheck on a database with a large amount of genes. Memory consumption exceeded 2gb which caused it to be terminated by LSF. I changed ProteinTranslation so genes don't get loaded into memory all at once, but are only fetched as needed. It used less than 200m of memory on the same database.<commit_after># $Source$
# $Revision$
# $Date$
# $Author$
#
=head1 Bio::EnsEMBL::Healthcheck::Translation

	Protein tanslation healthcheck

	Iterates over all protein coding genes of a core database, checks every 
	gene, if it contains stop codons or comprises only of Xs. Fails, if it 
	finds such a gene. 

=cut
package Bio::EnsEMBL::Healthcheck::Translation;
use warnings;
use strict;
use Carp;
use base qw(Bio::EnsEMBL::Healthcheck);
use Data::Dumper;
use Bio::Seq;

sub new {
	my $caller = shift;
	my $class  = ref($caller) || $caller;
	my $self   = $class->SUPER::new(@_);
	
	return $self;
}

sub createGeneStableIdIterator {
	
	my $self = shift;
	
	my $sql =
      'select stable_id from gene join gene_stable_id using(gene_id) where biotype="protein_coding"';

	my $sth = $self->dba()->prepare($sql);
	$sth->execute();

	return sub {
		
		my $result = $sth->fetchrow_array();
		
		if ($result) {
			return $result;
		}
		
		$sth->finish();			
		return 
	}
}

sub fetchNumberOfGenes {
	
	my $self = shift;
	
	my $sql =
      'select count(*) from gene join gene_stable_id using(gene_id) where biotype="protein_coding"';

	my $sth = $self->dba()->prepare($sql);
	$sth->execute();

	return $sth->fetchrow_array();
}

=head2 run

	Iterates over all protein coding genes, checks if there is a stop codon 
	in them or if the sequence comprises only of Xs.
	
	If there were genes with problems, returns a table with all
	transcripts and all their stop codons.

=cut
sub run {
	my ($self) = @_;
	
	# Indicates whether or not the test has passed.
	#
	my $passes = 1;

	$self->log()->debug("Getting all protein coding genes");
	
	#$self->dba()->no_cache(1);

	#my $genes = $self->dba()->get_GeneAdaptor()->fetch_all_by_biotype("protein_coding");
	
	my $problem_report_stop_codons_tabular_all;
	my $table_header_report_stop_codons;
	
	#my $num_of_genes = @{$genes};
	my $num_of_genes = $self->fetchNumberOfGenes();
	my $genes_tested = 0;
	my $num_genes_until_lifesign_printed = 50;
	my $num_genes_until_printed_since_last_lifesign = 0;
	
	# Used for testing so a run of this testcase doesn't take forever.
	#
	my $max_genes_to_test  = 100;
	my $only_test_upto_max = 0; 
	
	my $stable_id_iterator = $self->createGeneStableIdIterator;
	my $gene_adaptor = $self->dba()->get_GeneAdaptor();
	
	while ( my $current_stable_id = $stable_id_iterator->() ) {
		
		my $gene = $gene_adaptor->fetch_by_stable_id($current_stable_id); 
		
		if ($only_test_upto_max && $genes_tested>$max_genes_to_test) {
			return $passes;
		}
		
		#$gene->adaptor()->db->no_cache(1);
		#$gene->adaptor()->db()->get_TranscriptAdaptor()->db->no_cache(1);

		for my $transcript ( @{ $gene->get_all_Transcripts() } ) {

			my $seq = $transcript->translate();
			if ($seq) {
				
				my $sequence = $seq->seq();

				if ($self->sequence_comprises_only_of_Xs($sequence)) {
					
					$self->problem( "Transcript for "
						  . "\ndbID:"                   . $transcript->dbID
						  . "\ndisplay_id:"             . $transcript->display_id
						  . "\ntranscript stable_id: "  . $transcript->stable_id
{'fraction_non_alphanumeric': 0.12698993595608418, 'fraction_numerical': 0.005306495882891125, 'mean_word_length': 3.7613240418118465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 58, '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': '6949791', 'n_tokens_mistral': 2185, 'n_tokens_neox': 1857, 'n_words': 533}