[Bio] / FigKernelScripts / get_neighbors_and_corr_to_ref.pl Repository:
ViewVC logotype

Annotation of /FigKernelScripts/get_neighbors_and_corr_to_ref.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : overbeek 1.1 ########################################################################
2 :    
3 : olson 1.6 # This is a SAS Component
4 :    
5 : overbeek 1.1 use SeedHTML;
6 :     use strict;
7 :     use SeedEnv;
8 :     use ProtSims;
9 :     use gjoseqlib;
10 : olson 1.6 use Data::Dumper;
11 : overbeek 1.1
12 :     my $usage = "usage: get_neighbors_and_corr_to_ref GenomeDir";
13 :     my $gdir;
14 :    
15 : olson 1.6 $| = 1;
16 :    
17 :     my $sapO = SAPserver->new;
18 :    
19 : overbeek 1.1 ($gdir = shift @ARGV)
20 :     || die $usage;
21 : overbeek 1.5 ($gdir =~ /(\d+\.\d+)$/) || die "Invalid Genome Directory: $gdir";
22 :     my $gdir_id = $1;
23 : overbeek 1.1
24 :     my @fasta = &gjoseqlib::read_fasta("$gdir/Features/peg/fasta");
25 : overbeek 1.4 my %id2seqH = map { ($_->[2] && (length($_->[2]) > 30)) ? ($_->[0] => $_->[2]) : () } @fasta;
26 : overbeek 1.1
27 :     &SeedUtils::verify_dir("$gdir/CorrToReferenceGenomes");
28 : olson 1.6 print "Finding neighbors\n";
29 : overbeek 1.1 my @poss_pegs = &prioritize_pegs_used_to_find_neighbors($gdir);
30 : olson 1.6 print "found " . scalar(@poss_pegs) . " poss_pegs\n";
31 : overbeek 1.1 my %counts;
32 :     my $best = 0;
33 :     my $tuple;
34 :     while (($best < 500) && ($tuple = shift @poss_pegs))
35 :     {
36 : overbeek 1.4 my($role,$peg) = @$tuple;
37 :     if ($id2seqH{$peg} && (length($id2seqH{$peg}) > 30))
38 :     {
39 :     &compute_hits_and_set_best($tuple,\%id2seqH,\%counts,\$best);
40 :     }
41 : overbeek 1.1 }
42 :     if ($best == 0) { die "$gdir describes a genome without enough RAST-called genes to identify neighbors" }
43 :     my @reference = sort { $counts{$b} <=> $counts{$a} } keys(%counts);
44 :     if (@reference > 30) { $#reference = 29 }
45 :    
46 :     my $genomesH = $sapO->all_genomes(-complete => 1);
47 :     open(CLOSE,">$gdir/closest.genomes") || die "could not open closest.genomes";
48 : olson 1.6 print "Generating correspondences for these genomes:\n";
49 :     print "\t$_\n" for @reference;
50 : overbeek 1.1 foreach my $g2 (@reference)
51 :     {
52 : overbeek 1.5 if ($g2 ne $gdir_id)
53 :     {
54 : olson 1.6 print "Generating correspondences for $g2...\n";
55 : overbeek 1.5 &generate_correspondence_table($g2,$gdir);
56 : olson 1.6 print "Generating correspondences for $g2...done\n";
57 : overbeek 1.5 print CLOSE join("\t",($g2,$genomesH->{$g2})),"\n";
58 :     }
59 : overbeek 1.1 }
60 :     close(CLOSE);
61 :    
62 :     sub generate_correspondence_table {
63 :     my($g2,$gdir) = @_;
64 :    
65 :     ($gdir =~ /(\d+\.\d+)$/) || die "Invalid Genome Directory: $gdir";
66 :     my $g1 = $1;
67 : overbeek 1.5 if ($g1 ne $g2)
68 :     {
69 :     system "svr_corresponding_genes -d $gdir $g1 $g2 > $gdir/CorrToReferenceGenomes/$g2";
70 :     }
71 : overbeek 1.1 }
72 :    
73 :     sub prioritize_pegs_used_to_find_neighbors {
74 :     my($gdir) = @_;
75 :    
76 :     my %by_func;
77 :     (-s "$gdir/assigned_functions") || die "$gdir contains no assigned_functions";
78 :    
79 : overbeek 1.4 my %uniqH;
80 : overbeek 1.1 foreach my $line (`cat $gdir/assigned_functions`)
81 :     {
82 :     if ($line =~ /^(fig\|\d+\.\d+\.peg\.\d+)\t(\S[^\#]+\S)/)
83 :     {
84 : overbeek 1.4 $uniqH{$1} = $2;
85 : overbeek 1.1 }
86 :     }
87 : overbeek 1.4
88 :     foreach my $peg (keys(%uniqH))
89 :     {
90 :     my $func = $uniqH{$peg};
91 :     $func =~ s/\s*\#.*$//;
92 :     push(@{$by_func{$func}},$peg);
93 :     }
94 :    
95 : overbeek 1.1 my @synthetases = map {[$_,$by_func{$_}->[0]] } grep { @{$by_func{$_}} == 1 } grep { $_ =~ /tRNA synthetase/ } keys(%by_func);
96 :     my @ribosomal_proteins = map {[$_,$by_func{$_}->[0]] } grep { @{$by_func{$_}} == 1 } grep { $_ =~ /ribosomal protein/ } keys(%by_func);
97 :     my @ok_pegs = map {[$_,$by_func{$_}->[0]] } grep { @{$by_func{$_}} == 1 } keys(%by_func);
98 :     my @prioritized = ();
99 :     my %seen;
100 :     foreach my $tuple (@synthetases,@ribosomal_proteins,@ok_pegs)
101 :     {
102 :     if (! $seen{$tuple->[0]})
103 :     {
104 :     $seen{$tuple->[0]} = 1;
105 :     push(@prioritized,$tuple);
106 :     }
107 :     }
108 :     return @prioritized;
109 :     }
110 :    
111 :     sub compute_hits_and_set_best {
112 :     my($tuple,$id2seqH,$counts,$bestP) = @_;
113 :    
114 :     my($role,$peg) = @$tuple;
115 : olson 1.6 print "Get figfam pegs for $role\n";
116 : overbeek 1.1 my $figfam_pegs = &figfam_pegs_for_role($role);
117 :    
118 : olson 1.6 print "Compute sims\n";
119 : overbeek 1.1 my @sims = &ProtSims::blastP([[$peg,'',$id2seqH->{$peg}]],$figfam_pegs,10);
120 : olson 1.6 print "Computed " . scalar(@sims) . " sims\n";
121 : overbeek 1.1 my $i;
122 :     for ($i=0; (($i < @sims) && ($i < 50)); $i++)
123 :     {
124 :     my $g2 = &SeedUtils::genome_of($sims[$i]->id2);
125 :     $counts->{$g2} += 50 - $i;
126 :     if ($counts->{$g2} > $$bestP) { $$bestP = $counts->{$g2} }
127 :     }
128 :     }
129 :    
130 :     sub figfam_pegs_for_role {
131 :     my($role) = @_;
132 :    
133 :     my %figfams;
134 : olson 1.6
135 :     my $res = $sapO->all_figfams(-roles => $role);
136 :     my @pegs;
137 :     for my $ff (keys %$res)
138 : overbeek 1.1 {
139 : olson 1.6 my $fids = $sapO->figfam_fids(-id => $ff);
140 :     push(@pegs, @$fids);
141 : overbeek 1.1 }
142 : olson 1.6
143 :     my $idsH = $sapO->ids_to_sequences(-ids => \@pegs, -protein => 1);
144 :    
145 :     return [map { my $seq = $idsH->{$_}; $seq ? [$_,'',$seq] : () } keys(%$idsH)];
146 : overbeek 1.1 }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3