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

Annotation of /FigKernelPackages/ApplyTransactions.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 : olson 1.5 #
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 : parrello 1.7 #
8 : olson 1.5 # The SEED Toolkit is free software. You can redistribute
9 :     # it and/or modify it under the terms of the SEED Toolkit
10 : parrello 1.7 # Public License.
11 : olson 1.5 #
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 : parrello 1.1
20 :     package ApplyTransactions;
21 :    
22 :     use TransactionProcessor;
23 :     @ISA = ('TransactionProcessor');
24 :    
25 :     use strict;
26 :     use Tracer;
27 :     use PageBuilder;
28 :     use FIG;
29 :    
30 :     =head1 Apply Transactions
31 :    
32 :     =head2 Introduction
33 :    
34 :     This is a TransactionProcessor subclass that applies the transaction changes to
35 :     the FIG data store.
36 :    
37 :     =head2 Methods
38 :    
39 :     =head3 new
40 :    
41 : parrello 1.7 my $xprc = ApplyTransactions->new(\%options, $command, $directory, $idFile);
42 : parrello 1.1
43 :     Construct a new ApplyTransactions object.
44 :    
45 :     =over 4
46 :    
47 :     =item options
48 :    
49 :     Reference to a hash table containing the command-line options.
50 :    
51 :     =item command
52 :    
53 :     Command specified on the B<TransactFeatures> command line. This command determines
54 :     which TransactionProcessor subclass is active.
55 :    
56 :     =item directory
57 :    
58 :     Directory containing the transaction files.
59 :    
60 :     =item idFile
61 :    
62 :     Name of the ID file (if needed).
63 :    
64 :     =back
65 :    
66 :     =cut
67 :    
68 :     sub new {
69 :     # Get the parameters.
70 :     my ($class, $options, $command, $directory, $idFile) = @_;
71 :     # Construct via the subclass.
72 :     return TransactionProcessor::new($class, $options, $command, $directory, $idFile);
73 :     }
74 :    
75 :     =head3 Setup
76 :    
77 : parrello 1.7 $xprc->Setup();
78 : parrello 1.1
79 :     Set up to apply the transactions. This includes reading the ID file.
80 :    
81 :     =cut
82 :     #: Return Type ;
83 :     sub Setup {
84 :     # Get the parameters.
85 :     my ($self) = @_;
86 :     # Read the ID hash from the ID file.
87 :     $self->ReadIDHash();
88 : parrello 1.2 # Memorize the user ID we want to use for the annotations.
89 :     $self->{user} = "master:automated_assignments";
90 : parrello 1.4 # Insure the subsystems are properly indexed.
91 :     FIG::run("index_subsystems");
92 : parrello 1.1 }
93 :    
94 :     =head3 SetupGenome
95 :    
96 : parrello 1.7 $xprc->SetupGenome();
97 : parrello 1.1
98 : parrello 1.2 Set up for processing this genome.
99 : parrello 1.1
100 :     =cut
101 :     #: Return Type ;
102 :     sub SetupGenome {
103 :     # Get the parameters.
104 :     my ($self) = @_;
105 :     my $fig = $self->FIG();
106 :     # If we're in safe mode, start a database transaction.
107 :     if ($self->Option("safe")) {
108 :     $fig->db_handle->begin_tran();
109 :     }
110 : parrello 1.3 # If we're producing a TBL file, open it.
111 :     if ($self->Option("tlbFiles")) {
112 :     my $fileName = $self->CurrentFileName() . ".tbl";
113 :     Open(\*TBLFILE, ">$fileName");
114 :     }
115 : parrello 1.1 }
116 :    
117 :     =head3 TeardownGenome
118 :    
119 : parrello 1.7 $xprc->TeardownGenome();
120 : parrello 1.1
121 : parrello 1.2 Clean up after processing this genome. This involves optionally committing any updates.
122 : parrello 1.1
123 :     =cut
124 :     #: Return Type ;
125 :     sub TeardownGenome {
126 :     # Get the parameters.
127 :     my ($self) = @_;
128 :     my $fig = $self->FIG();
129 :     # If we're in safe mode, commit the database transaction.
130 :     if ($self->Option("safe")) {
131 :     $fig->db_handle->commit_tran();
132 :     }
133 : parrello 1.3 # If we're producing a TBL file, open it.
134 :     if ($self->Option("tlbFiles")) {
135 :     close TBLFILE;
136 :     }
137 : parrello 1.1 }
138 :    
139 :     =head3 Add
140 :    
141 : parrello 1.7 $xprc->Add($newID, $locations, $translation);
142 : parrello 1.1
143 :     Add a new feature to the data store.
144 :    
145 :     =over 4
146 :    
147 :     =item newID
148 :    
149 :     ID to give to the new feature.
150 :    
151 :     =item locations
152 :    
153 :     Location of the new feature, in the form of a comma-separated list of location
154 :     strings in SEED format.
155 :    
156 :     =item translation (optional)
157 :    
158 :     Protein translation string for the new feature. If this field is omitted and
159 :     the feature is a peg, the translation will be generated by normal means.
160 :    
161 :     =back
162 :    
163 :     =cut
164 :    
165 :     sub Add {
166 :     my ($self, $newID, $locations, $translation) = @_;
167 :     my $fig = $self->{fig};
168 :     # Extract the feature type and ordinal number from the new ID.
169 :     my ($ftype, $ordinal, $key) = $self->ParseNewID($newID);
170 :     # Add the new feature.
171 :     my $realID = $self->AddFeature($ordinal, $key, $ftype,
172 :     $locations, "", $translation);
173 :     Trace("Feature $realID added for pseudo-ID $newID.") if T(4);
174 : parrello 1.2 # Create the annotation.
175 :     my $message = "New gene predicted based on similarity to putative genes in closely-related genomes";
176 :     $fig->add_annotation($realID, $self->{user}, $message);
177 : parrello 1.3 # If we're producing a TBL file, write out a transaction.
178 :     if ($self->Option("tblFiles")) {
179 :     print TBLFILE "ADD\$realID\t$locations\t$translation\n";
180 :     }
181 : parrello 1.4
182 : parrello 1.1 }
183 :    
184 :     =head3 Change
185 :    
186 : parrello 1.7 $xprc->Change($fid, $newID, $locations, $aliases, $translation);
187 : parrello 1.1
188 :     Replace a feature to the data store. The feature will be marked for deletion and
189 :     a new feature will be put in its place.
190 :    
191 :     This is a much more complicated process than adding a feature. In addition to
192 : parrello 1.2 the add, we have to create new aliases (or copy over the old ones) and transfer
193 : parrello 1.3 across the assignment, subsystem linkages, and the annotations.
194 : parrello 1.1
195 :     =over 4
196 :    
197 :     =item fid
198 :    
199 :     ID of the feature being changed.
200 :    
201 :     =item newID
202 :    
203 :     New ID to give to the feature.
204 :    
205 :     =item locations
206 :    
207 :     New location to give to the feature, in the form of a comma-separated list of location
208 :     strings in SEED format.
209 :    
210 :     =item aliases (optional)
211 :    
212 :     A new list of alias names for the feature.
213 :    
214 :     =item translation (optional)
215 :    
216 :     New protein translation string for the feature. If this field is omitted and
217 :     the feature is a peg, the translation will be generated by normal means.
218 :    
219 :     =back
220 :    
221 :     =cut
222 :    
223 :     sub Change {
224 :     my ($self, $fid, $newID, $locations, $aliases, $translation) = @_;
225 :     my $fig = $self->{fig};
226 :     # Extract the feature type and ordinal number from the new ID.
227 :     my ($ftype, $ordinal, $key) = $self->ParseNewID($newID);
228 :     # Here we can go ahead and change the feature. First, we must
229 :     # get the old feature's assignment and annotations. Note that
230 :     # for the annotations we ask for the time in its raw format.
231 :     my @functions = $fig->function_of($fid);
232 :     my @annotations = $fig->feature_annotations($fid, 1);
233 :     # Create some counters.
234 : parrello 1.2 my ($assignCount, $annotateCount, $attributeCount) = (0, 0, 0);
235 :     # Check for aliases.
236 :     if (! $aliases) {
237 :     # The user didn't provide any, so we need to copy the old feature's
238 :     # aliases.
239 :     $aliases = $fig->feature_aliases($fid);
240 :     }
241 : parrello 1.1 # Add the new version of the feature and get its ID.
242 :     my $realID = AddFeature($self, $ordinal, $key, $ftype, $locations,
243 :     $aliases, $translation);
244 :     # Copy over the assignments.
245 :     for my $assignment (@functions) {
246 :     my ($user, $function) = @{$assignment};
247 :     $fig->assign_function($realID, $user, $function);
248 :     $assignCount++;
249 :     }
250 :     # Copy over the annotations.
251 :     for my $annotation (@annotations) {
252 :     my ($oldID, $timestamp, $user, $annotation) = @{$annotation};
253 :     $fig->add_annotation($realID, $user, $annotation, $timestamp);
254 :     $annotateCount++;
255 :     }
256 : parrello 1.3 # Copy over the attributes.
257 : parrello 1.2 my @attributes = $fig->get_attributes($fid);
258 :     # Loop through the attributes, adding them to the replacement feature.
259 :     for my $attribute (@attributes) {
260 :     # The attribute descriptor is actually a four-tuple.
261 :     my ($oldID, $key, $value, $url) = @{$attribute};
262 :     # Add the attribute.
263 :     $fig->add_attribute($realID, $key, $value, $url);
264 :     $attributeCount++;
265 :     }
266 : parrello 1.3 # Fix up the subsystems.
267 :     $self->FixSubsystems($fid, $realID);
268 : parrello 1.1 # Mark the old feature for deletion.
269 :     $fig->delete_feature($fid);
270 : parrello 1.3 # Write out the transaction.
271 : parrello 1.4 if ($self->Option("tblFiles")) {
272 : parrello 1.3 print TBLFILE "CHANGE\t$fid\t$realID\t$locations\t$aliases\t$translation\n";
273 :     }
274 : parrello 1.1 # Tell the user what we did.
275 :     $self->{stats}->Add("assignments", $assignCount);
276 :     $self->{stats}->Add("annotations", $annotateCount);
277 : parrello 1.2 $self->{stats}->Add("attributes", $attributeCount);
278 :     Trace("Feature $realID created from $fid. $assignCount assignments, $attributeCount attributes, and $annotateCount annotations copied.") if T(4);
279 :     # Create the annotation.
280 :     my $message = "Derived from $fid by changing the location to $locations.";
281 :     $fig->add_annotation($realID, $self->{user}, $message);
282 : parrello 1.1 }
283 :    
284 :     =head3 Delete
285 :    
286 : parrello 1.7 $xprc->Delete($fid);
287 : parrello 1.1
288 :     Delete a feature from the data store. The feature will be marked as deleted,
289 :     which will remove it from consideration by most FIG methods. A garbage
290 :     collection job will be run later to permanently delete the feature.
291 :    
292 :     =over 4
293 :    
294 :     =item fid
295 :    
296 :     ID of the feature to delete.
297 :    
298 :     =back
299 :    
300 :     =cut
301 :    
302 :     sub Delete {
303 :     my ($self, $fid) = @_;
304 :     my $fig = $self->{fig};
305 :     # Extract the feature type and count it.
306 :     my $ftype = FIG::ftype($fid);
307 :     $self->{stats}->Add($ftype, 1);
308 : parrello 1.3 # Fix up the subsystems.
309 :     $self->FixSubsystems($fid);
310 : parrello 1.1 # Mark the feature for deletion.
311 :     $fig->delete_feature($fid);
312 : parrello 1.3 # Write out the transaction.
313 :     if ($self->Option("tlbFiles")) {
314 :     print TBLFILE "DELETE\t$fid\n";
315 :     }
316 :     }
317 :    
318 :     =head3 FixSubsystems
319 :    
320 : parrello 1.7 $sfx->FixSubsystems($fid, $newID);
321 : parrello 1.3
322 :     Remove the specified feature from all subsystems. If the feature is being replaced
323 :     by a new feature, the specified new feature will be added in the old feature's place.
324 :    
325 :     =over 4
326 :    
327 :     =item fid
328 :    
329 :     ID of the feature to be deleted.
330 :    
331 :     =item newID (optional)
332 :    
333 :     ID of the new feature replacing the old one, if any. This should be a real ID, not a
334 :     pseudo-ID.
335 :    
336 :     =back
337 :    
338 :     =cut
339 :    
340 :     sub FixSubsystems {
341 :     # Get the parameters.
342 :     my ($self, $fid, $newID) = @_;
343 :     my $fig = $self->FIG();
344 :     # Get the genome ID.
345 :     my $genomeID = $self->GenomeID();
346 :     # Get the incoming PEG's subsystems.
347 :     my @subsystems = $fig->subsystems_for_peg($fid);
348 :     for my $pair (@subsystems) {
349 :     # The subsystem tuple contains a subsystem name and a role.
350 :     my ($subsysName, $role) = @{$pair};
351 :     # Get the subsystem and retrieve the PEGs from the appropriate cell.
352 : parrello 1.4 # Note we have to be careful, since the subsystem methods fail if
353 :     # the data is bad.
354 : parrello 1.3 my $subsystem = $fig->get_subsystem($subsysName);
355 : parrello 1.4 my @pegsInCell;
356 :     my $working = 0;
357 :     eval {
358 :     @pegsInCell = $subsystem->get_pegs_from_cell($genomeID, $role);
359 :     $working = 1;
360 :     };
361 :     # Only proceed if we found the cell.
362 :     if ($working) {
363 :     Trace("Role $role returned (" . join(", ", @pegsInCell) . ").") if T(4);
364 :     # Delete the incoming PEG.
365 :     my @newPegsInCell = grep { $_ ne $fid } @pegsInCell;
366 :     # If there's a replacement PEG, add it.
367 :     push @newPegsInCell, $newID;
368 :     # Update the subsystem.
369 :     eval {
370 :     $subsystem->set_pegs_in_cell($genomeID, $role, \@newPegsInCell);
371 :     };
372 :     $subsystem->write_subsystem();
373 :     $self->IncrementStat("subsystems");
374 :     }
375 : parrello 1.3 }
376 : parrello 1.1 }
377 :    
378 :     =head3 AddFeature
379 :    
380 : parrello 1.7 my $realID = $self->AddFeature($ordinal, $key, $ftype, $locations, $aliases, $translation);
381 : parrello 1.1
382 :     Add the specified feature to the FIG data store. This involves generating the new feature's
383 : parrello 1.2 ID, creating the translation (if needed), and adding the feature to the data store. The
384 :     generated ID will be returned to the caller.
385 : parrello 1.1
386 :     =over 4
387 :    
388 :     =item ordinal
389 :    
390 :     Zero-based ordinal number of the proposed feature in the ID space. This is added to the
391 :     base ID number to get the real ID number.
392 :    
393 :     =item key
394 :    
395 :     Key to use for getting the base ID number from the ID hash.
396 :    
397 :     =item ftype
398 :    
399 :     Proposed feature type (C<peg>, C<rna>, etc.)
400 :    
401 :     =item locations
402 :    
403 :     Location of the new feature, in the form of a comma-separated list of location
404 :     strings in SEED format.
405 :    
406 :     =item aliases (optional)
407 :    
408 :     A new list of alias names for the feature.
409 :    
410 :     =item translation (optional)
411 :    
412 :     Protein translation string for the new feature. If this field is omitted and
413 :     the feature is a peg, the translation will be generated by normal means.
414 :    
415 :     =back
416 :    
417 :     =cut
418 :    
419 :     sub AddFeature {
420 :     # Get the parameters.
421 :     my ($self, $ordinal, $key, $ftype, $locations, $aliases, $translation) = @_;
422 :     my $fig = $self->{fig};
423 :     # We want to add a new feature using the information provided. First, we
424 :     # generate its ID.
425 :     my $retVal = $self->GetRealID($ordinal, $key);
426 :     # Next, we insure that we have a translation.
427 :     my $actualTranslation = $self->CheckTranslation($ftype, $locations,
428 :     $translation);
429 :     # Now we add it to FIG.
430 :     $fig->add_feature($self->{genomeID}, $ftype, $locations, $aliases,
431 :     $actualTranslation, $retVal);
432 : parrello 1.2 # Append it to the NR file.
433 :     Open(\*FASTANR, ">>$FIG_Config::global/nr");
434 :     FIG::display_id_and_seq($retVal, \$actualTranslation, \*FASTANR);
435 :     close FASTANR;
436 : parrello 1.1 # Tell FIG to recompute the similarities.
437 :     $fig->enqueue_similarities([$retVal]);
438 :     # Return the ID we generated.
439 :     return $retVal;
440 :     }
441 :    
442 : golsen 1.6 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3