on second though, remove post-only restriction on organism tree image cache flushing...
[sgn.git] / cgi-bin / phenome / associate_ontology_term.pl
blobab1b26389e08849f02ab99e14517174c173a3274
1 use strict;
2 use warnings;
4 use CXGN::Scrap::AjaxPage;
5 use CXGN::DB::Connection;
6 use CXGN::Login;
7 use CXGN::People::Person;
8 use CXGN::Contact;
9 use CXGN::Feed;
11 use CXGN::Phenome::LocusDbxref;
12 use CXGN::Phenome::Locus::LocusDbxrefEvidence;
13 use CXGN::Phenome::Individual::IndividualDbxref;
14 use CXGN::Phenome::Individual::IndividualDbxrefEvidence;
16 use JSON;
19 my $dbh = CXGN::DB::Connection->new();
21 my($login_person_id,$login_user_type)=CXGN::Login->new($dbh)->verify_session();
23 if ($login_user_type eq 'curator' || $login_user_type eq 'submitter' || $login_user_type eq 'sequencer') {
24 my $doc = CXGN::Scrap::AjaxPage->new();
25 $doc->send_http_header();
27 my %error = ();
28 my $json = JSON->new();
31 my ($type, $object_id, $dbxref_id, $relationship_id, $evidence_code_id, $evidence_description_id, $evidence_with_id, $reference_id ) = $doc->get_encoded_arguments("type", "object_id", "dbxref_id", "relationship_id", "evidence_code_id", "evidence_description_id", "evidence_with_id", "reference_id");
33 if (!$evidence_with_id) {$evidence_with_id = undef};
34 if (!$evidence_description_id) {$evidence_description_id = undef};
35 if (!$reference_id) {$reference_id = CXGN::Chado::Publication::get_curator_ref($dbh)};
37 my $link;
38 eval {
39 #print STDERR "trying to store a new $type _dbxref ! \n\n";
40 my ($object_dbxref, $object_dbxref_id, $object_dbxref_evidence);
41 my @evidences;
42 if ($type eq 'locus') {
43 $object_dbxref_id= CXGN::Phenome::LocusDbxref::locus_dbxref_exists($dbh,$object_id, $dbxref_id);
44 $object_dbxref=CXGN::Phenome::LocusDbxref->new($dbh, $object_dbxref_id);
45 $object_dbxref->set_locus_id($object_id);
47 $object_dbxref_evidence= CXGN::Phenome::Locus::LocusDbxrefEvidence->new($dbh); ##
48 $link= "http://www.sgn.cornell.edu/phenome/locus_display.pl?locus_id=$object_id";
50 }elsif ($type eq 'individual') {
51 $object_dbxref_id= CXGN::Phenome::Individual::IndividualDbxref::individual_dbxref_exists($dbh,$object_id, $dbxref_id);
52 $object_dbxref=CXGN::Phenome::Individual::IndividualDbxref->new($dbh, $object_dbxref_id);
53 $object_dbxref->set_individual_id($object_id);
54 $object_dbxref_evidence= CXGN::Phenome::Individual::IndividualDbxrefEvidence->new($dbh);
57 $link= "http://www.sgn.cornell.edu/phenome/individual.pl?individual_id=$object_id";
60 $object_dbxref->set_dbxref_id($dbxref_id);
61 $object_dbxref->set_sp_person_id($login_person_id);
63 #this store should insert a new locus_dbxref if !$locus_dbxref_id
64 #update obsolete to 'f' if $locus_dbxref_id and obsolete ='t'
65 #do nothing if $locus_dbxref_id and obsolete = 'f'
66 my $obsolete = $object_dbxref->get_obsolete();
68 #print STDERR "associate_ontology.pl is about to store a new $type _dbxref...\n";
70 #if the dbxref exists this should just return the database id to be used for storing a dbxref_evidence
71 $object_dbxref_id=$object_dbxref->store();
72 print STDERR "object_dbxref_id = $object_dbxref_id ! \n";
73 if ($type eq 'locus') { $object_dbxref_evidence->set_object_dbxref_id($object_dbxref_id); }
74 elsif ($type eq 'individual') { $object_dbxref_evidence->set_individual_dbxref_id($object_dbxref_id); }
76 $object_dbxref_evidence->set_relationship_type_id($relationship_id);
77 $object_dbxref_evidence->set_evidence_code_id($evidence_code_id);
78 $object_dbxref_evidence->set_evidence_description_id($evidence_description_id);
79 $object_dbxref_evidence->set_evidence_with($evidence_with_id);
80 $object_dbxref_evidence->set_reference_id($reference_id);
81 $object_dbxref_evidence->set_sp_person_id($login_person_id);
83 #$object_dbxref_evidence->set_updated_by($login_person_id); # evidences are now handled directly.
84 #see update_ontology_evidence.pl
86 #if the evidence code already exists for this annotation, do not store
87 #if ($object_dbxref_evidence->evidence_exists() ) {
88 #return here some javascript error code...
89 # $error{"error"} = "associate_ontology_term.pl failed. Evidence code already exists in db...(relationship_id = $relationship_id, evidence_code_id = $evidence_code_id, $type _dbxref_id = $object_dbxref_id \n";
91 #LocusDbxrefEvidence->store() should unobsolete if the evidence exists with obsolete= 't'
92 my $object_dbxref_evidence_id= $object_dbxref_evidence->store() ;
95 if ($@) {
96 $error{"error"} = 'Failed associating ontology term! An email message has been sent to the SGN development team. We are working on fixing this problem.';
97 CXGN::Contact::send_email('associate_ontology_term.pl died',"$@ \n params: relationship_id=$relationship_id\n evidence_code_id= $evidence_code_id\n evidence_description_id =$evidence_description_id\nevidence_with_id=$evidence_with_id \n reference_id=$reference_id\n sp_person_id=$login_person_id\n type=$type\n object_id= $object_id\n dbxref_id=$dbxref_id", 'sgn-bugs@sgn.cornell.edu');
98 warn "associating ontology term failed! $@";
100 else {
102 my $subject="[New ontology term loaded] $type $object_id";
103 my $person= CXGN::People::Person->new($dbh, $login_person_id);
104 my $user=$person->get_first_name()." ".$person->get_last_name();
105 my $user_link = qq |http://www.sgn.cornell.edu/solpeople/personal-info.pl?sp_person_id=$login_person_id|;
107 my $fdbk_body="$user ($user_link has submitted a new ontology term for $type $object_id \n
108 $link";
109 CXGN::Contact::send_email($subject,$fdbk_body, 'sgn-db-curation@sgn.cornell.edu');
110 CXGN::Feed::update_feed($subject,$fdbk_body);
112 my $jobj = $json->encode(\%error);
113 print $jobj;