<commit_before>=head1 NAME

CXGN::Stock - a second-level object for Stock

Version: 2.0

=head1 DESCRIPTION

This object was re-factored from CXGN::Chado::Stock and moosified.

Functions such as 'get_obsolete' , 'store' , and 'exists_in_database' are required , and do not use standard DBIC syntax.

=head1 AUTHOR

<NAME> <<EMAIL>>
<NAME> <<EMAIL>>

=cut

package CXGN::Stock ;

use Moose;

use Carp;
use Data::Dumper;
use Bio::Chado::Schema;
use CXGN::Metadata::Schema;
use SGN::Model::Cvterm;
use Bio::GeneticRelationships::Pedigree;
use Bio::GeneticRelationships::Individual;
use base qw / CXGN::DB::Object / ;
use CXGN::Stock::StockLookup;
use Try::Tiny;

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

has 'phenome_schema' => (
    isa => 'CXGN::Phenome::Schema',
    is => 'rw',
);

has 'check_name_exists' => (
    isa => 'Bool',
    is => 'rw',
    default => 1
);

has 'stock' => (
    isa => 'Bio::Chado::Schema::Result::Stock::Stock',
    is => 'rw',
);

has 'stock_id' => (
    isa => 'Maybe[Int]',
    is => 'rw',
);

has 'is_saving' => (
    isa => 'Bool',
    is => 'rw',
    default => 0
);

# Returns the stock_owners as [sp_person_id, sp_person_id2, ..]
has 'owners' => (
    isa => 'Maybe[ArrayRef[Int]]',
    is => 'rw',
    lazy     => 1,
    builder  => '_retrieve_stock_owner',
);

has 'organism' => (
    isa => 'Bio::Chado::Schema::Result::Organism::Organism',
    is => 'rw',
);

has 'organism_id' => (
    isa => 'Maybe[Int]',
    is => 'rw',
);

has 'species' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'genus' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'organism_common_name' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'organism_abbreviation' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'organism_comment' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'type' => (
    isa => 'Str',
    is => 'rw',
    default => 'accession',
);

has 'type_id' => (
    isa => 'Int',
    is => 'rw',
);

has 'name' => (
    isa => 'Str',
    is => 'rw',
);

has 'uniquename' => (
    isa => 'Str',
    is => 'rw',
);

has 'description' => (
    isa => 'Maybe[Str]',
    is => 'rw',
    default => '',
);

has 'is_obsolete' => (
    isa => 'Bool',
    is => 'rw',
    default => 0,
);

has 'organization_name' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'population_name' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'populations' => (
    isa => 'Maybe[ArrayRef[ArrayRef]]',
    is => 'rw'
);

has 'sp_person_id' => (
    isa => 'Int',
    is => 'rw',
);

has 'user_name' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'modification_note' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

sub BUILD {
    my $self = shift;

    #print STDERR "RUNNING BUILD FOR STOCK.PM...\n";
    my $stock;
    if ($self->stock_id){
        $stock = $self->schema()->resultset("Stock::Stock")->find({ stock_id => $self->stock_id() });
        $self->stock($stock);
        $self->stock_id($stock->stock_id);
    }
    if (defined $stock && !$self->is_saving) {
        $self->organism_id($stock->organism_id);
        $self->name($stock->name);
        $self->uniquename($stock->uniquename);
        $self->description($stock->description() || '');
        $self->type_id($stock->type_id);
        $self->type($self->schema()->resultset("Cv::Cvterm")->find({ cvterm_id=>$self->type_id() })->name());
        $self->is_obsolete($stock->is_obsolete);
        $self->organization_name($self->_retrieve_stockprop('organization'));
        $self->_retrieve_populations();
    }

    return $self;
}

sub _retrieve_stock_owner {
    my $self = shift;
    my $owner_rs = $self->phenome_schema->resultset("StockOwner")->search({
        stock_id => $self->stock_id,
    });
    my @owners;
    while (my $r = $owner_rs->next){
        push @owners, $r->sp_person_id;
    }
    $self->owners(\@owners);
}

=head2 store
<commit_msg>add a objects() and subjects() accessor.<commit_after>=head1 NAME

CXGN::Stock - a second-level object for Stock

Version: 2.0

=head1 DESCRIPTION

This object was re-factored from CXGN::Chado::Stock and moosified.

Functions such as 'get_obsolete' , 'store' , and 'exists_in_database' are required , and do not use standard DBIC syntax.

=head1 AUTHOR

<NAME> <<EMAIL>>
<NAME> <<EMAIL>>

=cut

package CXGN::Stock ;

use Moose;

use Carp;
use Data::Dumper;
use Bio::Chado::Schema;
use CXGN::Metadata::Schema;
use SGN::Model::Cvterm;
use Bio::GeneticRelationships::Pedigree;
use Bio::GeneticRelationships::Individual;
use base qw / CXGN::DB::Object / ;
use CXGN::Stock::StockLookup;
use Try::Tiny;

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

has 'phenome_schema' => (
    isa => 'CXGN::Phenome::Schema',
    is => 'rw',
);

has 'check_name_exists' => (
    isa => 'Bool',
    is => 'rw',
    default => 1
);

has 'stock' => (
    isa => 'Bio::Chado::Schema::Result::Stock::Stock',
    is => 'rw',
);

has 'stock_id' => (
    isa => 'Maybe[Int]',
    is => 'rw',
);

has 'is_saving' => (
    isa => 'Bool',
    is => 'rw',
    default => 0
);

# Returns the stock_owners as [sp_person_id, sp_person_id2, ..]
has 'owners' => (
    isa => 'Maybe[ArrayRef[Int]]',
    is => 'rw',
    lazy     => 1,
    builder  => '_retrieve_stock_owner',
);

has 'organism' => (
    isa => 'Bio::Chado::Schema::Result::Organism::Organism',
    is => 'rw',
);

has 'organism_id' => (
    isa => 'Maybe[Int]',
    is => 'rw',
);

has 'species' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'genus' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'organism_common_name' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'organism_abbreviation' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'organism_comment' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'type' => (
    isa => 'Str',
    is => 'rw',
    default => 'accession',
);

has 'type_id' => (
    isa => 'Int',
    is => 'rw',
);

has 'name' => (
    isa => 'Str',
    is => 'rw',
);

has 'uniquename' => (
    isa => 'Str',
    is => 'rw',
);

has 'description' => (
    isa => 'Maybe[Str]',
    is => 'rw',
    default => '',
);

has 'is_obsolete' => (
    isa => 'Bool',
    is => 'rw',
    default => 0,
);

has 'organization_name' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'population_name' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'populations' => (
    isa => 'Maybe[ArrayRef[ArrayRef]]',
    is => 'rw'
);

has 'sp_person_id' => (
    isa => 'Int',
    is => 'rw',
);

has 'user_name' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'modification_note' => (
    isa => 'Maybe[Str]',
    is => 'rw',
);

has 'objects' => (
    isa => 'Maybe[Ref]',
    is => 'rw',
);

has 'subjects' => ( 
    isa => 'Maybe[Ref]',
    is => 'rw',
);

sub BUILD {
    my $self = shift;

    #print STDERR "RUNNING BUILD FOR STOCK.PM...\n";
    my $stock;
    if ($self->stock_id){
        $stock = $self->schema()->resultset("Stock::Stock")->find({ stock_id => $self->stock_id() });
        $self->stock($stock);
        $self->stock_id($stock->stock_id);
    }
    if (defined $stock && !$self->is_saving) {
        $self->organism_id($stock->organism_id);
        $self->name($stock->name);
        $self->uniquename($stock->uniquename);
        $self->description($stock->description() || '');
        $self->type_id($stock->type_id);
        $self->type($self->schema()->resultset("Cv::Cvterm")->find({ cvterm_id=>$self->type_id() })->name());
        $self->is_obsolete($stock->is_obsolete);
        $self->organization_name($self->_retrieve_stockprop('organization'));
        $self->_retrieve_populations();
    }

    
    if ($self->stock_id()) { 
	
	my @objects;
	my $object_rs = $self->schema()->resultset("Stock::Stock")->find( { stock_id => $self->stock_id() })->stock_relationship_objects();
	foreach my $object ($object_rs->all()) { 
	    push @objects, [ $object->object->stock_id, $object->object->uniquename(), $object->type->name() ];
	}
	$self->objects(\@objects);
	
	my @subjects;
	my $subject_rs = $self->schema()->resultset("Stock::Stock")->find( { stock_id => $self->stock_id() })->stock_relationship_subjects();
	foreach my $subject ($subject_rs->all()) { 
	    push @subjects, [ $subject->subject->stock_id, $subject->subject->uniquename(), $subject->type->name() ];
	}
	
	$self->subjects(\@subjects);
    }
    return $self;
}


sub _retrieve_stock_owner {
    my $self = shift;
    my $owner_rs = $self->phenome_schema->resultset("StockOwner")->search({
        stock_id => $self->stock_id,
    });
    my @owners;
    while (my $r = $owner_rs->next){
        push @owners, $r->sp_person_id;
    }
    $self->owners(\@owners);
}

=head2 store

{'fraction_non_alphanumeric': 0.22410244683283786, 'fraction_numerical': 0.002744111593871484, 'mean_word_length': 3.0721601489757915, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 302, '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': '5677381', 'n_tokens_mistral': 3446, 'n_tokens_neox': 3273, 'n_words': 667}