Merge pull request #1890 from solgenomics/topic/UpdateBrapiSearchDBlist
[sgn.git] / db / 00003 / CreateSolcapMarkerTables.pm
blobcd33676cda9649aaa42de6efa9c87332b902925a
1 #!/usr/bin/env perl
4 =head1 NAME
6 CreateSolcapMarkerTables.pm
8 =head1 SYNOPSIS
10 mx-run ThisPackageName [options] -H hostname -D dbname -u username [-F]
12 this is a subclass of L<CXGN::Metadata::Dbpatch>
13 see the perldoc of parent class for more details.
15 =head1 DESCRIPTION
17 This is a patch for creating the necessary tables and rows to accommodate the solcap marker data. Since the SolCap markers have 2-5 primers, we cannot use pcr_experiment.primer_id (which holds only 2 primers), and need instead pcr_experiment_sequence.
19 This subclass uses L<Moose>. The parent class uses L<MooseX::Runnable>
21 =head1 AUTHOR
23 Naama Menda<nm249@cornell.edu>
25 =head1 COPYRIGHT & LICENSE
27 Copyright 2010 Boyce Thompson Institute for Plant Research
29 This program is free software; you can redistribute it and/or modify
30 it under the same terms as Perl itself.
32 =cut
35 package CreateSolcapMarkerTables;
37 use Moose;
38 extends 'CXGN::Metadata::Dbpatch';
40 use Bio::Chado::Schema;
42 sub init_patch {
43 my $self=shift;
44 my $name = __PACKAGE__;
45 print "dbpatch name is ':" . $name . "\n\n";
46 my $description = 'altering marker schema to accommodate SolCap markers';
47 my @previous_requested_patches = (); #ADD HERE
48 print "This patch requires version 1.280 of Sequence Ontology";
49 $self->name($name);
50 $self->description($description);
51 $self->prereq(\@previous_requested_patches);
55 sub patch {
56 my $self=shift;
59 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
61 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
64 print STDOUT "\nExecuting the SQL commands.\n";
66 $self->dbh->do(<<EOSQL);
67 --do your SQL here
69 ALTER TABLE sgn.pcr_experiment ADD COLUMN stock_id integer REFERENCES public.stock;
71 CREATE TABLE sgn.pcr_experiment_sequence (
72 pcr_experiment_sequence_id serial PRIMARY KEY,
73 pcr_experiment_id integer NOT NULL REFERENCES sgn.pcr_experiment ON DELETE CASCADE,
74 sequence_id integer NOT NULL REFERENCES sgn.sequence ON DELETE CASCADE,
75 type_id integer REFERENCES public.cvterm(cvterm_id)
78 --copying the pcr_experimet.primer_id columns to pcr_experiment_sequence
79 --first create a tmp table
80 create TEMP TABLE tmp_pcr_rev as SELECT pcr_experiment_id, primer_id_rev FROM sgn.pcr_experiment WHERE primer_id_rev is not null;
82 --populate the new table
83 INSERT INTO sgn.pcr_experiment_sequence (pcr_experiment_id, sequence_id ) SELECT tmp_pcr_rev.* FROM tmp_pcr_rev ;
85 --update the type_id
86 UPDATE sgn.pcr_experiment_sequence SET type_id = (SELECT cvterm_id FROM public.cvterm where name = 'reverse_primer');
88 CREATE TEMP TABLE tmp_pcr_fwd as SELECT pcr_experiment_id, primer_id_fwd FROM sgn.pcr_experiment WHERE primer_id_fwd is not null;
90 INSERT INTO sgn.pcr_experiment_sequence (pcr_experiment_id, sequence_id) SELECT tmp_pcr_fwd.* FROM tmp_pcr_fwd;
92 UPDATE sgn.pcr_experiment_sequence SET type_id = (SELECT cvterm_id FROM public.cvterm where name = 'forward_primer') WHERE type_id is null;
94 CREATE TEMP TABLE tmp_pcr_pd as SELECT pcr_experiment_id, primer_id_fwd FROM sgn.pcr_experiment WHERE primer_id_pd is not null;
96 INSERT INTO sgn.pcr_experiment_sequence (pcr_experiment_id, sequence_id) SELECT tmp_pcr_pd.* FROM tmp_pcr_pd;
98 UPDATE sgn.pcr_experiment_sequence SET type_id = (SELECT cvterm_id FROM public.cvterm where name ilike 'dcaps_primer') WHERE type_id is null;
101 --need to refactor the code that uses pcr_experiment.primer_id_fwd/rev/pd (dCAPs?). This will be done in the topic/solcap_marker branch
104 --add SNP to the trigger
106 alter table sgn.marker_experiment drop constraint marker_experiment_protocol_check;
108 alter table sgn.marker_experiment add constraint marker_experiment_protocol_check CHECK (protocol = 'AFLP'::text OR protocol = 'CAPS'::text OR protocol = 'RAPD'::text OR protocol = 'SNP'::text OR protocol = 'SSR'::text OR protocol = 'RFLP'::text OR protocol = 'PCR'::text OR protocol = 'dCAPS'::text OR protocol = 'DART'::text OR protocol = 'OPA'::text OR protocol = 'unknown'::text OR protocol = 'ASPE'::text OR protocol = 'INDEL'::text);
110 --grant permissions to web_usr
111 grant SELECT on sgn.pcr_experiment_sequence to web_usr ;
112 grant SELECT on sgn.pcr_experiment_sequence_pcr_experiment_sequence_id_seq TO web_usr ;
114 EOSQL
116 print "You're done!\n";
121 ####
122 1; #
123 ####