From 690568247820926db0f19a267199ec1fc29f7f80 Mon Sep 17 00:00:00 2001 From: Kim Rutherford Date: Mon, 25 Feb 2019 12:17:40 +1300 Subject: [PATCH] Add allele loading for JSON importing Refs pombase/canto#1779 --- lib/Canto/Curs/AlleleManager.pm | 35 +++++++++++++++++++++ lib/Canto/Track/LoadUtil.pm | 47 ++++++++++++++++++++++++++--- t/60_load_util.t | 22 ++++++++++++-- t/data/sessions_from_json_test.json | 2 +- 4 files changed, 99 insertions(+), 7 deletions(-) diff --git a/lib/Canto/Curs/AlleleManager.pm b/lib/Canto/Curs/AlleleManager.pm index ac7adcbb3..d303229b5 100644 --- a/lib/Canto/Curs/AlleleManager.pm +++ b/lib/Canto/Curs/AlleleManager.pm @@ -207,4 +207,39 @@ sub allele_from_json return $schema->create_with_type('Allele', \%create_args); } +=head2 create_simple_allele + + Usage : my $allele = $allele_manager->create_simple_allele(...); + Function: Create an Allele object given some allele properties and a gene + Args : $primary_identifier - the primary key in the database + $allele_type + $name + $description + $expression + $gene - a Gene object + Returns : the new Allele + +=cut + +sub create_simple_allele +{ + my $self = shift; + + my $primary_identifier = shift; + my $allele_type = shift; + my $name = shift; + my $description = shift; + my $gene = shift; + + my %create_args = ( + primary_identifier => $primary_identifier, + type => $allele_type, + name => $name || undef, + description => $description || undef, + gene => $gene->gene_id(), + ); + + return $self->curs_schema()->create_with_type('Allele', \%create_args); +} + 1; diff --git a/lib/Canto/Track/LoadUtil.pm b/lib/Canto/Track/LoadUtil.pm index 0ffc01b2c..419bd18f6 100644 --- a/lib/Canto/Track/LoadUtil.pm +++ b/lib/Canto/Track/LoadUtil.pm @@ -50,6 +50,8 @@ use feature qw(state); use Package::Alias PubmedUtil => 'Canto::Track::PubmedUtil'; use Canto::Curs::GeneManager; +use Canto::Curs::AlleleManager; +use Canto::Curs::GenotypeManager; use Canto::Track; @@ -858,8 +860,14 @@ sub create_sessions_from_json my ($curs, $cursdb) = Canto::Track::create_curs($config, $self->schema(), $pub, $connect_options); - my $gene_manager = Canto::Curs::GeneManager->new(config => $config, - curs_schema => $cursdb); + push @results, [$curs, $cursdb]; + + my $allele_manager = + Canto::Curs::AlleleManager->new(config => $config, curs_schema => $cursdb); + my $genotype_manager = + Canto::Curs::GenotypeManager->new(config => $config, curs_schema => $cursdb); + my $gene_manager = + Canto::Curs::GeneManager->new(config => $config, curs_schema => $cursdb); $curator_manager->set_curator($curs->curs_key(), $curator_email_address); @@ -871,12 +879,43 @@ sub create_sessions_from_json print "created session: ", $curs->curs_key(), " pub: ", $pub->uniquename(), " for: $curator_email_address\n"; + my %db_genes = (); + for my $gene_uniquename (@{$session_data->{genes}}) { my $lookup_result = $gene_lookup->lookup([$gene_uniquename]); - $gene_manager->create_genes_from_lookup($lookup_result); + my %result = $gene_manager->create_genes_from_lookup($lookup_result); + while (my ($result_uniquename, $result_gene) = each %result) { + $db_genes{$result_uniquename} = $result_gene; + } } - } + my $alleles = $session_data->{alleles}; + + if ($alleles) { + while (my ($allele_uniquename, $allele_details) = each %$alleles) { + my $allele_gene_uniquename = $allele_details->{gene}; + if (!defined $allele_gene_uniquename) { + die qq|no "gene" field in details for $allele_uniquename in $pub_uniquename|; + } + my $gene = $db_genes{$allele_gene_uniquename}; + if (!defined $gene) { + die qq|no gene the session for $allele_uniquename in $pub_uniquename|; + } + + my $type = $allele_details->{allele_type} || 'other'; + my $name = $allele_details->{allele_name} || undef; + my $description = $allele_details->{allele_description} || undef; + my @args = ($allele_uniquename, $type, $name, $description, $gene); + + my @alleles_for_make_genotype = + $allele_manager->create_simple_allele(@args); + + $genotype_manager->make_genotype(undef, undef, \@alleles_for_make_genotype, + $gene->organism()->taxonid(), + "genotype-$allele_uniquename"); + } + } + } return @results; } diff --git a/t/60_load_util.t b/t/60_load_util.t index 5cb01a220..16b4a04b1 100644 --- a/t/60_load_util.t +++ b/t/60_load_util.t @@ -1,6 +1,6 @@ use strict; use warnings; -use Test::More tests => 4; +use Test::More tests => 10; use Test::Exception; use Canto::TestUtil; @@ -31,6 +31,24 @@ my $gene_load = Canto::Track::GeneLoad->new(organism => $fly, schema => $schema) $gene_load->create_gene('FBgn0004107', 'Dmel\Cdk2', [], 'Cyclin-dependent kinase 2'); $gene_load->create_gene('FBgn0016131', 'Dmel\Cdk4', [], 'Cyclin-dependent kinase 4'); my $test_json_file = $test_util->root_dir() . '/t/data/sessions_from_json_test.json'; -my ($curs, $cursdb, $person) = +my @created_sessions = $load_util->create_sessions_from_json($config, $test_json_file, 'test.user@3926fef56bb23eb871ee91dc2e3fdd7c46ef1385.org'); + +is (@created_sessions, 1); + +my ($created_curs, $created_cursdb) = @{$created_sessions[0]}; + +my $FBal0119310_allele = + $created_cursdb->resultset('Allele')->find({ primary_identifier => "FBal0119310" }); + +ok (defined $FBal0119310_allele); + +is($FBal0119310_allele->type(), 'other'); +is($FBal0119310_allele->name(), 'Dmel\Cdk2_UAS.Tag:MYC'); +is($FBal0119310_allele->description(), 'description of FBal0119310'); + +my $genotype_FBal0119310 = + $created_cursdb->resultset('Genotype')->find({ identifier => "genotype-FBal0119310" }); + +is(($genotype_FBal0119310->alleles()->all())[0]->name(), 'Dmel\Cdk2_UAS.Tag:MYC'); diff --git a/t/data/sessions_from_json_test.json b/t/data/sessions_from_json_test.json index 6597fd728..8fa17ffec 100644 --- a/t/data/sessions_from_json_test.json +++ b/t/data/sessions_from_json_test.json @@ -8,7 +8,7 @@ "gene": "FBgn0004107", "allele_type": "other", "allele_name": "Dmel\\Cdk2_UAS.Tag:MYC", - "allele_description": null + "allele_description": "description of FBal0119310" }, "FBal0064432": { "gene": "FBgn0016131",