| 1 | package Bio::ConnectDots::DotQuery::OuterCs; | ||
| 2 | use vars qw(@ISA @AUTO_ATTRIBUTES @OTHER_ATTRIBUTES %SYNONYMS %DEFAULTS); | ||
| 3 | use strict; | ||
| 4 | use DBI; | ||
| 5 | use Class::AutoClass; | ||
| 6 | use Bio::ConnectDots::Util; | ||
| 7 | use Bio::ConnectDots::DotQuery; | ||
| 8 | use Bio::ConnectDots::DotQuery::CsMixin; | ||
| 9 | @ISA = qw(Bio::ConnectDots::DotQuery Bio::ConnectDots::DotQuery::CsMixin); | ||
| 10 | |||
| 11 | @AUTO_ATTRIBUTES=qw(); | ||
| 12 | %SYNONYMS=(); | ||
| 13 | @OTHER_ATTRIBUTES=qw(); | ||
| 14 | %DEFAULTS=(); | ||
| 15 | Class::AutoClass::declare(__PACKAGE__); | ||
| 16 | |||
| 17 | sub _init_self { | ||
| 18 | my($self,$class,$args)=@_; | ||
| 19 | return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this | ||
| 20 | $self->execute if $args->execute; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub db_execute { | ||
| 24 | my($self)=@_; | ||
| 25 | my $db=$self->db; | ||
| 26 | $self->throw("Cannot run query: database is not connected") unless $db->is_connected; | ||
| 27 | $self->throw("Cannot run query: database does not exist") unless $db->exists; | ||
| 28 | my $name=$self->name; | ||
| 29 | my $cs_id=$self->cs_id; | ||
| 30 | my $preview = $self->dottable->preview; | ||
| 31 | my $plimit = $self->dottable->preview_limit; | ||
| 32 | |||
| 33 | my(@targets,@columns,@fo_joins); | ||
| 34 | my @where; | ||
| 35 | my %cslabel2cd; | ||
| 36 | my $i=0; | ||
| 37 | # use full outer join for star join | ||
| 38 | my $firstcd = "cd"; | ||
| 39 | my $subselect = "(SELECT * FROM connectdot WHERE connectorset_id=$cs_id"; | ||
| 40 | $subselect .= " LIMIT $plimit" if $preview; | ||
| 41 | $subselect .= ") AS $firstcd"; | ||
| 42 | push(@fo_joins,$subselect); # center of star | ||
| 43 | for my $output (@{$self->outputs}) { | ||
| 44 | my($output_name,$label_id)=($output->output_name,$output->label_id); | ||
| 45 | my $cd="cd$i"; | ||
| 46 | push(@targets,"$cd.id AS $output_name"); | ||
| 47 | push(@columns,$output_name); | ||
| 48 | my $on= "ON $firstcd.connector_id=$cd.connector_id"; | ||
| 49 | my $subselect = "(SELECT * FROM connectdot WHERE connectorset_id=$cs_id AND label_id=$label_id"; | ||
| 50 | $subselect .= " LIMIT $plimit" if $preview; | ||
| 51 | $subselect .= ") AS $cd $on"; | ||
| 52 | push(@fo_joins,$subselect); | ||
| 53 | $cslabel2cd{$cs_id}->{$label_id} = $cd; | ||
| 54 | $i++; | ||
| 55 | } | ||
| 56 | |||
| 57 | # add constraints | ||
| 58 | for my $constraint (@{$self->constraints}) { | ||
| 59 | foreach my $label_id (@{ $constraint->label_ids }) { | ||
| 60 | my $this_cd = $cslabel2cd{$cs_id}->{$label_id}; | ||
| 61 | push(@where,$self->constraint_where($constraint,$cs_id,$this_cd)); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | my $targets=join(', ',@targets); | ||
| 67 | my $fo_joins=join(' FULL OUTER JOIN ',@fo_joins); | ||
| 68 | my $where=join(' AND ',@where); | ||
| 69 | my $sql="SELECT DISTINCT $targets FROM $fo_joins"; | ||
| 70 | $sql .=" WHERE $where" if $where; | ||
| 71 | $db->create_table_sql($name,$sql,\@columns); | ||
| 72 | |||
| 73 | # make it centric | ||
| 74 | $db->do_sql("DELETE FROM $name WHERE $self->{centric} IS NULL") if $self->{centric}; | ||
| 75 | |||
| 76 | # remove subsets | ||
| 77 | my @col_names; | ||
| 78 | grep push(@col_names,/.*AS (.+)/), @targets; # get col names from targets AS clause | ||
| 79 | $self->remove_subsets($db->{dbh}, $name,$self->{remove_subsets}, \@col_names) if $self->{remove_subsets}; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | 1; |