Skip to content

Commit

Permalink
Merge pull request #4747 from solgenomics/topic/add_host_to_vectors
Browse files Browse the repository at this point in the history
Add auto generate vector uniquename option
  • Loading branch information
titima15 authored Dec 4, 2023
2 parents 5486f09 + faae8c3 commit 8df5975
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 41 deletions.
91 changes: 91 additions & 0 deletions lib/CXGN/Stock/Vector/ParseUpload.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package CXGN::Stock::Vector::ParseUpload;

use Moose;
use Data::Dumper;
use MooseX::FollowPBP;
use Moose::Util::TypeConstraints;

with 'MooseX::Object::Pluggable';


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

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

has 'editable_stock_props' => (
is => 'ro',
isa => 'ArrayRef',
required => 1,
);

has 'do_fuzzy_search' => (
is => 'ro',
isa => 'Bool',
default => 1
);

has 'autogenerate_uniquename' => (
is => 'ro',
isa => 'Bool',
default => 0
);

has 'parse_errors' => (
is => 'ro',
isa => 'HashRef',
writer => '_set_parse_errors',
reader => 'get_parse_errors',
predicate => 'has_parse_errors',
);

has '_parsed_data' => (
is => 'ro',
isa => 'HashRef',
writer => '_set_parsed_data',
predicate => '_has_parsed_data',
);

sub parse {
my $self = shift;

if (!$self->_validate_with_plugin()) {
my $errors = $self->get_parse_errors();
print STDERR "\nCould not validate trial file: ".$self->get_filename()."\nError:".Dumper($errors)."\n";
return;
}

print STDERR "Check 3.1: CXGN::Stock::ParseUpload ".localtime();

if (!$self->_parse_with_plugin()) {
my $errors = $self->get_parse_errors();
print STDERR "\nCould not parse trial file: ".$self->get_filename()."\nError:".Dumper($errors)."\n";
return;
}

print STDERR "Check 3.2: CXGN::Stock::ParseUpload ".localtime();

if (!$self->_has_parsed_data()) {
my $errors = $self->get_parse_errors();
print STDERR "\nNo parsed data for trial file: ".$self->get_filename()."\nError:".Dumper($errors)."\n";
return;
} else {
return $self->_parsed_data();
}

print STDERR "Check 3.3: CXGN::Stock::ParseUpload ".localtime();

my $errors = $self->get_parse_errors();
print STDERR "\nError parsing trial file: ".$self->get_filename()."\nError:".Dumper($errors)."\n";
return;
}


1;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package CXGN::Stock::ParseUpload::Plugin::VectorsXLS;
package CXGN::Stock::Vector::ParseUpload::Plugin::VectorsXLS;

use Moose::Role;
use Spreadsheet::ParseExcel;
Expand All @@ -16,9 +16,10 @@ sub _validate_with_plugin {
my $filename = $self->get_filename();
my $schema = $self->get_chado_schema();
my $editable_stockprops = $self->get_editable_stock_props();
my $autogenerate_uniquename = $self->get_autogenerate_uniquename();

# Match a dot, extension .xls / .xlsx
my $extension = $filename =~ /(\.[^.]+)$/;
my ($extension) = $filename =~ /(\.[^.]+)$/;
my $parser;

if ($extension eq '.xlsx') {
Expand Down Expand Up @@ -106,7 +107,9 @@ sub _validate_with_plugin {
}

if (!$vector_name || $vector_name eq '' ) {
push @error_messages, "Cell A$row_name: vector_name missing.";
if (! $autogenerate_uniquename ){
push @error_messages, "Cell A$row_name: vector_name missing.";
}
} else {
$vector_name =~ s/^\s+|\s+$//g; #trim whitespace from front and end...
$seen_vector_names{$vector_name}=$row_name;
Expand Down Expand Up @@ -154,6 +157,7 @@ sub _parse_with_plugin {
my $filename = $self->get_filename();
my $schema = $self->get_chado_schema();
my $do_fuzzy_search = $self->get_do_fuzzy_search();
my $autogenerate_uniquename = $self->get_autogenerate_uniquename();

# Match a dot, extension .xls / .xlsx
my ($extension) = $filename =~ /(\.[^.]+)$/;
Expand Down Expand Up @@ -181,12 +185,33 @@ sub _parse_with_plugin {

my %seen_vector_names;
my %seen_species_names;
my $vector_max_id;

if($autogenerate_uniquename > 0){
my $stock_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'vector_construct', 'stock_type')->cvterm_id();

my $stocks = $schema->resultset("Stock::Stock")->search({ type_id => $stock_type_id, });

my $id;
$vector_max_id = 0;
while (my $r = $stocks->next()) {
$id = $r->uniquename;
if ($id =~ m/T[0-9]+/){
$id =~ s/T//;
if($vector_max_id < $id){
$vector_max_id = $id;
}
}
}
}


for my $row ( 1 .. $row_max ) {
my $vector_name;
my $species_name;

if ($worksheet->get_cell($row,0)) {
if($autogenerate_uniquename > 0 ){
$vector_name = "T" . ($vector_max_id + $row);
} elsif ($worksheet->get_cell($row,0)) {
$vector_name = $worksheet->get_cell($row,0)->value();
}
if ($worksheet->get_cell($row,1)) {
Expand Down Expand Up @@ -238,7 +263,9 @@ sub _parse_with_plugin {
my $vector_name;
my $species_name;

if ($worksheet->get_cell($row,0)) {
if($autogenerate_uniquename > 0 ){
$vector_name = "T" . ($vector_max_id + $row);
} elsif ($worksheet->get_cell($row,0)) {
$vector_name = $worksheet->get_cell($row,0)->value();
}
if ($worksheet->get_cell($row,1)) {
Expand Down Expand Up @@ -336,6 +363,7 @@ sub _parse_with_plugin {
}
}


%return_data = (
parsed_data => \%parsed_entries,
found_vectors => $found_vectors,
Expand All @@ -345,7 +373,6 @@ sub _parse_with_plugin {
fuzzy_organisms => $fuzzy_organisms,
absent_organisms => $absent_organisms
);
# print STDERR "\n\nVectorsXLS parsed results :\n".Data::Dumper::Dumper(%return_data)."\n\n";

$self->_set_parsed_data(\%return_data);
return 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/SGN/Controller/AJAX/Search/Vector.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ sub stock_search :Path('/ajax/search/vectors') Args(0) {
people_schema=>$people_schema,
phenome_schema=>$phenome_schema,
match_type=>$params->{any_name_matchtype},
match_name=>$params->{any_name},
match_name=>$params->{any_name},
operator=>$params->{operator},
stockprops_values=>$stockprops_values,
stockprop_columns_view=>$stockprop_columns_view,
Expand Down
33 changes: 32 additions & 1 deletion lib/SGN/Controller/AJAX/VectorConstruct.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use Data::Dumper;
use JSON;
use SGN::Model::Cvterm;
use CXGN::Stock::Vector;
use CXGN::Stock::Vector::ParseUpload;
use Try::Tiny;
use Encode;
use JSON::XS qw | decode_json |;
Expand Down Expand Up @@ -279,6 +280,7 @@ sub verify_vectors_file_POST : Args(0) {
my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
my $upload = $c->req->upload('new_vectors_upload_file');
my $do_fuzzy_search = $user_role eq 'curator' && !$c->req->param('fuzzy_check_upload_vectors') ? 0 : 1;
my $autogenerate_uniquename = !$c->req->param('autogenerate_uniquename') ? 0 : 1;

if ($user_role ne 'curator' && !$do_fuzzy_search) {
$c->stash->{rest} = {error=>'Only a curator can add vectors without using the fuzzy search!'};
Expand Down Expand Up @@ -316,7 +318,7 @@ sub verify_vectors_file_POST : Args(0) {
unlink $upload_tempfile;

my @editable_vector_props = split ',', $c->config->{editable_vector_props};
my $parser = CXGN::Stock::ParseUpload->new(chado_schema => $schema, filename => $archived_filename_with_path, editable_stock_props=>\@editable_vector_props, do_fuzzy_search=>$do_fuzzy_search);
my $parser = CXGN::Stock::Vector::ParseUpload->new(chado_schema => $schema, filename => $archived_filename_with_path, editable_stock_props=>\@editable_vector_props, do_fuzzy_search=>$do_fuzzy_search, autogenerate_uniquename=>$autogenerate_uniquename);
$parser->load_plugin('VectorsXLS');
my $parsed_data = $parser->parse();

Expand Down Expand Up @@ -414,6 +416,35 @@ sub verify_vectors_fuzzy_options_POST : Args(0) {
}


sub get_new_vector_uniquename : Path('/ajax/get_new_vector_uniquename') : ActionClass('REST') { }

sub get_new_vector_uniquename_GET : Args(0) {
my ($self, $c) = @_;
my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');

my $stock_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'vector_construct', 'stock_type')->cvterm_id();

my $stocks = $schema->resultset("Stock::Stock")->search({
type_id => $stock_type_id,
});

my $id;
my $max=0;
while (my $r = $stocks->next()) {
$id = $r->uniquename;
if ($id =~ m/T[0-9]+/){
$id =~ s/T//;
if($max < $id){
$max = $id;
}
}
}
$max += 1;
#Vector construct has letter T before autogenerated number.
$c->stash->{rest} = [ "T". $max];
}


sub _parse_list_from_json {
my $c = shift;
my $list_json = shift;
Expand Down
6 changes: 6 additions & 0 deletions mason/breeders_toolbox/add_vectors_dialogs.mas
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ $editable_stock_props_definitions
<small>Note: Use the fuzzy search to match similar names to prevent uploading of duplicate vectors. Fuzzy searching is much slower than regular search. Only a curator can disable the fuzzy search.</small>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label"> Auto generate uniquename</label><br>
<div class="col-sm-8">
<input type="checkbox" id="autogenerate_uniquename" name="autogenerate_uniquename">
</div>
</div>
</form>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions mason/search/vectors.mas
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ my $stock_type_select = simple_selectbox_html(
<table id="stock_search_results" width="100%" class="table table-hover table-striped">
<thead>
<tr>
<th>Stock Name</th>
<th>Vector Name</th>
<th>Stock Type</th>
<th>Organism</th>
<th>Synonyms</th>
Expand Down Expand Up @@ -231,7 +231,7 @@ my $stock_type_select = simple_selectbox_html(
stockprop_extra_columns_view[selected_property] = 1;
stockprop_extra_columns_view_array.push(selected_property);

var table_header_html = '<table id="stock_search_results" class="table table-hover table-striped"><thead><tr><th>Stock Name</th><th>Stock Type</th><th>Organism</th><th>Synonyms</th><th>Submitters</th>';
var table_header_html = '<table id="stock_search_results" class="table table-hover table-striped"><thead><tr><th>Vector Name</th><th>Stock Type</th><th>Organism</th><th>Synonyms</th><th>Submitters</th>';
for (var i=0; i<stockprop_extra_columns_view_array.length; i++){
table_header_html = table_header_html + '<th>' + stockprop_extra_columns_view_array[i] + '</th>';
}
Expand All @@ -253,7 +253,7 @@ my $stock_type_select = simple_selectbox_html(
var index = stockprop_extra_columns_view_array.indexOf(selected_property);
stockprop_extra_columns_view_array.splice(index, 1);

var table_header_html = '<table id="stock_search_results" class="table table-hover table-striped"><thead><tr><th>Stock Name</th><th>Stock Type</th><th>Organism</th><th>Synonyms</th><th>Submitters</th>';
var table_header_html = '<table id="stock_search_results" class="table table-hover table-striped"><thead><tr><th>Vector Name</th><th>Stock Type</th><th>Organism</th><th>Synonyms</th><th>Submitters</th>';
for (var i=0; i<stockprop_extra_columns_view_array.length; i++){
table_header_html = table_header_html + '<th>' + stockprop_extra_columns_view_array[i] + '</th>';
}
Expand Down
Loading

0 comments on commit 8df5975

Please sign in to comment.