<commit_before>=head1 NAME

CXGN::BreedingProgram - class for retrieving breeding program information and filtering by location/s, year/s, etc.

=head1 AUTHORS

<NAME> <<EMAIL>>


=head1 METHODS

=cut

package CXGN::BreedingProgram;

use Moose;
use Data::Dumper;
use Try::Tiny;
use SGN::Model::Cvterm;


has 'schema' => (
    isa => 'Bio::Chado::Schema',
    is => 'rw',
    required => 1,
);

sub BUILD {
    my $self = shift;
    my $breeding_program_cvterm_id = $self->get_breeding_program_cvterm_id;
    my $row = $self->schema->resultset("Project::Project")->find( 
	{ project_id             => $self->get_program_id(),
	  'projectprops.type_id' => $breeding_program_cvterm_id },
	{ join => 'projectprops' }
	);
    $self->set_project_object($row);
    if (!$row) {
	die "The breeding program  ".$self->get_program_id()." does not exist";
    }
}

=head2 accessors get_program_id()

 Desc: get the breeding program project_id

=cut

has 'program_id' => (isa => 'Int',
		     is => 'rw',
		     reader => 'get_program_id',
		     writer => 'set_program_id',
    );



=head2 accessors get_name, set_name

 Usage:
 Desc:
 Property
 Side Effects:
 Example:

=cut

sub get_name {
    my $self = shift;
    my $project_obj = $self->get_project_object;

    if ($project_obj) {
	return $project_obj->name();
    }
}

sub set_name {
    my $self = shift;
    my $name = shift;
    my $project_obj = $self->get_project_object;
    if ($project_obj) {
	$project_obj->name($name);
	$project_obj->update();
    }
}

sub get_project_object {
  my $self = shift;
  return $self->{project_object}; 
}

sub set_project_object {
  my $self = shift;
  $self->{project_object} = shift;
}


=head2 accessors get_description, set_description

 Usage:
 Desc:
 Property
 Side Effects:
 Example:

=cut

sub get_description {
  my $self = shift;
  return $self->{description}; 
}

sub set_description {
  my $self = shift;
  $self->{description} = shift;
}


sub get_breeding_program_cvterm_id {
    my $self = shift;
    
    my $breeding_program_cvterm_id;
    my $breeding_program_cvterm = SGN::Model::Cvterm->get_cvterm_row($self->schema, 'breeding_program', 'project_property');
    if ($breeding_program_cvterm) {
        $breeding_program_cvterm_id = $breeding_program_cvterm->cvterm_id();
    }
    return $breeding_program_cvterm_id;
}

sub get_project_year_cvterm_id {
    my $self = shift;
    my $year_cvterm_row = SGN::Model::Cvterm->get_cvterm_row( $self->schema, 'project year', 'project_property' );
    return $year_cvterm_row->cvterm_id();
}

sub get_accession_cvterm_id {
    my $self = shift;
    my $accession_cvterm_row = SGN::Model::Cvterm->get_cvterm_row( $self->schema, 'accession', 'stock_type' );
    return $accession_cvterm_row->cvterm_id();
}
=head2 get_trials

 Usage: $self->get_trials
 Desc:  find the trials (projects) associated with the breeding program. Will fetch only trials that have a design. This is to avoid printing crosses etc.
 Ret:   BCS Project resultset 
 Args:  none
 Side Effects: none
 Example:

=cut

sub get_trials {
    my $self = shift;
    my $project_obj = $self->get_project_object;
    
    my $trials_rs;
    my $trial_rel_rs = $project_obj->project_relationship_object_projects;

    if ($trial_rel_rs) {
	my $design_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($self->schema, 'design', 'project_property');
	
	$trials_rs = $trial_rel_rs->search_related('subject_project', {
	    'projectprop.type_id' => $design_cvterm_id }, {
		join => 'projectprop' }
	    );
    }
    return $trials_rs;
}


=head2 function get_traits_assayed()
 Usage:
 Desc: Find the traits assayed in the breeding program
 Ret:  arrayref of [cvterm_id, cvterm_name]
 Args:
 Side Effects:
 Example:

=cut
sub get_traits_assayed {
    my $self= shift;
    my $dbh = $self->schema->storage()->dbh();
    <commit_msg>fix fetching trials with design for breeding program pages<commit_after>=head1 NAME

CXGN::BreedingProgram - class for retrieving breeding program information and filtering by location/s, year/s, etc.

=head1 AUTHORS

<NAME> <<EMAIL>>


=head1 METHODS

=cut

package CXGN::BreedingProgram;

use Moose;
use Data::Dumper;
use Try::Tiny;
use SGN::Model::Cvterm;


has 'schema' => (
    isa => 'Bio::Chado::Schema',
    is => 'rw',
    required => 1,
);

sub BUILD {
    my $self = shift;
    my $breeding_program_cvterm_id = $self->get_breeding_program_cvterm_id;
    my $row = $self->schema->resultset("Project::Project")->find( 
	{ project_id             => $self->get_program_id(),
	  'projectprops.type_id' => $breeding_program_cvterm_id },
	{ join => 'projectprops' }
	);
    $self->set_project_object($row);
    if (!$row) {
	die "The breeding program  ".$self->get_program_id()." does not exist";
    }
}

=head2 accessors get_program_id()

 Desc: get the breeding program project_id

=cut

has 'program_id' => (isa => 'Int',
		     is => 'rw',
		     reader => 'get_program_id',
		     writer => 'set_program_id',
    );



=head2 accessors get_name, set_name

 Usage:
 Desc:
 Property
 Side Effects:
 Example:

=cut

sub get_name {
    my $self = shift;
    my $project_obj = $self->get_project_object;

    if ($project_obj) {
	return $project_obj->name();
    }
}

sub set_name {
    my $self = shift;
    my $name = shift;
    my $project_obj = $self->get_project_object;
    if ($project_obj) {
	$project_obj->name($name);
	$project_obj->update();
    }
}

sub get_project_object {
  my $self = shift;
  return $self->{project_object}; 
}

sub set_project_object {
  my $self = shift;
  $self->{project_object} = shift;
}


=head2 accessors get_description, set_description

 Usage:
 Desc:
 Property
 Side Effects:
 Example:

=cut

sub get_description {
  my $self = shift;
  return $self->{description}; 
}

sub set_description {
  my $self = shift;
  $self->{description} = shift;
}


sub get_breeding_program_cvterm_id {
    my $self = shift;
    
    my $breeding_program_cvterm_id;
    my $breeding_program_cvterm = SGN::Model::Cvterm->get_cvterm_row($self->schema, 'breeding_program', 'project_property');
    if ($breeding_program_cvterm) {
        $breeding_program_cvterm_id = $breeding_program_cvterm->cvterm_id();
    }
    return $breeding_program_cvterm_id;
}

sub get_project_year_cvterm_id {
    my $self = shift;
    my $year_cvterm_row = SGN::Model::Cvterm->get_cvterm_row( $self->schema, 'project year', 'project_property' );
    return $year_cvterm_row->cvterm_id();
}

sub get_accession_cvterm_id {
    my $self = shift;
    my $accession_cvterm_row = SGN::Model::Cvterm->get_cvterm_row( $self->schema, 'accession', 'stock_type' );
    return $accession_cvterm_row->cvterm_id();
}
=head2 get_trials

 Usage: $self->get_trials
 Desc:  find the trials (projects) associated with the breeding program. Will fetch only trials that have a design. This is to avoid printing crosses etc.
 Ret:   BCS Project resultset 
 Args:  none
 Side Effects: none
 Example:

=cut

sub get_trials {
    my $self = shift;
    my $project_obj = $self->get_project_object;
    
    my $trials_rs;
    my $trials_fetched;
    my $trial_rel_rs = $project_obj->project_relationship_object_projects;

    if ($trial_rel_rs) {
	my $design_cvterm = SGN::Model::Cvterm->get_cvterm_row($self->schema, 'design', 'project_property');
	$trials_rs = $trial_rel_rs->search_related('subject_project');
	$trials_fetched = $trials_rs->search(
	    {
		'projectprops.type_id' => $design_cvterm->cvterm_id 
	    },
	    {
		join => 'projectprops' 
	    }
	    );
    }
    return $trials_fetched;
}


=head2 function get_traits_assayed()
 Usage:
 Desc: Find the traits assayed in the breeding program
 Ret:  arrayref of [cvterm_id, cvterm_name]
 Args:
 Side Effects:
 Example:

=cut
sub get_traits_assayed {
    my $self= shift;
    my $dbh = $self->schema->storage()->dbh();
    
{'fraction_non_alphanumeric': 0.13115384615384615, 'fraction_numerical': 0.002307692307692308, 'mean_word_length': 3.9062893081761008, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 107, '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': '2945112', 'n_tokens_mistral': 2899, 'n_tokens_neox': 2778, 'n_words': 630}