<commit_before>package solGS::Cluster;


use Moose;
use namespace::autoclean;

use CXGN::Tools::Run;
use File::Spec::Functions qw / catfile catdir/;
use File::Temp qw / tempfile tempdir /;
use File::Slurp qw /write_file read_file/;
use Try::Tiny;
use Storable qw/ nstore retrieve /;
use solGS::AnalysisReport;
use Carp qw/ carp confess croak /;

use SGN::Model::solGS::solGS;
use SGN::Controller::solGS::solGS;
use SGN::Controller::solGS::List;

with 'MooseX::Getopt';
with 'MooseX::Runnable';


has "data_type" => (
    is       => 'ro',
    isa      => 'Str',
    required => 1, 
    );


has "population_type" => (
    is       => 'ro',
    isa      => 'Str',
    required => 1, 
    );

has "args_file" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );




sub run {
    my $self = shift;
    
    my $args_file  = $self->args_file;
    my $data_type  = $self->data_type;
    my $pop_type   = $self->population_type;
  
    print STDERR "\nrun data type: $data_type\n";
    print STDERR "\nrun pop type: $pop_type\n";
    print STDERR "\nrun report file: $args_file\n";
   
    if  ($self->population_type =~ /trial/) {
	if ($self->data_type =~ /phenotype/) {
	    $self->trial_phenotype_data();
	} elsif ($self->data_type =~ /genotype/) {
	    $self->trial_genotype_data();	
	}
    } elsif ($self->population_type =~ /list/) {
	
	if ($self->data_type =~ /phenotype/) {
	    $self->plots_list_phenotype_data();
	} elsif ($self->data_type =~ /genotype/) {
	    $self->genotypes_list_genotype_data();	
	}   
    } elsif ($self->population_type =~ /dataset/) {
	
	if ($self->data_type =~ /phenotype/) {
	    $self->plots_list_phenotype_data();
	} elsif ($self->data_type =~ /genotype/) {
	    $self->dataset_genotype_data();	
	}   
    }
   
}


sub trial_genotype_data {
    my $self = shift;

    my $args       = retrieve($self->args_file);

    my $geno_file  = $args->{genotype_file}; 
    my $pop_id     = ($args->{selection_pop_id} ? $args->{selection_pop_id} : $args->{training_pop_id});

    my $model = $self->get_model();
					      
    my $geno_data = $model->genotype_data($args);
   
    if ($geno_data)
    {
	write_file($geno_file, $geno_data);
    }
    
}


sub trial_phenotype_data {
    my $self = shift;
    
    my $args       = retrieve($self->args_file);
    
    my $pheno_file  = $args->{phenotype_file};
    my $pop_id      = $args->{population_id};
    my $traits_file = $args->{traits_list_file};
    my $metadata_file = $args->{metadata_file};

    my $model = $self->get_model();
   
    my $pheno_data = $model->phenotype_data($pop_id);
    my $metadata   = $model->trial_metadata();

    if ($pheno_data)
    {
	my $pheno_data = SGN::Controller::solGS::solGS->format_phenotype_dataset($pheno_data, $metadata, $traits_file);
	write_file($pheno_file, $pheno_data);
    }

    write_file($metadata_file, join("\t", @$metadata));
}


sub genotypes_list_genotype_data {
    my $self = shift;
    
    my $args = retrieve($self->args_file);
    
    my $list_pop_id   = $args->{model_id} || $args->{list_pop_id} || $args->{selection_pop_id};
    my $genotypes     = $args->{genotypes_list};
    my $genotypes_ids = $args->{genotypes_ids};
    my $data_dir      = $args->{list_data_dir};
    my $geno_file     = $args->{genotype_file};

    my $model = $self->get_model();
   
    my $geno_data = $model->genotypes_list_genotype_data($genotypes_ids);
    write_file($geno_file, $geno_data);

}


sub plots_list_phenotype_data {
    my $self= shift;

    my $args = retrieve($self->args_file);

    my $list_id = $args->{list_id};
    my $plots_ids   = $args->{plots_ids};
    my $traits_file = $args->{traits_file};
    #my $data_dir    = $args->{data_dir};
    my $pheno_file  = $args->{phenotype_file};
    my $metadata_file = $args->{metadata_file};
   
    my $model = $self->get_model();
    my $pheno_data = $model->plots_list_phenotype_data($plots_ids);
    my $metadata = $model->trial_metadata();
  
    $pheno_data = SGN::Controller::solGS::solGS->format_phenotype_dataset($pheno_data, $metadata, $traits_file);
        
    write_file($pheno_file, $pheno_data);
    write_file($metadata_file, join("\t", @$metadata));
      
}


sub dataset_genotype_data {
    my $self = shift;
    
    my $args = retrieve($self->args_file);
    
    my $dataset_id    = $args->{dataset_id};
    my $data_dir      = $args->{list_data_dir};
    my $geno_file     = $args->{genotype_file};

    my $model = $self->get_model();
   
    my $geno_data = $model->get_dataset_genotype_data($dataset_id);
    write_file($geno_file, $geno_data);

}


sub get_model {
    my $self = shift;

    my $model = SGN::Model::solGS::solGS->new({context => 'SGN::Context', 
					       schema => SGN::Context->dbic_schema("Bio::Chado::Schema")});

    return $model;
    
}



__PACKAGE__->meta->make_immutable;




####
1; #
####<commit_msg>tweak genotype data query call<commit_after>package solGS::Cluster;


use Moose;
use namespace::autoclean;

use CXGN::Tools::Run;
use File::Spec::Functions qw / catfile catdir/;
use File::Temp qw / tempfile tempdir /;
use File::Slurp qw /write_file read_file/;
use Try::Tiny;
use Storable qw/ nstore retrieve /;
use solGS::AnalysisReport;
use Carp qw/ carp confess croak /;

use SGN::Model::solGS::solGS;
use SGN::Controller::solGS::solGS;
use SGN::Controller::solGS::List;

with 'MooseX::Getopt';
with 'MooseX::Runnable';


has "data_type" => (
    is       => 'ro',
    isa      => 'Str',
    required => 1, 
    );


has "population_type" => (
    is       => 'ro',
    isa      => 'Str',
    required => 1, 
    );

has "args_file" => (
     is       => 'ro',
     isa      => 'Str',
     required => 1, 
 );




sub run {
    my $self = shift;
    
    my $args_file  = $self->args_file;
    my $data_type  = $self->data_type;
    my $pop_type   = $self->population_type;
  
    print STDERR "\nrun data type: $data_type\n";
    print STDERR "\nrun pop type: $pop_type\n";
    print STDERR "\nrun report file: $args_file\n";
   
    if  ($self->population_type =~ /trial/) {
	if ($self->data_type =~ /phenotype/) {
	    $self->trial_phenotype_data();
	} elsif ($self->data_type =~ /genotype/) {
	    $self->trial_genotype_data();	
	}
    } elsif ($self->population_type =~ /list/) {
	
	if ($self->data_type =~ /phenotype/) {
	    $self->plots_list_phenotype_data();
	} elsif ($self->data_type =~ /genotype/) {
	    $self->genotypes_list_genotype_data();	
	}   
    } elsif ($self->population_type =~ /dataset/) {
	
	if ($self->data_type =~ /phenotype/) {
	    $self->plots_list_phenotype_data();
	} elsif ($self->data_type =~ /genotype/) {
	    $self->dataset_genotype_data();	
	}   
    }
   
}


sub trial_genotype_data {
    my $self = shift;

    my $args       = retrieve($self->args_file);

    my $geno_file  = $args->{genotype_file}; 
    my $pop_id     = ($args->{selection_pop_id} ? $args->{selection_pop_id} : $args->{training_pop_id});

    my $model = $self->get_model();
					      
    my $geno_data = $model->genotype_data($args);
   
    if ($geno_data)
    {
	write_file($geno_file, $geno_data);
    }
    
}


sub trial_phenotype_data {
    my $self = shift;
    
    my $args       = retrieve($self->args_file);
    
    my $pheno_file  = $args->{phenotype_file};
    my $pop_id      = $args->{population_id};
    my $traits_file = $args->{traits_list_file};
    my $metadata_file = $args->{metadata_file};

    my $model = $self->get_model();
   
    my $pheno_data = $model->phenotype_data($pop_id);
    my $metadata   = $model->trial_metadata();

    if ($pheno_data)
    {
	my $pheno_data = SGN::Controller::solGS::solGS->format_phenotype_dataset($pheno_data, $metadata, $traits_file);
	write_file($pheno_file, $pheno_data);
    }

    write_file($metadata_file, join("\t", @$metadata));
}


sub genotypes_list_genotype_data {
    my $self = shift;
    
    my $args = retrieve($self->args_file);
    
   # my $list_pop_id   = $args->{model_id} || $args->{list_pop_id} || $args->{selection_pop_id};
   # my $genotypes     = $args->{genotypes_list};
    my $genotypes_ids = $args->{genotypes_ids};
    my $data_dir      = $args->{data_dir};
    my $geno_file     = $args->{genotype_file};

    my $model = $self->get_model();
   
    my $geno_data = $model->genotypes_list_genotype_data($genotypes_ids);
    write_file($geno_file, $geno_data);

}


sub plots_list_phenotype_data {
    my $self= shift;

    my $args = retrieve($self->args_file);

    my $list_id = $args->{list_id};
    my $plots_ids   = $args->{plots_ids};
    my $traits_file = $args->{traits_file};
    #my $data_dir    = $args->{data_dir};
    my $pheno_file  = $args->{phenotype_file};
    my $metadata_file = $args->{metadata_file};
   
    my $model = $self->get_model();
    my $pheno_data = $model->plots_list_phenotype_data($plots_ids);
    my $metadata = $model->trial_metadata();
  
    $pheno_data = SGN::Controller::solGS::solGS->format_phenotype_dataset($pheno_data, $metadata, $traits_file);
        
    write_file($pheno_file, $pheno_data);
    write_file($metadata_file, join("\t", @$metadata));
      
}


sub dataset_genotype_data {
    my $self = shift;
    
    my $args = retrieve($self->args_file);   
    my $dataset_id = $args->{dataset_id};

    if ($dataset_id)
    {
    my $geno_file = $args->{genotype_file};

    my $model = $self->get_model();  
    my $geno_data = $model->get_dataset_genotype_data($dataset_id);
    
    write_file($geno_file, $geno_data);
    } 
    elsif ($args->{genotypes_ids})
    {
	$self->genotypes_list_genotype_data();
    }

}


sub get_model {
    my $self = shift;

    my $model = SGN::Model::solGS::solGS->new({context => 'SGN::Context', 
					       schema => SGN::Context->dbic_schema("Bio::Chado::Schema")});

    return $model;
    
}



__PACKAGE__->meta->make_immutable;




####
1; #
####
{'fraction_non_alphanumeric': 0.17005695687550854, 'fraction_numerical': 0.0008136696501220504, 'mean_word_length': 3.0953769262807165, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 162, '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': '4399345', 'n_tokens_mistral': 3912, 'n_tokens_neox': 3672, 'n_words': 648}