[Bio] / FortyEight / rp_glue_contigs.pl Repository:
ViewVC logotype

Annotation of /FortyEight/rp_glue_contigs.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : olson 1.1
2 :     #
3 :     # Glue split scaffolded contigs back together.
4 :     #
5 :    
6 :     use strict;
7 : olson 1.3 use Data::Dumper;
8 : olson 1.1 use FIG;
9 :     use FIG_Config;
10 :     use File::Basename;
11 :     use GenomeMeta;
12 : olson 1.2 use Carp 'croak';
13 :    
14 : olson 1.1
15 :     @ARGV == 1 or die "Usage: $0 job-dir\n";
16 :    
17 :     my $jobdir = shift;
18 :    
19 :     -d $jobdir or die "$0: job dir $jobdir does not exist\n";
20 :    
21 :     my $genome = &FIG::file_head("$jobdir/GENOME_ID");
22 :     chomp $genome;
23 :     $genome =~ /^\d+\.\d+/ or die "$0: Cannnot find genome ID for jobdir $jobdir\n";
24 :    
25 :     my $job = basename($jobdir);
26 :    
27 :     my $meta_file = "$jobdir/meta.xml";
28 :     my $meta = new GenomeMeta($genome, $meta_file);
29 :    
30 :     my $raw_dir = "$jobdir/raw/$genome";
31 :     my $rp_dir = "$jobdir/rp/$genome";
32 :    
33 :     my $errdir = "$jobdir/rp.errors";
34 :     &FIG::verify_dir($errdir);
35 :    
36 :     if (! -d $raw_dir)
37 :     {
38 :     &fatal("raw genome directory $raw_dir does not exist");
39 :     }
40 :    
41 :     if (! -d $rp_dir)
42 :     {
43 :     &fatal("processed genome directory $rp_dir does not exist");
44 :     }
45 :    
46 :     #
47 :     # Replace the original scaffolded contigs.
48 :     #
49 :    
50 :     my $new_contigs = "$rp_dir/contigs";
51 :     my $raw_contigs = "$rp_dir/unformatted_contigs";
52 :     my $split_contigs = "$rp_dir/split_contigs";
53 :     my $scaffold_map = "$rp_dir/scaffold.map";
54 :    
55 :     -f $new_contigs or &fatal("cannot find new contigs file $new_contigs");
56 :     -f $raw_contigs or &fatal("cannot find raw contigs file $raw_contigs");
57 :    
58 :     #
59 :     # Contigs moves to split_contigs for future reference.
60 :     # Unformatted contigs get formatted into contigs.
61 :     # When all is complete, map_to_scaffolds is run to fix the tbl files.
62 :     #
63 : olson 1.4 # We only do this if scaffold_map exists - if it doesn't exist, we hadn't
64 :     # split the contigs, so none of this is necessary.
65 :     #
66 : olson 1.1
67 : olson 1.4 if (-f $scaffold_map)
68 :     {
69 :     rename($new_contigs, $split_contigs) or &fatal("cannot rename $new_contigs $split_contigs: $!");
70 : olson 1.1
71 : olson 1.4 my @cmd = ("$FIG_Config::bin/map_to_scaffold", $scaffold_map, $rp_dir);
72 :     print "Run @cmd\n";
73 :     $meta->add_log_entry($0, ['running', @cmd]);
74 :    
75 :     my $rc = system(@cmd);
76 :     if ($rc != 0)
77 :     {
78 :     &fatal("map_to_scaffold failed with rc=$rc: @cmd");
79 :     }
80 :    
81 :     my $reformat_log = "$errdir/reformat_contigs_glue.stderr";
82 :    
83 :     my @cmd = ("$FIG_Config::bin/reformat_contigs", "-v", "-logfile=$reformat_log", $raw_contigs, $new_contigs);
84 :    
85 :     print "Run @cmd\n";
86 : olson 1.1
87 : olson 1.4 $meta->add_log_entry($0, ['running', @cmd]);
88 :    
89 :     my $rc = system(@cmd);
90 :     if ($rc != 0)
91 :     {
92 :     &fatal("reformat command failed with rc=$rc: @cmd\n");
93 :     }
94 : olson 1.1 }
95 : olson 1.4 else
96 : olson 1.1 {
97 : olson 1.4 #
98 :     # We didn't need to map. Don't need to do anything here.
99 :     #
100 : olson 1.1 }
101 :    
102 : olson 1.3 #
103 :     # Now that our contig coordinates are back to their original state, we can map
104 :     # original assigned_functions.
105 :     #
106 :    
107 :     if (-s "$raw_dir/assigned_functions")
108 :     {
109 :     $meta->add_log_entry($0, "begin mapping $raw_dir/assigned_functions");
110 :     open(MAP, "$FIG_Config::bin/make_peg_map_from_tbl $raw_dir $rp_dir |") or
111 :     &fatal("cannot open pipe from$FIG_Config::bin/make_peg_map_from_tbl $raw_dir $rp_dir: $!");
112 :     my %map;
113 :     while (<MAP>)
114 :     {
115 :     chomp;
116 :     my($f, $t) = split(/\t/);
117 :     $map{$f} = $t;
118 :     }
119 :     close(MAP);
120 :    
121 :     open(OLD, "<$raw_dir/assigned_functions") or &fatal("Cannot open $raw_dir/assigned_functions: !");
122 :     open(NEW, ">$rp_dir/assigned_functions") or &fatal("Cannot open $rp_dir/assigned_functions: !");
123 :    
124 :     while (<OLD>)
125 :     {
126 :     chomp;
127 :     my($opeg, $fun) = split(/\t/);
128 :     my $npeg = $map{$opeg};
129 :    
130 :     if ($npeg)
131 :     {
132 :     print NEW "$npeg\t$fun\n";
133 :     }
134 :     }
135 :     close(OLD);
136 :     close(NEW);
137 :     }
138 :    
139 :     #
140 :     # And since we now have all of our parts and pieces, create annotations & evidence.
141 :     #
142 :    
143 :     unlink("$rp_dir/annotations");
144 :     my $rc = system("$FIG_Config::bin/initialize_ann_and_ev $rp_dir 2> $errdir/initialize_ann_and_ev.stderr");
145 :     if ($rc != 0)
146 :     {
147 :     &fatal("initialize_ann_and_ev $rp_dir failed with rc=$rc");
148 :     }
149 : olson 1.1
150 : olson 1.5 system("$FIG_Config::bin/rp_index_attributes", $jobdir);
151 :    
152 : olson 1.1 $meta->add_log_entry($0, "glue_contigs completed\n");
153 :     $meta->set_metadata("glue_contigs.running", "no");
154 :     $meta->set_metadata("status.glue_contigs", "complete");
155 :    
156 :     exit;
157 :    
158 :     sub fatal
159 :     {
160 :     my($msg) = @_;
161 :    
162 :     $meta->add_log_entry($0, ['fatal error', $msg]);
163 :     $meta->set_metadata("status.glue_contigs", "error");
164 : olson 1.4 $meta->set_metadata("glue_contigs.running", "no");
165 : olson 1.1
166 :     croak "$0: $msg";
167 :     }
168 :    

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3