| 1 | package Bio::ConnectDots::DotQuery::OuterCt; | ||
| 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::CtMixin; | ||
| 9 | @ISA = qw(Bio::ConnectDots::DotQuery Bio::ConnectDots::DotQuery::CtMixin); | ||
| 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 | $self->{centric} = $args->centric; | ||
| 20 | $self->{remove_subsets} = $args->remove_subsets; | ||
| 21 | return unless $class eq __PACKAGE__; # to prevent subclasses from re-running this | ||
| 22 | $self->execute if $args->execute; | ||
| 23 | } | ||
| 24 | |||
| 25 | sub db_execute { | ||
| 26 | my($self)=@_; | ||
| 27 | my $db=$self->db; | ||
| 28 | $self->throw("Cannot run query: database is not connected") unless $db->is_connected; | ||
| 29 | $self->throw("Cannot run query: database does not exist") unless $db->exists; | ||
| 30 | my $name=$self->name; | ||
| 31 | my $preview = $self->dottable->preview; | ||
| 32 | my $plimit = $self->dottable->preview_limit; | ||
| 33 | |||
| 34 | my %cds; # hash on {cs_id}{label_id} -> cd# | ||
| 35 | my(@targets,@columns,@left_joins,@where); | ||
| 36 | # we're doing a star join. use left join to combine 'points' | ||
| 37 | my $i=0; | ||
| 38 | my %out_cols; | ||
| 39 | for my $output (@{$self->outputs}) { | ||
| 40 | my($output_name,$column,$cs_id,$label_id)=$output->get(qw(output_name column cs_id label_id)); | ||
| 41 | $out_cols{$column} = 1; | ||
| 42 | my $cd="cd$i"; | ||
| 43 | push(@targets,"$cd.id AS $output_name"); | ||
| 44 | push(@columns,$output_name); | ||
| 45 | my $on= "ON ct.$column=$cd.connector_id"; | ||
| 46 | my $subselect = "(SELECT * FROM connectdot WHERE connectorset_id=$cs_id AND label_id=$label_id"; | ||
| 47 | $subselect .= " LIMIT $plimit" if $preview; | ||
| 48 | $subselect .= ") AS $cd $on"; | ||
| 49 | push(@left_joins,$subselect); | ||
| 50 | $cds{$cs_id}{$label_id} = $cd; | ||
| 51 | $i++; | ||
| 52 | } | ||
| 53 | |||
| 54 | my @tables=("(SELECT ". join(',', keys %out_cols) ." FROM ". $self->input .") AS ct"); | ||
| 55 | |||
| 56 | for my $constraint (@{$self->constraints}) { | ||
| 57 | my($column,$cs_id)=($constraint->column,$constraint->cs_id); | ||
| 58 | my $label_id = $constraint->label_ids->[0]; | ||
| 59 | next unless $cs_id && $label_id && $column; | ||
| 60 | my $cd="cd$i"; | ||
| 61 | my $on= "ON ct.$column=$cd.connector_id"; | ||
| 62 | my $subselect = "(SELECT * FROM connectdot WHERE connectorset_id=$cs_id AND label_id=$label_id"; | ||
| 63 | $subselect .= " LIMIT $plimit" if $preview; | ||
| 64 | $subselect .= ") AS $cd $on"; | ||
| 65 | push(@left_joins,$subselect); | ||
| 66 | $cds{$cs_id}{$label_id} = $cd; | ||
| 67 | $i++; | ||
| 68 | push(@where,$self->constraint_where($constraint,$cs_id,$cd)); | ||
| 69 | } | ||
| 70 | |||
| 71 | my $targets=join(', ',@targets); | ||
| 72 | my $tables=join(', ',@tables); | ||
| 73 | my $left_joins=join(' LEFT JOIN ',@left_joins); | ||
| 74 | my $where=@where? "WHERE ".join(' AND ',@where): undef; | ||
| 75 | my $sql="SELECT DISTINCT $targets FROM $tables LEFT JOIN $left_joins $where"; | ||
| 76 | $db->create_table_sql($name,$sql,\@columns); | ||
| 77 | |||
| 78 | # make it centric | ||
| 79 | $db->do_sql("DELETE FROM $name WHERE $self->{centric} IS NULL") if $self->{centric}; | ||
| 80 | |||
| 81 | # remove subsets | ||
| 82 | my @col_names; | ||
| 83 | grep push(@col_names,/.*AS (.+)/), @targets; # get col names from targets AS clause | ||
| 84 | $self->remove_subsets($db->{dbh}, $name,$self->{remove_subsets}, \@col_names) if $self->{remove_subsets}; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | 1; |