[Bio] / FigKernelPackages / FIG.pm Repository:
ViewVC logotype

Annotation of /FigKernelPackages/FIG.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (view) (download) (as text)

1 : efrank 1.1 package FIG;
2 :    
3 :     use DBrtns;
4 :     use Sim;
5 :     use Blast;
6 :     use FIG_Config;
7 :    
8 :     use FileHandle;
9 :    
10 :     use Carp;
11 :     use Data::Dumper;
12 :    
13 :     use strict;
14 :     use Fcntl qw/:flock/; # import LOCK_* constants
15 :    
16 :     sub new {
17 :     my($class) = @_;
18 :    
19 :     my $rdbH = new DBrtns;
20 :     bless {
21 :     _dbf => $rdbH,
22 :     }, $class;
23 :     }
24 :    
25 :     sub DESTROY {
26 :     my($self) = @_;
27 :     my($rdbH);
28 :    
29 :     if ($rdbH = $self->db_handle)
30 :     {
31 :     $rdbH->DESTROY;
32 :     }
33 :     }
34 :    
35 :     sub add_genome {
36 :     my($self,$genomeF) = @_;
37 :    
38 :     my $rc = 0;
39 :     if ($genomeF =~ /((.*\/)?(\d+\.\d+))$/)
40 :     {
41 :     my $genome = $3;
42 :     my @errors = `$FIG_Config::bin/verify_genome_directory $genomeF`;
43 :     if (@errors == 0)
44 :     {
45 :     &run("cp -r $genomeF $FIG_Config::organisms");
46 :     chmod 0777, "$FIG_Config::organisms/$genomeF";
47 :     &run("load_features $genome");
48 :     &run("index_contigs $genome");
49 :     $rc = 1;
50 :     if (-s "$FIG_Config::organisms/$genome/Features/peg/fasta")
51 :     {
52 :     &run("index_translations $genome");
53 :     my @tmp = `cut -f1 $FIG_Config::organisms/$genome/Features/peg/tbl`;
54 :     chop @tmp;
55 :     # &run("cat $FIG_Config::organisms/$genome/Features/peg/fasta >> $FIG_Config::data/Global/nr");
56 : overbeek 1.3 # &make_similarities(\@tmp);
57 : efrank 1.1 }
58 :     if ((-s "$FIG_Config::organisms/$genome/assigned_functions") ||
59 :     (-d "$FIG_Config::organisms/$genome/UserModels"))
60 :     {
61 :     &run("add_assertions_of_function $genome");
62 :     }
63 :     }
64 :     }
65 :     return $rc;
66 :     }
67 :    
68 :     sub make_similarities {
69 :     my($fids) = @_;
70 :     my $fid;
71 :    
72 :     open(TMP,">>$FIG_Config::global/queued_similarities")
73 :     || die "could not open $FIG_Config::global/queued_similarities";
74 :     foreach $fid (@$fids)
75 :     {
76 :     print TMP "$fid\n";
77 :     }
78 :     close(TMP);
79 :     }
80 :    
81 :     sub set_urls {
82 : overbeek 1.3
83 :     my $name = defined($FIG_Config::hostname) ? $FIG_Config::hostname : `hostname`;
84 : efrank 1.1 if ($name)
85 :     {
86 :     chop $name;
87 :     my @tmp = `cat $FIG_Config::fig/Packages/FIG_Config.pm`;
88 :     open(TMP,">$FIG_Config::fig/Packages/FIG_Config.pm") || die "could not open $FIG_Config::fig/Packages/FIG_Config.pm";
89 :     my $line;
90 :     foreach $line (@tmp)
91 :     {
92 :     $line =~ s/((cgi|temp)_url.*\/\/)[^\/]+\/FIG/$1$name\/FIG/;
93 :     print TMP $line;
94 :     }
95 :     close(TMP);
96 :     }
97 :     }
98 :    
99 :     sub cgi_url {
100 :     return &plug_url($FIG_Config::cgi_url);
101 :     }
102 :    
103 :     sub temp_url {
104 :     return &plug_url($FIG_Config::temp_url);
105 :     }
106 :    
107 :     sub plug_url {
108 :     my($url) = @_;
109 :    
110 : overbeek 1.3 my $name = defined($FIG_Config::hostname) ? $FIG_Config::hostname : `hostname`;
111 : efrank 1.1 if ($name && ($url =~ /^http:\/\/[^\/]+(.*)/))
112 :     {
113 :     $url = "http://$name$1";
114 :     }
115 :     return $url;
116 :     }
117 :    
118 :     =pod
119 :    
120 :     =head1 hiding/caching in a FIG object
121 :    
122 :     We save the DB handle, cache taxonomies, and put a few other odds and ends in the
123 :     FIG object. We expect users to invoke these services using the object $fig constructed
124 :     using:
125 :    
126 :     use FIG;
127 :     my $fig = new FIG;
128 :    
129 :     $fig is then used as the basic mechanism for accessing FIG services. It is, of course,
130 :     just a hash that is used to retain/cache data. The most commonly accessed item is the
131 :     DB filehandle, which is accessed via $self->db_handle.
132 :    
133 :     We cache genus/species expansions, taxonomies, distances (very crudely estimated) estimated
134 :     between genomes, and a variety of other things. I am not sure that using cached/2 was a
135 :     good idea, but I did it.
136 :    
137 :     =cut
138 :    
139 :     sub db_handle {
140 :     my($self) = @_;
141 :    
142 :     return $self->{_dbf};
143 :     }
144 :    
145 :     sub cached {
146 :     my($self,$what) = @_;
147 :    
148 :     my $x = $self->{$what};
149 :     if (! $x)
150 :     {
151 :     $x = $self->{$what} = {};
152 :     }
153 :     return $x;
154 :     }
155 :    
156 :     ################ Basic Routines [ existed since WIT ] ##########################
157 :    
158 :    
159 :     =pod
160 :    
161 :     =head1 min
162 :    
163 :     usage: $n = &FIG::min(@x)
164 :    
165 :     Assumes @x contains numeric values. Returns the minimum of the values.
166 :    
167 :     =cut
168 :    
169 :     sub min {
170 :     my(@x) = @_;
171 :     my($min,$i);
172 :    
173 :     (@x > 0) || return undef;
174 :     $min = $x[0];
175 :     for ($i=1; ($i < @x); $i++)
176 :     {
177 :     $min = ($min > $x[$i]) ? $x[$i] : $min;
178 :     }
179 :     return $min;
180 :     }
181 :    
182 :     =pod
183 :    
184 :     =head1 max
185 :    
186 :     usage: $n = &FIG::max(@x)
187 :    
188 :     Assumes @x contains numeric values. Returns the maximum of the values.
189 :    
190 :     =cut
191 :    
192 :     sub max {
193 :     my(@x) = @_;
194 :     my($max,$i);
195 :    
196 :     (@x > 0) || return undef;
197 :     $max = $x[0];
198 :     for ($i=1; ($i < @x); $i++)
199 :     {
200 :     $max = ($max < $x[$i]) ? $x[$i] : $max;
201 :     }
202 :     return $max;
203 :     }
204 :    
205 :     =pod
206 :    
207 :     =head1 between
208 :    
209 :     usage: &FIG::between($x,$y,$z)
210 :    
211 :     Returns true iff $y is between $x and $z.
212 :    
213 :     =cut
214 :    
215 :     sub between {
216 :     my($x,$y,$z) = @_;
217 :    
218 :     if ($x < $z)
219 :     {
220 :     return (($x <= $y) && ($y <= $z));
221 :     }
222 :     else
223 :     {
224 :     return (($x >= $y) && ($y >= $z));
225 :     }
226 :     }
227 :    
228 :     =pod
229 :    
230 :     =head1 standard_genetic_code
231 :    
232 :     usage: $code = &FIG::standard_genetic_code()
233 :    
234 :     Routines like "translate" can take a "genetic code" as an argument. I implemented such
235 :     codes using hashes that assumed uppercase DNA triplets as keys.
236 :    
237 :     =cut
238 :    
239 :     sub standard_genetic_code {
240 :    
241 :     my $code = {};
242 :    
243 :     $code->{"AAA"} = "K";
244 :     $code->{"AAC"} = "N";
245 :     $code->{"AAG"} = "K";
246 :     $code->{"AAT"} = "N";
247 :     $code->{"ACA"} = "T";
248 :     $code->{"ACC"} = "T";
249 :     $code->{"ACG"} = "T";
250 :     $code->{"ACT"} = "T";
251 :     $code->{"AGA"} = "R";
252 :     $code->{"AGC"} = "S";
253 :     $code->{"AGG"} = "R";
254 :     $code->{"AGT"} = "S";
255 :     $code->{"ATA"} = "I";
256 :     $code->{"ATC"} = "I";
257 :     $code->{"ATG"} = "M";
258 :     $code->{"ATT"} = "I";
259 :     $code->{"CAA"} = "Q";
260 :     $code->{"CAC"} = "H";
261 :     $code->{"CAG"} = "Q";
262 :     $code->{"CAT"} = "H";
263 :     $code->{"CCA"} = "P";
264 :     $code->{"CCC"} = "P";
265 :     $code->{"CCG"} = "P";
266 :     $code->{"CCT"} = "P";
267 :     $code->{"CGA"} = "R";
268 :     $code->{"CGC"} = "R";
269 :     $code->{"CGG"} = "R";
270 :     $code->{"CGT"} = "R";
271 :     $code->{"CTA"} = "L";
272 :     $code->{"CTC"} = "L";
273 :     $code->{"CTG"} = "L";
274 :     $code->{"CTT"} = "L";
275 :     $code->{"GAA"} = "E";
276 :     $code->{"GAC"} = "D";
277 :     $code->{"GAG"} = "E";
278 :     $code->{"GAT"} = "D";
279 :     $code->{"GCA"} = "A";
280 :     $code->{"GCC"} = "A";
281 :     $code->{"GCG"} = "A";
282 :     $code->{"GCT"} = "A";
283 :     $code->{"GGA"} = "G";
284 :     $code->{"GGC"} = "G";
285 :     $code->{"GGG"} = "G";
286 :     $code->{"GGT"} = "G";
287 :     $code->{"GTA"} = "V";
288 :     $code->{"GTC"} = "V";
289 :     $code->{"GTG"} = "V";
290 :     $code->{"GTT"} = "V";
291 :     $code->{"TAA"} = "*";
292 :     $code->{"TAC"} = "Y";
293 :     $code->{"TAG"} = "*";
294 :     $code->{"TAT"} = "Y";
295 :     $code->{"TCA"} = "S";
296 :     $code->{"TCC"} = "S";
297 :     $code->{"TCG"} = "S";
298 :     $code->{"TCT"} = "S";
299 :     $code->{"TGA"} = "*";
300 :     $code->{"TGC"} = "C";
301 :     $code->{"TGG"} = "W";
302 :     $code->{"TGT"} = "C";
303 :     $code->{"TTA"} = "L";
304 :     $code->{"TTC"} = "F";
305 :     $code->{"TTG"} = "L";
306 :     $code->{"TTT"} = "F";
307 :    
308 :     return $code;
309 :     }
310 :    
311 :     =pod
312 :    
313 :     =head1 translate
314 :    
315 :     usage: $aa_seq = &FIG::translate($dna_seq,$code,$fix_start);
316 :    
317 :     If $code is undefined, I use the standard genetic code. If $fix_start is true, I
318 :     will translate initial TTG or GTG to 'M'.
319 :    
320 :     =cut
321 :    
322 :     sub translate {
323 :     my( $dna,$code,$start) = @_;
324 :     my( $i,$j,$ln );
325 :     my( $x,$y );
326 :     my( $prot );
327 :    
328 :     if (! defined($code))
329 :     {
330 :     $code = &FIG::standard_genetic_code;
331 :     }
332 :     $ln = length($dna);
333 :     $prot = "X" x ($ln/3);
334 :     $dna =~ tr/a-z/A-Z/;
335 :    
336 :     for ($i=0,$j=0; ($i < ($ln-2)); $i += 3,$j++)
337 :     {
338 :     $x = substr($dna,$i,3);
339 :     if ($y = $code->{$x})
340 :     {
341 :     substr($prot,$j,1) = $y;
342 :     }
343 :     }
344 :    
345 :     if (($start) && ($ln >= 3) && (substr($dna,0,3) =~ /^[GT]TG$/))
346 :     {
347 :     substr($prot,0,1) = 'M';
348 :     }
349 :     return $prot;
350 :     }
351 :    
352 :     =pod
353 :    
354 :     =head1 reverse_comp and rev_comp
355 :    
356 :     usage: $dnaR = &FIG::reverse_comp($dna) or
357 :     $dnaRP = &FIG::rev_comp($seqP)
358 :    
359 :     In WIT, we implemented reverse complement passing a pointer to a sequence and returning
360 :     a pointer to a sequence. In most cases the pointers are a pain (although in a few they
361 :     are just what is needed). Hence, I kept both versions of the function to allow you
362 :     to use whichever you like. Use rev_comp only for long strings where passing pointers is a
363 :     reasonable effeciency issue.
364 :    
365 :     =cut
366 :    
367 :     sub reverse_comp {
368 :     my($seq) = @_;
369 :    
370 :     return ${&rev_comp(\$seq)};
371 :     }
372 :    
373 :     sub rev_comp {
374 :     my( $seqP ) = @_;
375 :     my( $rev );
376 :    
377 :     $rev = reverse( $$seqP );
378 :     $rev =~ tr/a-z/A-Z/;
379 :     $rev =~ tr/ACGTUMRWSYKBDHV/TGCAAKYWSRMVHDB/;
380 :     return \$rev;
381 :     }
382 :    
383 :     =pod
384 :    
385 :     =head1 verify_dir
386 :    
387 :     usage: &FIG::verify_dir($dir)
388 :    
389 :     Makes sure that $dir exists. If it has to create it, it sets permissions to 0777.
390 :    
391 :     =cut
392 :    
393 :     sub verify_dir {
394 :     my($dir) = @_;
395 :    
396 :     if (-d $dir) { return }
397 :     if ($dir =~ /^(.*)\/[^\/]+$/)
398 :     {
399 :     &verify_dir($1);
400 :     }
401 :     mkdir($dir,0777) || die "could not make $dir";
402 :     chmod 0777,$dir;
403 :     }
404 :    
405 :     =pod
406 :    
407 :     =head1 run
408 :    
409 :     usage: &FIG::run($cmd)
410 :    
411 :     Runs $cmd and fails (with trace) if the command fails.
412 :    
413 :     =cut
414 :    
415 :     sub run {
416 :     my($cmd) = @_;
417 :    
418 :     # my @tmp = `date`; chop @tmp; print STDERR "$tmp[0]: running $cmd\n";
419 :     (system($cmd) == 0) || confess "FAILED: $cmd";
420 :     }
421 :    
422 :     =pod
423 :    
424 :     =head1 display_id_and_seq
425 :    
426 :     usage: &FIG::display_id_and_seq($id_and_comment,$seqP,$fh)
427 :    
428 :     This command has always been used to put out fasta sequences. Note that it
429 :     takes a pointer to the sequence. $fh is optional and defalts to STDOUT.
430 :    
431 :     =cut
432 :    
433 :     sub display_id_and_seq {
434 :     my( $id, $seq, $fh ) = @_;
435 :    
436 :     if (! defined($fh) ) { $fh = \*STDOUT; }
437 :    
438 :     print $fh ">$id\n";
439 :     &display_seq($seq, $fh);
440 :     }
441 :    
442 :     sub display_seq {
443 :     my ( $seq, $fh ) = @_;
444 :     my ( $i, $n, $ln );
445 :    
446 :     if (! defined($fh) ) { $fh = \*STDOUT; }
447 :    
448 :     $n = length($$seq);
449 :     # confess "zero-length sequence ???" if ( (! defined($n)) || ($n == 0) );
450 :     for ($i=0; ($i < $n); $i += 60)
451 :     {
452 :     if (($i + 60) <= $n)
453 :     {
454 :     $ln = substr($$seq,$i,60);
455 :     }
456 :     else
457 :     {
458 :     $ln = substr($$seq,$i,($n-$i));
459 :     }
460 :     print $fh "$ln\n";
461 :     }
462 :     }
463 :    
464 :     ########## I commented the pods on the following routines out, since they should not
465 :     ########## be part of the SOAP/WSTL interface
466 :     #=pod
467 :     #
468 :     #=head1 file2N
469 :     #
470 :     #usage: $n = $fig->file2N($file)
471 :     #
472 :     #In some of the databases I need to store filenames, which can waste a lot of
473 :     #space. Hence, I maintain a database for converting filenames to/from integers.
474 :     #
475 :     #=cut
476 :     #
477 :     sub file2N {
478 :     my($self,$file) = @_;
479 :     my($relational_db_response);
480 :    
481 :     my $rdbH = $self->db_handle;
482 :    
483 :     if (($relational_db_response = $rdbH->SQL("SELECT fileno FROM file_table WHERE ( file = \'$file\')")) &&
484 :     (@$relational_db_response == 1))
485 :     {
486 :     return $relational_db_response->[0]->[0];
487 :     }
488 :     elsif (($relational_db_response = $rdbH->SQL("SELECT MAX(fileno) FROM file_table ")) && (@$relational_db_response == 1) && ($relational_db_response->[0]->[0]))
489 :     {
490 :     my $fileno = $relational_db_response->[0]->[0] + 1;
491 :     if ($rdbH->SQL("INSERT INTO file_table ( file, fileno ) VALUES ( \'$file\', $fileno )"))
492 :     {
493 :     return $fileno;
494 :     }
495 :     }
496 :     elsif ($rdbH->SQL("INSERT INTO file_table ( file, fileno ) VALUES ( \'$file\', 1 )"))
497 :     {
498 :     return 1;
499 :     }
500 :     return undef;
501 :     }
502 :    
503 :     #=pod
504 :     #
505 :     #=head1 N2file
506 :     #
507 :     #usage: $filename = $fig->N2file($n)
508 :     #
509 :     #In some of the databases I need to store filenames, which can waste a lot of
510 :     #space. Hence, I maintain a database for converting filenames to/from integers.
511 :     #
512 :     #=cut
513 :     #
514 :     sub N2file {
515 :     my($self,$fileno) = @_;
516 :     my($relational_db_response);
517 :    
518 :     my $rdbH = $self->db_handle;
519 :    
520 :     if (($relational_db_response = $rdbH->SQL("SELECT file FROM file_table WHERE ( fileno = $fileno )")) &&
521 :     (@$relational_db_response == 1))
522 :     {
523 :     return $relational_db_response->[0]->[0];
524 :     }
525 :     return undef;
526 :     }
527 :    
528 :    
529 :     #=pod
530 :     #
531 :     #=head1 openF
532 :     #
533 :     #usage: $fig->openF($filename)
534 :     #
535 :     #Parts of the system rely on accessing numerous different files. The most obvious case is
536 :     #the situation with similarities. It is important that the system be able to run in cases in
537 :     #which an arbitrary number of files cannot be open simultaneously. This routine (with closeF) is
538 :     #a hack to handle this. I should probably just pitch them and insist that the OS handle several
539 :     #hundred open filehandles.
540 :     #
541 :     #=cut
542 :     #
543 :     sub openF {
544 :     my($self,$file) = @_;
545 :     my($fxs,$x,@fxs,$fh);
546 :    
547 :     $fxs = $self->cached('_openF');
548 :     if ($x = $fxs->{$file})
549 :     {
550 :     $x->[1] = time();
551 :     return $x->[0];
552 :     }
553 :    
554 :     @fxs = keys(%$fxs);
555 :     if (defined($fh = new FileHandle "<$file"))
556 :     {
557 :     if (@fxs >= 200)
558 :     {
559 :     @fxs = sort { $fxs->{$a}->[1] <=> $fxs->{$b}->[1] } @fxs;
560 :     $x = $fxs->{$fxs[0]};
561 :     undef $x->[0];
562 :     delete $fxs->{$fxs[0]};
563 :     }
564 :     $fxs->{$file} = [$fh,time()];
565 :     return $fh;
566 :     }
567 :     return undef;
568 :     }
569 :    
570 :     #=pod
571 :     #
572 :     #=head1 closeF
573 :     #
574 :     #usage: $fig->closeF($filename)
575 :     #
576 :     #Parts of the system rely on accessing numerous different files. The most obvious case is
577 :     #the situation with similarities. It is important that the system be able to run in cases in
578 :     #which an arbitrary number of files cannot be open simultaneously. This routine (with openF) is
579 :     #a hack to handle this. I should probably just pitch them and insist that the OS handle several
580 :     #hundred open filehandles.
581 :     #
582 :     #=cut
583 :     #
584 :     sub closeF {
585 :     my($self,$file) = @_;
586 :     my($fxs,$x);
587 :    
588 :     if (($fxs = $self->{_openF}) &&
589 :     ($x = $fxs->{$file}))
590 :     {
591 :     undef $x->[0];
592 :     delete $fxs->{$file};
593 :     }
594 :     }
595 :    
596 :     =pod
597 :    
598 :     =head1 ec_name
599 :    
600 :     usage: $enzymatic_function = $fig->ec_name($ec)
601 :    
602 :     Returns enzymatic name for EC.
603 :    
604 :     =cut
605 :    
606 :     sub ec_name {
607 :     my($self,$ec) = @_;
608 :    
609 :     ($ec =~ /^\d+\.\d+\.\d+\.\d+$/) || return "";
610 :     my $rdbH = $self->db_handle;
611 :     my $relational_db_response = $rdbH->SQL("SELECT name FROM ec_names WHERE ( ec = \'$ec\' )");
612 :    
613 :     return (@$relational_db_response == 1) ? $relational_db_response->[0]->[0] : "";
614 :     return "";
615 :     }
616 :    
617 :     =pod
618 :    
619 :     =head1 all_roles
620 :    
621 :     usage: @roles = $fig->all_roles
622 :    
623 :     Supposed to return all known roles. For now, we ghet all ECs with "names".
624 :    
625 :     =cut
626 :    
627 :     sub all_roles {
628 :     my($self) = @_;
629 :    
630 :     my $rdbH = $self->db_handle;
631 :     my $relational_db_response = $rdbH->SQL("SELECT ec,name FROM ec_names");
632 :    
633 :     return @$relational_db_response;
634 :     }
635 :    
636 :     =pod
637 :    
638 :     =head1 expand_ec
639 :    
640 :     usage: $expanded_ec = $fig->expand_ec($ec)
641 :    
642 :     Expands "1.1.1.1" to "1.1.1.1 - alcohol dehydrogenase" or something like that.
643 :    
644 :     =cut
645 :    
646 :     sub expand_ec {
647 :     my($self,$ec) = @_;
648 :     my($name);
649 :    
650 :     return ($name = $self->ec_name($ec)) ? "$ec - $name" : $ec;
651 :     }
652 :    
653 :    
654 :     =pod
655 :    
656 :     =head1 clean_tmp
657 :    
658 :     usage: &FIG::clean_tmp
659 :    
660 :     We store temporary files in $FIG_Config::temp. There are specific classes of files
661 :     that are created and should be saved for at least a few days. This routine can be
662 :     invoked to clean out those that are over two days old.
663 :    
664 :     =cut
665 :    
666 :     sub clean_tmp {
667 :    
668 :     my($file);
669 :     if (opendir(TMP,"$FIG_Config::temp"))
670 :     {
671 :     # change the pattern to pick up other files that need to be cleaned up
672 :     my @temp = grep { $_ =~ /^(Geno|tmp)/ } readdir(TMP);
673 :     foreach $file (@temp)
674 :     {
675 :     if (-M "$FIG_Config::temp/$file" > 2)
676 :     {
677 :     unlink("$FIG_Config::temp/$file");
678 :     }
679 :     }
680 :     }
681 :     }
682 :    
683 :     ################ Routines to process genomes and genome IDs ##########################
684 :    
685 :    
686 :     =pod
687 :    
688 :     =head1 genomes
689 :    
690 :     usage: @genome_ids = $fig->genomes;
691 :    
692 :     Genomes are assigned ids of the form X.Y where X is the taxonomic id maintained by
693 :     NCBI for the species (not the specific strain), and Y is a sequence digit assigned to
694 :     this particular genome (as one of a set with the same genus/species). Genomes also
695 :     have versions, but that is a separate issue.
696 :    
697 :     =cut
698 :    
699 :     sub genomes {
700 :     opendir(GENOMES,"$FIG_Config::fig/Data/Organisms")
701 :     || die "could not open $FIG_Config::fig/Data/Organisms";
702 :     my @genomes = sort { $a <=> $b } grep { $_ =~ /^\d/ } readdir(GENOMES);
703 :     return @genomes;
704 :     }
705 :    
706 : efrank 1.2 sub genome_counts {
707 :     my($x);
708 :     my($a,$b,$e,$v) = (0,0,0,0);
709 :    
710 :     if (open(TMP,"cat $FIG_Config::organisms/*/TAXONOMY |"))
711 :     {
712 :     while (defined($x = <TMP>))
713 :     {
714 :     if ($x =~ /^Eukaryota/) { $e++ }
715 :     elsif ($x =~ /^Bacteria/) { $b++ }
716 :     elsif ($x =~ /^Archaea/) { $a++ }
717 :     elsif ($x =~ /^Vir/) { $v++ }
718 :     }
719 :     close(TMP);
720 :     }
721 :     return ($a,$b,$e,$v);
722 :     }
723 :    
724 : efrank 1.1 =pod
725 :    
726 :     =head1 genome_version
727 :    
728 :     usage: $version = $fig->genome_version($genome_id);
729 :    
730 :     Versions are incremented for major updates. They are put in as major
731 :     updates of the form 1.0, 2.0, ...
732 :    
733 :     Users may do local "editing" of the DNA for a genome, but when they do,
734 :     they increment the digits to the right of the decimal. Two genomes remain
735 :     comparable only if the versions match identically. Hence, minor updating should be
736 :     committed only by the person/group responsible for updating that genome.
737 :    
738 :     We can, of course, identify which genes are identical between any two genomes (by matching
739 :     the DNA or amino acid sequences). However, the basic intent of the system is to
740 :     support editing by the main group issuing periodic major updates.
741 :    
742 :     =cut
743 :    
744 :     sub genome_version {
745 :     my($self,$genome) = @_;
746 :    
747 :     my(@tmp);
748 :     if ((-s "$FIG_Config::organisms/$genome/VERSION") &&
749 :     (@tmp = `cat $FIG_Config::organisms/$genome/VERSION`) &&
750 :     ($tmp[0] =~ /^(\d+(\.\d+)?)$/))
751 :     {
752 :     return $1;
753 :     }
754 :     return undef;
755 :     }
756 :    
757 :     =pod
758 :    
759 :     =head1 genus_species
760 :    
761 :     usage: $gs = $fig->genus_species($genome_id)
762 :    
763 :     Returns the genus and species (and strain if that has been properly recorded)
764 :     in a printable form.
765 :    
766 :     =cut
767 :    
768 :     sub genus_species {
769 :     my ($self,$genome) = @_;
770 :    
771 :     my $ans;
772 :     my $genus_species = $self->cached('_genus_species');
773 :     if (! ($ans = $genus_species->{$genome}))
774 :     {
775 :     if (open(TMP,"<$FIG_Config::organisms/$genome/GENOME"))
776 :     {
777 :     $ans = <TMP>;
778 :     chop $ans;
779 :     close(TMP);
780 :     $genus_species->{$genome} = $ans;
781 :     }
782 :     }
783 :     return $ans;
784 :     }
785 :    
786 :     =pod
787 :    
788 :     =head1 taxonomy_of
789 :    
790 :     usage: $gs = $fig->taxonomy_of($genome_id)
791 :    
792 :     Returns the taxonomy of the specified genome. Gives the taxonomy down to
793 :     genus and species.
794 :    
795 :     =cut
796 :    
797 :     sub taxonomy_of {
798 :     my($self,$genome) = @_;
799 :     my($tax);
800 :     my $taxonomy = $self->cached('_taxonomy');
801 :    
802 :     if (! $taxonomy->{$genome})
803 :     {
804 :     foreach $genome ($self->genomes)
805 :     {
806 :     if (open(TMP,"<$FIG_Config::organisms/$genome/TAXONOMY"))
807 :     {
808 :     $tax = <TMP>;
809 :     chop $tax;
810 :     $self->{_taxonomy}->{$genome} = $tax;
811 :     close(TMP);
812 :     }
813 :     }
814 :     $taxonomy = $self->{_taxonomy};
815 :     }
816 :     return $taxonomy->{$genome};
817 :     }
818 :    
819 :     =pod
820 :    
821 :     =head1 is_bacterial
822 :    
823 :     usage: $fig->is_bacterial($genome)
824 :    
825 :     Returns true iff the genome is bacterial.
826 :    
827 :     =cut
828 :    
829 :     sub is_bacterial {
830 :     my($self,$genome) = @_;
831 :    
832 :     return ($self->taxonomy_of($genome) =~ /^Bacteria/);
833 :     }
834 :    
835 :    
836 :     =pod
837 :    
838 :     =head1 is_archaeal
839 :    
840 :     usage: $fig->is_archaeal($genome)
841 :    
842 :     Returns true iff the genome is archaeal.
843 :    
844 :     =cut
845 :    
846 :     sub is_archaeal {
847 :     my($self,$genome) = @_;
848 :    
849 :     return ($self->taxonomy_of($genome) =~ /^Archaea/);
850 :     }
851 :    
852 :    
853 :     =pod
854 :    
855 :     =head1 is_prokaryotic
856 :    
857 :     usage: $fig->is_prokaryotic($genome)
858 :    
859 :     Returns true iff the genome is prokaryotic
860 :    
861 :     =cut
862 :    
863 :     sub is_prokaryotic {
864 :     my($self,$genome) = @_;
865 :    
866 :     return ($self->taxonomy_of($genome) =~ /^(Archaea|Bacteria)/);
867 :     }
868 :    
869 :    
870 :     =pod
871 :    
872 :     =head1 is_eukaryotic
873 :    
874 :     usage: $fig->is_eukaryotic($genome)
875 :    
876 :     Returns true iff the genome is eukaryotic
877 :    
878 :     =cut
879 :    
880 :     sub is_eukaryotic {
881 :     my($self,$genome) = @_;
882 :    
883 :     return ($self->taxonomy_of($genome) =~ /^Eukarota/);
884 :     }
885 :    
886 :     =pod
887 :    
888 :     =head1 sort_genomes_by_taxonomy
889 :    
890 :     usage: @genomes = $fig->sort_genomes_by_taxonomy(@list_of_genomes)
891 :    
892 :     This routine is used to sort a list of genome IDs to put them
893 :     into taxonomic order.
894 :    
895 :     =cut
896 :    
897 :     sub sort_genomes_by_taxonomy {
898 :     my($self,@fids) = @_;
899 :    
900 :     return map { $_->[0] }
901 :     sort { $a->[1] cmp $b->[1] }
902 :     map { [$_,$self->taxonomy_of($_)] }
903 :     @fids;
904 :     }
905 :    
906 :     =pod
907 :    
908 :     =head1 crude_estimate_of_distance
909 :    
910 :     usage: $dist = $fig->crude_estimate_of_distance($genome1,$genome2)
911 :    
912 :     There are a number of places where we need estimates of the distance between
913 :     two genomes. This routine will return a value between 0 and 1, where a value of 0
914 :     means "the genomes are essentially identical" and a value of 1 means
915 :     "the genomes are in different major groupings" (the groupings are archaea, bacteria,
916 :     euks, and viruses). The measure is extremely crude.
917 :    
918 :     =cut
919 :    
920 :     sub crude_estimate_of_distance {
921 :     my($self,$genome1,$genome2) = @_;
922 :     my($i,$v,$d,$dist);
923 :    
924 :     if ($genome1 > $genome2) { ($genome1,$genome2) = ($genome2,$genome1) }
925 :     $dist = $self->cached('_dist');
926 :     if (! $dist->{"$genome1,$genome2"})
927 :     {
928 :     my @tax1 = split(/\s*;\s*/,$self->taxonomy_of($genome1));
929 :     my @tax2 = split(/\s*;\s*/,$self->taxonomy_of($genome2));
930 :    
931 :     $d = 1;
932 :     for ($i=0, $v=0.5; ($i < @tax1) && ($i < @tax2) && ($tax1[$i] eq $tax2[$i]); $i++, $v = $v/2)
933 :     {
934 :     $d -= $v;
935 :     }
936 :     $dist->{"$genome1,$genome2"} = $d;
937 :     }
938 :     return $dist->{"$genome1,$genome2"};
939 :     }
940 :    
941 :     =pod
942 :    
943 :     =head1 org_of
944 :    
945 :     usage: $org = $fig->org_of($prot_id)
946 :    
947 :     In the case of external proteins, we can usually determine an organism, but not
948 :     anything more precise than genus/species (and often not that). This routine takes
949 : efrank 1.2 a protein ID (which may be a feature ID) and returns "the organism".
950 : efrank 1.1
951 :     =cut
952 :    
953 :     sub org_of {
954 :     my($self,$prot_id) = @_;
955 :     my $relational_db_response;
956 :     my $rdbH = $self->db_handle;
957 :    
958 :     if ($prot_id =~ /^fig\|/)
959 :     {
960 :     return $self->genus_species($self->genome_of($prot_id));
961 :     }
962 :    
963 :     if (($relational_db_response = $rdbH->SQL("SELECT org FROM external_orgs WHERE ( prot = \'$prot_id\' )")) &&
964 :     (@$relational_db_response >= 1))
965 :     {
966 :     return $relational_db_response->[0]->[0];
967 :     }
968 :     return "";
969 :     }
970 :    
971 :     =pod
972 :    
973 :     =head1 abbrev
974 :    
975 :     usage: $abbreviated_name = $fig->abbrev($genome_name)
976 :    
977 :     For alignments and such, it is very useful to be able to produce an abbreviation of genus/species.
978 :     That's what this does. Note that multiple genus/species might reduce to the same abbreviation, so
979 :     be careful (disambiguate them, if you must).
980 :    
981 :     =cut
982 :    
983 :     sub abbrev {
984 :     my($genome_name) = @_;
985 :    
986 :     $genome_name =~ s/^(\S{3})\S+/$1./;
987 :     $genome_name =~ s/^(\S+\s+\S{4})\S+/$1./;
988 :     if (length($genome_name) > 13)
989 :     {
990 :     $genome_name = substr($genome_name,0,13);
991 :     }
992 :     return $genome_name;
993 :     }
994 :    
995 :     ################ Routines to process Features and Feature IDs ##########################
996 :    
997 :     =pod
998 :    
999 :     =head1 ftype
1000 :    
1001 :     usage: $type = &FIG::ftype($fid)
1002 :    
1003 :     Returns the type of a feature, given the feature ID. This just amounts
1004 :     to lifting it out of the feature ID, since features have IDs of tghe form
1005 :    
1006 :     fig|x.y.f.n
1007 :    
1008 :     where
1009 :     x.y is the genome ID
1010 :     f is the type pf feature
1011 :     n is an integer that is unique within the genome/type
1012 :    
1013 :     =cut
1014 :    
1015 :     sub ftype {
1016 :     my($feature_id) = @_;
1017 :    
1018 :     if ($feature_id =~ /^fig\|\d+\.\d+\.([^\.]+)/)
1019 :     {
1020 :     return $1;
1021 :     }
1022 :     return undef;
1023 :     }
1024 :    
1025 :     =pod
1026 :    
1027 :     =head1 genome_of
1028 :    
1029 :     usage: $genome_id = $fig->genome_of($fid)
1030 :    
1031 :     This just extracts the genome ID from a feature ID.
1032 :    
1033 :     =cut
1034 :    
1035 :    
1036 :     sub genome_of {
1037 :     my $prot_id = (@_ == 1) ? $_[0] : $_[1];
1038 :    
1039 :     if ($prot_id =~ /^fig\|(\d+\.\d+)/) { return $1; }
1040 :     return undef;
1041 :     }
1042 :    
1043 :     =pod
1044 :    
1045 :     =head1 by_fig_id
1046 :    
1047 :     usage: @sorted_by_fig_id = sort { &FIG::by_fig_id($a,$b) } @fig_ids
1048 :    
1049 :     This is a bit of a clutzy way to sort a list of FIG feature IDs, but it works.
1050 :    
1051 :     =cut
1052 :    
1053 :     sub by_fig_id {
1054 :     my($a,$b) = @_;
1055 :     my($g1,$g2,$t1,$t2,$n1,$n2);
1056 :     if (($a =~ /^fig\|(\d+\.\d+).([^\.]+)\.(\d+)$/) && (($g1,$t1,$n1) = ($1,$2,$3)) &&
1057 :     ($b =~ /^fig\|(\d+\.\d+).([^\.]+)\.(\d+)$/) && (($g2,$t2,$n2) = ($1,$2,$3)))
1058 :     {
1059 :     ($g1 <=> $g2) or ($t1 cmp $t2) or ($n1 <=> $n2);
1060 :     }
1061 :     else
1062 :     {
1063 :     $a cmp $b;
1064 :     }
1065 :     }
1066 :    
1067 :     =pod
1068 :    
1069 :     =head1 sort_fids_by_taxonomy
1070 :    
1071 :     usage: @sorted_by_taxonomy = $fig->sort_fids_by_taxonomy(@list_of_fids)
1072 :    
1073 :     Sorts a list of feature IDs based on the taxonomies of the genomes that contain the features.
1074 :    
1075 :     =cut
1076 :    
1077 :     sub sort_fids_by_taxonomy {
1078 :     my($self,@fids) = @_;
1079 :    
1080 :     return map { $_->[0] }
1081 :     sort { $a->[1] cmp $b->[1] }
1082 :     map { [$_,$self->taxonomy_of(&genome_of($_))] }
1083 :     @fids;
1084 :     }
1085 :    
1086 :     =pod
1087 :    
1088 :     =head1 genes_in_region
1089 :    
1090 :     usage: ($features_in_region,$beg1,$end1) = $fig->genes_in_region($genome,$contig,$beg,$end)
1091 :    
1092 :     It is often important to be able to find the genes that occur in a specific region on
1093 :     a chromosome. This routine is designed to provide this information. It returns all genes
1094 :     that overlap the region ($genome,$contig,$beg,$end). $beg1 is set to the minimum coordinate of
1095 :     the returned genes (which may be before the given region), and $end1 the maximum coordinate.
1096 :    
1097 :     The routine assumes that genes are not more than 10000 bases long, which is certainly not true
1098 :     in eukaryotes. Hence, in euks you may well miss genes that overlap the boundaries of the specified
1099 :     region (sorry).
1100 :    
1101 :     =cut
1102 :    
1103 :    
1104 :     sub genes_in_region {
1105 :     my($self,$genome,$contig,$beg,$end) = @_;
1106 :     my($x,$relational_db_response,$feature_id,$b1,$e1,@feat,@tmp,$l,$u);
1107 :    
1108 :     my $pad = 10000;
1109 :     my $rdbH = $self->db_handle;
1110 :    
1111 :     my $minV = $beg - $pad;
1112 :     my $maxV = $end + $pad;
1113 :     if (($relational_db_response = $rdbH->SQL("SELECT id FROM features
1114 :     WHERE ( minloc > $minV ) AND ( minloc < $maxV ) AND (maxloc < $maxV) AND
1115 :     ( genome = \'$genome\' ) AND ( contig = \'$contig\' );")) &&
1116 :     (@$relational_db_response >= 1))
1117 :     {
1118 :     @tmp = sort { ($a->[1] cmp $b->[1]) or
1119 :     ($a->[2] <=> $b->[2]) or
1120 :     ($a->[3] <=> $b->[3])
1121 :     }
1122 :     map { $feature_id = $_->[0];
1123 :     $x = $self->feature_location($feature_id);
1124 :     $x ? [$feature_id,&boundaries_of($x)] : ()
1125 :     } @$relational_db_response;
1126 :    
1127 :    
1128 :     ($l,$u) = (10000000000,0);
1129 :     foreach $x (@tmp)
1130 :     {
1131 :     ($feature_id,undef,$b1,$e1) = @$x;
1132 :     if (&between($beg,&min($b1,$e1),$end) || &between(&min($b1,$e1),$beg,&max($b1,$e1)))
1133 :     {
1134 :     push(@feat,$feature_id);
1135 :     $l = &min($l,&min($b1,$e1));
1136 :     $u = &max($u,&max($b1,$e1));
1137 :     }
1138 :     }
1139 :     (@feat <= 0) || return ([@feat],$l,$u);
1140 :     }
1141 :     return ([],$l,$u);
1142 :     }
1143 :    
1144 :     sub close_genes {
1145 :     my($self,$fid,$dist) = @_;
1146 :    
1147 :     my $loc = $self->feature_location($fid);
1148 :     if ($loc)
1149 :     {
1150 :     my($contig,$beg,$end) = &FIG::boundaries_of($loc);
1151 :     if ($contig && $beg && $end)
1152 :     {
1153 :     my $min = &min($beg,$end) - $dist;
1154 :     my $max = &max($beg,$end) + $dist;
1155 :     my $feat;
1156 :     ($feat,undef,undef) = $self->genes_in_region(&FIG::genome_of($fid),$contig,$min,$max);
1157 :     return @$feat;
1158 :     }
1159 :     }
1160 :     return ();
1161 :     }
1162 :    
1163 :    
1164 :     =pod
1165 :    
1166 :     =head1 feature_location
1167 :    
1168 :     usage: $loc = $fig->feature_location($fid) OR
1169 :     @loc = $fig->feature_location($fid)
1170 :    
1171 :     The location of a feature in a scalar context is
1172 :    
1173 :     contig_b1_e1,contig_b2_e2,... [one contig_b_e for each exon]
1174 :    
1175 :     In a list context it is
1176 :    
1177 :     (contig_b1_e1,contig_b2_e2,...)
1178 :    
1179 :     =cut
1180 :    
1181 :     sub feature_location {
1182 :     my($self,$feature_id) = @_;
1183 :     my($relational_db_response,$locations,$location);
1184 :    
1185 :     $locations = $self->cached('_location');
1186 :     if (! ($location = $locations->{$feature_id}))
1187 :     {
1188 :     my $rdbH = $self->db_handle;
1189 :     if (($relational_db_response = $rdbH->SQL("SELECT location FROM features WHERE ( id = \'$feature_id\' )")) &&
1190 :     (@$relational_db_response == 1))
1191 :     {
1192 :     $locations->{$feature_id} = $location = $relational_db_response->[0]->[0];
1193 :     }
1194 :     }
1195 :    
1196 :     if ($location)
1197 :     {
1198 :     return wantarray() ? split(/,/,$location) : $location;
1199 :     }
1200 :     return undef;
1201 :     }
1202 :    
1203 :     =pod
1204 :    
1205 :     =head1 boundaries_of
1206 :    
1207 :     usage: ($contig,$beg,$end) = $fig->boundaries_of($loc)
1208 :    
1209 :     The location of a feature in a scalar context is
1210 :    
1211 :     contig_b1_e1,contig_b2_e2,... [one contig_b_e for each exon]
1212 :    
1213 :     This routine takes as input such a location and reduces it to a single
1214 :     description of the entire region containing the gene.
1215 :    
1216 :     =cut
1217 :    
1218 :     sub boundaries_of {
1219 :     my($location) = (@_ == 1) ? $_[0] : $_[1];
1220 :     my($contigQ);
1221 :    
1222 :     if (defined($location))
1223 :     {
1224 :     my @exons = split(/,/,$location);
1225 :     my($contig,$beg,$end);
1226 :     if (($exons[0] =~ /^(\S+)_(\d+)_\d+$/) &&
1227 :     (($contig,$beg) = ($1,$2)) && ($contigQ = quotemeta $contig) &&
1228 :     ($exons[$#exons] =~ /^$contigQ\_\d+_(\d+)$/) &&
1229 :     ($end = $1))
1230 :     {
1231 :     return ($contig,$beg,$end);
1232 :     }
1233 :     }
1234 :     return undef;
1235 :     }
1236 :    
1237 :    
1238 :     =pod
1239 :    
1240 :     =head1 all_features
1241 :    
1242 :     usage: $fig->all_features($genome,$type)
1243 :    
1244 :     Returns a list of all feature IDs of a specified type in the designated genome. You would
1245 :     usually use just
1246 :    
1247 :     $fig->pegs_of($genome) or
1248 :     $fig->rnas_of($genome)
1249 :    
1250 :     which simply invoke this routine.
1251 :    
1252 :     =cut
1253 :    
1254 :     sub all_features {
1255 :     my($self,$genome,$type) = @_;
1256 :    
1257 :     my $rdbH = $self->db_handle;
1258 :     my $relational_db_response = $rdbH->SQL("SELECT id FROM features WHERE (genome = \'$genome\' AND (type = \'$type\'))");
1259 :    
1260 :     if (@$relational_db_response > 0)
1261 :     {
1262 :     return map { $_->[0] } @$relational_db_response;
1263 :     }
1264 :     return ();
1265 :     }
1266 :    
1267 :    
1268 :     =pod
1269 :    
1270 :     =head1 all_pegs_of
1271 :    
1272 :     usage: $fig->all_pegs_of($genome)
1273 :    
1274 :     Returns a list of all PEGs in the specified genome. Note that order is not
1275 :     specified.
1276 :    
1277 :     =cut
1278 :    
1279 :     sub pegs_of {
1280 :     my($self,$genome) = @_;
1281 :    
1282 :     return $self->all_features($genome,"peg");
1283 :     }
1284 :    
1285 :    
1286 :     =pod
1287 :    
1288 :     =head1 all_rnas_of
1289 :    
1290 :     usage: $fig->all_rnas($genome)
1291 :    
1292 :     Returns a list of all RNAs for the given genome.
1293 :    
1294 :     =cut
1295 :    
1296 :     sub rnas_of {
1297 :     my($self,$genome) = @_;
1298 :    
1299 :     return $self->all_features($genome,"rna");
1300 :     }
1301 :    
1302 :     =pod
1303 :    
1304 :     =head1 feature_aliases
1305 :    
1306 :     usage: @aliases = $fig->feature_aliases($fid) OR
1307 :     $aliases = $fig->feature_aliases($fid)
1308 :    
1309 :     Returns a list of aliases (gene IDs, arbitrary numbers assigned by authors, etc.) for the feature.
1310 :     These must come from the tbl files, so add them there if you want to see them here.
1311 :    
1312 :     In a scalar context, the aliases come back with commas separating them.
1313 :    
1314 :     =cut
1315 :    
1316 :     sub feature_aliases {
1317 :     my($self,$feature_id) = @_;
1318 :     my($rdbH,$relational_db_response,$aliases);
1319 :    
1320 :     $rdbH = $self->db_handle;
1321 :     if (($relational_db_response = $rdbH->SQL("SELECT aliases FROM features WHERE ( id = \'$feature_id\' )")) &&
1322 :     (@$relational_db_response == 1))
1323 :     {
1324 :     $aliases = $relational_db_response->[0]->[0];
1325 :     }
1326 :     return $aliases ? (wantarray ? split(/,/,$aliases) : $aliases) : undef;
1327 :     }
1328 :    
1329 :     =pod
1330 :    
1331 :     =head1 possibly_truncated
1332 :    
1333 :     usage: $fig->possibly_truncated($fid)
1334 :    
1335 :     Returns true iff the feature occurs near the end of a contig.
1336 :    
1337 :     =cut
1338 :    
1339 :     sub possibly_truncated {
1340 :     my($self,$feature_id) = @_;
1341 :     my($loc);
1342 :    
1343 :     if ($loc = $self->feature_location($feature_id))
1344 :     {
1345 :     my $genome = &genome_of($feature_id);
1346 :     my ($contig,$beg,$end) = &boundaries_of($loc);
1347 :     if ((! $self->near_end($genome,$contig,$beg)) && (! $self->near_end($genome,$contig,$end)))
1348 :     {
1349 :     return 0;
1350 :     }
1351 :     }
1352 :     return 1;
1353 :     }
1354 :    
1355 :     sub near_end {
1356 :     my($self,$genome,$contig,$x) = @_;
1357 :    
1358 :     return (($x < 300) || ($x > ($self->contig_ln($genome,$contig) - 300)));
1359 :     }
1360 :    
1361 :     ################ Routines to process functional coupling for PEGs ##########################
1362 :    
1363 :     =pod
1364 :    
1365 :     =head1 coupling_and_evidence
1366 :    
1367 :     usage: @coupling_data = $fig->coupling_and_evidence($fid,$bound,$sim_cutoff,$coupling_cutoff,$keep_record)
1368 :    
1369 :     A computation of couplings and evidence starts with a given peg and produces a list of
1370 :     3-tuples. Each 3-tuple is of the form
1371 :    
1372 :     [Score,CoupledToFID,Evidence]
1373 :    
1374 :     Evidence is a list of 2-tuples of FIDs that are close in other genomes (producing
1375 :     a "pair of close homologs" of [$peg,CoupledToFID]). The maximum score for a single
1376 :     PCH is 1, but "Score" is the sum of the scores for the entire set of PCHs.
1377 :    
1378 :     If $keep_record is true, the system records the information, asserting coupling for each
1379 :     of the pairs in the set of evidence, and asserting a pin from the given $fd through all
1380 :     of the PCH entries used in forming the score.
1381 :    
1382 :     =cut
1383 :    
1384 :     sub coupling_and_evidence {
1385 :     my($self,$feature_id,$bound,$sim_cutoff,$coupling_cutoff,$keep_record) = @_;
1386 :     my($neighbors,$neigh,$similar1,$similar2,@hits,$sc,$ev,$genome1);
1387 :    
1388 :     if ($feature_id =~ /^fig\|(\d+\.\d+)/)
1389 :     {
1390 :     $genome1 = $1;
1391 :     }
1392 :    
1393 :     my($contig,$beg,$end) = &FIG::boundaries_of($self->feature_location($feature_id));
1394 :     if (! $contig) { return () }
1395 :    
1396 :     ($neighbors,undef,undef) = $self->genes_in_region(&genome_of($feature_id),
1397 :     $contig,
1398 :     &min($beg,$end) - $bound,
1399 :     &max($beg,$end) + $bound);
1400 :     if (@$neighbors == 0) { return () }
1401 :     $similar1 = $self->acceptably_close($feature_id,$sim_cutoff);
1402 :     @hits = ();
1403 :    
1404 :     foreach $neigh (grep { $_ =~ /peg/ } @$neighbors)
1405 :     {
1406 :     next if ($neigh eq $feature_id);
1407 :     $similar2 = $self->acceptably_close($neigh,$sim_cutoff);
1408 :     ($sc,$ev) = $self->coupling_ev($genome1,$similar1,$similar2,$bound);
1409 :     if ($sc >= $coupling_cutoff)
1410 :     {
1411 :     push(@hits,[$sc,$neigh,$ev]);
1412 :     }
1413 :     }
1414 :     if ($keep_record)
1415 :     {
1416 :     $self->add_chr_clusters_and_pins($feature_id,\@hits);
1417 :     }
1418 :     return sort { $b->[0] <=> $a->[0] } @hits;
1419 :     }
1420 :    
1421 :    
1422 :     =pod
1423 :    
1424 :     =head1 add_chr_clusters_and_pins
1425 :    
1426 :     usage: $fig->add_chr_clusters_and_pins($peg,$hits)
1427 :    
1428 :     The system supports retaining data relating to functional coupling. If a user
1429 :     computes evidence once and then saves it with this routine, data relating to
1430 :     both "the pin" and the "clusters" (in all of the organisms supporting the
1431 :     functional coupling) will be saved.
1432 :    
1433 :     $hits must be a pointer to a list of 3-tuples of the sort returned by
1434 :     $fig->coupling_and_evidence.
1435 :    
1436 :     =cut
1437 :    
1438 :     sub add_chr_clusters_and_pins {
1439 :     my($self,$peg,$hits) = @_;
1440 :     my(@clusters,@pins,$x,$sc,$neigh,$pairs,$y,@corr,@orgs,%projection);
1441 :     my($genome,$cluster,$pin,$peg2);
1442 :    
1443 :     if (@$hits > 0)
1444 :     {
1445 :     @clusters = ();
1446 :     @pins = ();
1447 :     push(@clusters,[$peg,map { $_->[1] } @$hits]);
1448 :     foreach $x (@$hits)
1449 :     {
1450 :     ($sc,$neigh,$pairs) = @$x;
1451 :     push(@pins,[$neigh,map { $_->[1] } @$pairs]);
1452 :     foreach $y (@$pairs)
1453 :     {
1454 :     $peg2 = $y->[0];
1455 :     if ($peg2 =~ /^fig\|(\d+\.\d+)/)
1456 :     {
1457 :     $projection{$1}->{$peg2} = 1;
1458 :     }
1459 :     }
1460 :     }
1461 :     @corr = ();
1462 :     @orgs = keys(%projection);
1463 :     if (@orgs > 0)
1464 :     {
1465 :     foreach $genome (sort { $a <=> $b } @orgs)
1466 :     {
1467 :     push(@corr,sort { &FIG::by_fig_id($a,$b) } keys(%{$projection{$genome}}));
1468 :     }
1469 :     push(@pins,[$peg,@corr]);
1470 :     }
1471 :    
1472 :     foreach $cluster (@clusters)
1473 :     {
1474 :     $self->add_chromosomal_cluster($cluster);
1475 :     }
1476 :    
1477 :     foreach $pin (@pins)
1478 :     {
1479 :     $self->add_pch_pin($pin);
1480 :     }
1481 :     }
1482 :     }
1483 :    
1484 :     sub coupling_ev {
1485 :     my($self,$genome1,$sim1,$sim2,$bound) = @_;
1486 :     my($ev,$sc,$i,$j);
1487 :    
1488 :     $ev = [];
1489 :     $sc = 0;
1490 :    
1491 :     $i = 0;
1492 :     $j = 0;
1493 :     while (($i < @$sim1) && ($j < @$sim2))
1494 :     {
1495 :     if ($sim1->[$i]->[0] < $sim2->[$j]->[0])
1496 :     {
1497 :     $i++;
1498 :     }
1499 :     elsif ($sim1->[$i]->[0] > $sim2->[$j]->[0])
1500 :     {
1501 :     $j++;
1502 :     }
1503 :     else
1504 :     {
1505 :     $sc += $self->accumulate_ev($genome1,$sim1->[$i]->[1],$sim2->[$j]->[1],$bound,$ev);
1506 :     $i++;
1507 :     $j++;
1508 :     }
1509 :     }
1510 :     return ($sc,$ev);
1511 :     }
1512 :    
1513 :     sub accumulate_ev {
1514 :     my($self,$genome1,$feature_ids1,$feature_ids2,$bound,$ev) = @_;
1515 :     my($genome2,@locs1,@locs2,$i,$j,$sc,$x);
1516 :    
1517 :     if ((@$feature_ids1 == 0) || (@$feature_ids2 == 0)) { return 0 }
1518 :    
1519 :     $feature_ids1->[0] =~ /^fig\|(\d+\.\d+)/;
1520 :     $genome2 = $1;
1521 :     $sc = 0;
1522 :     @locs1 = map { $x = $self->feature_location($_); $x ? [&boundaries_of($x)] : () } @$feature_ids1;
1523 :     @locs2 = map { $x = $self->feature_location($_); $x ? [&boundaries_of($x)] : () } @$feature_ids2;
1524 :    
1525 :     for ($i=0; ($i < @$feature_ids1); $i++)
1526 :     {
1527 :     for ($j=0; ($j < @$feature_ids2); $j++)
1528 :     {
1529 :     if (($feature_ids1->[$i] ne $feature_ids2->[$j]) &&
1530 :     &close_enough($locs1[$i],$locs2[$j],$bound))
1531 :     {
1532 :     $sc += $self->crude_estimate_of_distance($genome1,$genome2);
1533 :     push(@$ev,[$feature_ids1->[$i],$feature_ids2->[$j]]);
1534 :     }
1535 :     }
1536 :     }
1537 :     return $sc;
1538 :     }
1539 :    
1540 :     sub close_enough {
1541 :     my($locs1,$locs2,$bound) = @_;
1542 :    
1543 :     # print STDERR &Dumper(["close enough",$locs1,$locs2]);
1544 :     return (($locs1->[0] eq $locs2->[0]) && (abs((($locs1->[1]+$locs1->[2])/2) - (($locs2->[1]+$locs2->[2])/2)) <= $bound));
1545 :     }
1546 :    
1547 :     sub acceptably_close {
1548 :     my($self,$feature_id,$sim_cutoff) = @_;
1549 :     my(%by_org,$id2,$genome,$sim);
1550 :    
1551 :     my($ans) = [];
1552 :    
1553 :     foreach $sim ($self->sims($feature_id,1000,$sim_cutoff,"fig",0))
1554 :     {
1555 :     $id2 = $sim->id2;
1556 :     if ($id2 =~ /^fig\|(\d+\.\d+)/)
1557 :     {
1558 :     my $genome = $1;
1559 :     if ($self->taxonomy_of($genome) !~ /^Euk/)
1560 :     {
1561 :     push(@{$by_org{$genome}},$id2);
1562 :     }
1563 :     }
1564 :     }
1565 :     foreach $genome (sort { $a <=> $b } keys(%by_org))
1566 :     {
1567 :     push(@$ans,[$genome,$by_org{$genome}]);
1568 :     }
1569 :     return $ans;
1570 :     }
1571 :    
1572 :     ################ Translations of PEGsand External Protein Sequences ##########################
1573 :    
1574 :    
1575 :     =pod
1576 :    
1577 :     =head1 translatable
1578 :    
1579 :     usage: $fig->translatable($prot_id)
1580 :    
1581 :     The system takes any number of sources of protein sequences as input (and builds an nr
1582 :     for the purpose of computing similarities). For each of these input fasta files, it saves
1583 :     (in the DB) a filename, seek address and length so that it can go get the translation if
1584 :     needed. This routine simply returns true iff info on the translation exists.
1585 :    
1586 :     =cut
1587 :    
1588 :    
1589 :     sub translatable {
1590 :     my($self,$prot) = @_;
1591 :    
1592 :     return &translation_length($self,$prot) ? 1 : 0;
1593 :     }
1594 :    
1595 :    
1596 :     =pod
1597 :    
1598 :     =head1 translation_length
1599 :    
1600 :     usage: $len = $fig->translation_length($prot_id)
1601 :    
1602 :     The system takes any number of sources of protein sequences as input (and builds an nr
1603 :     for the purpose of computing similarities). For each of these input fasta files, it saves
1604 :     (in the DB) a filename, seek address and length so that it can go get the translation if
1605 :     needed. This routine returns the length of a translation. This does not require actually
1606 :     retrieving the translation.
1607 :    
1608 :     =cut
1609 :    
1610 :     sub translation_length {
1611 :     my($self,$prot) = @_;
1612 :    
1613 :     $prot =~ s/^([^\|]+\|[^\|]+)\|.*$/$1/;
1614 :     my $rdbH = $self->db_handle;
1615 :     my $relational_db_response = $rdbH->SQL("SELECT slen FROM protein_sequence_seeks
1616 :     WHERE id = \'$prot\' ");
1617 :    
1618 :     return (@$relational_db_response == 1) ? $relational_db_response->[0]->[0] : undef;
1619 :     }
1620 :    
1621 :    
1622 :     =pod
1623 :    
1624 :     =head1 get_translation
1625 :    
1626 :     usage: $translation = $fig->get_translation($prot_id)
1627 :    
1628 :     The system takes any number of sources of protein sequences as input (and builds an nr
1629 :     for the purpose of computing similarities). For each of these input fasta files, it saves
1630 :     (in the DB) a filename, seek address and length so that it can go get the translation if
1631 :     needed. This routine returns a protein sequence.
1632 :    
1633 :     =cut
1634 :    
1635 :     sub get_translation {
1636 :     my($self,$id) = @_;
1637 :     my($rdbH,$relational_db_response,$fileN,$file,$fh,$seek,$ln,$tran);
1638 :    
1639 :     $rdbH = $self->db_handle;
1640 :     $id =~ s/^([^\|]+\|[^\|]+)\|.*$/$1/;
1641 :    
1642 :     $relational_db_response = $rdbH->SQL("SELECT fileno, seek, len FROM protein_sequence_seeks WHERE id = \'$id\' ");
1643 :    
1644 :     if ($relational_db_response && @$relational_db_response == 1)
1645 :     {
1646 :     ($fileN,$seek,$ln) = @{$relational_db_response->[0]};
1647 :     if (($fh = $self->openF($self->N2file($fileN))) &&
1648 :     ($ln > 10))
1649 :     {
1650 :     seek($fh,$seek,0);
1651 :     read($fh,$tran,$ln-1);
1652 :     $tran =~ s/\s//g;
1653 :     return $tran;
1654 :     }
1655 :     }
1656 :     return '';
1657 :     }
1658 :    
1659 :     =pod
1660 :    
1661 :     =head1 mapped_prot_ids
1662 :    
1663 :     usage: @mapped = $fig->mapped_prot_ids($prot)
1664 :    
1665 :     This routine is at the heart of maintaining synonyms for protein sequences. The system
1666 :     determines which protein sequences are "essentially the same". These may differ in length
1667 :     (presumably due to miscalled starts), but the tails are identical (and the heads are not "too" extended).
1668 :     Anyway, the set of synonyms is returned as a list of 2-tuples [Id,length] sorted
1669 :     by length.
1670 :    
1671 :     =cut
1672 :    
1673 :     sub mapped_prot_ids {
1674 :     my($self,$id) = @_;
1675 :    
1676 :     my $rdbH = $self->db_handle;
1677 :     my $relational_db_response = $rdbH->SQL("SELECT maps_to FROM peg_synonyms WHERE syn_id = \'$id\' ");
1678 :     if ($relational_db_response && (@$relational_db_response == 1))
1679 :     {
1680 :     $id = $relational_db_response->[0]->[0];
1681 :     }
1682 :    
1683 :     $relational_db_response = $rdbH->SQL("SELECT syn_id,syn_ln,maps_to_ln FROM peg_synonyms WHERE maps_to = \'$id\' ");
1684 :     if ($relational_db_response && (@$relational_db_response > 0))
1685 :     {
1686 :     return ([$id,$relational_db_response->[0]->[2]],map { [$_->[0],$_->[1]] } @$relational_db_response);
1687 :     }
1688 :     else
1689 :     {
1690 :     return ([$id,$self->translation_length($id)]);
1691 :     }
1692 :     }
1693 :    
1694 :     ################ Assignments of Function to PEGs ##########################
1695 :    
1696 :     =pod
1697 :    
1698 :     =head1 function_of
1699 :    
1700 :     usage: @functions = $fig->function_of($peg) OR
1701 :     $function = $fig->function_of($peg,$user)
1702 :    
1703 :     In a list context, you get back a list of 2-tuples. Each 2-tuple is of the
1704 :     form [MadeBy,Function].
1705 :    
1706 :     In a scalar context,
1707 :    
1708 :     1. user is "master" if not specified
1709 :     2. function returned is the user's, if one exists; otherwise, master's, if one exists
1710 :    
1711 :     In a scalar context, you get just the function.
1712 :    
1713 :     =cut
1714 :    
1715 :     # Note that we do not return confidence. I propose a separate function to get both
1716 :     # function and confidence
1717 :     #
1718 :     sub function_of {
1719 :     my($self,$id,$user) = @_;
1720 :     my($relational_db_response,@tmp,$entry,$i);
1721 :     my $wantarray = wantarray();
1722 :     my $rdbH = $self->db_handle;
1723 :    
1724 :     if (($id =~ /^fig\|(\d+\.\d+\.peg\.\d+)/) && ($wantarray || $user))
1725 :     {
1726 :     if (($relational_db_response = $rdbH->SQL("SELECT made_by,assigned_function FROM assigned_functions WHERE ( prot = \'$id\' )")) &&
1727 :     (@$relational_db_response >= 1))
1728 :     {
1729 :     @tmp = sort { $a->[0] cmp $b->[0] } map { [$_->[0],$_->[1]] } @$relational_db_response;
1730 :     for ($i=0; ($i < @tmp) && ($tmp[$i]->[0] ne "master"); $i++) {}
1731 :     if ($i < @tmp)
1732 :     {
1733 :     $entry = splice(@tmp,$i,1);
1734 :     unshift @tmp, ($entry);
1735 :     }
1736 :    
1737 :     my $val;
1738 :     if ($wantarray) { return @tmp }
1739 :     elsif ($user && ($val = &extract_by_who(\@tmp,$user))) { return $val }
1740 :     elsif ($user && ($val = &extract_by_who(\@tmp,"master"))) { return $val }
1741 :     else { return "" }
1742 :     }
1743 :     }
1744 :     else
1745 :     {
1746 :     if (($relational_db_response = $rdbH->SQL("SELECT assigned_function FROM assigned_functions WHERE ( prot = \'$id\' AND made_by = \'master\' )")) &&
1747 :     (@$relational_db_response >= 1))
1748 :     {
1749 :     return $wantarray ? (["master",$relational_db_response->[0]->[0]]) : $relational_db_response->[0]->[0];
1750 :     }
1751 :     }
1752 :    
1753 :     return $wantarray ? () : "";
1754 :     }
1755 :    
1756 :     =pod
1757 :    
1758 :     =head1 translated_function_of
1759 :    
1760 :     usage: $function = $fig->translated_function_of($peg,$user)
1761 :    
1762 :     You get just the translated function.
1763 :    
1764 :     =cut
1765 :    
1766 :     sub translated_function_of {
1767 :     my($self,$id,$user) = @_;
1768 :    
1769 :     my $func = $self->function_of($id,$user);
1770 :     if ($func)
1771 :     {
1772 :     $func = $self->translate_function($func);
1773 :     }
1774 :     return $func;
1775 :     }
1776 :    
1777 :    
1778 :     sub extract_by_who {
1779 :     my($xL,$who) = @_;
1780 :     my($i);
1781 :    
1782 :     for ($i=0; ($i < @$xL) && ($xL->[$i]->[0] ne $who); $i++) {}
1783 :     return ($i < @$xL) ? $xL->[$i]->[1] : "";
1784 :     }
1785 :    
1786 :    
1787 :     =pod
1788 :    
1789 :     =head1 translate_function
1790 :    
1791 :     usage: $translated_func = $fig->translate_function($func)
1792 :    
1793 :     Translates a function based on the function.synonyms table.
1794 :    
1795 :     =cut
1796 :    
1797 :     sub translate_function {
1798 :     my($self,$function) = @_;
1799 :    
1800 :     my ($tran,$from,$to,$line);
1801 :     if (! ($tran = $self->{_function_translation}))
1802 :     {
1803 :     $tran = {};
1804 :     if (open(TMP,"<$FIG_Config::global/function.synonyms"))
1805 :     {
1806 :     while (defined($line = <TMP>))
1807 :     {
1808 :     chop $line;
1809 :     ($from,$to) = split(/\t/,$line);
1810 :     $tran->{$from} = $to;
1811 :     }
1812 :     close(TMP);
1813 :     }
1814 :     $self->{_function_translation} = $tran;
1815 :     }
1816 :     $to = $tran->{$function};
1817 :     return $to ? $to : $function;
1818 :     }
1819 :    
1820 :     =pod
1821 :    
1822 :     =head1 assign_function
1823 :    
1824 :     usage: $fig->assign_function($peg,$user,$function,$confidence)
1825 :    
1826 :     Assigns a function. Note that confidence can (and should be if unusual) included.
1827 :     Note that no annotation is written. This should normally be done in a separate
1828 :     call of the form
1829 :    
1830 :    
1831 :    
1832 :     =cut
1833 :    
1834 :     sub assign_function {
1835 :     my($self,$peg,$user,$function,$confidence) = @_;
1836 :     my($role,$roleQ);
1837 :    
1838 :     my $rdbH = $self->db_handle;
1839 :     $confidence = $confidence ? $confidence : "";
1840 :     my $genome = $self->genome_of($peg);
1841 :    
1842 :     $rdbH->SQL("DELETE FROM assigned_functions WHERE ( prot = \'$peg\' AND made_by = \'$user\' )");
1843 :    
1844 :     my $funcQ = quotemeta $function;
1845 :     $rdbH->SQL("INSERT INTO assigned_functions ( prot, made_by, assigned_function, quality, org ) VALUES ( \'$peg\', \'$user\', \'$funcQ\', \'$confidence\', \'$genome\' )");
1846 :     $rdbH->SQL("DELETE FROM roles WHERE ( prot = \'$peg\' AND made_by = \'$user\' )");
1847 :    
1848 :     foreach $role (&roles_of_function($function))
1849 :     {
1850 :     $roleQ = quotemeta $role;
1851 :     $rdbH->SQL("INSERT INTO roles ( prot, role, made_by, org ) VALUES ( \'$peg\', '$roleQ\', \'$user\', \'$genome\' )");
1852 :     }
1853 :    
1854 :     &verify_dir("$FIG_Config::organisms/$genome/UserModels");
1855 :     if ($user ne "master")
1856 :     {
1857 :     &verify_dir("$FIG_Config::organisms/$genome/UserModels/$user");
1858 :     }
1859 :    
1860 :     if ((($user eq "master") && open(TMP,">>$FIG_Config::organisms/$genome/assigned_functions")) ||
1861 :     (($user ne "master") && open(TMP,">>$FIG_Config::organisms/$genome/UserModels/$user/assigned_functions")))
1862 :     {
1863 :     flock(TMP,LOCK_EX) || confess "cannot lock assigned_functions";
1864 :     seek(TMP,0,2) || confess "failed to seek to the end of the file";
1865 :     print TMP "$peg\t$function\t$confidence\n";
1866 :     close(TMP);
1867 :     return 1;
1868 :     }
1869 :     return 0;
1870 :     }
1871 :    
1872 :     sub hypo {
1873 :     my $x = (@_ == 1) ? $_[0] : $_[1];
1874 :    
1875 :     return ((! $x) ||
1876 :     ($x =~ /hypoth/i) ||
1877 :     ($x =~ /,.*genes/i) ||
1878 :     ($x =~ /identical/i) ||
1879 :     ($x =~ /\bregion\b/i) ||
1880 :     ($x =~ /\bcomplete cds\b/i) ||
1881 :     ($x =~ /\breading frame\b/i) ||
1882 :     ($x =~ /\bsimilar to hypo\b/i) ||
1883 :     ($x =~ /cl\.41\b/i) ||
1884 :     ($x =~ /HD-GYP domain/i) ||
1885 :     ($x =~ /SI:bY1/i) ||
1886 :     ($x =~ /defext in/i) ||
1887 :     ($x =~ /^(expressed|conserved) protein$/i) ||
1888 :     ($x =~ /gene \d/i) ||
1889 :     ($x =~ /^[a-zA-Z]{2,4}\d{2,8}/) ||
1890 :     ($x =~ /\d{3}.pep/i) ||
1891 :     ($x =~ /\bFROM\b/i) ||
1892 :     ($x =~ /\bA\.L/i) ||
1893 :     ($x =~ /\bA\d\d/i) ||
1894 :     ($x =~ /^C$/i) ||
1895 :     ($x =~ /^\([A-Z]+\d+\)$/) ||
1896 :     ($x =~ /dna fragment/i) ||
1897 :     ($x =~ /Rv\d+[a-z](-like)?\b/i) ||
1898 :     ($x =~ /\bORF_/i) ||
1899 :     # ($x =~ /conserved protein\b/) ||
1900 :     ($x =~ /^[XY]\d\S+/i) ||
1901 :     ($x =~ /^[Yy][a-z]{2}[A-Z]/) ||
1902 :     ($x =~ /^[Yy][A-Z]{3}\b/) ||
1903 :     ($x =~ /weak similarity/i) ||
1904 :     ($x =~ /similar to/i) ||
1905 :     ($x =~ /gene product/i) ||
1906 :     ($x =~ /ORF_/) ||
1907 :     ($x =~ /NO SWISS-PROT/) ||
1908 :     ($x =~ /predicted coding/i) ||
1909 :     ($x =~ /predicted protein/i) ||
1910 :     ($x =~ /predicted by/i) ||
1911 :     ($x =~ /pct identical/i) ||
1912 :     ($x =~ /\borf\d+/i) ||
1913 :     ($x =~ /\bcosmid\d+\b/i) ||
1914 :     ($x =~ /^[a-zA-Z0-9]+\d+[a-z]?$/i) ||
1915 :     ($x =~ /^[a-zA-Z0-9]+[\.-]\d+[a-z]?$/i) ||
1916 :     ($x =~ /^[a-zA-Z0-9]+[\.-]\d+[a-z]?\s+PROTEIN$/i) ||
1917 :     ($x =~ /^cosmid\s+\S+$/i) ||
1918 :     ($x =~ /^\([A-Z0-9]+\) [A-Z][a-z]{2}[a-zA-Z] \[\S+ \S+\]\s*$/) ||
1919 :     ($x =~ /region orf/i) ||
1920 :     ($x =~ /unnamed protein product/i) ||
1921 :     ($x =~ /^[A-Z][0-9\.]{3,10}\S+ protein/i) ||
1922 :     ($x =~ /HYDROPHOBIC PROTEIN/) ||
1923 :     ($x =~ /\bORF\b/i) ||
1924 :     ($x =~ /\b[a-zB-Z]\d{3,10}\b/i) ||
1925 :     ($x =~ /protein similarity/) ||
1926 :     ($x =~ /Uncharacterized/) ||
1927 :     ($x =~ /UNIDENTIFIED/) ||
1928 :     ($x =~ /belongs to the family/) ||
1929 :     ($x =~ /predicted protein/) ||
1930 :     ($x =~ /1-EVIDENCE=PREDICTED BY MATCH/) ||
1931 :     ($x =~ /INTERGENIC REGION/) ||
1932 :     ($x =~ /NO SWISS-PROT SIMILARITIES/) ||
1933 :     ($x =~ /no known similarities/) ||
1934 :     ($x =~ /alternate gene name/) ||
1935 :     ($x =~ /alternate open reading frame/) ||
1936 :     ($x =~ /similar to GenBank Accession Number/) ||
1937 :     ($x =~ /family with/) ||
1938 :     ($x =~ /No definition/) ||
1939 :     ($x =~ /id:/i) ||
1940 :     ($x =~ /cDNA/) ||
1941 :     ($x =~ /SP:/) ||
1942 :     ($x =~ /COMPLETE CDS/) ||
1943 :     ($x =~ /GENE CLUSTER/) ||
1944 :     ($x =~ /\dp,Lp/) ||
1945 :     ($x =~ /3\' END/) ||
1946 :     ($x =~ /START CODON/) ||
1947 :     ($x =~ /_\S+_/) ||
1948 :     ($x =~ /GTG START/i) ||
1949 :     ($x =~ /TTG START/i) ||
1950 :     ($x =~ /chain length determinant/i) ||
1951 :     ($x =~ /f135/i) ||
1952 :     ($x =~ /KDA PROTEIN/i) ||
1953 :     ($x =~ /yole/i) ||
1954 :     ($x =~ /\bMAP\b/) ||
1955 :     ($x =~ /\(\d+-\d+\)/i) ||
1956 :     ($x =~ /D9719.36p/i) ||
1957 :     ($x =~ /THYMOCYTE PROTEIN CTHY28KD/i) ||
1958 :     ($x =~ /PHAC1, PHAC2 AND PHAD GENES/i) ||
1959 :     ($x =~ /OR23peptide/i) ||
1960 :     ($x =~ /\(AE/i) ||
1961 :     ($x =~ /Bem3p,Lph12p/i) ||
1962 :     ($x =~ /Rlm1p,Lpg19p/i) ||
1963 :     ($x =~ /unnamed/i) ||
1964 :     ($x =~ /\b\d{3,20}/i) ||
1965 :     ($x =~ /orf\d{2,20}/i) ||
1966 :     ($x =~ /\d{3,20}\b/i) ||
1967 :     ($x =~ /Intergenic-region/i) ||
1968 :     ($x =~ /and \d+ orf/i) ||
1969 :     ($x =~ /domain protein/i) ||
1970 :     ($x =~ /protein \d{2}[A-Z]{1,3}\d+/i) ||
1971 :     ($x =~ /\bTll\d{3,5}/i) ||
1972 :     ($x =~ /unknown/i));
1973 :     }
1974 :    
1975 :     ############################ Similarities ###############################
1976 :    
1977 :     =pod
1978 :    
1979 :     =head1 sims
1980 :    
1981 :     usage: @sims = $fig->sims($peg,$maxN,$maxP,$select)
1982 :    
1983 :     Returns a list of similarities for $peg such that
1984 :    
1985 :     there will be at most $maxN similarities,
1986 :    
1987 :     each similarity will have a P-score <= $maxP, and
1988 :    
1989 :     $select gives processing instructions:
1990 :    
1991 :     "raw" means that the similarities will not be expanded (by far fastest option)
1992 :     "fig" means return only similarities to fig genes
1993 :     "all" means that you want all the expanded similarities.
1994 :    
1995 :     By "expanded", we refer to taking a "raw similarity" against an entry in the non-redundant
1996 :     protein collection, and converting it to a set of similarities (one for each of the
1997 :     proteins that are essentially identical to the representative in the nr).
1998 :    
1999 :     =cut
2000 :    
2001 :     sub sims {
2002 :     my ($self,$id,$maxN,$maxP,$select) = @_;
2003 :     my($sim);
2004 :    
2005 :     my @sims = ();
2006 :     my @maps_to = $self->mapped_prot_ids($id);
2007 :     if (@maps_to > 0)
2008 :     {
2009 :     my $rep_id = $maps_to[0]->[0];
2010 :     my @entry = grep { $_->[0] eq $id } @maps_to;
2011 :     if ((@entry == 1) && defined($entry[0]->[1]))
2012 :     {
2013 :     if ((! defined($maps_to[0]->[1])) ||
2014 :     (! defined($entry[0]->[1])))
2015 :     {
2016 :     print STDERR &Dumper(\@maps_to,\@entry);
2017 :     confess "bad";
2018 :     }
2019 :     my $delta = $maps_to[0]->[1] - $entry[0]->[1];
2020 :     my @raw_sims = &get_raw_sims($self,$rep_id,$maxN,$maxP);
2021 : efrank 1.2
2022 :     if ($id ne $rep_id)
2023 : efrank 1.1 {
2024 : efrank 1.2 foreach $sim (@raw_sims)
2025 :     {
2026 : efrank 1.1
2027 :     $sim->[0] = $id;
2028 :     $sim->[6] -= $delta;
2029 :     $sim->[7] -= $delta;
2030 :     }
2031 :     }
2032 : efrank 1.2 unshift(@raw_sims,bless([$id,$rep_id,100.00,undef,undef,undef,1,$entry[0]->[1],$delta+1,$maps_to[0]->[1],0.0,,undef,$entry[0]->[1],$maps_to[0]->[1],"blastp",0,0],'Sim'));
2033 :     @sims = grep { $_->id1 ne $_->id2 } &expand_raw_sims($self,\@raw_sims,$maxP,$select,0);
2034 : efrank 1.1 }
2035 :     }
2036 :     return @sims;
2037 :     }
2038 :    
2039 :     sub expand_raw_sims {
2040 :     my($self,$raw_sims,$maxP,$select,$dups) = @_;
2041 :     my($sim,$id2,%others,$x);
2042 :    
2043 :     my @sims = ();
2044 :     foreach $sim (@$raw_sims)
2045 :     {
2046 :     next if ($sim->psc > $maxP);
2047 :     $id2 = $sim->id2;
2048 :     next if ($others{$id2} && (! $dups));
2049 :     $others{$id2} = 1;
2050 :    
2051 :     if ($select && ($select eq "raw"))
2052 :     {
2053 :     push(@sims,$sim);
2054 :     }
2055 :     else
2056 :     {
2057 :     my @relevant;
2058 :     my @maps_to = $self->mapped_prot_ids($id2);
2059 :     if ((! $select) || ($select eq "fig"))
2060 :     {
2061 :     @relevant = grep { $_->[0] =~ /^fig/ } @maps_to;
2062 :     }
2063 :     elsif ($select && ($select =~ /^ext/i))
2064 :     {
2065 :     @relevant = grep { $_->[0] !~ /^fig/ } @maps_to;
2066 :     }
2067 :     else
2068 :     {
2069 :     @relevant = @maps_to;
2070 :     }
2071 :    
2072 :     foreach $x (@relevant)
2073 :     {
2074 :     my $sim1 = [@$sim];
2075 :     my($x_id,$x_ln) = @$x;
2076 :     defined($x_ln) || confess "x_ln id2=$id2 x_id=$x_id";
2077 :     defined($maps_to[0]->[1]) || confess "maps_to";
2078 :     my $delta2 = $maps_to[0]->[1] - $x_ln;
2079 :     $sim1->[1] = $x_id;
2080 :     $sim1->[8] -= $delta2;
2081 :     $sim1->[9] -= $delta2;
2082 :     bless($sim1,"Sim");
2083 :     push(@sims,$sim1);
2084 :     }
2085 :     }
2086 :     }
2087 :     return @sims;
2088 :     }
2089 :    
2090 :     sub get_raw_sims {
2091 :     my($self,$rep_id,$maxN,$maxP) = @_;
2092 :     my(@sims,$seek,$fileN,$ln,$fh,$file,$readN,$readC,@lines,$i,$sim);
2093 :     my($sim_chunk,$psc,$id2);
2094 :    
2095 :     $maxN = $maxN ? $maxN : 500;
2096 :    
2097 :     @sims = ();
2098 :     my $rdbH = $self->db_handle;
2099 :     my $relational_db_response = $rdbH->SQL("SELECT seek, fileN, len FROM sim_seeks WHERE id = \'$rep_id\' ");
2100 :     foreach $sim_chunk (@$relational_db_response)
2101 :     {
2102 :     ($seek,$fileN,$ln) = @$sim_chunk;
2103 :     $file = $self->N2file($fileN);
2104 :     $fh = $self->openF($file);
2105 :     if (! $fh)
2106 :     {
2107 :     confess "could not open sims for $file";
2108 :     }
2109 :     seek($fh,$seek,0);
2110 :     $readN = read($fh,$readC,$ln-1);
2111 :     ($readN == ($ln-1))
2112 :     || confess "could not read the block of sims at $seek for $ln - 1 characters; $readN actually read from $file\n$readC";
2113 :     @lines = grep {
2114 :     (@$_ == 15) &&
2115 :     ($_->[12] =~ /^\d+$/) &&
2116 :     ($_->[13] =~ /^\d+$/) &&
2117 :     ($_->[6] =~ /^\d+$/) &&
2118 :     ($_->[7] =~ /^\d+$/) &&
2119 :     ($_->[8] =~ /^\d+$/) &&
2120 :     ($_->[9] =~ /^\d+$/) &&
2121 :     ($_->[2] =~ /^[0-9.]+$/) &&
2122 :     ($_->[10] =~ /^[0-9.e-]+$/)
2123 :     }
2124 :     map { [split(/\t/,$_),"blastp"] }
2125 :     split(/\n/,$readC);
2126 :    
2127 :     @lines = sort { $a->[10] <=> $b->[10] } @lines;
2128 :    
2129 :     for ($i=0; ($i < @lines); $i++)
2130 :     {
2131 :     $psc = $lines[$i]->[10];
2132 :     $id2 = $lines[$i]->[1];
2133 :     if ($maxP >= $psc)
2134 :     {
2135 :     $sim = $lines[$i];
2136 :     bless($sim,"Sim");
2137 :     push(@sims,$sim);
2138 :     if (@sims == $maxN) { return @sims }
2139 :     }
2140 :     }
2141 :     }
2142 :     return @sims;
2143 :     }
2144 :    
2145 :     =pod
2146 :    
2147 :     =head1 dsims
2148 :    
2149 :     usage: @sims = $fig->dsims($peg,$maxN,$maxP,$select)
2150 :    
2151 :     Returns a list of similarities for $peg such that
2152 :    
2153 :     there will be at most $maxN similarities,
2154 :    
2155 :     each similarity will have a P-score <= $maxP, and
2156 :    
2157 :     $select gives processing instructions:
2158 :    
2159 :     "raw" means that the similarities will not be expanded (by far fastest option)
2160 :     "fig" means return only similarities to fig genes
2161 :     "all" means that you want all the expanded similarities.
2162 :    
2163 :     By "expanded", we refer to taking a "raw similarity" against an entry in the non-redundant
2164 :     protein collection, and converting it to a set of similarities (one for each of the
2165 :     proteins that are essentially identical to the representative in the nr).
2166 :    
2167 :     The "dsims" or "dynamic sims" are not precomputed. They are computed using a heuristic which
2168 :     is much faster than blast, but misses some similarities. Essentially, you have an "index" or
2169 :     representative sequences, a quick blast is done against it, and if there are any hits these are
2170 :     used to indicate which sub-databases to blast against.
2171 :    
2172 :     =cut
2173 :    
2174 :     sub dsims {
2175 :     my($self,$id,$seq,$maxN,$maxP,$select) = @_;
2176 :     my($sim,$sub_dir,$db,$hit,@hits,%in);
2177 :    
2178 :     my @index = &blastit($id,$seq,"$FIG_Config::global/SimGen/exemplar.fasta",1.0e-3);
2179 :     foreach $sim (@index)
2180 :     {
2181 :     if ($sim->id2 =~ /_(\d+)$/)
2182 :     {
2183 :     $in{$1}++;
2184 :     }
2185 :     }
2186 :    
2187 :     @hits = ();
2188 :     foreach $db (keys(%in))
2189 :     {
2190 :     $sub_dir = $db % 1000;
2191 :     push(@hits,&blastit($id,$seq,"$FIG_Config::global/SimGen/AccessSets/$sub_dir/$db",$maxP));
2192 :    
2193 :     }
2194 :    
2195 :     if (@hits == 0)
2196 :     {
2197 :     push(@hits,&blastit($id,$seq,"$FIG_Config::global/SimGen/nohit.fasta",$maxP));
2198 :     }
2199 :    
2200 :     @hits = sort { ($a->psc <=> $b->psc) or ($a->iden cmp $b->iden) } grep { $_->id2 ne $id } @hits;
2201 :     if ($maxN && ($maxN < @hits)) { $#hits = $maxN - 1 }
2202 :     return &expand_raw_sims($self,\@hits,$maxP,$select,0);
2203 :     }
2204 :    
2205 :     sub blastit {
2206 :     my($id,$seq,$db,$maxP) = @_;
2207 :    
2208 :     if (! $maxP) { $maxP = 1.0e-5 }
2209 :     my $tmp = &Blast::blastp([[$id,$seq]],$db,"-e $maxP");
2210 :     my $tmp1 = $tmp->{$id};
2211 :     if ($tmp1)
2212 :     {
2213 :     return @$tmp1;
2214 :     }
2215 :     return ();
2216 :     }
2217 :    
2218 :     ################################# chromosomal clusters ####################################
2219 :    
2220 :     =pod
2221 :    
2222 :     =head1 in_cluster_with
2223 :    
2224 :     usage: @pegs = $fig->in_cluster_with($peg)
2225 :    
2226 :     Returns the set of pegs that are thought to be clustered with $peg (on the
2227 :     chromosome).
2228 :    
2229 :     =cut
2230 :    
2231 :     sub in_cluster_with {
2232 :     my($self,$peg) = @_;
2233 :     my($set,$id,%in);
2234 :    
2235 :     return $self->in_set_with($peg,"chromosomal_clusters","cluster_id");
2236 :     }
2237 :    
2238 :     =pod
2239 :    
2240 :     =head1 add_chromosomal_clusters
2241 :    
2242 :     usage: $fig->add_chromosomal_clusters($file)
2243 :    
2244 :     The given file is supposed to contain one predicted chromosomal cluster per line (either
2245 :     comma or tab separated pegs). These will be added (to the extent they are new) to those
2246 :     already in $FIG_Config::global/chromosomal_clusters.
2247 :    
2248 :     =cut
2249 :    
2250 :    
2251 :     sub add_chromosomal_clusters {
2252 :     my($self,$file) = @_;
2253 :     my($set,$added);
2254 :    
2255 :     open(TMPCLUST,"<$file")
2256 :     || die "aborted";
2257 :     while (defined($set = <TMPCLUST>))
2258 :     {
2259 :     print STDERR ".";
2260 :     chop $set;
2261 :     $added += $self->add_chromosomal_cluster([split(/[\t,]+/,$set)]);
2262 :     }
2263 :     close(TMPCLUST);
2264 :    
2265 :     if ($added)
2266 :     {
2267 :     my $rdbH = $self->db_handle;
2268 :     $self->export_set("chromosomal_clusters","cluster_id","$FIG_Config::global/chromosomal_clusters");
2269 :     return 1;
2270 :     }
2271 :     return 0;
2272 :     }
2273 :    
2274 :     #=pod
2275 :     #
2276 :     #=head1 export_chromosomal_clusters
2277 :     #
2278 :     #usage: $fig->export_chromosomal_clusters
2279 :     #
2280 :     #Invoking this routine writes the set of chromosomal clusters as known in the
2281 :     #relational DB back to $FIG_Config::global/chromosomal_clusters.
2282 :     #
2283 :     #=cut
2284 :     #
2285 :     sub export_chromosomal_clusters {
2286 :     my($self) = @_;
2287 :    
2288 :     $self->export_set("chromosomal_clusters","cluster_id","$FIG_Config::global/chromosomal_clusters");
2289 :     }
2290 :    
2291 :     sub add_chromosomal_cluster {
2292 :     my($self,$ids) = @_;
2293 :     my($id,$set,%existing,%in,$new,$existing,$new_id);
2294 :    
2295 :     # print STDERR "adding cluster ",join(",",@$ids),"\n";
2296 :     foreach $id (@$ids)
2297 :     {
2298 :     foreach $set ($self->in_sets($id,"chromosomal_clusters","cluster_id"))
2299 :     {
2300 :     $existing{$set} = 1;
2301 :     foreach $id ($self->ids_in_set($set,"chromosomal_clusters","cluster_id"))
2302 :     {
2303 :     $in{$id} = 1;
2304 :     }
2305 :     }
2306 :     }
2307 :     # print &Dumper(\%existing,\%in);
2308 :    
2309 :     $new = 0;
2310 :     foreach $id (@$ids)
2311 :     {
2312 :     if (! $in{$id})
2313 :     {
2314 :     $in{$id} = 1;
2315 :     $new++;
2316 :     }
2317 :     }
2318 :     # print STDERR "$new new ids\n";
2319 :     if ($new)
2320 :     {
2321 :     foreach $existing (keys(%existing))
2322 :     {
2323 :     $self->delete_set($existing,"chromosomal_clusters","cluster_id");
2324 :     }
2325 :     $new_id = $self->next_set("chromosomal_clusters","cluster_id");
2326 :     # print STDERR "adding new cluster $new_id\n";
2327 :     $self->insert_set($new_id,[keys(%in)],"chromosomal_clusters","cluster_id");
2328 :     return 1;
2329 :     }
2330 :     return 0;
2331 :     }
2332 :    
2333 :     ################################# PCH pins ####################################
2334 :    
2335 :     =pod
2336 :    
2337 :     =head1 in_pch_pin_with
2338 :    
2339 :     usage: $fig->in_pch_pin_with($peg)
2340 :    
2341 :     Returns the set of pegs that are believed to be "pinned" to $peg (in the
2342 :     sense that PCHs occur containing these pegs over significant phylogenetic
2343 :     distances).
2344 :    
2345 :     =cut
2346 :    
2347 :     sub in_pch_pin_with {
2348 :     my($self,$peg) = @_;
2349 :     my($set,$id,%in);
2350 :    
2351 :     return $self->in_set_with($peg,"pch_pins","pin");
2352 :     }
2353 :    
2354 :     =pod
2355 :    
2356 :     =head1 add_pch_pins
2357 :    
2358 :     usage: $fig->add_pch_pins($file)
2359 :    
2360 :     The given file is supposed to contain one set of pinned pegs per line (either
2361 :     comma or tab seprated pegs). These will be added (to the extent they are new) to those
2362 :     already in $FIG_Config::global/pch_pins.
2363 :    
2364 :     =cut
2365 :    
2366 :     sub add_pch_pins {
2367 :     my($self,$file) = @_;
2368 :     my($set,$added);
2369 :    
2370 :     open(TMPCLUST,"<$file")
2371 :     || die "aborted";
2372 :     while (defined($set = <TMPCLUST>))
2373 :     {
2374 :     print STDERR ".";
2375 :     chop $set;
2376 :     my @tmp = split(/[\t,]+/,$set);
2377 :     if (@tmp < 200)
2378 :     {
2379 :     $added += $self->add_pch_pin([@tmp]);
2380 :     }
2381 :     }
2382 :     close(TMPCLUST);
2383 :    
2384 :     if ($added)
2385 :     {
2386 :     my $rdbH = $self->db_handle;
2387 :     $self->export_set("pch_pins","pin","$FIG_Config::global/pch_pins");
2388 :     return 1;
2389 :     }
2390 :     return 0;
2391 :     }
2392 :    
2393 :     sub export_pch_pins {
2394 :     my($self) = @_;
2395 :    
2396 :     $self->export_set("pch_pins","pin","$FIG_Config::global/pch_pins");
2397 :     }
2398 :    
2399 :     sub add_pch_pin {
2400 :     my($self,$ids) = @_;
2401 :     my($id,$set,%existing,%in,$new,$existing,$new_id);
2402 :    
2403 :     # print STDERR "adding cluster ",join(",",@$ids),"\n";
2404 :     foreach $id (@$ids)
2405 :     {
2406 :     foreach $set ($self->in_sets($id,"pch_pins","pin"))
2407 :     {
2408 :     $existing{$set} = 1;
2409 :     foreach $id ($self->ids_in_set($set,"pch_pins","pin"))
2410 :     {
2411 :     $in{$id} = 1;
2412 :     }
2413 :     }
2414 :     }
2415 :     # print &Dumper(\%existing,\%in);
2416 :    
2417 :     $new = 0;
2418 :     foreach $id (@$ids)
2419 :     {
2420 :     if (! $in{$id})
2421 :     {
2422 :     $in{$id} = 1;
2423 :     $new++;
2424 :     }
2425 :     }
2426 :    
2427 :     if ($new)
2428 :     {
2429 :     foreach $existing (keys(%existing))
2430 :     {
2431 :     $self->delete_set($existing,"pch_pins","pin");
2432 :     }
2433 :     $new_id = $self->next_set("pch_pins","pin");
2434 :     # print STDERR "adding new pin $new_id\n";
2435 :     $self->insert_set($new_id,[keys(%in)],"pch_pins","pin");
2436 :     return 1;
2437 :     }
2438 :     return 0;
2439 :     }
2440 :    
2441 :     ################################# Annotations ####################################
2442 :    
2443 :     =pod
2444 :    
2445 :     =head1 add_annotation
2446 :    
2447 :     usage: $fig->add_annotation($fid,$user,$annotation)
2448 :    
2449 :     $annotation is added as a time-stamped annotation to $peg showing $user as the
2450 :     individual who added the annotation.
2451 :    
2452 :     =cut
2453 :    
2454 :     sub add_annotation {
2455 :     my($self,$feature_id,$user,$annotation) = @_;
2456 :     my($genome);
2457 :    
2458 :     # print STDERR "add: fid=$feature_id user=$user annotation=$annotation\n";
2459 :     if ($genome = $self->genome_of($feature_id))
2460 :     {
2461 :     my $file = "$FIG_Config::organisms/$genome/annotations";
2462 :     my $fileno = $self->file2N($file);
2463 :     my $time_made = time;
2464 :    
2465 :     if (open(TMP,">>$file"))
2466 :     {
2467 :     flock(TMP,LOCK_EX) || confess "cannot lock assigned_functions";
2468 :     seek(TMP,0,2) || confess "failed to seek to the end of the file";
2469 :    
2470 :     my $seek1 = tell TMP;
2471 :     print TMP "$feature_id\n$time_made\n$user\n$annotation", (substr($annotation,-1) eq "\n") ? "" : "\n","//\n";
2472 :     my $seek2 = tell TMP;
2473 :     close(TMP);
2474 :     chmod 0777, $file;
2475 :     my $ln = $seek2 - $seek1;
2476 :     my $rdbH = $self->db_handle;
2477 :     if ($rdbH->SQL("INSERT INTO annotation_seeks ( fid, fileno, seek, len ) VALUES ( \'$feature_id\', $fileno, $seek1, $ln )"))
2478 :     {
2479 :     return 1;
2480 :     }
2481 :     }
2482 :     }
2483 :     return 0;
2484 :     }
2485 :    
2486 :     =pod
2487 :    
2488 :     =head1 feature_annotations
2489 :    
2490 :     usage: @annotations = $fig->feature_annotations($fid)
2491 :    
2492 :     The set of annotations of $fid is returned as a list of 4-tuples. Each entry in the list
2493 :     is of the form [$fid,$timestamp,$user,$annotation].
2494 :    
2495 :     =cut
2496 :    
2497 :    
2498 :     sub feature_annotations {
2499 :     my($self,$feature_id) = @_;
2500 :     my($tuple,$fileN,$seek,$ln,$readN,$readC,$annotation,$feature_idQ);
2501 :     my($file,$fh);
2502 :    
2503 :     my $rdbH = $self->db_handle;
2504 :     my $relational_db_response = $rdbH->SQL("SELECT fileno, seek, len FROM annotation_seeks WHERE fid = \'$feature_id\' ");
2505 :     my @annotations = ();
2506 :    
2507 :     foreach $tuple (@$relational_db_response)
2508 :     {
2509 :     ($fileN,$seek,$ln) = @$tuple;
2510 :     $file = $self->N2file($fileN);
2511 :     $fh = $self->openF($file);
2512 :     if (! $fh)
2513 :     {
2514 :     confess "could not open annotations for $file";
2515 :     }
2516 :     seek($fh,$seek,0);
2517 :     $readN = read($fh,$readC,$ln);
2518 :     ($readN == $ln)
2519 :     || confess "could not read the block of annotations at $seek for $ln characters; $readN actually read from $file\n$readC";
2520 :     $feature_idQ = quotemeta $feature_id;
2521 :     foreach $annotation (split(/\n\/\/\n/, $readC))
2522 :     {
2523 :     if ($annotation =~ /^$feature_idQ\n(\d+)\n([^\n]+)\n(.*)/s)
2524 :     {
2525 :     push(@annotations,[$feature_id,$1,$2,$3]);
2526 :     }
2527 :     else
2528 :     {
2529 :     print STDERR "malformed annotation\n$annotation\n";
2530 :     }
2531 :     }
2532 :     }
2533 :     return map { $_->[1] = localtime($_->[1]); $_ } sort { $a->[1] <=> $b->[1] } @annotations;
2534 :     }
2535 :    
2536 :     ################################# Indexing Features and Functional Roles ####################################
2537 :    
2538 :     =pod
2539 :    
2540 :     =head1 search_index
2541 :    
2542 :     usage: ($pegs,$roles) = $fig->search_pattern($pattern)
2543 :    
2544 :     All pegs that "match" $pattern are put into a list, and $pegs will be a
2545 :     pointer to that list.
2546 :    
2547 :     All roles that "match" $pattern are put into a list, and $roles will be a
2548 :     pointer to that list.
2549 :    
2550 :     The notion of "match $pattern" is intentionally left undefined. For now, you
2551 :     will probably get only entries in which each word id $pattern occurs exactly,
2552 :     but that is not a long term commitment.
2553 :    
2554 :     =cut
2555 :    
2556 :     sub search_index {
2557 :     my($self,$pattern) = @_;
2558 :     my($patternQ,@raw,@pegs,@roles);
2559 :    
2560 :     &clean_tmp;
2561 :     $patternQ = $pattern;
2562 :     $patternQ =~ s/\s+/;/g;
2563 :     $patternQ =~ s/\./\\./g;
2564 :    
2565 :     # print STDERR "pattern=$pattern patternQ=$patternQ\n";
2566 :     @raw = `$FIG_Config::ext_bin/glimpse -y -H $FIG_Config::data/Indexes -i -w \'$patternQ\'`;
2567 :     @pegs = sort { &FIG::by_fig_id($a->[0],$b->[0]) }
2568 :     map { $_ =~ s/^\S+:\s+//; [split(/\t/,$_)] }
2569 :     grep { $_ =~ /^\S+peg.index/ } @raw;
2570 :     my %roles = map { $_ =~ s/^\S+:\s+//; $_ => 1} grep { $_ =~ /^\S+role.index/ } @raw;
2571 :     @roles = sort keys(%roles);
2572 :    
2573 :     return ([@pegs],[@roles]);
2574 :     }
2575 :    
2576 :     ################################# Loading Databases ####################################
2577 :    
2578 :    
2579 :     #=pod
2580 :     #
2581 :     #=head1 load_all
2582 :     #
2583 :     #usage: load_all
2584 :     #
2585 :     #This function is supposed to reload all entries into the database and do
2586 :     #whatever is required to properly support indexing of pegs and roles.
2587 :     #
2588 :     #=cut
2589 :    
2590 :     sub load_all {
2591 :    
2592 :     &run("load_features");
2593 :     &run("index_sims");
2594 :     &run("load_peg_mapping");
2595 :     &run("index_translations");
2596 :     &run("add_assertions_of_function");
2597 :     &run("load_protein_families");
2598 :     &run("load_external_orgs");
2599 :     &run("load_chromosomal_clusters");
2600 :     &run("load_pch_pins");
2601 :     &run("index_neighborhoods");
2602 :     &run("index_annotations");
2603 :     &run("load_ec_names");
2604 :     &run("load_kegg");
2605 :     &run("index_contigs");
2606 :     &run("make_indexes");
2607 :     }
2608 :    
2609 :     ################################# Automated Assignments ####################################
2610 :    
2611 :     =pod
2612 :    
2613 :     =head1 auto_assign
2614 :    
2615 :     usage: $assignment = &FIG::auto_assign($peg,$seq)
2616 :    
2617 :     This returns an automated assignment for $peg. $seq is optional; if it is not
2618 :     present, then it is assumed that similarities already exist for $peg. $assignment is set
2619 :     to either
2620 :    
2621 :     Function
2622 :     or
2623 :     Function\tW
2624 :    
2625 :     if it is felt that the assertion is pretty weak.
2626 :    
2627 :     =cut
2628 :    
2629 :     sub auto_assign {
2630 :     my($peg,$seq) = @_;
2631 :    
2632 :     my $cmd = $seq ? "echo \"$peg\t$seq\" | auto_assign | make_calls" : "echo \"$peg\" | auto_assign | make_calls";
2633 :     # print STDERR $cmd;
2634 :     my(@tmp) = `$cmd`;
2635 :     if ((@tmp == 1) && ($tmp[0] =~ /^\S+\t(\S.*\S)/))
2636 :     {
2637 :     return $1;
2638 :     }
2639 :     else
2640 :     {
2641 :     return "hypothetical protein";
2642 :     }
2643 :     }
2644 :    
2645 :     ################################# Protein Families ####################################
2646 :    
2647 :     =pod
2648 :    
2649 :     =head1 all_protein_families
2650 :    
2651 :     usage: @all = $fig->all_protein_families
2652 :    
2653 :     Returns a list of the ids of all of the protein families currently defined.
2654 :    
2655 :     =cut
2656 :    
2657 :     sub all_protein_families {
2658 :     my($self) = @_;
2659 :    
2660 :     return $self->all_sets("protein_families","family");
2661 :     }
2662 :    
2663 :     =pod
2664 :    
2665 :     =head1 ids_in_family
2666 :    
2667 :     usage: @pegs = $fig->ids_in_family($family)
2668 :    
2669 :     Returns a list of the pegs in $family.
2670 :    
2671 :     =cut
2672 :    
2673 :     sub ids_in_family {
2674 :     my($self,$family) = @_;
2675 :    
2676 :     return $self->ids_in_set($family,"protein_families","family");
2677 :     }
2678 :    
2679 :     =pod
2680 :    
2681 :     =head1 family_function
2682 :    
2683 :     usage: $func = $fig->family_function($family)
2684 :    
2685 :     Returns the putative function of all of the pegs in $family. Remember, we
2686 :     are defining "protein family" as a set of homologous proteins that have the
2687 :     same function.
2688 :    
2689 :     =cut
2690 :    
2691 :     sub family_function {
2692 :     my($self,$family) = @_;
2693 :     my($relational_db_response);
2694 :     my $rdbH = $self->db_handle;
2695 :    
2696 :     defined($family) || confess "family is missing";
2697 :     if (($relational_db_response = $rdbH->SQL("SELECT function FROM family_function WHERE ( family = $family)")) &&
2698 :     (@$relational_db_response >= 1))
2699 :     {
2700 :     return $relational_db_response->[0]->[0];
2701 :     }
2702 :     return "";
2703 :     }
2704 :    
2705 :     =pod
2706 :    
2707 :     =head1 sz_family
2708 :    
2709 :     usage: $n = $fig->sz_family($family)
2710 :    
2711 :     Returns the number of pegs in $family.
2712 :    
2713 :     =cut
2714 :    
2715 :     sub sz_family {
2716 :     my($self,$family) = @_;
2717 :    
2718 :     return $self->sz_set($family,"protein_families","family");
2719 :     }
2720 :    
2721 :     =pod
2722 :    
2723 :     =head1 in_family
2724 :    
2725 :     usage: @pegs = $fig->in_family($family)
2726 :    
2727 :     Returns the pegs in $family.
2728 :    
2729 :     =cut
2730 :    
2731 :     sub in_family {
2732 :     my($self,$id) = @_;
2733 :    
2734 :     my @in = $self->in_sets($id,"protein_families","family");
2735 :     return (@in > 0) ? $in[0] : "";
2736 :     }
2737 :    
2738 :     ################################# Abstract Set Routines ####################################
2739 :    
2740 :     sub all_sets {
2741 :     my($self,$relation,$set_name) = @_;
2742 :     my($relational_db_response);
2743 :    
2744 :     my $rdbH = $self->db_handle;
2745 :    
2746 :     if (($relational_db_response = $rdbH->SQL("SELECT DISTINCT $set_name FROM $relation")) &&
2747 :     (@$relational_db_response >= 1))
2748 :     {
2749 :     return map { $_->[0] } @$relational_db_response;
2750 :     }
2751 :     return ();
2752 :     }
2753 :    
2754 :     sub next_set {
2755 :     my($self,$relation,$set_name) = @_;
2756 :     my($relational_db_response);
2757 :    
2758 :     my $rdbH = $self->db_handle;
2759 :    
2760 :     if (($relational_db_response = $rdbH->SQL("SELECT MAX($set_name) FROM $relation")) &&
2761 :     (@$relational_db_response == 1))
2762 :     {
2763 :     return $relational_db_response->[0]->[0] + 1;
2764 :     }
2765 :     }
2766 :    
2767 :     sub ids_in_set {
2768 :     my($self,$which,$relation,$set_name) = @_;
2769 :     my($relational_db_response);
2770 :    
2771 :     my $rdbH = $self->db_handle;
2772 :     if (defined($which) && ($which =~ /^\d+$/))
2773 :     {
2774 :     if (($relational_db_response = $rdbH->SQL("SELECT id FROM $relation WHERE ( $set_name = $which)")) &&
2775 :     (@$relational_db_response >= 1))
2776 :     {
2777 :     return sort { by_fig_id($a,$b) } map { $_->[0] } @$relational_db_response;
2778 :     }
2779 :     }
2780 :     return ();
2781 :     }
2782 :    
2783 :     sub in_sets {
2784 :     my($self,$id,$relation,$set_name) = @_;
2785 :     my($relational_db_response);
2786 :    
2787 :     my $rdbH = $self->db_handle;
2788 :    
2789 :     if (($relational_db_response = $rdbH->SQL("SELECT $set_name FROM $relation WHERE ( id = \'$id\' )")) &&
2790 :     (@$relational_db_response >= 1))
2791 :     {
2792 :     return map { $_->[0] } @$relational_db_response;
2793 :     }
2794 :     return ();
2795 :     }
2796 :    
2797 :     sub sz_set {
2798 :     my($self,$which,$relation,$set_name) = @_;
2799 :     my($relational_db_response);
2800 :    
2801 :     my $rdbH = $self->db_handle;
2802 :    
2803 :     if (($relational_db_response = $rdbH->SQL("SELECT COUNT(*) FROM $relation WHERE ( $set_name = $which)")) &&
2804 :     (@$relational_db_response == 1))
2805 :     {
2806 :     return $relational_db_response->[0]->[0];
2807 :     }
2808 :     return 0;
2809 :     }
2810 :    
2811 :     sub delete_set {
2812 :     my($self,$set,$relation,$set_name) = @_;
2813 :    
2814 :     # print STDERR "deleting set $set\n";
2815 :     my $rdbH = $self->db_handle;
2816 :    
2817 :     return $rdbH->SQL("DELETE FROM $relation WHERE ( $set_name = $set )");
2818 :     }
2819 :    
2820 :     sub insert_set {
2821 :     my($self,$set,$ids,$relation,$set_name) = @_;
2822 :     my($id);
2823 :    
2824 :     # print STDERR "inserting set $set containing ",join(",",@$ids),"\n";
2825 :     my $rdbH = $self->db_handle;
2826 :    
2827 :     my $rc = 1;
2828 :     foreach $id (@$ids)
2829 :     {
2830 :     if (! $rdbH->SQL("INSERT INTO $relation ( $set_name,id ) VALUES ( $set,\'$id\' )"))
2831 :     {
2832 :     $rc = 0;
2833 :     }
2834 :     }
2835 :     # print STDERR " rc=$rc\n";
2836 :     return $rc;
2837 :     }
2838 :    
2839 :     sub in_set_with {
2840 :     my($self,$peg,$relation,$set_name) = @_;
2841 :     my($set,$id,%in);
2842 :    
2843 :     foreach $set ($self->in_sets($peg,$relation,$set_name))
2844 :     {
2845 :     foreach $id ($self->ids_in_set($set,$relation,$set_name))
2846 :     {
2847 :     $in{$id} = 1;
2848 :     }
2849 :     }
2850 :     return sort { &by_fig_id($a,$b) } keys(%in);
2851 :     }
2852 :    
2853 :    
2854 :     sub export_set {
2855 :     my($self,$relation,$set_name,$file) = @_;
2856 :     my($pair);
2857 :    
2858 :     my $rdbH = $self->db_handle;
2859 :     my $relational_db_response = $rdbH->SQL("SELECT $set_name, id FROM $relation");
2860 :    
2861 :     open(TMP,">$file")
2862 :     || die "could not open $file";
2863 :     flock(TMP,LOCK_EX) || confess "cannot lock $file";
2864 :     seek(TMP,0,2) || confess "failed to seek to the end of the file";
2865 :    
2866 :     foreach $pair (sort { ($a->[0] <=> $b->[0]) or &by_fig_id($a->[1],$b->[1]) } @$relational_db_response)
2867 :     {
2868 :     print TMP join("\t",@$pair),"\n";
2869 :     }
2870 :     close(TMP);
2871 :     return 1;
2872 :     }
2873 :    
2874 :     ################################# KEGG Stuff ####################################
2875 :    
2876 :    
2877 :     =pod
2878 :    
2879 :     =head1 all_compounds
2880 :    
2881 :     usage: @compounds = $fig->all_compounds
2882 :    
2883 :     Returns a list containing all of the KEGG compounds.
2884 :    
2885 :     =cut
2886 :    
2887 :     sub all_compounds {
2888 :     my($self) = @_;
2889 :    
2890 :     my $rdbH = $self->db_handle;
2891 :     my $relational_db_response = $rdbH->SQL("SELECT DISTINCT cid FROM comp_name");
2892 :     if (@$relational_db_response > 0)
2893 :     {
2894 :     return sort map { $_->[0] } @$relational_db_response;
2895 :     }
2896 :     return ();
2897 :     }
2898 :    
2899 :     =pod
2900 :    
2901 :     =head1 names_of_compound
2902 :    
2903 :     usage: @names = $fig->names_of_compound
2904 :    
2905 :     Returns a list containing all of the names assigned to the KEGG compounds. The list
2906 :     will be ordered as given by KEGG.
2907 :    
2908 :     =cut
2909 :    
2910 :     sub names_of_compound {
2911 :     my($self,$cid) = @_;
2912 :    
2913 :     my $rdbH = $self->db_handle;
2914 :     my $relational_db_response = $rdbH->SQL("SELECT pos,name FROM comp_name where cid = \'$cid\'");
2915 :     if (@$relational_db_response > 0)
2916 :     {
2917 :     return map { $_->[1] } sort { $a->[0] <=> $b->[0] } @$relational_db_response;
2918 :     }
2919 :     return ();
2920 :     }
2921 :    
2922 :     =pod
2923 :    
2924 :     =head1 comp2react
2925 :    
2926 :    
2927 :     usage: @rids = $fig->comp2react($cid)
2928 :    
2929 :     Returns a list containing all of the reaction IDs for reactions that take $cid
2930 :     as either a substrate or a product.
2931 :    
2932 :     =cut
2933 :    
2934 :     sub comp2react {
2935 :     my($self,$cid) = @_;
2936 :    
2937 :     my $rdbH = $self->db_handle;
2938 :     my $relational_db_response = $rdbH->SQL("SELECT rid FROM reaction_to_compound where cid = \'$cid\'");
2939 :     if (@$relational_db_response > 0)
2940 :     {
2941 :     return sort map { $_->[0] } @$relational_db_response;
2942 :     }
2943 :     return ();
2944 :     }
2945 :    
2946 :     =pod
2947 :    
2948 :     =head1 cas
2949 :    
2950 :     usage: $cas = $fig->cas($cid)
2951 :    
2952 :     Returns the CAS ID for the compound, if known.
2953 :    
2954 :     =cut
2955 :    
2956 :     sub cas {
2957 :     my($self,$cid) = @_;
2958 :    
2959 :     my $rdbH = $self->db_handle;
2960 :     my $relational_db_response = $rdbH->SQL("SELECT cas FROM comp_cas where cid = \'$cid\'");
2961 :     if (@$relational_db_response == 1)
2962 :     {
2963 :     return $relational_db_response->[0]->[0];
2964 :     }
2965 :     return "";
2966 :     }
2967 :    
2968 :     =pod
2969 :    
2970 :     =head1 cas_to_cid
2971 :    
2972 :     usage: $cid = $fig->cas_to_cid($cas)
2973 :    
2974 :     Returns the compound id (cid), given the CAS ID.
2975 :    
2976 :     =cut
2977 :    
2978 :     sub cas_to_cid {
2979 :     my($self,$cas) = @_;
2980 :    
2981 :     my $rdbH = $self->db_handle;
2982 :     my $relational_db_response = $rdbH->SQL("SELECT cid FROM comp_cas where cas = \'$cas\'");
2983 :     if (@$relational_db_response == 1)
2984 :     {
2985 :     return $relational_db_response->[0]->[0];
2986 :     }
2987 :     return "";
2988 :     }
2989 :    
2990 :     =pod
2991 :    
2992 :     =head1 all_reactions
2993 :    
2994 :     usage: @rids = $fig->all_reactions
2995 :    
2996 :     Returns a list containing all of the KEGG reaction IDs.
2997 :    
2998 :     =cut
2999 :    
3000 :     sub all_reactions {
3001 :     my($self) = @_;
3002 :    
3003 :     my $rdbH = $self->db_handle;
3004 :     my $relational_db_response = $rdbH->SQL("SELECT DISTINCT rid FROM reaction_to_compound");
3005 :     if (@$relational_db_response > 0)
3006 :     {
3007 :     return sort map { $_->[0] } @$relational_db_response;
3008 :     }
3009 :     return ();
3010 :     }
3011 :    
3012 :     =pod
3013 :    
3014 :     =head1 reversible
3015 :    
3016 :     usage: $rev = $fig->reversible($rid)
3017 :    
3018 :     Returns true iff the reactions had a "main direction" designated as "<=>";
3019 :    
3020 :     =cut
3021 :    
3022 :     sub reversible {
3023 :     my($self,$rid) = @_;
3024 :    
3025 :     my $rdbH = $self->db_handle;
3026 :     my $relational_db_response = $rdbH->SQL("SELECT reversible FROM reversible where rid = \'$rid\'");
3027 :     if (@$relational_db_response == 1)
3028 :     {
3029 :     return $relational_db_response->[0]->[0];
3030 :     }
3031 :     return 1;
3032 :     }
3033 :    
3034 :     =pod
3035 :    
3036 :     =head1 reaction2comp
3037 :    
3038 :     usage: @tuples = $fig->reaction2comp($rid,$which)
3039 :    
3040 :     Returns the "substrates" iff $which == 0. In any event (i.e., whether you ask for substrates
3041 :     or products), you get back a list of 3-tuples. Each 3-tuple will contain
3042 :    
3043 :     [$cid,$stoich,$main]
3044 :    
3045 :     Stoichiometry is normally numeric, but can be things like "n" or "(n+1)".
3046 :     $main is 1 iff the compound is considered "main" or "connectable".
3047 :    
3048 :     =cut
3049 :    
3050 :     sub reaction2comp {
3051 :     my($self,$rid,$which) = @_;
3052 :    
3053 :     my $rdbH = $self->db_handle;
3054 :     my $relational_db_response = $rdbH->SQL("SELECT cid,stoich,main FROM reaction_to_compound where rid = \'$rid\' and setn = \'$which\'");
3055 :     if (@$relational_db_response > 0)
3056 :     {
3057 :     return sort { $a->[0] cmp $b->[0] } map { $_->[1] =~ s/\s+//g; $_ } @$relational_db_response;
3058 :     }
3059 :     return ();
3060 :     }
3061 :    
3062 :     =pod
3063 :    
3064 :     =head1 catalyzed_by
3065 :    
3066 :     usage: @ecs = $fig->catalyzed_by($rid)
3067 :    
3068 :     Returns the ECs that are reputed to catalyze the reaction. Note that we are currently
3069 :     just returning the ECs that KEGG gives. We need to handle the incompletely specified forms
3070 :     (e.g., 1.1.1.-), but we do not do it yet.
3071 :    
3072 :     =cut
3073 :    
3074 :     sub catalyzed_by {
3075 :     my($self,$rid) = @_;
3076 :    
3077 :     my $rdbH = $self->db_handle;
3078 :     my $relational_db_response = $rdbH->SQL("SELECT role FROM reaction_to_enzyme where rid = \'$rid\'");
3079 :     if (@$relational_db_response > 0)
3080 :     {
3081 :     return sort map { $_->[0] } @$relational_db_response;
3082 :     }
3083 :     return ();
3084 :     }
3085 :    
3086 :     =pod
3087 :    
3088 :     =head1 catalyzes
3089 :    
3090 :     usage: @ecs = $fig->catalyzes($role)
3091 :    
3092 :     Returns the rids of the reactions catalyzed by the "role" (normally an EC).
3093 :    
3094 :     =cut
3095 :    
3096 :     sub catalyzes {
3097 :     my($self,$role) = @_;
3098 :    
3099 :     my $rdbH = $self->db_handle;
3100 :     my $relational_db_response = $rdbH->SQL("SELECT rid FROM reaction_to_enzyme where role = \'$role\'");
3101 :     if (@$relational_db_response > 0)
3102 :     {
3103 :     return sort map { $_->[0] } @$relational_db_response;
3104 :     }
3105 :     return ();
3106 :     }
3107 :    
3108 :    
3109 :     =pod
3110 :    
3111 :     =head1 displayable_reaction
3112 :    
3113 :     usage: $display_format = $fig->displayable_reaction($rid)
3114 :    
3115 :     Returns a string giving the displayable version of a reaction.
3116 :    
3117 :     =cut
3118 :    
3119 :     sub displayable_reaction {
3120 :     my($self,$rid) = @_;
3121 :    
3122 :     my @tmp = `grep $rid $FIG_Config::data/KEGG/reaction_name.lst`;
3123 :     if (@tmp > 0)
3124 :     {
3125 :     chop $tmp[0];
3126 :     return $tmp[0];
3127 :     }
3128 :     return $rid;
3129 :     }
3130 :    
3131 :     =pod
3132 :    
3133 :     =head1 all_maps
3134 :    
3135 :     usage: @maps = $fig->all_maps
3136 :    
3137 :     Returns a list containing all of the KEGG maps that the system knows about (the
3138 :     maps need to be periodically updated).
3139 :    
3140 :     =cut
3141 :    
3142 :     sub all_maps {
3143 :     my($self,$ec) = @_;
3144 :    
3145 :     my $rdbH = $self->db_handle;
3146 :     my $relational_db_response = $rdbH->SQL("SELECT DISTINCT map FROM ec_map ");
3147 :     if (@$relational_db_response > 0)
3148 :     {
3149 :     return map { $_->[0] } @$relational_db_response;
3150 :     }
3151 :     return ();
3152 :     }
3153 :    
3154 :     =pod
3155 :    
3156 :     =head1 ec_to_maps
3157 :    
3158 :     usage: @maps = $fig->ec_to_maps($ec)
3159 :    
3160 :     Returns the set of maps that contain $ec as a functional role. $ec is usually an EC number,
3161 :     but in the more general case, it can be a functional role.
3162 :    
3163 :     =cut
3164 :    
3165 :     sub ec_to_maps {
3166 :     my($self,$ec) = @_;
3167 :    
3168 :     my $rdbH = $self->db_handle;
3169 :     my $relational_db_response = $rdbH->SQL("SELECT map FROM ec_map WHERE ( ec = \'$ec\' )");
3170 :     if (@$relational_db_response > 0)
3171 :     {
3172 :     return map { $_->[0] } @$relational_db_response;
3173 :     }
3174 :     return ();
3175 :     }
3176 :    
3177 :    
3178 :     =pod
3179 :    
3180 :     =head1 map_to_ecs
3181 :    
3182 :     usage: @ecs = $fig->map_to_ecs($map)
3183 :    
3184 :     Returns the set of functional roles (usually ECs) that are contained in the functionality
3185 :     depicted by $map.
3186 :    
3187 :     =cut
3188 :    
3189 :     sub map_to_ecs {
3190 :     my($self,$map) = @_;
3191 :    
3192 :     my $rdbH = $self->db_handle;
3193 :     my $relational_db_response = $rdbH->SQL("SELECT ec FROM ec_map WHERE ( map = \'$map\' )");
3194 :     if (@$relational_db_response > 0)
3195 :     {
3196 :     return map { $_->[0] } @$relational_db_response;
3197 :     }
3198 :     return ();
3199 :     }
3200 :    
3201 :     =pod
3202 :    
3203 :     =head1 map_name
3204 :    
3205 :     usage: $name = $fig->map_name($map)
3206 :    
3207 :     Returns the descriptive name covering the functionality depicted by $map.
3208 :    
3209 :     =cut
3210 :    
3211 :     sub map_name {
3212 :     my($self,$map) = @_;
3213 :    
3214 :     my $rdbH = $self->db_handle;
3215 :     my $relational_db_response = $rdbH->SQL("SELECT mapname FROM map_name WHERE ( map = \'$map\' )");
3216 :     if (@$relational_db_response == 1)
3217 :     {
3218 :     return $relational_db_response->[0]->[0];
3219 :     }
3220 :     return "";
3221 :     }
3222 :    
3223 :     ################################# Functional Roles ####################################
3224 :    
3225 :     =pod
3226 :    
3227 :     =head1 neighborhood_of_role
3228 :    
3229 :     usage: @roles = $fig->neighborhood_of_role($role)
3230 :    
3231 :     Returns a list of functional roles that we consider to be "the neighborhood" of $role.
3232 :    
3233 :     =cut
3234 :    
3235 :     sub neighborhood_of_role {
3236 :     my($self,$role) = @_;
3237 :     my($readC);
3238 :    
3239 :     my $file = "$FIG_Config::global/role.neighborhoods";
3240 :     my $rdbH = $self->db_handle;
3241 :     my $roleQ = quotemeta $role;
3242 :     my $relational_db_response = $rdbH->SQL("SELECT seek, len FROM neigh_seeks WHERE role = \'$roleQ\' ");
3243 :     if (@$relational_db_response == 1)
3244 :     {
3245 :     my($seek,$ln) = @{$relational_db_response->[0]};
3246 :     my $fh = $self->openF($file);
3247 :     seek($fh,$seek,0);
3248 :     my $readN = read($fh,$readC,$ln-1);
3249 :     ($readN == ($ln-1))
3250 :     || confess "could not read the block of sims at $seek for $ln - 1 characters; $readN actually read from $file\n$readC";
3251 :     return grep { $_ && ($_ !~ /^\/\//) } split(/\n/,$readC);
3252 :     }
3253 :     return ();
3254 :     }
3255 :    
3256 :     =pod
3257 :    
3258 :     =head1 roles_of_function
3259 :    
3260 :     usage: @roles = $fig->roles_of_function($func)
3261 :    
3262 :     Returns a list of the functional roles implemented by $func.
3263 :    
3264 :     =cut
3265 :    
3266 :     sub roles_of_function {
3267 :     my($func) = @_;
3268 :    
3269 :     return (split(/\s*[\/;]\s+/,$func),($func =~ /\d+\.\d+\.\d+\.\d+/g));
3270 :     }
3271 :    
3272 :     =pod
3273 :    
3274 :     =head1 seqs_with_role
3275 :    
3276 :     usage: @pegs = $fig->seqs_with_role($role,$who)
3277 :    
3278 :     Returns a list of the pegs that implement $role. If $who is not given, it
3279 :     defaults to "master". The system returns all pegs with an assignment made by
3280 :     either "master" or $who (if it is different than the master) that implement $role.
3281 :     Note that this includes pegs for which the "master" annotation disagrees with that
3282 :     of $who, the master's implements $role, and $who's does not.
3283 :    
3284 :     =cut
3285 :    
3286 :     sub seqs_with_role {
3287 :     my($self,$role,$who) = @_;
3288 :     my $relational_db_response;
3289 :    
3290 :     $who = $who ? $who : "master";
3291 :     my $rdbH = $self->db_handle;
3292 :    
3293 :     my $who_cond;
3294 :     if ($who eq "master")
3295 :     {
3296 :     $who_cond = "( made_by = \'master\' OR made_by = \'unknown\' )";
3297 :     }
3298 :     else
3299 :     {
3300 :     $who_cond = "( made_by = \'master\' OR made_by = \'$who\' OR made_by = \'unknown\')";
3301 :     }
3302 :     my $query = "SELECT distinct prot FROM roles WHERE (( role = \'$role\' ) AND $who_cond )";
3303 :     return (($relational_db_response = $rdbH->SQL($query)) && (@$relational_db_response >= 1)) ?
3304 :     map { $_->[0] } @$relational_db_response : ();
3305 :     }
3306 :    
3307 :     =pod
3308 :    
3309 :     =head1 seqs_with_roles_in_genomes
3310 :    
3311 :     usage: $result = $fig->seqs_with_roles_in_genomes($genomes,$roles,$made_by)
3312 :    
3313 :     This routine takes a pointer to a list of genomes ($genomes) and a pointer to a list of
3314 :     roles ($roles) and looks up all of the sequences that connect to those roles according
3315 :     to either the master assignments or those made by $made_by. Again, you will get assignments
3316 :     for which the "master" assignment connects, but the $made_by does not.
3317 :    
3318 :     A hash is returned. The keys to the hash are genome IDs for which at least one sequence
3319 :     was found. $result->{$genome} will itself be a hash, assuming that at least one sequence
3320 :     was found for $genome. $result->{$genome}->{$role} will be set to a pointer to a list of
3321 :     2-tuples. Each 2-tuple will contain [$peg,$function], where $function is the one for
3322 :     $made_by (which may not be the one that connected).
3323 :    
3324 :     =cut
3325 :    
3326 :     sub seqs_with_roles_in_genomes {
3327 :     my($self,$genomes,$roles,$made_by) = @_;
3328 :     my($genome,$role,$roleQ,$role_cond,$made_by_cond,$query,$relational_db_response,$peg,$genome_cond,$hit);
3329 :     my $rdbH = $self->db_handle;
3330 :     my $result = {}; # foreach $genome ($self->genomes) { $result->{$genome} = {} }
3331 :     if (! $made_by) { $made_by = 'master' }
3332 :     if ((@$genomes > 0) && (@$roles > 0))
3333 :     {
3334 :     $genome_cond = "(" . join(" OR ",map { "( org = \'$_\' )" } @$genomes) . ")";
3335 :     $role_cond = "(" . join(" OR ",map { $roleQ = quotemeta $_; "( role = \'$roleQ\' )" } @$roles) . ")";
3336 :     $made_by_cond = ($made_by eq 'master') ? "(made_by = 'master')" : "(made_by = 'master' OR made_by = '$made_by')";
3337 :     $query = "SELECT distinct prot, role FROM roles WHERE ( $made_by_cond AND $genome_cond AND $role_cond )";
3338 :     if (($relational_db_response = $rdbH->SQL($query)) && (@$relational_db_response >= 1))
3339 :     {
3340 :     foreach $hit (@$relational_db_response)
3341 :     {
3342 :     ($peg,$role) = @$hit;
3343 :     $genome = $self->genome_of($peg);
3344 :     push(@{ $result->{$genome}->{$role} },[$peg,scalar $self->function_of($peg,$made_by)]);
3345 :     }
3346 :     }
3347 :     }
3348 :     return $result;
3349 :     }
3350 :    
3351 :     =pod
3352 :    
3353 :     =head1 largest_clusters
3354 :    
3355 :     usage: @clusters = $fig->largest_clusters($roles,$user)
3356 :    
3357 :     This routine can be used to find the largest clusters containing the some of the
3358 :     designated set of roles. A list of clusters is returned. Each cluster is a pointer to
3359 :     a list of pegs.
3360 :    
3361 :     =cut
3362 :    
3363 :     sub largest_clusters {
3364 :     my($self,$roles,$user,$sort_by_unique_functions) = @_;
3365 :     my($genome,$x,$role,$y,$peg,$loc,$contig,$beg,$end,%pegs,@pegs,$i,$j);
3366 :    
3367 :     my $ss = $self->seqs_with_roles_in_genomes([$self->genomes],$roles,$user);
3368 :     my @clusters = ();
3369 :    
3370 :     foreach $genome (keys(%$ss))
3371 :     {
3372 :     my %pegs;
3373 :     $x = $ss->{$genome};
3374 :     foreach $role (keys(%$x))
3375 :     {
3376 :     $y = $x->{$role};
3377 :     foreach $peg (map { $_->[0] } @$y)
3378 :     {
3379 :     if ($loc = $self->feature_location($peg))
3380 :     {
3381 :     ($contig,$beg,$end) = &FIG::boundaries_of($loc);
3382 :     $pegs{$peg} = [$peg,$contig,int(($beg + $end) / 2)];
3383 :     }
3384 :     }
3385 :     }
3386 :    
3387 :     @pegs = sort { ($pegs{$a}->[1] cmp $pegs{$b}->[1]) or ($pegs{$a}->[2] <=> $pegs{$b}->[2]) } keys(%pegs);
3388 :     $i = 0;
3389 :     while ($i < $#pegs)
3390 :     {
3391 :     for ($j=$i+1; ($j < @pegs) && &close_enough_locs($pegs{$pegs[$j-1]},$pegs{$pegs[$j]}); $j++) {}
3392 :     if ($j > ($i+1))
3393 :     {
3394 :     push(@clusters,[@pegs[$i..$j-1]]);
3395 :     }
3396 :     $i = $j;
3397 :     }
3398 :     }
3399 :     if ($sort_by_unique_functions)
3400 :     {
3401 :     @clusters = sort { $self->unique_functions($b,$user) <=> $self->unique_functions($a,$user) } @clusters;
3402 :     }
3403 :     else
3404 :     {
3405 :     @clusters = sort { @$b <=> @$a } @clusters;
3406 :     }
3407 :     return @clusters;
3408 :     }
3409 :    
3410 :     sub unique_functions {
3411 :     my($self,$pegs,$user) = @_;
3412 :     my($peg,$func,%seen);
3413 :    
3414 :     foreach $peg (@$pegs)
3415 :     {
3416 :     if ($func = $self->function_of($peg,$user))
3417 :     {
3418 :     $seen{$func} = 1;
3419 :     }
3420 :     }
3421 :     return scalar keys(%seen);
3422 :     }
3423 :    
3424 :     sub close_enough_locs {
3425 :     my($x,$y) = @_;
3426 :    
3427 :     return (($x->[1] eq $y->[1]) && (abs($x->[2] - $y->[2]) < 5000));
3428 :     }
3429 :    
3430 :     ################################# DNA sequence Stuff ####################################
3431 :    
3432 :     =pod
3433 :    
3434 :     =head1 extract_seq
3435 :    
3436 :     usage: $seq = &FIG::extract_seq($contigs,$loc)
3437 :    
3438 :     This is just a little utility routine that I have found convenient. It assumes that
3439 :     $contigs is a hash that contains IDs as keys and sequences as values. $loc must be of the
3440 :     form
3441 :     Contig_Beg_End
3442 :    
3443 :     where Contig is the ID of one of the sequences; Beg and End give the coordinates of the sought
3444 :     subsequence. If Beg > End, it is assumed that you want the reverse complement of the subsequence.
3445 :     This routine plucks out the subsequence for you.
3446 :    
3447 :     =cut
3448 :    
3449 :     sub extract_seq {
3450 :     my($contigs,$loc) = @_;
3451 :     my($contig,$beg,$end,$contig_seq);
3452 :     my($plus,$minus);
3453 :    
3454 :     $plus = $minus = 0;
3455 :     my $strand = "";
3456 :     my @loc = split(/,/,$loc);
3457 :     my @seq = ();
3458 :     foreach $loc (@loc)
3459 :     {
3460 :     if ($loc =~ /^\S+_(\d+)_(\d+)$/)
3461 :     {
3462 :     if ($1 < $2)
3463 :     {
3464 :     $plus++;
3465 :     }
3466 :     elsif ($2 < $1)
3467 :     {
3468 :     $minus++;
3469 :     }
3470 :     }
3471 :     }
3472 :     if ($plus > $minus)
3473 :     {
3474 :     $strand = "+";
3475 :     }
3476 :     elsif ($plus < $minus)
3477 :     {
3478 :     $strand = "-";
3479 :     }
3480 :    
3481 :     foreach $loc (@loc)
3482 :     {
3483 :     if ($loc =~ /^(\S+)_(\d+)_(\d+)$/)
3484 :     {
3485 :     ($contig,$beg,$end) = ($1,$2,$3);
3486 :     if (($beg < $end) || (($beg == $end) && ($strand eq "+")))
3487 :     {
3488 :     $strand = "+";
3489 :     push(@seq,substr($contigs->{$contig},$beg-1,($end+1-$beg)));
3490 :     }
3491 :     else
3492 :     {
3493 :     $strand = "-";
3494 :     push(@seq,&reverse_comp(substr($contigs->{$contig},$end-1,($beg+1-$end))));
3495 :     }
3496 :     }
3497 :     }
3498 :     return join("",@seq);
3499 :     }
3500 :    
3501 :     =pod
3502 :    
3503 :     =head1 contig_ln
3504 :    
3505 :     usage: $n = $fig->contig_ln($genome,$contig)
3506 :    
3507 :     Returns the length of $contig from $genome.
3508 :    
3509 :     =cut
3510 :    
3511 :     sub contig_ln {
3512 :     my($self,$genome,$contig) = @_;
3513 :     my($rdbH,$relational_db_response);
3514 :    
3515 :     $rdbH = $self->db_handle;
3516 :     if (defined($genome) && defined($contig))
3517 :     {
3518 :     if (($relational_db_response = $rdbH->SQL("SELECT len FROM contig_lengths WHERE ( genome = \'$genome\' ) and ( contig = \'$contig\' )")) &&
3519 :    
3520 :     (@$relational_db_response == 1))
3521 :     {
3522 :     return $relational_db_response->[0]->[0];
3523 :     }
3524 :     }
3525 :     return undef;
3526 :     }
3527 :    
3528 :     =pod
3529 :    
3530 :     =head1 dna_seq
3531 :    
3532 :     usage: $seq = dna_seq($genome,@locations)
3533 :    
3534 :     Returns the concatenated subsequences described by the list of locations. Each location
3535 :     must be of the form
3536 :    
3537 :     Contig_Beg_End
3538 :    
3539 :     where Contig must be the ID of a contig for genome $genome. If Beg > End the location
3540 :     describes a stretch of the complementary strand.
3541 :    
3542 :     =cut
3543 :    
3544 :     sub dna_seq {
3545 :     my($self,$genome,@locations) = @_;
3546 :     my(@pieces,$loc,$contig,$beg,$end,$ln,$rdbH);
3547 :    
3548 :     @pieces = ();
3549 :     foreach $loc (@locations)
3550 :     {
3551 :     if ($loc =~ /^(\S+)_(\d+)_(\d+)$/)
3552 :     {
3553 :     ($contig,$beg,$end) = ($1,$2,$3);
3554 :     $ln = $self->contig_ln($genome,$contig);
3555 :    
3556 :     if (! $ln) {
3557 :     print STDERR "$genome/$contig: could not get length\n";
3558 :     return "";
3559 :     }
3560 :    
3561 :     if (&between(1,$beg,$ln) && &between(1,$end,$ln))
3562 :     {
3563 :     if ($beg < $end)
3564 :     {
3565 :     push(@pieces, $self->get_dna($genome,$contig,$beg,$end));
3566 :     }
3567 :     else
3568 :     {
3569 :     push(@pieces, &reverse_comp($self->get_dna($genome,$contig,$end,$beg)));
3570 :     }
3571 :     }
3572 :     }
3573 :     }
3574 :     return join("",@pieces);
3575 :     }
3576 :    
3577 :     sub get_dna {
3578 :     my($self,$genome,$contig,$beg,$end) = @_;
3579 :     my $relational_db_response;
3580 :    
3581 :     my $rdbH = $self->db_handle;
3582 :     my $indexpt = int(($beg-1)/10000) * 10000;
3583 :     if (($relational_db_response = $rdbH->SQL("SELECT startN,fileno,seek FROM contig_seeks WHERE ( genome = \'$genome\' ) AND ( contig = \'$contig\' ) AND ( indexpt = $indexpt )")) &&
3584 :     (@$relational_db_response == 1))
3585 :     {
3586 :     my($startN,$fileN,$seek) = @{$relational_db_response->[0]};
3587 :     my $fh = $self->openF($self->N2file($fileN));
3588 :     if (seek($fh,$seek,0))
3589 :     {
3590 :     my $chunk = "";
3591 :     read($fh,$chunk,int(($end + 1 - $startN) * 1.03));
3592 :     $chunk =~ s/\s//g;
3593 :     my $ln = ($end - $beg) + 1;
3594 :     if (length($chunk) >= $ln)
3595 :     {
3596 :     return substr($chunk,(($beg-1)-$startN),$ln);
3597 :     }
3598 :     }
3599 :     }
3600 :     return undef;
3601 :     }
3602 :    
3603 :     1

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3