1 package SGN
::Controller
::AJAX
::Organism
;
3 use List
::MoreUtils qw
| any
|;
8 BEGIN { extends
'Catalyst::Controller::REST' }
12 default => 'application/json',
14 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
20 Public Path: /organism/autocomplete
22 Autocomplete an organism species name. Takes a single GET param,
23 C<term>, responds with a JSON array of completions for that term.
27 sub autocomplete
:Path
('/organism/autocomplete') :ActionClass
('REST') {}
29 sub autocomplete_GET
:Args
(0) {
30 my ( $self, $c ) = @_;
32 my $term = $c->req->param('term');
34 # trim and regularize whitespace
35 $term =~ s/(^\s+|\s+)$//g;
38 my $s = $c->dbic_schema('Bio::Chado::Schema','sgn_chado')
39 ->resultset('Organism::Organism');
41 my @results = $s->search({ species
=> { ilike
=> '%'.$term.'%' }},
44 ->get_column('species')
47 $c->stash->{rest
} = \
@results;
52 =head2 project_metadata
54 Usage: Action to update metadata information about sequencing
56 Desc: Stores the sequencing metadata for each accession in an
57 organismprop, using a JSON data structure for storing the
59 Side Effects: stores/updates/deletes metadata information
63 sub project_metadata
:Chained
('/organism/find_organism') :PathPart
('metadata') :Args
(0) {
67 my $action = $c->req->param('action');
69 #object id is a combination of prop_id and organism_id, separated by a "-"
70 my $organism = $c->stash->{organism
};
71 my ($prop_id, undef) = split "-", $c->req->param('object_id') || '';
72 my $organism_id = $organism->organism_id;
73 my $login_user_id = 0;
74 my $login_user_can_modify = 0;
76 $c->stash->{json
} = JSON
::Any
->new();
79 $login_user_id = $c->user()->get_object()->get_sp_person_id();
80 $login_user_can_modify = any
{ $_ =~ /curator|sequencer|submitter/i } ($c->user()->roles());
83 # 1. get all the props associated with the organism
84 # 2. if it is a view, render them all
85 # 3. if it is an edit, render them, but render the selected prop_id as an editable
86 # 4. if it is a store, store the selected prop_id, display everything as static
87 # 5. if it is a delete, delete the selected prop_id, display everthing as static
92 if (!$action) { $action = 'view'; }
93 if ($login_user_can_modify && ($action ne 'view')) {
94 if (!$login_user_id) {
95 $self->status_bad_request( $c, message
=> 'Must be logged in to edit' );
98 if ($action eq 'confirm_delete') {
99 $organism->search_related('organismprops',{ organismprop_id
=>$prop_id })->delete;
102 if ($action eq 'store') {
103 my $props = $c->req->parameters();
104 my %props = $self->project_metadata_prop_list();
105 my $store_props = {};
106 foreach my $p (keys(%props )) {
107 $store_props->{$p}=$props->{$p};
109 my $json = $c->stash->{json
}->objToJson($store_props);
111 $form = $self->metadata_form($c, $json, $prop_id, $organism_id);
113 $form->process($c->req());
115 if ($form->submitted_and_valid()) {
117 my $prop = $organism->find_related( 'organismprops', {
118 organismprop_id
=> $prop_id,
121 $self->status_bad_request(
123 'no such organismprop',
127 $prop->update({ value
=> $json });
130 $organism->create_organismprops(
131 { 'organism_sequencing_metadata' => $json },
134 definitions
=> { organism_sequencing_metadata
=> "metadata about this organism's sequencing status" },
138 $c->forward('/organism/invalidate_organism_tree_cache');
141 $self->status_bad_request( $c, message
=> 'Form is not valid' );
147 my @proplist = $self->get_organism_metadata_props( $c );
149 foreach my $p (@proplist) {
151 if (exists($p->{organismprop_id
}) && defined $prop_id && $prop_id eq $p->{organismprop_id
} && $action eq "edit") {
152 if ($login_user_can_modify) {
153 #make the form editable
154 $form = $self->metadata_form($c, $p->{json
}, $prop_id, $organism_id);
155 $html .= $form->render();
160 $html .= $self->metadata_static($c, $p->{json
});
164 if ($login_user_can_modify) {
165 # add appropriate edit and delete links
166 $html .= "<a href=\"javascript:organismObjectName.setObjectId('$p->{organismprop_id}-$organism_id'); organismObjectName.printForm('edit'); \">Edit</a> <a href=\"javascript:organismObjectName.setObjectId('$p->{organismprop_id}-$organism_id'); organismObjectName.printDeleteDialog();\">Delete</a><hr />";
171 if ($login_user_can_modify && $action eq 'new') {
172 $form = $self->metadata_form($c, undef, undef, $organism_id);
173 $html .= $form->render();
174 $html .= qq | <br
/><a href="javascript:organismObjectName.render();">Cancel</a><br
/><br /> | ;
176 elsif ($login_user_can_modify) {
177 $html .= "<br /><a href=\"javascript:organismObjectName.setObjectId('-$organism_id' ); organismObjectName.printForm('new');\">New</a>";
180 if ( $action eq 'new' || $action eq 'view' || !$action || !$login_user_can_modify) {
183 entity
=> { login_user_id
=> $login_user_id,
184 editable_form_id
=> 'organism_project_metadata_form',
185 is_owner
=> $login_user_can_modify,
190 elsif ($action eq 'store') {
191 $self->status_ok( $c, entity
=> [ 'success' ] );
196 ### get project metadata information for that organism
197 $self->status_ok( $c, entity
=> {
198 login_user_id
=> $login_user_id,
199 editable_form_id
=> 'organism_project_metadata_form',
200 is_owner
=> $login_user_can_modify,
207 my ($self, $c, $json, $prop_id, $organism_id) = @_;
211 #print STDERR "CONVERTING JSON...\n";
212 $data = $c->stash->{json
}->jsonToObj($json); }
214 #print STDERR "No JSON data provided...\n";
218 my $object_id = ($prop_id || '')."-".$organism_id;
219 my $form = HTML
::FormFu
->new(Load
(<<YAML));
222 name: organism_project_metadata_form
223 id: organism_project_metadata_form
235 my %fields = $self->project_metadata_prop_list();
237 foreach my $k (keys %fields) {
239 $form->element( { type
=>'Text', name
=>$k, label
=>$fields{$k}, value
=>$data->{$k}, size
=>30 });
246 sub metadata_static
{
251 if (!$json) { return; }
253 my %props = %{$c->{stash
}->{json
}->jsonToObj($json)};
255 my %fields = $self->project_metadata_prop_list();
257 my $static = '<table>';
259 foreach my $k (keys %fields) {
260 no warnings
'uninitialized';
261 $static .= '<tr><td>'.$fields{$k}.'</td><td> </td><td><b>'.$props{$k}.'</b></td></tr>';
264 $static .= '</table>';
269 =head2 project_metadata_prop_list()
271 defines the prop list as a hash. the key is the name of the property (stored in cvterm as a 'local' cv and referenced through 'type_id' and the value is the display text for that property.
275 sub project_metadata_prop_list
{
276 return ("genome_project_sequencing_center" => "Sequencing Center",
277 "genome_project_sequenced_accessions" => "Accession",
278 "genome_project_dates" => "Project start, end",
279 "genome_project_funding_agencies" => "Funding Agencies",
280 "genome_project_url" => "Project URL",
281 "genome_project_genbank_link" => "Genbank link",
282 "genome_project_contact_person" => "Contact (name, email)",
283 "genome_project_seed_source" => "Seed source",
288 sub get_organism_metadata_props
{
289 my ( $self, $c ) = @_;
291 my $props = $c->stash->{organism
}
292 ->search_related('organismprops',
293 { 'type.name' => 'organism_sequencing_metadata' },
294 { join => 'type', prefetch
=> 'type' },
298 { +{ organismprop_id
=> $_->organismprop_id, json
=> $_->value, name
=> $_->type->name } }
305 Public Path: /organism/verify_name
307 Verifies that a species name exists in the database. Returns false if the species name is not found.
311 sub verify_name
:Path
('/organism/verify_name') :ActionClass
('REST') {}
313 sub verify_name_GET
:Args
(0) {
314 my ( $self, $c ) = @_;
315 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
316 my $species_name = $c->req->param('species_name');
318 $organism = $schema->resultset("Organism::Organism")->find({species
=> $species_name});
320 $c->stash->{rest
} = {error
=> "Species name $species_name not found." };
324 $c->stash->{rest
} = {success
=> "1",};