4 use Moose
::Util
::TypeConstraints
;
8 use Bio
::Chado
::Schema
;
9 use SGN
::Model
::Cvterm
;
12 isa
=> 'Bio::Chado::Schema',
21 returns: terms in namespace
31 my $query = "SELECT cvterm_id, (((cvterm.name::text || '|'::text) || db.name::text) || ':'::text) || dbxref.accession::text AS name
33 JOIN dbxref USING(dbxref_id)
35 LEFT JOIN cvterm_relationship is_subject ON cvterm.cvterm_id = is_subject.subject_id
36 LEFT JOIN cvterm_relationship is_object ON cvterm.cvterm_id = is_object.object_id
37 WHERE cv_id = ? AND is_object.object_id IS NULL AND is_subject.subject_id IS NOT NULL
41 my $h = $self->schema->storage->dbh->prepare($query);
45 while (my ($id, $name) = $h->fetchrow_array()) {
46 push @results, [$id, $name];
56 my $query = "SELECT cv.cv_id, (((db.name::text || ':'::text) || dbxref.accession::text) || ' '::text) || cvterm.name AS name
58 JOIN cvprop ON(cv.cv_id = cvprop.cv_id AND cvprop.type_id IN (SELECT cvterm_id from cvterm where cvterm.name = ?))
59 JOIN cvterm on(cvprop.cv_id = cvterm.cv_id)
60 JOIN dbxref USING(dbxref_id)
62 LEFT JOIN cvterm_relationship ON(cvterm.cvterm_id=cvterm_relationship.subject_id)
63 WHERE cvterm_relationship.subject_id IS NULL AND cvterm.is_obsolete= 0 AND cvterm.is_relationshiptype = 0";
65 my $h = $self->schema->storage->dbh->prepare($query);
66 $h->execute($cv_type);
69 while (my ($id, $name) = $h->fetchrow_array()) {
70 push @results, [$id, $name];
77 sub store_composed_term
{
79 my $new_trait_names = shift;
81 my $schema = $self->schema();
82 my $dbh = $schema->storage->dbh;
85 foreach my $name (sort keys %$new_trait_names){
86 my $ids = $new_trait_names->{$name};
87 my @component_ids = split ',', $ids;
89 if (scalar(@component_ids)<2){
90 die "Should not save postcomposed term with less than 2 components\n";
93 my $existing_trait_id = SGN
::Model
::Cvterm
->get_trait_from_exact_components($schema, \
@component_ids);
94 if ($existing_trait_id) {
95 print STDERR
"Skipping: This trait already exists $name with the following component_ids".Dumper
(\
@component_ids)."\n";
99 my $db = $schema->resultset("General::Db")->find_or_create({ name
=> 'COMP' });
100 my $cv= $schema->resultset('Cv::Cv')->find_or_create( { name
=> 'composed_trait' });
102 my $accession_query = "SELECT nextval('composed_trait_ids')";
103 my $h = $dbh->prepare($accession_query);
105 my $accession = $h->fetchrow_array();
107 my $new_term_dbxref = $schema->resultset("General::Dbxref")->create(
108 { db_id
=> $db->get_column('db_id'),
109 accession
=> sprintf("%07d",$accession)
112 my $parent_term= $schema->resultset("Cv::Cvterm")->find(
113 { cv_id
=>$cv->cv_id(),
114 name
=> 'Composed traits',
117 #print STDERR "Parent cvterm_id = " . $parent_term->cvterm_id();
119 my $new_term = $schema->resultset('Cv::Cvterm')->find({ name
=>$name });
121 print STDERR
"Cvterm with name $name already exists... so components must be new\n";
123 $new_term= $schema->resultset("Cv::Cvterm")->create({
124 cv_id
=>$cv->cv_id(),
126 dbxref_id
=> $new_term_dbxref-> dbxref_id
()
131 #print STDERR "New term cvterm_id = " . $new_term->cvterm_id();
133 my $contains_relationship = $schema->resultset("Cv::Cvterm")->find({ name
=> 'contains' });
134 my $variable_relationship = $schema->resultset("Cv::Cvterm")->find({ name
=> 'VARIABLE_OF' });
136 my $variable_rel = $schema->resultset('Cv::CvtermRelationship')->create({
137 subject_id
=> $new_term->cvterm_id(),
138 object_id
=> $parent_term->cvterm_id(),
139 type_id
=> $variable_relationship->cvterm_id()
142 foreach my $component_id (@component_ids) {
143 my $contains_rel = $schema->resultset('Cv::CvtermRelationship')->create({
144 subject_id
=> $component_id,
145 object_id
=> $new_term->cvterm_id(),
146 type_id
=> $contains_relationship->cvterm_id()
150 push @new_terms, [$new_term->cvterm_id, $new_term->name().'|COMP:'.sprintf("%07d",$accession)];
153 #Takes long on cassavabase.. instead the materialized view is refreshed automatically in a background ajax process.
154 #my $refresh1 = "REFRESH MATERIALIZED VIEW traits";
155 #my $h = $dbh->prepare($refresh1);
158 #my $refresh2 = "REFRESH MATERIALIZED VIEW trait_componentsXtraits";
159 #$h = $dbh->prepare($refresh2);