Skip to content

Commit

Permalink
Merge pull request #2822 from pombase/issue-2677-change-gene-id-action
Browse files Browse the repository at this point in the history
Add start of --change-gene-id admin action
  • Loading branch information
kimrutherford authored Apr 16, 2024
2 parents 699fe31 + 9bd6476 commit 391e3ca
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
107 changes: 107 additions & 0 deletions lib/Canto/Track/TrackUtil.pm
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,111 @@ sub update_annotation_curators
Canto::Track::curs_map($self->config(), $track_schema, $proc);
}

=head2 change_gene_id
Usage : $util->change_gene_id($from_id, $to_id)
Function: Change a gene ID (primary_identifier) in every session.
Also change allele IDs containing the $from_id.
Args : $from_id - an existing primary_identifier
$to_id
Returns : nothing - dies on failures
=cut

sub change_gene_id
{
my $self = shift;

my $from_id = shift;
my $to_id = shift;

my $gene_lookup = Canto::Track::get_adaptor($self->config(), 'gene');

my $from_id_lookup_result = $gene_lookup->lookup([$from_id]);

if (@{$from_id_lookup_result->{found}} == 0) {
die qq|no gene found in the database for "from" ID $from_id\n|;
}

if (@{$from_id_lookup_result->{found}} > 1) {
die qq|more than one result for $from_id\n|;
}

my $to_id_lookup_result = $gene_lookup->lookup([$to_id]);

if (@{$to_id_lookup_result->{found}} == 0) {
die qq|no gene found in the database for "to" ID $to_id\n|;
}

if (@{$to_id_lookup_result->{found}} > 1) {
die qq|more than one result for $to_id\n|;
}

my $old_name = $from_id_lookup_result->{found}->[0]->{primary_name};
my $new_name = $to_id_lookup_result->{found}->[0]->{primary_name};

my $track_schema = $self->schema();

my $proc = sub {
my $curs = shift;
my $cursdb = shift;

my $gene_rs = $cursdb->resultset('Gene');

while (defined (my $gene = $gene_rs->next())) {
if ($gene->primary_identifier() eq $from_id) {
print $curs->curs_key(), "\n";
$gene->primary_identifier($to_id);
$gene->update();
}
}

my $allele_rs = $cursdb->resultset('Allele');

while (defined (my $allele = $allele_rs->next())) {
my $primary_identifier = $allele->primary_identifier();
my $allele_name = $allele->name();
if ($primary_identifier =~ /^$from_id:/) {
$primary_identifier =~ s/^$from_id:/$to_id:/;
$allele->primary_identifier($primary_identifier);
$allele_name =~ s/$old_name/$new_name/;
$allele->name($allele_name);
$allele->update();
}
}

my $annotation_rs = $cursdb->resultset('Annotation');

while (defined (my $annotation = $annotation_rs->next())) {
my $data = $annotation->data();
my $changed = 0;

my $extension = $data->{extension};

if (defined $extension) {
map {
my $orPart = $_;
map {
my $andPart = $_;
if ($andPart->{rangeType} && $andPart->{rangeType} eq 'Gene') {
if ($andPart->{rangeValue} eq $from_id) {
$andPart->{rangeValue} = $to_id;
$andPart->{rangeDisplayName} = $new_name;
$changed = 1;
}
}
} @$orPart;
} @$extension;
}

if ($changed) {
$annotation->data($data);
$annotation->update();
}
}
};

Canto::Track::curs_map($self->config(), $track_schema, $proc);
}

1;
13 changes: 13 additions & 0 deletions script/canto_admin.pl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ BEGIN
my $change_taxonid = undef;
my $delete_unused_strains = undef;
my $update_annotation_curators = undef;
my $change_gene_id = undef;

my $dry_run = 0;
my $do_help = 0;
Expand All @@ -49,6 +50,7 @@ BEGIN
"change-taxonid" => \$change_taxonid,
"delete-unused-strains" => \$delete_unused_strains,
"update-annotation-curators" => \$update_annotation_curators,
"change-gene-id" => \$change_gene_id,
"dry-run|d" => \$dry_run,
"help|h" => \$do_help);

Expand Down Expand Up @@ -84,6 +86,8 @@ sub usage
Set the curator_orcid field of the annotations if available in the
person table
$0 --change-gene-id <from_id> <to_id>
Change <from_id> to <to_id> for all genes and alleles in every session
|;
}

Expand Down Expand Up @@ -191,6 +195,15 @@ sub usage
warn "failing to update annotation curator field: $_\n";
};
}

if (defined $change_gene_id) {
my $from_id = shift @ARGV;
my $to_id = shift @ARGV;

$util->change_gene_id($from_id, $to_id);

$exit_flag = 0;
}
};

if (defined $refresh_gene_cache) {
Expand Down

0 comments on commit 391e3ca

Please sign in to comment.