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

Annotation of /FigKernelPackages/PinnedRegions.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3