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

Annotation of /FigKernelPackages/PinnedRegions.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : dsouza 1.1 # -*- perl -*-
2 :     ########################################################################
3 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
4 :     # for Interpretations of Genomes. All Rights Reserved.
5 :     #
6 :     # This file is part of the SEED Toolkit.
7 :     #
8 :     # The SEED Toolkit is free software. You can redistribute
9 :     # it and/or modify it under the terms of the SEED Toolkit
10 :     # Public License.
11 :     #
12 :     # You should have received a copy of the SEED Toolkit Public License
13 :     # along with this program; if not write to the University of Chicago
14 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
15 :     # Genomes at veronika@thefig.info or download a copy from
16 :     # http://www.theseed.org/LICENSE.TXT.
17 :     ########################################################################
18 :    
19 :    
20 :     package PinnedRegions;
21 :    
22 :     use strict;
23 :     use warnings;
24 :    
25 :     use FIG;
26 : dsouza 1.5 use FigFams;
27 : dsouza 1.1 use FIG_Config;
28 :    
29 :     use Time::HiRes qw( usleep ualarm gettimeofday tv_interval );
30 :    
31 :     sub pinned_regions {
32 :     my($fig, $pin_desc, $fast_color, $sims_from, $map_sz) = @_;
33 :    
34 :     # Get list of pegs required by the description in $pin_desc
35 : dsouza 1.4 my $pinned_pegs = &expand_peg_list($fig, $pin_desc);
36 : dsouza 1.1
37 :     # Get regions around pinned pegs -- boundaries, features, etc.
38 :     my $regions = &define_regions($fig, $map_sz, $pinned_pegs);
39 :    
40 :     # Filter out overlapping regions caused by gene fusions, frame shifts etc. where multiple PEGs
41 :     # are pinned (by similarity or PCH) to the input PEG
42 :     $regions = &filter_regions_1($pin_desc, $regions);
43 :    
44 : dsouza 1.4 # Get information for each feature -- location, strand, function etc.
45 : dsouza 1.1 my $feature_data = &feature_data($fig, $regions);
46 :    
47 : dsouza 1.3 &add_functional_coupling($fig, $pin_desc, $regions, $feature_data);
48 : dsouza 1.5 &add_figfams($fig, $feature_data);
49 : dsouza 1.3 &add_subsystem_data($fig, $pin_desc, $feature_data);
50 :    
51 : dsouza 1.4 # Assign a set number to some PEGs through transitive closure based on similarity, from blast scores
52 : dsouza 1.1 &color_pegs($fig, $pin_desc, $pinned_pegs, $regions, $feature_data, $fast_color, $sims_from);
53 :    
54 :     # Filter out regions which have only a single PEG (the pinned one) colored.
55 : dsouza 1.3 # $regions = &filter_regions_2($pin_desc, $regions, $feature_data);
56 : dsouza 1.1
57 :     # Add feature data to the regions to make the final maps
58 :     my $maps = &make_maps($fig, $regions, $feature_data);
59 :    
60 :     return $maps;
61 :     }
62 :    
63 :     sub expand_peg_list {
64 :     my($fig, $pin_desc) = @_;
65 :     my @pegs = ();
66 :    
67 :     # Filter for legitimate genome IDS -- should handle deleted organisms and (in NMPDR) non-NMPDR orgs
68 :     my %ok_genome_id = map {$_ => 1} $fig->genomes;
69 :    
70 : dsouza 1.5 # If the user has selected the genomes to be displayed, filter out all other genomes
71 :     if ( @{ $pin_desc->{'show_genomes'} } )
72 :     {
73 :     # create new %ok_genome_id hash from intersection of selected genomes and legitimate genome IDs
74 :     %ok_genome_id = map {$_ => 1} grep {$ok_genome_id{$_}} @{ $pin_desc->{'show_genomes'} };
75 :     }
76 :    
77 : dsouza 1.1 if ( @{ $pin_desc->{'pegs'} } > 1 )
78 :     {
79 :     # Input already contains list of pegs, no need for expansion
80 :     @pegs = grep {$ok_genome_id{$fig->genome_of($_)}} @{ $pin_desc->{'pegs'} };
81 :     }
82 :     else
83 :     {
84 :     # Get PCH pinned pegs
85 :     my $pch_pinned_pegs = &pch_pinned_pegs($fig, $pin_desc, \%ok_genome_id);
86 :    
87 : dsouza 1.3 $pch_pinned_pegs = &select_pegs($fig, $pin_desc, $pin_desc->{'n_pch_pins'}, $pch_pinned_pegs);
88 : dsouza 1.1
89 :     # Get similarity pinned pegs
90 :     my $sim_pegs = &sim_pegs($fig, $pin_desc, \%ok_genome_id);
91 :    
92 : dsouza 1.3 # Remove PCH PEGs (if any) from similarity pinned PEG list
93 : dsouza 1.1 my %seen = map {$_ => 1} @$pch_pinned_pegs;
94 :     $sim_pegs = [grep {! $seen{$_}} @$sim_pegs];
95 : dsouza 1.3
96 : dsouza 1.1 $sim_pegs = &select_pegs($fig, $pin_desc, $pin_desc->{'n_sims'}, $sim_pegs);
97 :    
98 :     @pegs = ();
99 :    
100 :     # Get input peg
101 :     my $peg = $pin_desc->{'pegs'}[0];
102 :     if ( $pin_desc->{'sort_by'} eq 'phylogeny' )
103 :     {
104 :     # First add input peg, then sort by phylogeny
105 :     @pegs = $fig->sort_fids_by_taxonomy($peg, @$sim_pegs, @$pch_pinned_pegs);
106 :     }
107 :     else
108 :     {
109 :     # Sort by phylogenetic distance or organism name
110 :     my $g1 = $fig->genome_of($peg);
111 :     @pegs = map {$_->[0] }
112 :     sort {$a->[1] <=> $b->[1] or $a->[2] cmp $b->[2]}
113 :     map {[$_, $fig->crude_estimate_of_distance($g1,$fig->genome_of($_)), $fig->org_of($_)]} @$sim_pegs, @$pch_pinned_pegs;
114 :     # Add input peg at front of list
115 :     unshift @pegs, $peg;
116 :     }
117 :     }
118 :    
119 :     return \@pegs
120 :     }
121 :    
122 :     sub pch_pinned_pegs {
123 :     my($fig, $pin_desc, $ok_genome_id) = @_;
124 :    
125 :     my @pegs = ();
126 :    
127 :     if ( $pin_desc->{'n_pch_pins'} > 0 )
128 :     {
129 :     # Get input peg
130 :     my $peg = $pin_desc->{'pegs'}[0];
131 :    
132 :     # Get pegs in pch pin with input peg, and filter out deleted or non-NMPDR organisms
133 :     @pegs = grep {$ok_genome_id->{$fig->genome_of($_)}}
134 :     grep {$_ ne $peg}
135 :     map {$_->[0]} $fig->in_pch_pin_with_and_evidence($peg);
136 :     }
137 :    
138 :     return \@pegs;
139 :     }
140 :    
141 :     sub sim_pegs {
142 :     my($fig, $pin_desc, $ok_genome_id) = @_;
143 :    
144 :     # Return a list of PEGS similar to the input PEG, with evalue less than or equal to
145 :     # the user defined similarity cutoff.
146 :     # If the number of sims requested is 0, return the empty list
147 :    
148 :     my $n_sims = $pin_desc->{'n_sims'};
149 :     my $sim_cutoff = $pin_desc->{'sim_cutoff'};
150 :     my @pegs = ();
151 :    
152 :     if ( $n_sims > 0 )
153 :     {
154 :     my $peg = $pin_desc->{'pegs'}[0];
155 :     my %seen;
156 :     @pegs = grep {$ok_genome_id->{$fig->genome_of($_)}}
157 :     grep {! $seen{$_}++}
158 :     map {$_->[1]} $fig->sims($peg, 10000, $sim_cutoff, "fig");
159 :    
160 :     my $n_pegs = @pegs;
161 :     }
162 :    
163 :     return \@pegs;
164 :     }
165 :    
166 :     sub select_pegs {
167 :     my($fig, $pin_desc, $n_pegs, $pegs) = @_;
168 :    
169 :     if ( $n_pegs == 0 )
170 :     {
171 :     return [];
172 :     }
173 :    
174 : dsouza 1.3 if ( $pin_desc->{'collapse_close_genomes'} == 1 )
175 : dsouza 1.1 {
176 : dsouza 1.3 # The ordering of the PEGs done here is in order to =select= the correct set, the ordering for the
177 :     # display is done later.
178 : dsouza 1.2
179 : dsouza 1.3 # To collapse the PEG list, we want to:
180 :     # a. Return at most $n_pegs PEGs,
181 :     # b. include a representative PEG from every genus, subject to a, and
182 : dsouza 1.5 # c. include a representative PEG from each genus-species, subject to a and b.
183 :     # Individual strains will get represented by a single PEG, chosen arbitrarily.
184 :    
185 :     # The PEG selection is defined by the order of the PEGs. This could be done beter.
186 : dsouza 1.1
187 : dsouza 1.5 my(%seen, @unique_genus, @unique_genus_species);
188 : dsouza 1.1
189 : dsouza 1.3 foreach my $peg ( @$pegs )
190 :     {
191 :     my $org = $fig->org_of($peg);
192 :     # 'org_of' returns undef for deleted PEGs, these need to be omitted from the peg list returned
193 :    
194 :     if ( $org )
195 :     {
196 : dsouza 1.5 # Use only genus+species to drop strain information
197 :     my($genus, $species) = split(/\s+/, $org);
198 :     my $gs = "$genus $species";
199 : dsouza 1.3
200 : dsouza 1.5 if ( not $seen{$genus}++ )
201 :     {
202 :     # First PEG from this genus, add it to @unique_genus.
203 :     # Mark the genus+species as seen.
204 :     # A subsequent PEG with the same genus and species will be dropped.
205 :     # A subsequent PEG with the same genus but different species will be added to @unique_genus_species.
206 :     $seen{$gs}++;
207 : dsouza 1.3 push @unique_genus, $peg;
208 : dsouza 1.5 }
209 :     elsif ( not $seen{$gs}++ )
210 :     {
211 :     # First PEG from this genus+species, add it to @unique_genus_species.
212 :     push @unique_genus_species, $peg;
213 : dsouza 1.3 }
214 :     }
215 :     }
216 : dsouza 1.1
217 : dsouza 1.5 # Keep the unique_genus PEGS at the top, followed by the unique_genus_species
218 :     $pegs = [@unique_genus, @unique_genus_species];
219 : dsouza 1.1 }
220 :    
221 :     # Truncate list if necessary
222 : dsouza 1.3 if ( $n_pegs < @$pegs )
223 : dsouza 1.1 {
224 : dsouza 1.3 $#{ $pegs } = $n_pegs - 1;
225 : dsouza 1.1 }
226 :    
227 :     return $pegs;
228 :     }
229 :    
230 :     sub filter_regions_1 {
231 :     my($pin_desc, $regions) = @_;
232 :     my $new_regions;
233 :    
234 :     # Filter out some overlapping regions caused by gene fusions, frame shifts etc. where multiple PEGs
235 :     # are pinned (by similarity or PCH) to the input PEG
236 :     # Note that all overlaps are NOT eliminated
237 :    
238 :     if ( @{ $pin_desc->{'pegs'} } == 1 )
239 :     {
240 :     # Input pin description is for a single input peg
241 :     my %seen;
242 :    
243 :     foreach my $region ( @$regions )
244 :     {
245 :     my $pinned_peg = $region->{'pinned_peg'};
246 :    
247 :     # If the pinned peg from a region has already been 'seen', don't
248 :     # add the region into the @$new_regions array
249 :    
250 :     if ( not $seen{$pinned_peg} )
251 :     {
252 :     push @$new_regions, $region;
253 :    
254 :     my $fids = $region->{'features'};
255 :     foreach my $fid ( @$fids )
256 :     {
257 :     $seen{$fid} = 1;
258 :     }
259 :     }
260 :     }
261 :     }
262 :     else
263 :     {
264 :     # input PEGs is a list -- keep all of them
265 :     $new_regions = $regions;
266 :     }
267 :    
268 :     return $new_regions;
269 :     }
270 :    
271 :     sub filter_regions_2 {
272 :     my($pin_desc, $regions, $feature_data) = @_;
273 :     my $new_regions;
274 :    
275 :     if ( @{ $pin_desc->{'pegs'} } == 1 )
276 :     {
277 :     # Input is single peg
278 :     my $input_peg = $pin_desc->{'pegs'}[0];
279 :    
280 :     foreach my $region ( @$regions )
281 :     {
282 :     my $fids = $region->{'features'};
283 :     my $n_in_sets = grep {$feature_data->{$_}{'set_number'}} @$fids;
284 :    
285 :     if ( $n_in_sets > 1 or $region->{'pinned_peg'} eq $input_peg )
286 :     {
287 :     # Regions should be displayed only if:
288 :     # a. more than one PEG is in a 'colored set' OR
289 :     # b. the pinned PEG is the input PEG
290 :     push @$new_regions, $region;
291 :     }
292 :     }
293 :     }
294 :     else
295 :     {
296 :     # Input is a list of pegs -- keep all of the regions
297 :     $new_regions = $regions
298 :     }
299 :    
300 :     return $new_regions;
301 :     }
302 :    
303 :     sub make_maps {
304 :     my($fig, $regions, $feature_data) = @_;
305 :    
306 :     foreach my $region ( @$regions )
307 :     {
308 :     my $features = [];
309 :     my $fids = $region->{'features'};
310 :    
311 :     foreach my $fid ( @$fids )
312 :     {
313 :     push @$features, $feature_data->{$fid};
314 :     }
315 :    
316 :     $region->{'features'} = $features;
317 :     }
318 :    
319 :     return $regions;
320 :     }
321 :    
322 :     sub define_regions {
323 :     my($fig, $map_sz, $pinned_pegs) = @_;
324 :    
325 :     # Define the 'region' around the pinned peg, get features, and store some region information
326 :     my $regions = [];
327 :     my $half_map_sz = int($map_sz/2);
328 :    
329 :     foreach my $peg ( @$pinned_pegs )
330 :     {
331 :     my $genome = $fig->genome_of($peg);
332 :     my $loc = $fig->feature_location($peg);
333 :     my($contig, $beg, $end) = $fig->boundaries_of($loc);
334 :    
335 :     my $region_mid = int(($beg + $end)/2);
336 :     my $region_beg = $region_mid - $half_map_sz;
337 :     my $region_end = $region_mid + $half_map_sz;
338 :    
339 :     my($fids) = $fig->genes_in_region($genome, $contig, $region_beg, $region_end);
340 :    
341 :     my $region = {};
342 :     $region->{'genome_id'} = $fig->genome_of($peg);
343 :     $region->{'org_name'} = $fig->genus_species($region->{'genome_id'});
344 :     $region->{'contig'} = $contig;
345 :     $region->{'beg'} = $region_beg;
346 :     $region->{'mid'} = $region_mid;
347 :     $region->{'end'} = $region_end;
348 :     $region->{'features'} = $fids;
349 :     $region->{'pinned_peg_strand'} = ($beg <= $end)? '+' : '-';
350 :     $region->{'pinned_peg'} = $peg;
351 :    
352 :     push @$regions, $region;
353 :     }
354 :    
355 :     return $regions;
356 :     }
357 :    
358 :     sub feature_data {
359 :     my($fig, $regions) = @_;
360 :     my %feature_data;
361 :    
362 :     foreach my $region ( @$regions )
363 :     {
364 :     my $region_mid = $region->{'mid'};
365 :     my $fids = $region->{'features'};
366 :    
367 :     foreach my $fid ( @$fids )
368 :     {
369 :     # Get feature data if this feature has not been seen before, this avoids repeating
370 :     # this step when pegs occur in multiple regions
371 :     if ( not exists $feature_data{$fid} )
372 :     {
373 :     $feature_data{$fid} = &feature_entry($fig, $fid, $region_mid);
374 :     }
375 :     }
376 :     }
377 :    
378 :     return \%feature_data;
379 :     }
380 :    
381 :     sub feature_entry {
382 :     my($fig, $fid, $region_mid) = @_;
383 :    
384 :     my $type = $fig->ftype($fid);
385 :     my $loc = $fig->feature_location($fid);
386 :     my($contig, $beg, $end) = $fig->boundaries_of($loc);
387 :     my($left, $right) = sort {$a <=> $b} ($beg, $end);
388 : dsouza 1.3 my $size = $right - $left + 1;
389 : dsouza 1.1 my $strand = ($beg <= $end)? '+' : '-';
390 :     my $offset = int(($left + $right)/2) - $region_mid;
391 :     my $offset_beg = $left - $region_mid;
392 :     my $offset_end = $right - $region_mid;
393 :     my $func = scalar $fig->function_of($fid) || '';
394 :    
395 :     return {
396 :     'fid' => $fid,
397 :     'type' => $type,
398 :     'contig' => $contig,
399 :     'beg' => $beg,
400 :     'end' => $end,
401 : dsouza 1.3 'size' => $size,
402 : dsouza 1.1 'strand' => $strand,
403 :     'offset' => $offset,
404 :     'offset_beg' => $offset_beg,
405 :     'offset_end' => $offset_end,
406 :     'function' => $func,
407 :     };
408 :     }
409 :    
410 : dsouza 1.3 sub add_functional_coupling {
411 :     my($fig, $pin_desc, $regions, $feature_data) = @_;
412 :    
413 :     my $fc_cutoff = defined($pin_desc->{'fc_cutoff'})? $pin_desc->{'fc_cutoff'} : 4;
414 :    
415 :     foreach my $region ( @$regions )
416 :     {
417 :     my $pin = $region->{'pinned_peg'};
418 :     my @pegs = grep {$feature_data->{$_}{'type'} eq 'peg'} @{ $region->{'features'} };
419 :    
420 :     foreach my $couple ( $fig->coupled_to_batch(@pegs) )
421 :     {
422 :     my($peg1, $peg2, $sc) = @$couple;
423 :    
424 :     if ( $peg1 eq $pin and $sc >= $fc_cutoff )
425 :     {
426 :     $feature_data->{$peg2}{'fc_score'} = $sc;
427 :     }
428 :     }
429 :     }
430 :     }
431 :    
432 : dsouza 1.5 sub add_figfams {
433 :     my($fig, $feature_data) = @_;
434 :    
435 : dsouza 1.6 # Get FigFams directory from config file
436 :     my $figfam_dir = $FIG_Config::FigfamsData;
437 : dsouza 1.5
438 : dsouza 1.6 # Check if FigFams directory is defined and exists on current machine
439 :     if ( defined($figfam_dir) and (-d $figfam_dir) )
440 : dsouza 1.5 {
441 : dsouza 1.6 # Get all PEG IDs
442 :     my @pegs = grep {$feature_data->{$_}{'type'} eq 'peg'} keys %$feature_data;
443 :     # Get $figfams object
444 :     my $figfams = new FigFams($fig, $figfam_dir);
445 :     # Get FigFam family ID for @pegs
446 :     my $figfam = $figfams->families_containing_peg_bulk(\@pegs);
447 :     # Get hash of FigFam ID to family function
448 :     my $family_function = $figfams->family_functions();
449 :    
450 :     foreach my $fid ( keys %$feature_data )
451 : dsouza 1.5 {
452 : dsouza 1.6 if ( $figfam->{$fid} )
453 :     {
454 :     # Add FigFam information to hash -- to go into the popup text
455 :     $feature_data->{$fid}{'figfam'} = $figfam->{$fid} . ": " . $family_function->{$figfam->{$fid}};
456 :     }
457 : dsouza 1.5 }
458 :     }
459 :     }
460 :    
461 : dsouza 1.3 sub add_subsystem_data {
462 :     my($fig, $pin_desc, $feature_data) = @_;
463 :    
464 :     # Get subsystem_information for =all= pegs
465 :     my %peg_to_ss = $fig->subsystems_for_pegs([grep {$feature_data->{$_}{'type'} eq 'peg'} keys %$feature_data]);
466 :    
467 :     # Count number of occurences of each subsystem
468 :     my %ss_count;
469 :     foreach my $ss ( map {$_->[0]} map {@$_} values %peg_to_ss )
470 :     {
471 :     $ss_count{$ss}++;
472 :     }
473 :    
474 :     # Sort subsystems based on count and create hash with unique index for each subsystem where
475 :     # lower indices correspond to higher number of occurences of the subsystem.
476 :     my %ss_index;
477 :     my $index = 0;
478 :     foreach my $ss ( sort {$ss_count{$b} <=> $ss_count{$a} or $a cmp $b} keys %ss_count )
479 :     {
480 :     $ss_index{$ss} = ++$index;
481 :     }
482 :    
483 :     # Add subsystem information for pegs in subsystems
484 :     foreach my $fid ( keys %peg_to_ss )
485 :     {
486 :     my @subsystems = ();
487 :     foreach my $rec ( @{ $peg_to_ss{$fid} } )
488 :     {
489 :     if ( @$rec )
490 :     {
491 :     push @subsystems, $rec->[0];
492 :     }
493 :     }
494 :    
495 :     if ( @subsystems )
496 :     {
497 :     $feature_data->{$fid}{'subsystems'} = [sort {$a->[1] <=> $b->[1]} map {$_->[0] =~ s/_/ /g; $_} map {[$_, $ss_index{$_}]} @subsystems];
498 :     }
499 :     }
500 :     }
501 :    
502 : dsouza 1.1 sub color_pegs {
503 :     my($fig, $pin_desc, $pinned_pegs, $regions, $feature_data, $fast_color, $sims_from) = @_;
504 :    
505 :     # Run blastp and get connected pegs
506 :     my $blast_hit = {};
507 :     my $color_sim_cutoff = $pin_desc->{'color_sim_cutoff'};
508 :    
509 :     if ( $sims_from eq 'blast' )
510 :     {
511 : dsouza 1.4 $blast_hit = &blast_hits($fig, $regions, $feature_data, $fast_color, $color_sim_cutoff);
512 : dsouza 1.1 }
513 :     else
514 :     {
515 : dsouza 1.4 $blast_hit = &blast_hits_from_sims($fig, $regions, $feature_data, $fast_color, $color_sim_cutoff);
516 : dsouza 1.1 }
517 :    
518 :     # Assign set numbers to pegs based on blast scores
519 :     my $color_sets = &partition_pegs($blast_hit);
520 :    
521 :     # Sort peg sets to that a) larger sets have smaller set numbers, and
522 :     # b) sets closer to the pinned peg have smaller set numbers
523 : dsouza 1.4 $color_sets = &sort_color_sets($pin_desc, $pinned_pegs, $color_sets, $feature_data);
524 : dsouza 1.1
525 :     # Add color set number into $feature_data
526 :     for (my $i = 0; $i < @$color_sets; $i++)
527 :     {
528 :     # Use an index that starts at 1 (matches with coloring index)
529 :     my $n = $i + 1;
530 :     foreach my $peg ( @{ $color_sets->[$i] } )
531 :     {
532 :     $feature_data->{$peg}{'set_number'} = $n;
533 :    
534 :     # If this is the pinned set, set the 'color' to 'red'
535 :     if ( $n == 1 )
536 :     {
537 :     $feature_data->{$peg}{'color'} = 'red';
538 :     }
539 :     }
540 :     }
541 :     }
542 :    
543 :     sub sort_color_sets {
544 :     my($pin_desc, $pinned_pegs, $color_sets, $feature_data) = @_;
545 :    
546 : dsouza 1.3 # Find the color set containing the set of pinned pegs, i.e. the set containing the input peg.
547 :     # If the input peg is found (or if input is a list of pegs, returned value is the array index for the
548 :     # set.
549 :     # If the input peg is not found, the returned value is an reference to an array containing the input peg.
550 :     my $set = &pinned_set($pin_desc, $pinned_pegs, $color_sets);
551 : dsouza 1.1
552 : dsouza 1.3 my $pinned_color_set;
553 :     if ( $set =~ /^\d+$/ )
554 :     {
555 :     # Splice out the color set containing the input peg
556 :     ($pinned_color_set) = splice(@$color_sets,$set,1);
557 :     }
558 :     else
559 :     {
560 :     $pinned_color_set = $set;
561 :     }
562 : dsouza 1.1
563 : dsouza 1.3 # Add offset (summed distance from
564 :     my @color_sets = map {[$_, &offset($_, $feature_data)]} @$color_sets;
565 : dsouza 1.1 # Sort the remaining color sets based on:
566 :     # a. size (number of pegs) and
567 :     # b. offset from mid-point of region
568 :     @$color_sets = map {$_->[0]}
569 :     sort {@{$b->[0]} <=> @{$a->[0]} or $a->[1] <=> $b->[1]}
570 : dsouza 1.3 # sort {$a->[1] <=> $b->[1] or @{$b->[0]} <=> @{$a->[0]}}
571 : dsouza 1.1 map {[$_, &offset($_, $feature_data)]} @$color_sets;
572 :    
573 :     # Add the set of pinned pegs at the front of the returned list so that it gets colored red
574 :     return [$pinned_color_set, @$color_sets];
575 :     }
576 :    
577 : dsouza 1.3 sub pinned_set {
578 : dsouza 1.1 my($pin_desc, $pinned_pegs, $color_sets) = @_;
579 :    
580 : dsouza 1.3 # Returns an integer if the input is a peg-list or if the input peg is in a set.
581 :     # Returns the input peg (as an array reference) if the input peg is not in a set.
582 :    
583 : dsouza 1.1 if ( @{ $pin_desc->{'pegs'} } == 1 )
584 :     {
585 :     # Get input peg if it exists -- the set containing this peg should be colored red
586 :     my $peg = $pin_desc->{'pegs'}[0];
587 :    
588 :     # Iterate through the color sets until you find the set containing the input peg
589 :     for (my $i = 0; $i < @$color_sets; $i++)
590 :     {
591 :     foreach my $peg2 ( @{ $color_sets->[$i] } )
592 :     {
593 :     if ( $peg2 eq $peg )
594 :     {
595 :     # Return the set index
596 :     return $i;
597 :     }
598 :     }
599 :     }
600 :    
601 : dsouza 1.3 # The input peg may not be in a set if there is only one region or the coloring cutoff is very stringent.
602 :     return [$peg];
603 : dsouza 1.1 }
604 :     else
605 :     {
606 : dsouza 1.3 # Input is a peg-list, which may be split into multiple sets -- the largest will be called the
607 :     # "pinned" set.
608 : dsouza 1.1 my($i, $max_count);
609 :     my %pinned = map {$_ => 1} @$pinned_pegs;
610 :    
611 :     for (my $j = 0; $j < @$color_sets; $j++)
612 :     {
613 :     # count how many pinned pegs are found in each set
614 :     my $count = scalar grep {$pinned{$_}} @{ $color_sets->[$j] };
615 :    
616 :     if ( $max_count < $count )
617 :     {
618 :     # Keep track of the set having the largest number of pinned pegs
619 :     $max_count = $count;
620 :     $i = $j;
621 :     }
622 :     }
623 :    
624 :     return $i;
625 :     }
626 :     }
627 :    
628 :     sub offset {
629 :     my($set, $feature_data) = @_;
630 :    
631 :     my $offset;
632 :     foreach my $peg ( @$set )
633 :     {
634 :     $offset += abs($feature_data->{$peg}{'offset'});
635 :     }
636 : dsouza 1.3
637 :     return sprintf("%.2f", $offset/@$set);
638 : dsouza 1.1 }
639 :    
640 :     sub partition_pegs {
641 :     my($blast_hit) = @_;
642 :    
643 :     my %seen;
644 :     my $sets = [];
645 :    
646 :     # Iterate through the pegs with blast hits in arbitrary order
647 :     foreach my $peg1 ( keys %$blast_hit )
648 :     {
649 :     # If it has not been 'seen' (and placed in a set) use it as the seed for a set
650 :     if ( not $seen{$peg1} )
651 :     {
652 :     my $set = [$peg1];
653 :    
654 :     # Mark $peg1 as seen
655 :     $seen{$peg1} = 1;
656 :    
657 :     # Grow set through transitive closure -- note that @$set keeps growing
658 :     for (my $i = 0; $i < @$set; $i++)
659 :     {
660 :     $peg1 = $set->[$i];
661 :    
662 :     # Iterate through blast hits
663 :     foreach my $peg2 ( keys %{ $blast_hit->{$peg1} } )
664 :     {
665 :     # If $peg2 has not been seen, put it in the set, and mark it as seen
666 :     if ( not exists $seen{$peg2} )
667 :     {
668 :     push @$set, $peg2;
669 :     $seen{$peg2} = 1;
670 :     }
671 :     }
672 :     }
673 :    
674 :     # Add the new set to the set of sets
675 :     push @$sets, $set;
676 :     }
677 :     }
678 :    
679 :     return $sets;
680 :     }
681 :    
682 :     sub blast_hits {
683 :     my($fig, $regions, $feature_data, $fast_color, $color_sim_cutoff) = @_;
684 :     my($t0, $dt);
685 :    
686 :     # Set blast environment variables
687 :     $ENV{"BLASTMAT"} ||= "$FIG_Config::blastmat";
688 :     if ($ENV{"PATH"} !~ /fig\/bin/) { $ENV{"PATH"} = "$FIG_Config::bin:" . $ENV{"PATH"}; }
689 :    
690 :     # Get amino acid sequences
691 : dsouza 1.4 my $sequences = &get_peg_sequences($fig, $feature_data);
692 : dsouza 1.1
693 :     # Write the entire set of sequences to a fasta file
694 :     my $all_seqs_file = "$FIG_Config::temp/all_seqs.$$.tmp.fasta";
695 :     &write_seqs_to_file($sequences, $all_seqs_file);
696 :    
697 :     # Run formatdb
698 :     &formatdb_file($fig, $all_seqs_file);
699 :    
700 :     # If $fast_color == 0, the complete blast of all vs. all sequences is performed
701 :     # Otherwise, instead of all vs. all, the sequences from a single pinned peg region
702 :     # is blasted against all sequences. If any of the hits are better than a given cutoff
703 :     # ($cutoff_2), the pegs hit are deemed to be 'done', i.e. it is assumed that blasting this
704 :     # peg will not yield additional information for forming the peg sets. These 'done' pegs
705 :     # are then omitted from the blast when the sequences from the region they belong to
706 :     # is blasted.
707 :     my %blast_hit;
708 :     if ( $fast_color )
709 :     {
710 :     my $cutoff_2 = $color_sim_cutoff * 1e-20;
711 :     my %done_with;
712 :    
713 :     foreach my $region ( @$regions )
714 :     {
715 :     # Iterate through each region
716 :     my $fids = $region->{'features'};
717 :    
718 :     # Build up a hash of peg sequences which are not 'done_with'
719 :     my %region_seqs;
720 :     foreach my $peg ( grep {$feature_data->{$_}{'type'} eq 'peg'} @$fids )
721 :     {
722 :     if ( $sequences->{$peg} and not $done_with{$peg} )
723 :     {
724 :     $region_seqs{$peg} = $sequences->{$peg};
725 :     }
726 :     }
727 :    
728 :     if ( scalar keys %region_seqs )
729 :     {
730 :     # Write the region sequences to a file
731 :     my $region_seqs_file = "$FIG_Config::temp/region_seqs.$$.tmp.fasta";
732 :     &write_seqs_to_file(\%region_seqs, $region_seqs_file);
733 :    
734 :     # BLAST the region sequences against all other sequences
735 :     my $hits = &blast_files($fig, $region_seqs_file, $all_seqs_file, $color_sim_cutoff);
736 :    
737 :     # Build up hash of blast hits
738 :     foreach my $hit ( @$hits )
739 :     {
740 :     my($peg1, $peg2, $sc) = (split(/\s+/, $hit))[0,1,10];
741 :    
742 :     if ( $peg1 ne $peg2 )
743 :     {
744 :     $blast_hit{$peg1}{$peg2} = 1;
745 :     $blast_hit{$peg2}{$peg1} = 1;
746 :    
747 :     # If the blast score is less than the chosen cutoff, mark $peg2 as 'done_with' so
748 :     # that it will not be blasted with it's region sequences
749 :     if ( $sc <= $cutoff_2 )
750 :     {
751 :     $done_with{$peg2} = 1;
752 :     }
753 :     }
754 :     }
755 :     }
756 :     }
757 :     }
758 :     else
759 :     {
760 :     # BLAST sequence file against itself
761 :     my $hits = &blast_files($fig, $all_seqs_file, $all_seqs_file, $color_sim_cutoff);
762 :    
763 :     # Build up hash of blast hits
764 :     foreach my $hit ( @$hits )
765 :     {
766 :     my($peg1, $peg2, $sc) = (split(/\s+/, $hit))[0,1,10];
767 :    
768 :     if ( $peg1 ne $peg2 )
769 :     {
770 :     $blast_hit{$peg1}{$peg2} = 1;
771 :     $blast_hit{$peg2}{$peg1} = 1;
772 :     }
773 :     }
774 :     }
775 :    
776 :     return \%blast_hit;
777 :     }
778 :    
779 :     sub blast_hits_from_sims {
780 :     my($fig, $regions, $feature_data, $fast_color, $color_sim_cutoff) = @_;
781 :    
782 :     my %blast_hit;
783 :    
784 :     my $maxN = 2000;
785 :     my $maxP = $color_sim_cutoff;
786 :     my $select = 'fig';
787 :     my $max_expand = '';
788 :     my $filters = '';
789 :    
790 :     # Create a hash of all pegs
791 :     my %region_peg = map {$_ => 1} grep {$feature_data->{$_}{'type'} eq 'peg'} keys %$feature_data;
792 :    
793 :     if ( $fast_color == 1 )
794 :     {
795 :     my $cutoff_2 = $color_sim_cutoff * 1e-20;
796 :     my %done_with;
797 :    
798 :     # Iterate through each peg
799 :     foreach my $peg1 ( keys %region_peg )
800 :     {
801 :     # Skip the 'done_with' pegs
802 :     next if ($done_with{$peg1});
803 :    
804 :     my @sims = $fig->sims($peg1, $maxN, $maxP, $select, $max_expand, $filters);
805 :    
806 :     foreach my $sim ( grep {$region_peg{$_->[1]} and $_->[1] ne $peg1} @sims )
807 :     {
808 :     my($peg2, $sc) = @$sim[1,10];
809 :    
810 :     $blast_hit{$peg1}{$peg2} = 1;
811 :     $blast_hit{$peg2}{$peg1} = 1;
812 :    
813 :     # If the blast score is less than the chosen cutoff, mark $peg2 as 'done_with' so
814 :     # that it will not be blasted with it's region sequences
815 :     if ( $sc <= $cutoff_2 )
816 :     {
817 :     $done_with{$peg2} = 1;
818 :     }
819 :     }
820 :     }
821 :     }
822 :     else
823 :     {
824 :     # Iterate through each peg
825 :     foreach my $peg1 ( keys %region_peg )
826 :     {
827 :     my @sims = $fig->sims($peg1, $maxN, $maxP, $select, $max_expand, $filters);
828 :    
829 :     foreach my $peg2 ( map {$_->[1]} grep {$region_peg{$_->[1]} and $_->[1] ne $peg1} @sims )
830 :     {
831 :     $blast_hit{$peg1}{$peg2} = 1;
832 :     $blast_hit{$peg2}{$peg1} = 1;
833 :     }
834 :     }
835 :     }
836 :    
837 :     return \%blast_hit;
838 :     }
839 :    
840 :     sub blast_files {
841 :     my($fig, $input, $database, $cutoff) = @_;
842 :    
843 :     my $cmd = "$FIG_Config::ext_bin/blastall";
844 :     my @args = ('-p', 'blastp', '-i', $input, '-d', $database, '-m', 8, '-e', $cutoff);
845 :     my @blast_out = $fig->run_gathering_output($cmd, @args);
846 :    
847 :     return \@blast_out;
848 :     }
849 :    
850 :     sub formatdb_file {
851 :     my($fig, $file) = @_;
852 :    
853 :     my $cmd = "$FIG_Config::ext_bin/formatdb -i $file -p T";
854 :     $fig->run($cmd);
855 :     }
856 :    
857 :     sub write_seqs_to_file {
858 :     my($seq, $fasta_file) = @_;
859 :    
860 :     open(FASTA, ">$fasta_file") or die "could not create FASTA file '$fasta_file': $!";
861 :     foreach my $peg ( keys %$seq )
862 :     {
863 :     print FASTA ">$peg\n$seq->{$peg}\n";
864 :     }
865 :     close(FASTA) or die "could not close file FASTA file '$fasta_file': $!";
866 :     }
867 :    
868 :     sub get_peg_sequences {
869 :     my($fig, $feature_data) = @_;
870 :     my %sequences;
871 :    
872 :     # Iterate over each peg
873 :     foreach my $peg ( grep {$feature_data->{$_}{'type'} eq 'peg'} keys %$feature_data )
874 :     {
875 :     my $seq = $fig->get_translation($peg);
876 :    
877 :     if ( $seq )
878 :     {
879 :     $sequences{$peg} = $seq;
880 :     }
881 :     else
882 :     {
883 :     print STDERR "could not get sqeuence for $peg\n";
884 :     }
885 :     }
886 :    
887 :     return \%sequences;
888 :     }
889 :    
890 :    
891 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3