a class to extract sequences from the genome
[cxgn-corelibs.git] / lib / CXGN / Tools / RestrictionEnzyme.pm
blobafa7319bca2b2909de5b335241afeeba194b6e87
1 package CXGN::Tools::RestrictionEnzyme;
2 use strict;
3 use CXGN::DB::Connection;
5 =head1 NAME
7 CXGN::Tools::RestrictionEnzyme
9 =head1 SYNOPSIS
11 Abstract representation for pulling information about restriction enzymes
12 from SGN database. Will work in concert with CXGN::Tools::Sequence
13 to find restriction sites.
15 =head1 AUTHOR
17 C. Carpita <ccarpita@gmail.com>
19 =cut
21 #GLOBAL CLASS VARIABLES
22 our $DBH;
24 #Prepared statement references:
25 our ($ALL_ENZ_NAME_Q);
26 our ($VALID_ENZ_NAME_Q, $ENZ_SEQ_Q);
28 our %VALID_NAMES;
30 =head1 CLASS METHODS
32 =cut
34 sub setDBH {
35 my $class = shift;
36 my $dbh = shift;
37 die "Must call this function on the class and send a DBH argument\n" unless ref($dbh);
38 $DBH = $dbh;
39 $class->prepare();
42 sub prepare {
43 my $class = shift;
44 if($DBH->can("add_search_path")){
45 $DBH->add_search_path("sgn");
47 else {
48 $DBH->do("set search_path=sgn");
51 $ALL_ENZ_NAME_Q = $DBH->prepare("
52 SELECT DISTINCT enzyme_name
53 FROM enzymes
54 WHERE enzyme_id
55 IN (
56 SELECT DISTINCT enzyme_id
57 FROM enzyme_restriction_sites
58 WHERE restriction_site IS NOT NULL
60 ORDER BY enzyme_name
61 ");
63 $VALID_ENZ_NAME_Q = $DBH->prepare("
64 SELECT enzyme_name
65 FROM enzymes
66 WHERE enzyme_id
67 IN (
68 SELECT DISTINCT enzyme_id
69 FROM enzyme_restriction_sites
70 WHERE restriction_site IS NOT NULL
72 AND enzyme_name = ?
73 ORDER BY enzyme_name
74 ");
76 $ENZ_SEQ_Q = $DBH->prepare("
77 SELECT restriction_site
78 FROM enzyme_restriction_sites
79 WHERE enzyme_id =
80 ( SELECT enzyme_id
81 FROM enzymes
82 WHERE enzyme_name = ?
84 AND restriction_site IS NOT NULL
85 ");
88 sub preload_valid_names {
89 my $class = shift;
90 $ALL_ENZ_NAME_Q->execute();
91 while(my $row = $ALL_ENZ_NAME_Q->fetchrow_hashref){
92 my $enzyme_name = $row->{enzyme_name};
93 $VALID_NAMES{$enzyme_name} = 1;
97 sub createDBH {
98 my $class = shift;
99 print STDERR "$class is creating SGN database handle...\n";
100 my $dbh = CXGN::DB::Connection->new("sgn");
101 $class->setDBH($dbh);
104 =head2 all_enzymes
106 Ex: my @enzymes = CXGN::Tools::Enzyme::all_enzymes();
108 Returns an array of all enzyme objects, created from
109 the enzyme tables in the database.
111 =cut
113 sub all_enzymes {
114 my $class = shift;
115 $class->createDBH() unless ref($DBH);
116 $class->preload_valid_names() unless keys(%VALID_NAMES);
117 my @enzymes = ();
118 foreach my $name (keys %VALID_NAMES){
119 my $enzyme = $class->new($name);
120 push(@enzymes, $enzyme);
122 return @enzymes;
126 =head1 CONSTRUCTOR
128 Usage: my $enzyme = CXGN::Tools::Enzyme->new("hindIII");
129 Auto-fetches the matching sequences from the database,
130 dies if the name is not valid. Use eval{} tags to avoid
131 death.
133 =cut
135 sub new {
136 my $class = shift;
137 my $self = bless {}, $class;
139 my $name = shift;
141 #You can provide a different DBH to use instead of the
142 #class global, which we use to prepare queries efficiently
143 my $dbh = shift;
145 die "No enzyme name provided\n" unless $name;
146 $self->name($name);
148 if(ref($dbh)){
149 $self->setDBH($dbh);
151 else {
152 $class->createDBH() unless ref($DBH);
155 $self->{match_seqs} = [];
156 $self->check_name_valid();
157 $self->fetch_match_seqs();
159 return $self;
162 =head1 INSTANCE METHODS
164 =cut
166 sub name {
167 my $self = shift;
168 my $name = shift;
169 return $self->{name} unless $name;
170 $self->{name} = $name;
173 sub check_name_valid {
174 my $self = shift;
175 my $name = $self->name();
176 __PACKAGE__->preload_valid_names() unless defined %VALID_NAMES;
177 die "Name of enzyme ($name) not valid, or no restriction site identified\n" unless $VALID_NAMES{$name};
180 sub fetch_match_seqs {
181 my $self = shift;
182 $ENZ_SEQ_Q->execute($self->name());
183 while(my ($seq) = $ENZ_SEQ_Q->fetchrow_array){
184 push(@{$self->{match_seqs}}, $seq);