Debian Codegraph @

TFBS/DB/LocalTRANSFAC.pm

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # TFBS module for TFBS::DB::LocalTRANSFAC
2 #
3 # Copyright Stephen Montgomery [email protected]
4 #
5 # Contributors: Boris Lenhard, Leonardo Marino-Ramirez
6 #
7 # You may distribute this module under the same terms as perl itself
8 #
9
10 # POD
11
12 =head1 NAME
13
14 TFBS::DB::LocalTRANSFAC - interface to local transfac database
15 position frequency matrices (matrix.dat)
16
17 -------------------------------- NOTICE ----------------------------------
18 The TRANSFAC database is free for non-commercial use. For commercial use
19 the TRANSFAC databases and programs have to be licensed. Please read
20 the DISCLAIMER at http://transfac.gbf.de/TRANSFAC/disclaimer.htm.
21 -------------------------------------------------------------------------
22
23 =head1 SYNOPSIS
24
25 =over 4
26
27 =item * creating a database object by connecting to TRANSFAC data
28
29 my $db = TFBS::DB::LocalTRANSFAC->connect(-localdir => '/home/someusr');
30
31 localdir is the location of the matrix.dat TRANSFAC datafile
32
33 =item * retrieving a TFBS::Matrix::* object from the database
34
35 # retrieving a PFM by ID
36 my $pfm = $db->get_Matrix_by_ID('V$CEBPA_01','PFM');
37
38 #retrieving a PWM by TRANSFAC accession number
39 my $pwm = $db->get_Matrix_by_acc('M00116', 'PWM');
40
41 =back
42
43 =head1 DESCRIPTION
44
45 TFBS::DB::LocalTRANSFAC is a read only database interface that fetches
46 TRANSFAC matrix data from a local TRANSFAC install (matrix.dat)
47
48 =cut
49
50 package TFBS::DB::LocalTRANSFAC;
51
52 use vars qw(@ISA);
53 use strict;
54 use TFBS::DB::TRANSFAC;
55 use TFBS::Matrix::PFM;
56
57 @ISA = qw(TFBS::DB::TRANSFAC);
58
59 =head2 connect
60
61 Title : connect
62 Usage : my $db = TFBS::DB::TRANSFAC->connect(%args);
63 Function: Creates a TRANSFAC database connection object, which can be used
64 to retrieve matrices from a locally installed TRANSFAC database
65 Returns : a TFBS::DB::TRANSFAC object
66 Args : -localdir # REQUIRED: the directory of the matrix.dat TRANSFAC
67 # datafile. matrix.dat must have read access.
68 -accept_conditions # OPTIONAL: by setting this to a true
69 # value, you confirm that you
70 # have read and accepted the terms
71 # of use of TRANSFAC at
72 # http://transfac.gbf.de/TRANSFAC/disclaimer.htm;
73 # this also suppresses the annoying
74 # message that is printed to STDERR
75 # upon invoking the method
76
77 =cut
78
79 sub connect {
80 my ($caller, %args) = @_;
81 my $self = bless { 'loc' => $args{'-localdir'}}, ref $caller || $caller;
82
83 unless (defined ($args{-accept_conditions}) and $args{-accept_conditions}) {
84 print STDERR <<ENDNOTICE
85 -------------------------------- NOTICE ----------------------------------
86 The TRANSFAC database is free for non-commercial use. For commercial use
87 the TRANSFAC databases and programs have to be licensed. Please read
88 the DISCLAIMER at http://transfac.gbf.de/TRANSFAC/disclaimer.htm.
89 -----------
90 If you have read the disclaimer and accept the conditions stated therein,
91 you can suppres this notice by connecting to TRANSFAC like this:
92 my \$db = TFBS::DB::TRANSFAC->connect(-accept_conditions => 1);
93 --------------------------------------------------------------------------
94 ENDNOTICE
95 ;
96 }
97 unless (defined $args{-localdir}) {
98 $self->throw("Need directory of TRANSFAC database");
99 }
100
101 return $self;
102 }
103
104 =head2 get_Matrix_by_acc
105
106 Title : get_Matrix_by_acc
107 Usage : my $pfm = $db->get_Matrix_by_acc('V$CREB_01', 'PFM');
108 Function: fetches matrix data under the given TRANSFAC aaccession number
109 from database and returns a TFBS::Matrix::* object
110 Returns : a TFBS::Matrix::* object; the exact type of the
111 object depending on the second argument (allowed
112 values are 'PFM', 'ICM', and 'PWM'); returns undef if
113 matrix with the given ID is not found
114 Args : (Matrix_ID, Matrix_type)
115 Matrix_ID is a string; Matrix_type is one of the
116 following: 'PFM' (raw position frequency matrix),
117 'ICM' (information content matrix) or 'PWM' (position
118 weight matrix)
119 If Matrix_type is omitted, a PFM is retrieved by default.
120
121 =cut
122
123 sub get_Matrix_by_acc {
124 my ($self, $acc, $mt) = @_;
125 unless (defined $acc) {
126 $self->throw("No parameters passed to get_Matrix_by_ID.");
127 }
128
129 my $datablock = _get_Matrix_Block ( 'acc' => $acc,
130 'loc' => $self->{'loc'});
131
132 return $self->_get_Matrix_by_Block($datablock, $mt);
133 }
134
135 =head2 get_Matrix_by_ID
136
137 Title : get_Matrix_by_ID
138 Usage : my $pfm = $db->get_Matrix_by_ID('V$CREB_01', 'PFM');
139 Function: fetches matrix data under the given TRANSFAC ID from the
140 database and returns a TFBS::Matrix::* object
141 Returns : a TFBS::Matrix::* object; the exact type of the
142 object depending on the second argument (allowed
143 values are 'PFM', 'ICM', and 'PWM'); returns undef if
144 matrix with the given ID is not found
145 Args : (Matrix_ID, Matrix_type)
146 Matrix_ID is a string; Matrix_type is one of the
147 following: 'PFM' (raw position frequency matrix),
148 'ICM' (information content matrix) or 'PWM' (position
149 weight matrix)
150 If Matrix_type is omitted, a PFM is retrieved by default.
151
152 =cut
153
154 sub get_Matrix_by_ID {
155 my ($self, $ID, $mt) = @_;
156 unless (defined $ID) {
157 $self->throw("No parameters passed to get_Matrix_by_ID.");
158 }
159
160 my $datablock = _get_Matrix_Block ( 'ID' => $ID,
161 'loc' => $self->{'loc'});
162
163 return $self->_get_Matrix_by_Block($datablock, $mt);
164
165 }
166
167 sub _get_Matrix_Block {
168 my %params = @_;
169 my $loc = $params{'loc'};
170 my $acc = $params{'acc'};
171 my $ID = $params{'ID'};
172
173 $loc = $loc . "/matrix.dat";
174 open(HANDLE, $loc) || die ("File opening failed for matrix.dat: Check file permissions");
175 my @raw_data=<HANDLE>;
176
177 my @block = ();
178 my $hit = 0;
179
180 foreach my $line (@raw_data)
181 {
182 if ($line eq "//\n")
183 {
184 foreach my $lineinblock (@block)
185 {
186 if (defined $ID) {
187 if ($lineinblock eq "ID $ID\n") { $hit = 1 };
188 }
189 if (defined $acc) {
190 if ($lineinblock eq "AC $acc\n") { $hit = 1 };
191 }
192 }
193 if ($hit == 0) { @block = (); }
194 }
195 if ($hit == 0) { push @block, $line; }
196 }
197
198 close(HANDLE);
199 return \@block;
200 }
201
202
203 sub _get_Matrix_by_Block {
204 my ($self, $datablock, $mt) = @_;
205 my @datalines = @$datablock;
206 my (@As, @Cs, @Gs, @Ts, $name, $ID, $acc);
207
208 foreach my $line (@datalines)
209 {
210 if ($line =~ /NA\s+(\S+)\n/) {
211 $name = $1;
212 }
213 if ($line =~ /ID\s+(\S+)\n/) {
214 $ID = $1;
215 }
216 if ($line =~ /AC\s+(\S+)\n/) {
217 $acc = $1;
218 }
219 #if ($line =~ /\d{2}\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\n/) {
220 # change to allow for both older and newer file format
221 # contributed by Leonardo Marino-Ramirez:
222 # Updated 2003-09-05 to enable parsing of non-integer entries
223
224 if ($line =~ /^\d{2}\s+(\d+\.?\d*)\s+(\d+\.?\d*)\s+(\d+\.?\d*)\s+(\d+\.?\d*).*$/) {
225 push @As, $1;
226 push @Cs, $2;
227 push @Gs, $3;
228 push @Ts, $4;
229 }
230 }
231
232 return undef unless @As;
233
234 my $pfm = TFBS::Matrix::PFM-> new ( -ID => $ID,
235 -name => $name,
236 -tags => {acc=>$acc},
237 -matrix => [ \@As, \@Cs, \@Gs, \@Ts]
238 );
239
240 if (!defined($mt) or uc($mt) eq "PFM") {return $pfm;}
241 elsif (uc($mt) eq "ICM") {return $pfm->to_ICM;}
242 elsif (uc($mt) eq "PWM") {return $pfm->to_PWM;}
243 else { $self->throw("Unrecognized matrix format: $mt"); }
244 }
245
246 1;