Merge pull request #2505 from solgenomics/topic/brapi_authentication
[sgn.git] / db / 00009 / ApplyPopulationTypes.pm
blob07866e1b894d0549d634dd6f436e1dd5fe287583
2 package ApplyPopulationTypes;
4 use Moose;
5 extends 'CXGN::Metadata::Dbpatch';
7 use Bio::Chado::Schema;
9 sub init_patch {
11 my $self=shift;
12 my $name = __PACKAGE__;
13 print "dbpatch name is ':" . $name . "\n\n";
14 my $description = 'Adding explicit population types to stocks of type population';
15 my @previous_requested_patches = ('LoadPhenomeInStock'); #ADD HERE
16 $self->name($name);
17 $self->description($description);
18 $self->prereq(\@previous_requested_patches);
21 sub patch {
22 my $self=shift;
24 print STDOUT "Executing the patch:\n " . $self->name . ".\n\nDescription:\n ". $self->description . ".\n\nExecuted by:\n " . $self->username . " .";
26 print STDOUT "\nChecking if this db_patch was executed before or if previous db_patches have been executed.\n";
28 print STDOUT "\nExecuting the SQL commands.\n";
30 my $schema = Bio::Chado::Schema->connect( sub { $self->dbh->clone } );
32 my $coderef = sub {
33 my $cvterm_rs = $schema->resultset('Cv::Cvterm');
34 my $f2_cvterm = $cvterm_rs->create_with( {
35 name => 'f2 population',
36 cv => 'stock type', }
38 my $bc_cvterm = $cvterm_rs->create_with( {
39 name => 'backcross population',
40 cv => 'stock type', }
42 my $mutant_cvterm = $cvterm_rs->create_with( {
43 name => 'mutant population',
44 cv => 'stock type', }
46 my $population_stocks = $schema->resultset('Cv::Cvterm')->search( { 'me.name' => 'population' } )->
47 search_related('stocks');
48 while (my $population = $population_stocks->next) {
49 my $population_name = $population->name;
50 print "Looking at population $population_name..\n";
51 if ( grep { /F2/i } ($population_name) ) {
52 print "updating type to " . $f2_cvterm->name . "\n";
53 $population->update( { type_id=>$f2_cvterm->cvterm_id } );
55 if ( grep { /Backcross/i } ($population_name) ) {
56 print "updating type to " . $bc_cvterm->name . "\n";
57 $population->update( { type_id=>$bc_cvterm->cvterm_id } );
59 if ( grep { /mutant/i } ($population_name) ) {
60 print "updating type to " . $mutant_cvterm->name . "\n";
61 $population->update( { type_id=>$mutant_cvterm->cvterm_id } );
64 if ($self->trial) {
65 print "Trial mode! Rolling back transaction\n\n";
66 $schema->txn_rollback;
68 return 1;
71 try {
72 $schema->txn_do($coderef);
73 print "Data committed! \n";
74 } catch {
75 die "Load failed! " . $_ . "\n" ;
78 print "You're done!\n";
82 ####
83 1; #
84 ####