script/gmod_chado_fts_prep.pl untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 157 CC 6 SCAR 22.3
CC
6
Cov
0%
create_searchable_columns
20.0
validate_prereqs
12.0
create_search_triggers
2.0
CRAP 9.3
1 #!/usr/local/bin/perl
2 use strict;
3 use warnings;
4  
5 use Bio::GMOD::Config;
6 use Bio::GMOD::DB::Config;
7 use Getopt::Long;
8  
9 =pod
10  
11 =head1 NAME
12  
13 gmod_chado_fts_prep.pl - prepares a Chado schema to use full text searching
14  
15 =head1 SYNOPSIS
16  
17 % gmod_chado_fts_prep.pl [--dbprofile (name)]
18  
19 =head1 COMMAND-LINE OPTIONS
20  
21 --dbprfile Specify a gmod.conf profile name (otherwise use default)
22  
23 =head1 DESCRIPTION
24  
25 note about pg version
26 note about all_feature_names materialized view
27  
28 =head1 AUTHOR
29  
30 Scott Cain E<lt>scain@cpan.orgE<gt>
31  
32 Copyright (c) 2010
33  
34 This library is free software; you can redistribute it and/or modify
35 it under the same terms as Perl itself.
36  
37 =cut
38  
39 my ($DBPROFILE,);
40  
41 GetOptions(
42 'dbprofile=s' => \$DBPROFILE,
43 ) or ( system( 'pod2text', $0 ), exit -1 );
44  
45 $DBPROFILE ||='default';
46  
47 my $gmod_conf = Bio::GMOD::Config->new();
48 my $db_conf = Bio::GMOD::DB::Config->new($gmod_conf, $DBPROFILE);
49 my $schema = $db_conf->schema;
50 $schema ||= 'public';
51  
52 print STDERR "Making ".$db_conf->name()." database ready for use with\nfull text searching in Bio::DB::Das::Chado.\n\n";
53  
54 my $dbh = $db_conf->dbh();
55  
56 validate_prereqs($dbh);
57 create_searchable_columns($dbh,$schema);
58 create_all_feature_names($DBPROFILE,$schema);
59 create_search_triggers($dbh);
60  
61 exit(0);
62  
63 =head1 Internally used methods
64  
65  
66 =head2 validate_prereqs
67  
68 =over
69  
70 =item Usage
71  
72 validate_prereqs()
73  
74 =item Function
75  
76 Checks that the Pg version is OK and that all_feature_names is a
77 table an not a view.
78  
79 =item Returns
80  
81 Nothing.
82  
83 =item Arguments
84  
85 The DBI database handle for the database to be modified.
86  
87 =back
88  
89 =cut
90  
91 sub validate_prereqs {
92 my $dbh = shift;
93  
94 my $version_query = "select version()";
95 my $arrayref = $dbh->selectall_arrayref($version_query);
96  
97 #parse ugly not very useful version result
98 if ($$arrayref[0]->[0] =~ /PostgreSQL\s+(\S+)\s/) {
99 my $versionstring = $1;
100 my ($major, $minor, $rev) = split /\./, $versionstring;
101 my $version = $major + $minor/10;
102 if ($version < 8.4) {
103 warn "The PostgreSQL server version is less than the required 8.4 (it reported $version).\n";
104 warn "Exiting...\n";
105 exit(1);
106 }
107 }
108 else {
109 warn "Unable to determine PostgreSQL server version; exiting...\n";
110 exit(1);
111 }
112 }
113  
114 =head2 create_searchable_columns
115  
116 =over
117  
118 =item Usage
119  
120 create_searchable_columns($dbh);
121  
122 =item Function
123  
124 To modify existing feature, synonym and dbxref tables to add a "searchable"
125 column for names and accessions.
126  
127 =item Returns
128  
129 Nothing.
130  
131 =item Arguments
132  
133 The DBI database handle for the database to be modified.
134  
135 =back
136  
137 =cut
138  
139 sub create_searchable_columns {
140 my $dbh = shift;
141 my $schema = shift;
142  
143 my $exists_query = "select count(*) from information_schema.columns where table_schema='$schema' and table_name='feature' and column_name='searchable_name'";
144 my $arrayref = $dbh->selectall_arrayref($exists_query);
145 if ( $$arrayref[0]->[0] == 1) {
146 warn "Dropping feature.searchable_name so it can be replaced.\n";
147 $dbh->do("ALTER TABLE feature DROP COLUMN searchable_name") or die;
148 }
149  
150 $dbh->do("ALTER TABLE feature ADD COLUMN searchable_name tsvector") or die;
151 $dbh->do("UPDATE feature SET searchable_name = to_tsvector('pg_catalog.english', COALESCE((name,'') || ' ' || (uniquename,'')))") or die;
152  
153 $exists_query = "select count(*) from information_schema.columns where table_schema='$schema' and table_name='synonym' and column_name='searchable_synonym_sgml'";
154 $arrayref = $dbh->selectall_arrayref($exists_query);
155 if ( $$arrayref[0]->[0] == 1) {
156 warn "Dropping synonym.searchable_synonym_sgml so it can be replaced.\n";
157 $dbh->do("ALTER TABLE synonym DROP COLUMN searchable_synonym_sgml") or die;
158 }
159  
160 $dbh->do("ALTER TABLE synonym ADD COLUMN searchable_synonym_sgml tsvector") or die;
161 $dbh->do("UPDATE synonym SET searchable_synonym_sgml = to_tsvector('pg_catalog.english', synonym_sgml)") or die;
162  
163 $exists_query = "select count(*) from information_schema.columns where table_schema='$schema' and table_name='dbxref' and column_name='searchable_accession'";
164 $arrayref = $dbh->selectall_arrayref($exists_query);
165 if ( $$arrayref[0]->[0] == 1) {
166 warn "Dropping dbxref.searchable_accession so it can be replaced.\n\n";
167 $dbh->do("ALTER TABLE dbxref DROP COLUMN searchable_accession") or die;
168 }
169  
170 $dbh->do("ALTER TABLE dbxref ADD COLUMN searchable_accession tsvector") or die;
171 $dbh->do("UPDATE dbxref SET searchable_accession = to_tsvector('pg_catalog.english', accession)") or die;
172  
173 return;
174 }
175  
176 =head2 create_search_triggers
177  
178 =over
179  
180 =item Usage
181  
182 create_search_triggers($dbh)
183  
184 =item Function
185  
186 Creates database triggers on the searchable columns to keep them up
187 to date.
188  
189 =item Returns
190  
191 Nothing.
192  
193 =item Arguments
194  
195 The database handle, $dbh, for the database to be modified.
196  
197 =back
198  
199 From Leighton's notes:
200  
201 Add trigger function to each table to populate the
202 searchable column when a data-modifying operation occurs
203 on the target field.
204  
205 This is made much easier by the existence of the tsvector_update_trigger()
206 procedure.
207  
208 =cut
209  
210 sub create_search_triggers {
211 my $dbh = shift;
212  
213 $dbh->do("DROP TRIGGER IF EXISTS feature_searchable_iu ON feature") or die;
214 $dbh->do("CREATE TRIGGER feature_searchable_iu
215 BEFORE INSERT OR UPDATE ON feature
216 FOR EACH ROW
217 EXECUTE PROCEDURE
218 tsvector_update_trigger(searchable_name, 'pg_catalog.english', name, uniquename)") or die;
219  
220 $dbh->do("DROP TRIGGER IF EXISTS synonym_searchable_iu ON synonym") or die;
221 $dbh->do("CREATE TRIGGER synonym_searchable_iu
222 BEFORE INSERT OR UPDATE ON synonym
223 FOR EACH ROW
224 EXECUTE PROCEDURE
225 tsvector_update_trigger(searchable_synonym_sgml, 'pg_catalog.english', synonym_sgml)") or die;
226  
227 $dbh->do("DROP TRIGGER IF EXISTS dbxref_searchable_iu ON dbxref") or die;
228 $dbh->do("CREATE TRIGGER dbxref_searchable_iu
229 BEFORE INSERT OR UPDATE ON dbxref
230 FOR EACH ROW
231 EXECUTE PROCEDURE
232 tsvector_update_trigger(searchable_accession, 'pg_catalog.english', accession)") or die;
233  
234 return;
235 }
236  
237 =head2 create_all_feature_names
238  
239 =over
240  
241 =item Usage
242  
243 create_all_feature_names($dbprof)
244  
245 =item Function
246  
247 Creates the materialized view of all_feature_names, making sure to
248 dematerialize it first. It does this by making system calls to
249 the gmod_materialized_view_tool.pl script that comes with the Chado
250 distribution.
251  
252 =item Returns
253  
254 Nothing.
255  
256 =item Arguments
257  
258 The name of the Bio::GMOD::DB::Config profile.
259  
260 =back
261  
262 =cut
263  
264 sub create_all_feature_names {
265 my $dbprof = shift;
266 my $schema = shift;
267  
268 system("gmod_materialized_view_tool.pl --dbprof $dbprof --dem all_feature_names --yes" );
269  
270 system( <<END
271 gmod_materialized_view_tool.pl --create_view --view_name all_feature_names --table_name $schema.all_feature_names --refresh_time daily --column_def "feature_id integer,name varchar(255),organism_id integer,searchable_name tsvector" --sql_query "SELECT feature_id, CAST(substring(uniquename FROM 0 FOR 255) AS varchar(255)) AS name, organism_id, to_tsvector('english', CAST(substring(uniquename FROM 0 FOR 255) AS varchar(255))) AS searchable_name FROM feature UNION SELECT feature_id, name, organism_id, to_tsvector('english', name) AS searchable_name FROM feature WHERE name IS NOT NULL UNION SELECT fs.feature_id, s.name, f.organism_id, to_tsvector('english', s.name) AS searchable_name FROM feature_synonym fs, synonym s, feature f WHERE fs.synonym_id = s.synonym_id AND fs.feature_id = f.feature_id UNION SELECT fp.feature_id, CAST(substring(fp.value FROM 0 FOR 255) AS varchar(255)) AS name, f.organism_id, to_tsvector('english',CAST(substring(fp.value FROM 0 FOR 255) AS varchar(255))) AS searchable_name FROM featureprop fp, feature f WHERE f.feature_id = fp.feature_id UNION SELECT fd.feature_id, d.accession, f.organism_id,to_tsvector('english',d.accession) AS searchable_name FROM feature_dbxref fd, dbxref d,feature f WHERE fd.dbxref_id = d.dbxref_id AND fd.feature_id = f.feature_id" --index_fields "feature_id,name" --special_index "CREATE INDEX searchable_all_feature_names_idx ON all_feature_names USING gin(searchable_name)" --yes --dbprof $dbprof
272 END
273 );
274  
275 }
276