create and upload fieldbook phenotypes with treatments
[sgn.git] / lib / CXGN / Onto.pm
blob2e762ba7fbee071a3c6225bcdbbeea789de551c0
1 package CXGN::Onto;
3 use Moose;
4 use Moose::Util::TypeConstraints;
5 use Data::Dumper;
6 use JSON::Any;
7 use Try::Tiny;
8 use Bio::Chado::Schema;
9 use SGN::Model::Cvterm;
11 has 'schema' => (
12 isa => 'Bio::Chado::Schema',
13 is => 'rw',
14 required => 1
17 =head2 get_terms
19 parameters: namespace
21 returns: terms in namespace
23 Side Effects: none
25 =cut
27 sub get_terms {
28 my $self = shift;
29 my $cv_id = shift;
31 my $query = "SELECT cvterm_id, (((cvterm.name::text || '|'::text) || db.name::text) || ':'::text) || dbxref.accession::text AS name
32 FROM cvterm
33 JOIN dbxref USING(dbxref_id)
34 JOIN db USING(db_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
38 GROUP BY 1,2
39 ORDER BY 2,1";
41 my $h = $self->schema->storage->dbh->prepare($query);
42 $h->execute($cv_id);
44 my @results;
45 while (my ($id, $name) = $h->fetchrow_array()) {
46 push @results, [$id, $name];
49 return @results;
52 sub get_root_nodes {
53 my $self = shift;
54 my $cv_type = shift;
56 my $query = "SELECT cv.cv_id, (((db.name::text || ':'::text) || dbxref.accession::text) || ' '::text) || cvterm.name AS name
57 FROM cv
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)
61 JOIN db USING(db_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);
68 my @results;
69 while (my ($id, $name) = $h->fetchrow_array()) {
70 push @results, [$id, $name];
73 return @results;
77 sub store_composed_term {
78 my $self = shift;
79 my $new_trait_names = shift;
81 my $schema = $self->schema();
82 my $dbh = $schema->storage->dbh;
84 my @new_terms;
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";
96 next;
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);
104 $h->execute();
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 });
120 if ($new_term){
121 print STDERR "Cvterm with name $name already exists... so components must be new\n";
122 } else {
123 $new_term= $schema->resultset("Cv::Cvterm")->create({
124 cv_id =>$cv->cv_id(),
125 name => $name,
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);
156 #$h->execute();
158 #my $refresh2 = "REFRESH MATERIALIZED VIEW trait_componentsXtraits";
159 #$h = $dbh->prepare($refresh2);
160 #$h->execute();
162 return \@new_terms;