<commit_before>
package CXGN::Trial::TrialLayout::AbstractLayout;

use Moose;
use namespace::autoclean;
use MooseX::FollowPBP;
use Moose::Util::TypeConstraints;
use Try::Tiny;
use JSON;
use Data::Dumper;

use CXGN::Stock::StockLookup;
use CXGN::Location::LocationLookup;
use SGN::Model::Cvterm;
use CXGN::Chado::Stock;


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

has 'trial_id' => (
    isa => 'Int',
    is => 'rw',
    predicate => 'has_trial_id',
    #trigger => \&_lookup_trial_id,
    required => 1
);

has 'experiment_type' => (
    is       => 'rw',
    isa     => 'Str', #field_layout or genotyping_layout or analysis_experiment
    required => 1,
);

has 'source_stock_types' => (isa => 'ArrayRef[Str]', is=> 'rw', default =>sub {  [ 'accession' ]  });  # what is being placed on the layout

has 'source_stock_type_ids' => (isa => 'ArrayRef[Int]', is => 'rw' );

has 'source_primary_stock_types' => (isa => 'ArrayRef[Str]', is=> 'rw', default =>sub {  [ 'accession' ]  });

has 'source_primary_stock_type_ids' => (isa => 'ArrayRef[Int]', is => 'rw' );

has 'target_stock_types' => (isa => 'ArrayRef[Str]', is => 'rw', default => sub { [ 'plot' ] }); # the object things are placed on, such as plot

has 'target_stock_type_ids' => (isa => 'ArrayRef[Int]', is => 'rw');

has 'relationship_types' => (isa => 'ArrayRef[Str]', is => 'rw', default => sub { [ 'plot_of' ] }); # relationship between source and target

has 'relationship_type_ids' => (isa => 'ArrayRef[Int]', is => 'rw');

# To verify that all plots in the trial have valid props and relationships. This means that the plots have plot_number and block_number properties. All plots have an accession associated. The plot's accession is in sync with any plant's accession, subplot's accession, and seedlot's containing accession. If verify_relationships is set to 1, then get_design will not return the design anymore, but will instead indicate any errors in the stored layout.
has 'verify_layout' => (isa => 'Bool', is => 'rw', predicate => 'has_verify_layout', reader => 'get_verify_layout');
# verify_physical_map checks that all plot's in the trial have row and column props.
has 'verify_physical_map' => (isa => 'Bool', is => 'rw', predicate => 'has_verify_physical_map', reader => 'get_verify_physical_map');


has 'project' => ( is => 'ro', isa => 'Bio::Chado::Schema::Result::Project::Project', reader => 'get_project', writer => '_set_project', predicate => 'has_project');

has 'design_type' => (isa => 'Str', is => 'ro', predicate => 'has_design_type', reader => 'get_design_type', writer => '_set_design_type');

has 'trial_year' => (isa => 'Str', is => 'ro', predicate => 'has_trial_year', reader => 'get_trial_year', writer => '_set_trial_year');

has 'trial_name' => (isa => 'Str', is => 'ro', predicate => 'has_trial_name', reader => 'get_trial_name', writer => '_set_trial_name');

has 'trial_description' => (isa => 'Str', is => 'ro', predicate => 'has_trial_description', reader => 'get_trial_description', writer => '_set_trial_description');

has 'trial_location' => (
    isa => 'Str',
    is => 'ro',
    predicate => 'has_trial_location', reader => 'get_trial_location', writer => '_set_trial_location',
    lazy     => 1,
    builder  => '_retrieve_trial_location',
);


has 'design' => (isa => 'HashRef', is => 'ro', predicate => 'has_design', reader => 'get_design', writer => '_set_design');

has 'plot_names' => (isa => 'ArrayRef', is => 'ro', predicate => 'has_plot_names', reader => 'get_plot_names', writer => '_set_plot_names', default => sub { [] } );


has 'replicate_numbers' => (isa => 'ArrayRef', is => 'ro', predicate => 'has_replicate_numbers', reader => 'get_replicate_numbers', writer => '_set_replicate_numbers');

has 'accession_names' => (isa => 'ArrayRef', is => 'ro', predicate => 'has_accession_names', reader => 'get_accession_names', writer => '_set_accession_names');

has 'control_names' => (isa => 'ArrayRef', is => 'ro', predicate => 'has_control_names', reader => 'get_control_names', writer => '_set_control_names');

has 'row_numbers' => (isa => 'ArrayRef', is => 'rw', predicate => 'has_row_numbers', reader => 'get_row_numbers', writer => '_set_row_numbers');

has 'col_numbers' => (isa => 'ArrayRef', is => 'rw', predicate => 'has_col_numbers', reader => 'get_col_numbers', writer => '_set_col_numbers');

has 'cvterm_hash' => (isa => 'HashRef', is => 'rw');

sub BUILD {
    my $self = shift;
    my $args = shift;

    print STDERR "Build CXGN::Trial::TrialLayout::AbstractLayout... ($args->{trial_id})\n";

    $self->_build_cvterm_hash();
}


sub cvterm_id {
    my $self = shift;
    my $term = shift;
    my $id =  $self->get_cvterm_hash->{$term};
    if (! $id) { die "The cvterm '$term' does not exist in the database!"; }
    return $id;
}

sub convert_source_stock_types_to_ids {
    my $self = shift;

    print STDERR "Converting source stock types to ids... \n";

    my @source_cvterm_ids;
    foreach my $source_stock (@{$self->get_source_stock_types()}) {
        # print STDERR "Converting $source_stock to ... ";
        my $source_stock_cvterm_id = $self->cvterm_id($source_stock);
        # print STDERR "$source_stock_cvterm_id . \n";
        push @source_cvterm_ids, $source_stock_cvterm_id;
    }
    $self->set_source_stock_type_ids(\@source_cvterm_ids);

    my @source_primary_cvterm_ids;
    foreach my $source_stock (@{$self->get_source_primary_stock_types()}) {
        # print STDERR "Converting $source_stock to ... ";
        my $source_stock_cvterm_id = $self->cvterm_id($source_stock);
        # print STDERR "$source_stock_cvterm_id . \n";
        push @source_primary_cvterm_ids, $source_stock_cvterm_id;
    }
    $self->set_source_primary_stock_type_ids(\@source_primary_cvterm_ids);

    my @target_cvterm_ids;
    foreach my $target_stock (@{$self->get_target_stock_types()}) {
        # print STDERR "Converting $target_stock to ... ";
        my $target_stock_cvterm_id = $self->cvterm_id($target_stock);
        # print STDERR "$target_stock_cvterm_id . \n";
        push @target_cvterm_ids, $target_stock_cvterm_id;
    }
    $self->set_target_stock_type_ids(\@target_cvterm_ids);

    my @rel_type_cvterm_ids;
    foreach my $rel_type (@{$self->get_relationship_types()}) {
        # print STDERR "Converting $rel_type to ... ";
        my $rel_type_cvterm_id = $self->cvterm_id($rel_type);
        # print STDERR "$rel_type_cvterm_id . \n";
        push @rel_type_cvterm_ids, $rel_type_cvterm_id;
    }
    $self->set_relationship_type_ids(\@rel_type_cvterm_ids);
}


sub _lookup_trial_id {
    my $self = shift;
    print STDERR "CXGN::Trial::TrialLayout AbstractLayout _lookup_trial_id() ".localtime."\n";
    $self->get_schema->storage->dbh->do('SET search_path TO public,sgn');

  #print STDERR "Check 2.1: ".localtime()."\n";
  $self->_set_project_from_id();
  if (!$self->has_project()) {
      print STDERR "Trial id not found\n";
    return;
  }

    if (!$self->_get_trial_year_from_project()) {
	print STDERR "Trial has no associated trial year... quitting!\n";
      return;
  }

  $self->_set_trial_year($self->_get_trial_year_from_project());<commit_msg>design endpoints, don't require trial year<commit_after>
package CXGN::Trial::TrialLayout::AbstractLayout;

use Moose;
use namespace::autoclean;
use MooseX::FollowPBP;
use Moose::Util::TypeConstraints;
use Try::Tiny;
use JSON;
use Data::Dumper;

use CXGN::Stock::StockLookup;
use CXGN::Location::LocationLookup;
use SGN::Model::Cvterm;
use CXGN::Chado::Stock;


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

has 'trial_id' => (
    isa => 'Int',
    is => 'rw',
    predicate => 'has_trial_id',
    #trigger => \&_lookup_trial_id,
    required => 1
);

has 'experiment_type' => (
    is       => 'rw',
    isa     => 'Str', #field_layout or genotyping_layout or analysis_experiment
    required => 1,
);

has 'source_stock_types' => (isa => 'ArrayRef[Str]', is=> 'rw', default =>sub {  [ 'accession' ]  });  # what is being placed on the layout

has 'source_stock_type_ids' => (isa => 'ArrayRef[Int]', is => 'rw' );

has 'source_primary_stock_types' => (isa => 'ArrayRef[Str]', is=> 'rw', default =>sub {  [ 'accession' ]  });

has 'source_primary_stock_type_ids' => (isa => 'ArrayRef[Int]', is => 'rw' );

has 'target_stock_types' => (isa => 'ArrayRef[Str]', is => 'rw', default => sub { [ 'plot' ] }); # the object things are placed on, such as plot

has 'target_stock_type_ids' => (isa => 'ArrayRef[Int]', is => 'rw');

has 'relationship_types' => (isa => 'ArrayRef[Str]', is => 'rw', default => sub { [ 'plot_of' ] }); # relationship between source and target

has 'relationship_type_ids' => (isa => 'ArrayRef[Int]', is => 'rw');

# To verify that all plots in the trial have valid props and relationships. This means that the plots have plot_number and block_number properties. All plots have an accession associated. The plot's accession is in sync with any plant's accession, subplot's accession, and seedlot's containing accession. If verify_relationships is set to 1, then get_design will not return the design anymore, but will instead indicate any errors in the stored layout.
has 'verify_layout' => (isa => 'Bool', is => 'rw', predicate => 'has_verify_layout', reader => 'get_verify_layout');
# verify_physical_map checks that all plot's in the trial have row and column props.
has 'verify_physical_map' => (isa => 'Bool', is => 'rw', predicate => 'has_verify_physical_map', reader => 'get_verify_physical_map');


has 'project' => ( is => 'ro', isa => 'Bio::Chado::Schema::Result::Project::Project', reader => 'get_project', writer => '_set_project', predicate => 'has_project');

has 'design_type' => (isa => 'Str', is => 'ro', predicate => 'has_design_type', reader => 'get_design_type', writer => '_set_design_type');

has 'trial_year' => (isa => 'Str', is => 'ro', predicate => 'has_trial_year', reader => 'get_trial_year', writer => '_set_trial_year');

has 'trial_name' => (isa => 'Str', is => 'ro', predicate => 'has_trial_name', reader => 'get_trial_name', writer => '_set_trial_name');

has 'trial_description' => (isa => 'Str', is => 'ro', predicate => 'has_trial_description', reader => 'get_trial_description', writer => '_set_trial_description');

has 'trial_location' => (
    isa => 'Str',
    is => 'ro',
    predicate => 'has_trial_location', reader => 'get_trial_location', writer => '_set_trial_location',
    lazy     => 1,
    builder  => '_retrieve_trial_location',
);


has 'design' => (isa => 'HashRef', is => 'ro', predicate => 'has_design', reader => 'get_design', writer => '_set_design');

has 'plot_names' => (isa => 'ArrayRef', is => 'ro', predicate => 'has_plot_names', reader => 'get_plot_names', writer => '_set_plot_names', default => sub { [] } );


has 'replicate_numbers' => (isa => 'ArrayRef', is => 'ro', predicate => 'has_replicate_numbers', reader => 'get_replicate_numbers', writer => '_set_replicate_numbers');

has 'accession_names' => (isa => 'ArrayRef', is => 'ro', predicate => 'has_accession_names', reader => 'get_accession_names', writer => '_set_accession_names');

has 'control_names' => (isa => 'ArrayRef', is => 'ro', predicate => 'has_control_names', reader => 'get_control_names', writer => '_set_control_names');

has 'row_numbers' => (isa => 'ArrayRef', is => 'rw', predicate => 'has_row_numbers', reader => 'get_row_numbers', writer => '_set_row_numbers');

has 'col_numbers' => (isa => 'ArrayRef', is => 'rw', predicate => 'has_col_numbers', reader => 'get_col_numbers', writer => '_set_col_numbers');

has 'cvterm_hash' => (isa => 'HashRef', is => 'rw');

sub BUILD {
    my $self = shift;
    my $args = shift;

    print STDERR "Build CXGN::Trial::TrialLayout::AbstractLayout... ($args->{trial_id})\n";

    $self->_build_cvterm_hash();
}


sub cvterm_id {
    my $self = shift;
    my $term = shift;
    my $id =  $self->get_cvterm_hash->{$term};
    if (! $id) { die "The cvterm '$term' does not exist in the database!"; }
    return $id;
}

sub convert_source_stock_types_to_ids {
    my $self = shift;

    print STDERR "Converting source stock types to ids... \n";

    my @source_cvterm_ids;
    foreach my $source_stock (@{$self->get_source_stock_types()}) {
        # print STDERR "Converting $source_stock to ... ";
        my $source_stock_cvterm_id = $self->cvterm_id($source_stock);
        # print STDERR "$source_stock_cvterm_id . \n";
        push @source_cvterm_ids, $source_stock_cvterm_id;
    }
    $self->set_source_stock_type_ids(\@source_cvterm_ids);

    my @source_primary_cvterm_ids;
    foreach my $source_stock (@{$self->get_source_primary_stock_types()}) {
        # print STDERR "Converting $source_stock to ... ";
        my $source_stock_cvterm_id = $self->cvterm_id($source_stock);
        # print STDERR "$source_stock_cvterm_id . \n";
        push @source_primary_cvterm_ids, $source_stock_cvterm_id;
    }
    $self->set_source_primary_stock_type_ids(\@source_primary_cvterm_ids);

    my @target_cvterm_ids;
    foreach my $target_stock (@{$self->get_target_stock_types()}) {
        # print STDERR "Converting $target_stock to ... ";
        my $target_stock_cvterm_id = $self->cvterm_id($target_stock);
        # print STDERR "$target_stock_cvterm_id . \n";
        push @target_cvterm_ids, $target_stock_cvterm_id;
    }
    $self->set_target_stock_type_ids(\@target_cvterm_ids);

    my @rel_type_cvterm_ids;
    foreach my $rel_type (@{$self->get_relationship_types()}) {
        # print STDERR "Converting $rel_type to ... ";
        my $rel_type_cvterm_id = $self->cvterm_id($rel_type);
        # print STDERR "$rel_type_cvterm_id . \n";
        push @rel_type_cvterm_ids, $rel_type_cvterm_id;
    }
    $self->set_relationship_type_ids(\@rel_type_cvterm_ids);
}


sub _lookup_trial_id {
    my $self = shift;
    print STDERR "CXGN::Trial::TrialLayout AbstractLayout _lookup_trial_id() ".localtime."\n";
    $self->get_schema->storage->dbh->do('SET search_path TO public,sgn');

  #print STDERR "Check 2.1: ".localtime()."\n";
  $self->_set_project_from_id();
  if (!$self->has_project()) {
      print STDERR "Trial id not found\n";
    return;
  }

    if (!$self->_get_trial_year_from_project()) {
        print STDERR "Trial has no associated trial year... quitting!\n";
      #return;
  } else {
        $self->_set_trial_year($self->_get_trial_year_from_project());
    }

{'fraction_non_alphanumeric': 0.16682812067533906, 'fraction_numerical': 0.0009687240520343206, 'mean_word_length': 4.445742275810098, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 325, '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': '4813814', 'n_tokens_mistral': 5064, 'n_tokens_neox': 4818, 'n_words': 1191}