[Bio] / Sprout / SimLoad.pl Repository:
ViewVC logotype

Annotation of /Sprout/SimLoad.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     use strict;
4 :     use CGI;
5 :     use Tracer;
6 :     use Genome;
7 : parrello 1.7 use DBKernel;
8 : parrello 1.1 use SimBlocks;
9 :     use File::Path;
10 : parrello 1.2 use BasicLocation;
11 : parrello 1.3 use Cwd;
12 : parrello 1.1
13 :     =head1 Similarity Block Loader
14 :    
15 :     This script loads the similarity block database from
16 : parrello 1.3 the input files. The load process involves two steps:
17 :     converting the input files into C<dtx> load files
18 :     (B<generate>), and loading the C<dtx> files into the
19 :     database (B<load>).
20 : parrello 1.1
21 : parrello 1.3 The script takes a single parameter-- a directory name.
22 : parrello 1.7 The default directory name is C<"$FIG_Config::data/CloseStrainSets">.
23 :     The input files should be in subdirectories called
24 :     C<Block> under the subdirectories of the input directory.
25 :     The subdirectory names themselves are considered the name
26 :     of the close-strain set. So, for example
27 :    
28 :     Data/CloseStrainSets/Vibrio/Block
29 :    
30 :     would be presumed to contain the Vibrio strains.
31 :    
32 :     The output files will be produced in the similarity block
33 :     data directory C<$fig_config::SimBlockData>, which will be
34 :     created if it does not exist. The input directory and all its
35 : parrello 1.3 subdirectories will be processed for input files.
36 : parrello 1.1
37 :     In addition to the directory name, the following
38 :     option parameters are supported.
39 :    
40 :     =over 4
41 :    
42 :     =item trace
43 :    
44 :     Trace level for output messages. A higher number means more
45 : parrello 1.4 messages. The default is C<3>. Trace messages are sent to
46 : parrello 1.3 the file C<trace.log> in the B<$FIG_Config::tmp>
47 :     directory.
48 : parrello 1.1
49 : parrello 1.4 =item sql
50 :    
51 :     If specified, SQL activity will be traced at the specified
52 :     trace level.
53 :    
54 : parrello 1.1 =item load
55 :    
56 :     C<yes> to load the data into the database, else C<no>.
57 :     The default is C<yes>.
58 :    
59 :     =item generate
60 :    
61 :     C<yes> to generate output files from input files, else
62 :     C<no>. The default is C<yes>.
63 :    
64 : parrello 1.6 =item createDB
65 :    
66 :     If specified, the database will be dropped and
67 :     re-created before loading.
68 :    
69 : parrello 1.1 =back
70 :    
71 :     For example, the following command line will process the
72 : parrello 1.7 input files in the C</Users/fig/BlastData> directory tree
73 :     and run at a trace level of 3.
74 : parrello 1.1
75 : parrello 1.3 C<< SimLoad -trace=3 /Users/fig/BlastData >>
76 :    
77 :     The following command line converts the input files in
78 :     the default directory into load files but does not load
79 :     the database and runs at a trace level of 2.
80 : parrello 1.1
81 : parrello 1.3 C<< SimLoad -load=no >>
82 : parrello 1.1
83 :     =head2 Input Directory
84 :    
85 : parrello 1.7 The following files must exist in each C<Block> directory
86 :     under the input directory.
87 : parrello 1.1
88 :     =over 4
89 :    
90 :     =item Genome.tbl
91 :    
92 :     This is a tab-delimited file that contains the ID of each
93 :     genome followed by a description string.
94 :    
95 :     =item Block.tbl, InterGenic_Block.tbl
96 :    
97 :     These are tab-delimited files that associate a gene name
98 :     with each block. The InterGenic file is optional.
99 :    
100 :     =item Region.tbl, InterGenic_Region.tbl
101 :    
102 :     These are tab-delimited files that describe each region
103 :     of a block. The InterGenic file is optional.
104 :    
105 :     =back
106 :    
107 :     The format of each file is given below.
108 :    
109 :     =head3 Genome.tbl
110 :    
111 :     The Genome file is copied almost unmodified to the
112 :     load file for the B<Genome> entity. Each record
113 :     represents a single genome. It has the following
114 :     fields.
115 :    
116 :     =over 4
117 :    
118 :     =item genomeID
119 :    
120 :     The ID of the genome.
121 :    
122 :     =item description
123 :    
124 :     A text description of the genome (usually the species name with
125 :     a strain ID).
126 :    
127 :     =back
128 :    
129 :     =head3 Block.tbl, InterGenic_Block.tbl
130 :    
131 :     These files produce most of the data found in the B<GroupBlock>
132 :     entity. Each record represents a single block. Blocks either
133 :     correspond to genes or to inter-genic regions. Both types
134 :     of blocks may appear in multiple locations in multiple
135 :     contigs. The files should be sorted by block ID.
136 :    
137 :     =over 4
138 :    
139 :     =item blockID
140 :    
141 :     The unique ID of the block. This ID is also used in the
142 :     C<Region.tbl> file.
143 :    
144 :     =item blockName
145 :    
146 :     The name of the block. For a gene block, this is the gene
147 :     name. For an inter-genic block, it is a name computed
148 :     from the names of the genes that are commonly nearby.
149 :    
150 :     =back
151 :    
152 :     =head3 Region.tbl, InterGenic_Region.tbl
153 :    
154 :     These files describe the regions in each block. They are
155 :     used to derive the relationships between genomes and
156 :     contigs (B<ConsistsOf>), the contigs themselves
157 :     (B<Contig>), the relationships between blocks and
158 :     contigs (B<ContainsRegionIn>), and the derived
159 :     relationship between genomes and blocks
160 :     (B<HasInstanceOf>). The files must be sorted by block
161 :     ID, and each record in a file represents a single
162 :     region in a contig. Each region belongs to a
163 :     single block. Note that the C<Region.tbl> file contains
164 :     the regions for the C<Block.tbl> file, and the
165 :     C<InterGenic_Region.tbl> file contains the regions for
166 :     the C<InterGenic_Block.tbl> file. No mixing is allowed.
167 :    
168 :     =over 4
169 :    
170 :     =item regionPEG
171 :    
172 :     PEG ID for this region. If the region is in an
173 :     inter-genic block, this field will be composed of
174 :     the IDs for the neighboring genes.
175 :    
176 :     =item genomeID
177 :    
178 :     ID of the relevant genome.
179 :    
180 :     =item contigID
181 :    
182 :     ID of the contig containing this region. This is a standard contig
183 :     ID that does not include the genome ID. It will be converted to
184 :     a Sprout-style contig ID (which includes the genome data) before
185 :     it is written to the output files.
186 :    
187 :     =item begin
188 :    
189 :     The start point of the region. For a forward region this is the
190 :     left endpoint; for a reverse region it is the right endpoint. It
191 :     is a 1-based offset (which is consistent with Sprout usage), and
192 :     the identified location is inside the region.
193 :    
194 :     =item end
195 :    
196 :     The end point of the region. For a forward region this is the
197 :     right endpoint; for a reverse region it is the left endpoint. It
198 :     is a 1-based offset (which is consistent with Sprout usage), and
199 :     the identified location is inside the region.
200 :    
201 :     =item blockID
202 :    
203 :     The ID of the block containing this region.
204 :    
205 :     =item snippet
206 :    
207 :     A DNA snippet representing the contents of the region. The region
208 :     may be shorter than the block length. If that is the case, the
209 :     snippet will contain insertion characters (C<->). So, while it
210 :     is not the case that every region in a block must be the same
211 :     length, all of the snippets for a block must be the same length.
212 :     The snippets will be in alignment form. In other words, if the
213 :     region is reversed, the nucleotide letters will be the complement
214 :     in transcription order. (For example, if positions 20 through 25
215 :     of contig B<XXX> are C<AGCCTT>, then the snippet for C<XXX_25_20>
216 :     will be C<AAGGCT>.)
217 :    
218 :     =back
219 :    
220 :     =head2 Output File Notes
221 :    
222 :     =over 4
223 :    
224 :     =item Genome.dtx
225 :    
226 :     This file is a direct copy of the C<Genome.tbl> file; however, we
227 :     also use it to create a hash of genome IDs (C<%genomes>). The hash
228 :     is useful when validating the C<Region.tbl> file.
229 :    
230 :     =item Contig.dtx
231 :    
232 :     This file contains nothing but contig IDs. As contigs are
233 :     discovered from the C<Region.tbl> file their IDs are put
234 :     into the C<%contigs> hash. This hash maps contig IDs to
235 :     their parent genome IDs. When processing is complete,
236 :     this file is generated from the hash.
237 :    
238 :     =item GroupBlock.dtx
239 :    
240 :     This file describes the blocks. As records come in from
241 :     C<Region.tbl>, we build a hash called C<%blockData> that
242 :     contains our latest estimate of all the C<GroupBlock.dtx>
243 :     columns for the current block (with the exception of
244 :     B<variance>, which is computed by dividing the B<snip-count>
245 :     by the length (B<len>).
246 :    
247 :     =item ConsistsOf.dtx
248 :    
249 :     This file maps genomes to contigs, and is generated from
250 :     the C<%contigs> hash built while reading the C<Region.tbl>
251 :     file.
252 :    
253 :     =item HasInstanceOf.dtx
254 :    
255 :     This file lists the genomes containing each block. The
256 :     C<Region.tbl> file is sorted by block. While inside a
257 :     block's section of the file, we use a hash called
258 :     C<%genomesFound> that contains the ID of every genome
259 :     found for the block. When we finish with a block,
260 :     we run through the C<%genomesFound> hash to produce
261 :     the block's B<HasInstanceOf> data.
262 :    
263 :     =item Region.dtx
264 :    
265 :     This file describes the contig regions in the blocks.
266 :     As the C<Region.tbl> file is read in, we build a
267 :     hash called C<%regionMap> that maps a region's
268 :     SEED-style location string to the DNA content.
269 :     When we finish with a block, the DNA content is
270 :     converted into an alignment by comparing it to
271 :     the block's pattern in C<%blockData>. (Essentially,
272 :     we only include the region's content for the
273 :     positions that vary between regions in the block.)
274 :     From this and the region string itself, we have
275 :     enough data to create the B<Region>
276 :     data.
277 :    
278 :     =item IncludesRegion.dtx
279 :    
280 :     This file maps group blocks to regions. The first column
281 :     is the block ID and the second column is the SEED-style
282 :     region string for the target region. This file is built
283 :     in parallel with C<Region.dtx>. It will have one record
284 :     for each region.
285 :    
286 :     =item ContainsRegion.dtx
287 :    
288 :     This file maps contigs to regions. The first column is
289 :     the contig ID and the second column is the SEED-style
290 :     location string for the region. It contains two redundant
291 :     columns used for sorting-- the region length (column 3)
292 :     and the left-most region position (column 4). This
293 :     file is built in parallel with C<Region.dtx>. It will
294 :     have one record for each region.
295 :    
296 :     =cut
297 :    
298 :     # Create a huge number we can use for an end-of-file
299 :     # indicator in the block ID.
300 :     my $TRAILER = 999999999;
301 :    
302 :     # Parse the command line.
303 : parrello 1.5 my ($options, @arguments) = StandardSetup(['SimBlocks'],
304 :     { load => ['yes', "'no' to suppress loading the database"],
305 : parrello 1.6 generate => ['yes', "'no' to suppress generating the load files"],
306 :     createDB => [0, 'drop and create the database before loading']
307 : parrello 1.5 },
308 :     "directoryName",
309 : parrello 1.1 @ARGV);
310 :     # Extract the directory name from the argument array.
311 : parrello 1.7 my $inDirectoryTree = ($arguments[0] ? Cwd::abs_path($arguments[0]) : "$FIG_Config::data/CloseStrainSets");
312 :     # Check to see if we need to create the database.
313 :     if ($options->{createDB}) {
314 :     Trace("Creating database.") if T(2);
315 :     DBKernel::CreateDB($FIG_Config::simBlocksDB);
316 : parrello 1.1 }
317 :     # Get the output directory.
318 :     my $outDirectory = $FIG_Config::simBlocksData;
319 :     # Insure that it exists.
320 :     if (! -d $outDirectory) {
321 : parrello 1.3 Trace("Creating output directory $outDirectory.") if T(2);
322 : parrello 1.1 mkpath($outDirectory);
323 : parrello 1.4 } elsif ($options->{generate} eq 'yes') {
324 :     # Here we have an output directory already and are going to generate new
325 :     # load files. Clear any leftover data from previous runs.
326 : parrello 1.3 my @files = grep { $_ =~ /.dtx$/ } Tracer::OpenDir($outDirectory);
327 :     my $numFiles = @files;
328 :     if ($numFiles > 0) {
329 :     Trace("Deleting $numFiles old dtx files from $outDirectory.") if T(2);
330 :     unlink map { "$outDirectory/$_" } @files;
331 :     }
332 : parrello 1.1 }
333 : parrello 1.3 # Create an error counter and a directory counter.
334 : parrello 1.1 my $errorCount = 0;
335 : parrello 1.3 my $dirCount = 0;
336 : parrello 1.1 # Check to see if we should generate the output files.
337 :     if ($options->{generate} eq 'no') {
338 :     # Here we are to use existing output files.
339 : parrello 1.3 Trace("Existing database load files will be used.") if T(2);
340 : parrello 1.1 } else {
341 :     # Here we need to produce new output files.
342 :     # Verify that the input directory exists.
343 : parrello 1.3 if (! -d $inDirectoryTree) {
344 :     Confess("Input directory \"$inDirectoryTree\" not found.");
345 : parrello 1.1 }
346 : parrello 1.3 # Loop through the subdirectories.
347 : parrello 1.7 for my $inputDirectory (Tracer::OpenDir($inDirectoryTree, 1)) {
348 :     # Verify that this is a directory.
349 :     my $inDirectory = "$inDirectoryTree/$inputDirectory/Blocks";
350 :     if (-d $inDirectory) {
351 : parrello 1.3 # Here we have a directory to process. Check for a genome
352 :     # file.
353 :     my $genomeFileName = "$inDirectory/Genome.tbl";
354 :     if (! -e $genomeFileName) {
355 :     Trace("$genomeFileName not found. Directory skipped.") if T(1);
356 :     } else {
357 :     # Now we can process the directory and accumulate the error
358 :     # count.
359 : parrello 1.7 $errorCount += ProcessDirectory($inDirectory, $outDirectory, $inputDirectory);
360 : parrello 1.3 $dirCount++;
361 :     }
362 :     }
363 :     }
364 :     Trace("Load files generated from $dirCount directories.") if T(2);
365 :     }
366 :     # Check for errors.
367 :     if ($errorCount > 0) {
368 :     Trace("$errorCount errors found in input files.") if T(0);
369 :     } else {
370 :     # No errors, so it's okay to load the database.
371 :     if ($options->{load} eq 'yes') {
372 :     # Here we have no outstanding errors and the user wants us to load
373 :     # the database. First, we create a similarity block object.
374 :     my $simBlocks = SimBlocks->new();
375 :     # Use it to load the database. Note we specify that the tables are to be
376 :     # dropped and rebuilt.
377 :     $simBlocks->LoadTables($outDirectory, 1);
378 :     Trace("Database loaded.") if T(2);
379 :     }
380 :     }
381 :    
382 :     # Process a single input directory.
383 :     sub ProcessDirectory {
384 : parrello 1.7 my ($inDirectory, $outDirectory, $groupName) = @_;
385 : parrello 1.3 Trace("Processing directory $inDirectory.") if T(2);
386 : parrello 1.1 # Our first task is to copy the genome data to the output directory
387 : parrello 1.7 # and add the genomes to the genome list.
388 : parrello 1.1 my %genomes = ();
389 : parrello 1.3 Open(\*GENOMESIN, "<$inDirectory/Genome.tbl");
390 :     Open(\*GENOMESOUT, ">>$outDirectory/Genome.dtx");
391 :     # Count the genomes read and errors found.
392 : parrello 1.1 my $genomeCount = 0;
393 : parrello 1.3 my $errorCount = 0;
394 : parrello 1.1 # Loop through the input.
395 :     while (my $genomeData = <GENOMESIN>) {
396 :     # Echo the genome record to the output.
397 : parrello 1.7 my @fields = Tracer::ParseRecord($genomeData);
398 :     print GENOMESOUT join("\t", @fields, $groupName). "\n";
399 : parrello 1.1 # Extract the genome ID.
400 : parrello 1.7 my $genomeID = $fields[0];
401 : parrello 1.1 # Store it in the genomes hash. We start with a value of 0. If
402 :     # contig information for the genome is found, we change the value
403 :     # to 1. When we're all done with the regions, we can check the
404 :     # hash to insure all the genomes were represented in the input.
405 :     $genomes{$genomeID} = 0;
406 :     # Count this genome.
407 :     $genomeCount++;
408 :     }
409 : parrello 1.3 Trace("$genomeCount genomes found.") if T(2);
410 : parrello 1.1 # Close the files.
411 :     close GENOMESIN;
412 :     close GENOMESOUT;
413 :     # Create the contig hash used to associate contigs to their parent
414 :     # genomes.
415 :     my %contigs = ();
416 :     # Now we begin to read the Block and Region files in parallel. Both
417 :     # are sorted by block ID, so all processing for this section of the
418 :     # script is done a block at a time. The first task is to
419 :     # open the output files.
420 : parrello 1.3 Open(\*BLOCKSOUT, ">>$outDirectory/GroupBlock.dtx");
421 :     Open(\*REGIONSOUT, ">>$outDirectory/Region.dtx");
422 :     Open(\*INSTANCESOUT, ">>$outDirectory/HasInstanceOf.dtx");
423 :     Open(\*CONTAINSOUT, ">>$outDirectory/ContainsRegion.dtx");
424 :     Open(\*INCLUDESOUT, ">>$outDirectory/IncludesRegion.dtx");
425 : parrello 1.1 # Determine which file sets we'll be processing.
426 :     my @fileSets = ();
427 :     my @prefixes = ("", "InterGenic_");
428 :     for my $prefix (@prefixes) {
429 :     if (-e "$inDirectory/${prefix}Block.tbl") {
430 :     push @fileSets, $prefix;
431 :     }
432 :     }
433 : parrello 1.3 # Set up the duplicate-region check.
434 :     my %allRegions = ();
435 : parrello 1.1 # Set up some counters.
436 :     my ($blocksCount, $regionsCount) = (0, 0);
437 :     # Loop through the useful file sets.
438 :     for my $fileSet (@fileSets) {
439 : parrello 1.3 Open(\*BLOCKSIN, "<$inDirectory/${fileSet}Block.tbl");
440 :     Open(\*REGIONSIN, "<$inDirectory/${fileSet}Region.tbl");
441 :     Trace("Processing ${fileSet}Blocks.") if T(2);
442 : parrello 1.1 # The outer loop processes blocks. This is accomplished by reading
443 :     # through the block file. We prime the loop by reading the first
444 :     # region record. This is because we finish processing a block when
445 :     # the first record of the next block is found in the region file.
446 :     my %regionRecord = GetRegionRecord();
447 :     $regionsCount++;
448 :     while (my $blockRecord = <BLOCKSIN>) {
449 :     $blocksCount++;
450 :     # Parse the block record.
451 :     my ($blockID, $blockName, $pegID) = Tracer::ParseRecord($blockRecord);
452 :     # Create the block data for this block.
453 :     my %blockData = ( id => $blockID, description => $blockName );
454 :     # Initialize the tracking hashes. "genomesFound" tracks the
455 : parrello 1.2 # genomes whose contigs are represented by the block,
456 : parrello 1.1 # "regionMap" maps each region to its contents, and
457 :     # "regionPeg" maps each region to its PEG (if any).
458 :     my %genomesFound = ();
459 :     my %regionMap = ();
460 :     my %regionPeg = ();
461 :     # Count the number of regions found in this block.
462 :     my $regionCounter = 0;
463 :     # Loop through the regions in the block. Because of the way
464 :     # "GetRegionRecord" works, the "blockID" field will have an
465 :     # impossibly high value if we've hit end-of-file in the
466 :     # region input file.
467 :     while ($regionRecord{blockID} <= $blockID) {
468 :     # If this region's block ID is invalid, complain
469 :     # and skip it.
470 :     if ($regionRecord{blockID} != $blockID) {
471 : parrello 1.3 Trace("Block $regionRecord{blockID} in region record $regionsCount not found in block input file at record $blocksCount.") if T(0);
472 : parrello 1.1 $errorCount++;
473 :     } else {
474 :     # Here both files are in sync, which is good. The next step is
475 :     # to connect with the Genome and the Contig.
476 :     my $genomeID = $regionRecord{genomeID};
477 :     my $contigID = "$genomeID:$regionRecord{contigID}";
478 :     if (! exists $genomes{$genomeID}) {
479 : parrello 1.3 Trace("Genome $genomeID in region record $regionsCount not found in genome input file.") if T(0);
480 : parrello 1.1 $errorCount++;
481 :     } else {
482 :     # Denote this genome has an instance of this block.
483 :     $genomesFound{$genomeID} = 1;
484 :     # Denote this genome has occurred in the region file.
485 :     $genomes{$genomeID} = 1;
486 :     # Connect the contig to the genome.
487 :     $contigs{$contigID} = $genomeID;
488 :     # Now we need to process the snippet. First, we create a
489 :     # region string.
490 :     my $regionString = "${contigID}_$regionRecord{begin}_$regionRecord{end}";
491 :     # Next, we stuff the snippet and PEG in the region's hash entries.
492 :     my $snippet = $regionRecord{snippet};
493 :     $regionMap{$regionString} = $snippet;
494 :     $regionPeg{$regionString} = $regionRecord{peg};
495 :     # Check to see if this is the block's first snippet.
496 :     if (! exists $blockData{pattern}) {
497 :     # Here it is, so store the snippet as the pattern.
498 :     $blockData{pattern} = $snippet;
499 :     $blockData{"snip-count"} = 0;
500 :     $blockData{len} = length $snippet;
501 :     } elsif ($blockData{len} != length $snippet) {
502 :     # Here it is not the first, but the lengths do not match.
503 : parrello 1.3 Trace("Snippet for region record $regionsCount does not match block length $blockData{len}.") if T(0);
504 : parrello 1.1 $errorCount++;
505 :     } else {
506 :     # Here everything is legitimate, so we merge the new
507 :     # snippet into the pattern.
508 :     ($blockData{pattern}, $blockData{"snip-count"}) =
509 :     SimBlocks::MergeDNA($blockData{pattern}, $snippet);
510 :     }
511 :     }
512 :     # Count this region.
513 :     $regionCounter++;
514 :     }
515 :     # Get the next region record.
516 :     %regionRecord = GetRegionRecord();
517 :     }
518 :     # We have now processed all the regions in the block. Insure we found at least
519 :     # one.
520 :     if (! $regionCounter) {
521 : parrello 1.3 Trace("No regions found for block $blockID at $blocksCount in block input file.") if T(0);
522 : parrello 1.1 $errorCount++;
523 :     } else {
524 : parrello 1.4 Trace("$regionCounter regions found in block $blockID.") if T(4);
525 : parrello 1.7 # Write the block record. Note that the block ID is prefixed by the group name to
526 :     # make it unique.
527 : parrello 1.1 my $variance = $blockData{"snip-count"} / $blockData{len};
528 : parrello 1.7 print BLOCKSOUT join("\t", "$groupName:$blockID", $blockData{description}, $blockData{len},
529 : parrello 1.1 $blockData{"snip-count"}, $variance, $blockData{pattern}) . "\n";
530 :     # Find all the variance points in the block pattern. We'll use them to create
531 :     # the content strings for each region.
532 :     my @positions = SimBlocks::ParsePattern($blockData{pattern});
533 :     # Loop through the regions, writing them out to the region output file.
534 :     for my $region (keys %regionMap) {
535 : parrello 1.3 if (length($region) > 80) {
536 :     Trace("Invalid region key \"$region\".") if T(1);
537 :     }
538 : parrello 1.1 # Get the region's snips.
539 :     my $source = $regionMap{$region};
540 :     my $content = "";
541 :     for my $pos (@positions) {
542 :     $content .= substr $source, $pos, 1;
543 :     }
544 :     # Get the region's location data.
545 : parrello 1.2 my $location = BasicLocation->new($region);
546 : parrello 1.1 # Write this region to the output files.
547 :     print REGIONSOUT join("\t", $region, $location->Contig, $location->Dir,
548 : parrello 1.3 $location->Right, $location->Length,
549 :     $regionPeg{$region}, $location->Left, $content) . "\n";
550 : parrello 1.1 print CONTAINSOUT join("\t", $location->Contig, $region,
551 :     $location->Length, $location->Left) . "\n";
552 : parrello 1.7 print INCLUDESOUT join("\t", "$groupName:$blockID", $region) . "\n";
553 : parrello 1.1 }
554 :     # Finally, we need to connect this block to the genomes in which it occurs.
555 :     for my $genomeID (keys %genomesFound) {
556 : parrello 1.7 print INSTANCESOUT join("\t", $genomeID, "$groupName:$blockID") . "\n";
557 : parrello 1.1 }
558 :     # Count this block's regions.
559 :     $regionsCount += $regionCounter;
560 :     }
561 :     }
562 :     # Close the input files.
563 :     close BLOCKSIN;
564 :     close REGIONSIN;
565 :     }
566 :     # Close the output files.
567 :     close REGIONSOUT;
568 :     close BLOCKSOUT;
569 :     close INSTANCESOUT;
570 :     # All the block data has been written. Tell the user what we found.
571 : parrello 1.3 Trace("$blocksCount blocks processed, $regionsCount regions processed.") if T(2);
572 : parrello 1.1 # The next task is to write the genome/contig data. This is provided by the
573 :     # "%contigs" hash. First, we need to open the files.
574 :     my $contigsCount = 0;
575 : parrello 1.3 Open(\*CONTIGSOUT, ">>$outDirectory/Contig.dtx");
576 :     Open(\*CONSISTSOUT, ">>$outDirectory/ConsistsOf.dtx");
577 : parrello 1.1 for my $contigID (keys %contigs) {
578 :     print CONTIGSOUT "$contigID\n";
579 :     print CONSISTSOUT join("\t", $contigs{$contigID}, $contigID) . "\n";
580 :     $contigsCount++;
581 :     }
582 : parrello 1.3 Trace("$contigsCount contigs found.") if T(2);
583 : parrello 1.4 # Close the output files.
584 :     close CONTIGSOUT;
585 :     close CONSISTSOUT;
586 : parrello 1.1 # Now warn the user about all the genomes that didn't have blocks.
587 :     for my $genomeID (keys %genomes) {
588 :     if (! $genomes{$genomeID}) {
589 : parrello 1.3 Trace("Genome $genomeID did not have any regions.") if T(1);
590 : parrello 1.1 $errorCount++;
591 :     }
592 :     }
593 : parrello 1.3 return $errorCount;
594 : parrello 1.1 }
595 :     # Tell the user we're done.
596 : parrello 1.3 Trace("Processing complete.") if T(0);
597 : parrello 1.1
598 :     # Read a region record from the file and parse it into a hash
599 :     # for return to the caller. If we reach end-of-file, the
600 :     # hash returned will have $TRAILER in the blockID field.
601 :     sub GetRegionRecord {
602 :     # Create the return hash.
603 :     my %retVal = ();
604 :     # Read the record.
605 :     my $regionData = <REGIONSIN>;
606 :     # Check for end-of-file.
607 :     if (!defined $regionData) {
608 :     # Here we have end-of-file, so stuff in a trailer
609 :     # value for the block ID.
610 :     $retVal{blockID} = $TRAILER;
611 :     } else {
612 :     # Here we have a real record.
613 :     ($retVal{peg}, $retVal{genomeID}, $retVal{contigID},
614 :     $retVal{begin}, $retVal{end}, $retVal{blockID},
615 :     $retVal{snippet}) = Tracer::ParseRecord($regionData);
616 :     }
617 :     # Return the hash created.
618 :     return %retVal;
619 :     }
620 :    
621 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3