[Bio] / Sprout / ModelSaplingLoader.pm Repository:
ViewVC logotype

Diff of /Sprout/ModelSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.13, Mon Jan 24 18:13:56 2011 UTC revision 1.14, Sat Feb 26 19:05:32 2011 UTC
# Line 22  Line 22 
22      use strict;      use strict;
23      use Tracer;      use Tracer;
24      use ERDB;      use ERDB;
     use HTTP::Date;  
25      use base 'BaseSaplingLoader';      use base 'BaseSaplingLoader';
26    
27  =head1 Sapling ModelLoader Load Group Class  =head1 Sapling Model Load Group Class
28    
29  =head2 Introduction  =head2 Introduction
30    
31  The ModelLoader Load Group includes all of the major mdl tables.  The Model Load Group includes a small set of tables that describe reactions and compounds
32    and how they relate to the models in the main model database.
33    
34  =head3 new  =head3 new
35    
36      my $sl = ModelLoaderSaplingLoader->new($erdb, $options, @tables);      my $sl = ModelSaplingLoader->new($erdb, $options, @tables);
37    
38  Construct a new ModelLoaderSaplingLoader object.  Construct a new ModelSaplingLoader object.
39    
40  =over 4  =over 4
41    
# Line 59  Line 59 
59      # Get the parameters.      # Get the parameters.
60      my ($class, $erdb, $options) = @_;      my ($class, $erdb, $options) = @_;
61      # Create the table list.      # Create the table list.
62      my @tables = sort qw(StructuralCue IsFoundIn Compound HasAliasOf AliasType      my @tables = qw(Compound Reaction EcNumber Model Media IsTriggeredBy
63                           HasReactionAliasOf Reaction Involves IsTriggeredBy                      IsCategorizedInto IsConsistentWith IsModeledBy Involves
64                           RoleSet IsCombinationOf IsCategorizedInto EcNumber                      IsRequiredBy);
                          IsConsistentWith IsStimulatedBy  FeatureSet IsSetOf  
                          IsRequiredBy Model IsModeledBy ModelUser); # Encompasses);  
65      # Create the BaseSaplingLoader object.      # Create the BaseSaplingLoader object.
66      my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);      my $retVal = BaseSaplingLoader::new($class, $erdb, $options, @tables);
67        # Create the reaction tracking hash.
68        $retVal->{reactions} = {};
69      # Return it.      # Return it.
70      return $retVal;      return $retVal;
71  }  }
# Line 76  Line 76 
76    
77      $sl->Generate();      $sl->Generate();
78    
79  Generate the data for the mdl files.  Generate the data for the model files.
80    
81  =cut  =cut
82    
# Line 85  Line 85 
85      my ($self) = @_;      my ($self) = @_;
86      # Get the database object.      # Get the database object.
87      my $erdb = $self->db();      my $erdb = $self->db();
     # Get the source object.  
     my $fig = $self->source();  
88      # Is this the global section?      # Is this the global section?
     #my $model = new FIGMODEL;  
89      if ($self->global()) {      if ($self->global()) {
90          #Loading the compound entities and relationships          # Load the tables from the model dump files.
91      #    $self->LoadCompounds($model);          $self->LoadModelFiles();
         #Loading the reaction, and EC number entities and relationships  
     #    $self->LoadReactions($model);  
92      } else {      } else {
93          # Get the section ID.          # Get the section ID.
94          my $genomeID = $self->section();          my $genomeID = $self->section();
# Line 101  Line 96 
96      }      }
97  }  }
98    
99  sub LoadCompounds {  =head3 LoadModelFiles
     my ($self,$model) = @_;  
100    
101      #First getting the compound table which has a list of all compounds in the database as well as alot of compound information      $sl->LoadModelFiles();
     my $CompoundTable = $model->database()->GetDBTable("COMPOUNDS");  
102    
103      #Adding the universal AliasType that will always exist  Load the data from the six model dump files.
     $self->PutE(AliasType => "NAME", source => "NONE");  
     $self->PutE(AliasType => "SEARCHNAME", source => "NONE");  
     $self->PutE(AliasType => "KEGG", source => "http://www.kegg.com/");  
     $self->PutE(AliasType => "ARGONNE", source => "OBSOLETE IDs");  
104    
105      #Cycling through the compound list and adding each individual compound entity  =cut
     for (my $i=0; $i < $CompoundTable->size(); $i++) {  
         my $Row = $CompoundTable->get_row($i);  
         if (defined($Row) && defined($Row->{"DATABASE"}->[0]) && $Row->{"DATABASE"}->[0] =~ m/cpd\d\d\d\d\d/) {  
             my $Object = FIGMODELObject->load($model->{"compound directory"}->[0].$Row->{"DATABASE"}->[0],"\t");  
             my @LoadingArray = ($Row->{"DATABASE"}->[0],$Row->{"DATABASE"}->[0],0,"",0,"",0,10000000,10000000,"");  
             $self->Track(compoundRow => $LoadingArray[0], 1000);  
             if (defined($Row->{"NAME"}->[0])) {  
                 $LoadingArray[1] = $Row->{"NAME"}->[0];  
             }  
             if (defined($Object->{"MASS"}->[0])) {  
                 $LoadingArray[2] = $Object->{"MASS"}->[0];  
             }  
             if (defined($Object->{"FORMULA"}->[0])) {  
                 $LoadingArray[3] = $Object->{"FORMULA"}->[0];  
             }  
             if (defined($Object->{"CHARGE"}->[0])) {  
                 $LoadingArray[4] = $Object->{"CHARGE"}->[0];  
             }  
             if (defined($Object->{"CHARGE"}->[0]) && defined($Object->{"FORMULA"}->[0])) {  
                 $LoadingArray[5] = FIGMODEL::ConvertToNeutralFormula($Object->{"FORMULA"}->[0],$Object->{"CHARGE"}->[0]);  
             }  
             if (-e $model->{"Argonne molfile directory"}->[0].$Row->{"DATABASE"}->[0].".mol" || -e $model->{"Argonne molfile directory"}->[0]."pH7/".$Row->{"DATABASE"}->[0].".mol") {  
                 $LoadingArray[6] = 1;  
             }  
             if (defined($Object->{"DELTAG"}->[0])) {  
                 $LoadingArray[7] = $Object->{"DELTAG"}->[0];  
             }  
             if (defined($Object->{"DELTAGERR"}->[0])) {  
                 $LoadingArray[8] = $Object->{"DELTAGERR"}->[0];  
             }  
             if (defined($Row->{"STRINGCODE"}->[0])) {  
                 $LoadingArray[9] = $Row->{"STRINGCODE"}->[0];  
             }  
             $self->PutE(Compound => $LoadingArray[0], label => $LoadingArray[1], mass => $LoadingArray[2], chargedFormula => $LoadingArray[3], charge => $LoadingArray[4],neutralFormula => $LoadingArray[5], hasMolfile => $LoadingArray[6], deltaG => $LoadingArray[7], deltaGerr => $LoadingArray[8], stringcode => $LoadingArray[9]);  
             #Adding the relationship between compound and structural cue  
             if (defined($Object->{"STRUCTURAL_CUES"})) {  
                 foreach my $Cue (@{$Object->{"STRUCTURAL_CUES"}}) {  
                     my @TempArray = split(/:/,$Cue);  
                     if (@TempArray == 2) {  
                         $self->PutR(IsFoundIn => $TempArray[0],$LoadingArray[0],count => $TempArray[1]);  
                     }  
                 }  
             }  
             #Adding the relationship between compound and alias type  
             if (defined($Object->{"NAME"})) {  
                 foreach my $Name (@{$Object->{"NAME"}}) {  
                     $self->PutR(HasAliasOf => $LoadingArray[0],"NAME",alias => $Name);  
                     my @SearchNames = FIGMODEL::ConvertToSearchNames($Name);  
                     foreach my $SearchName (@SearchNames) {  
                         $self->PutR(HasAliasOf => $LoadingArray[0],"SEARCHNAME",alias => $SearchName);  
                     }  
                 }  
             }  
             if (defined($Object->{"DBLINKS"})) {  
                 foreach my $Link (@{$Object->{"DBLINKS"}}) {  
                     my @TempArray = split(/:/,$Link);  
                     if (@TempArray >= 2) {  
                         if ($TempArray[0] ne "KEGG" && $TempArray[0] ne "ARGONNE") {  
                             $self->PutE(AliasType => $TempArray[0], source => "PUBLISHED MODEL");  
                         }  
                         if ($TempArray[0] eq "ARGONNE") {  
                             splice(@TempArray,1,1);  
                         }  
                         for (my $j=1; $j < @TempArray; $j++) {  
                             $self->PutR(HasAliasOf => $LoadingArray[0],$TempArray[0],alias => $TempArray[$j]);  
                         }  
                     }  
                 }  
             }  
         }  
     }  
 }  
   
 sub LoadReactions {  
     my ($self,$model) = @_;  
     #First getting the reaction table which has a list of all reactions in the database as well as alot of reaction information  
     my $ReactionTable = $model->database()->GetDBTable("REACTIONS");  
106    
107      #Cycling through the compound list and adding each individual compound entity  sub LoadModelFiles {
108      my $Count = 0;      # Get the parameters.
109      for (my $i=0; $i < $ReactionTable->size(); $i++) {      my ($self) = @_;
110          my $Row = $ReactionTable->get_row($i);      # Get the model dump file directory.
111          if (defined($Row) && defined($Row->{"DATABASE"}->[0]) && $Row->{"DATABASE"}->[0] =~ m/rxn\d\d\d\d\d/) {      my $dir = $self->db()->LoadDirectory() . "/models";
112              my $Object = FIGMODELObject->load($model->{"reaction directory"}->[0].$Row->{"DATABASE"}->[0],"\t");      # First we read the compounds.
113              my @LoadingArray = ($Row->{"DATABASE"}->[0],"","","","","",10000000,10000000,10000000,"R");      my $ih = $self->CheckFile("$dir/CompoundName.txt", qw(CompoundID Name));
114              $self->Track(reactionRow => $LoadingArray[0], 1000);      while (! eof $ih) {
115              if (defined($Object->{"DEFINITION"}->[0])) {          # Get the next compound.
116                  $LoadingArray[1] = $Object->{"DEFINITION"}->[0];          my ($id, $label) = $self->ReadLine($ih);
117              }          # Create a compound record for it.
118              if (defined($Object->{"EQUATION"}->[0])) {          $self->PutE(Compound => $id, label => $label);
119                  my ($Direction,$Equation,$ReverseEquation,$FullEquation,$EquationCompartment,$Error) = $model->ConvertEquationToCode($Object->{"EQUATION"}->[0],{"Test" => "Test"});      }
120                  $LoadingArray[2] = $Equation;      # Next, the compound-reactions relationship. We create the reactions here, too.
121              }      $ih = $self->CheckFile("$dir/CompoundReaction.txt", qw(CompoundID ReactionID
122              if (defined($Object->{"OPERATOR"}->[0])) {                             Stoichiometry));
123                  $LoadingArray[3] = $Object->{"OPERATOR"}->[0];      while (! eof $ih) {
124              }          # Get the next link.
125              if (defined($Object->{"PATHWAY"}->[0])) {          my ($compound, $reaction, $stoich) = $self->ReadLine($ih);
126                  $LoadingArray[4] = join(";",@{$Object->{"PATHWAY"}});          # Insure the reaction exists.
127              }          $self->CreateReaction($reaction);
128              if (defined($Row->{"KEGG MAPS"}->[0])) {          # Check for product or substrate.
129                  $LoadingArray[5] = join(";",@{$Row->{"KEGG MAPS"}});          my $product;
130              }          if ($stoich < 0) {
131              if (defined($Object->{"DELTAG"}->[0])) {              $product = 0;
132                  $LoadingArray[6] = $Object->{"DELTAG"}->[0];              $stoich = -$stoich;
             }  
             if (defined($Object->{"MMDELTAG"}->[0])) {  
                 $LoadingArray[7] = $Object->{"MMDELTAG"}->[0];  
             }  
             if (defined($Object->{"DELTAGERR"}->[0])) {  
                 $LoadingArray[8] = $Object->{"DELTAGERR"}->[0];  
             }  
             if (defined($Object->{"THERMODYNAMIC REVERSIBILITY"}->[0])) {  
                 my $code = $Object->{"THERMODYNAMIC REVERSIBILITY"}->[0];  
                 if ($code eq '<=>') {  
                     $LoadingArray[9] = "R";  
                 } elsif ($code eq '<=') {  
                     $LoadingArray[9] = "B";  
                 } elsif ($code eq '=>') {  
                     $LoadingArray[9] = "F";  
                 }  
             }  
             $self->PutE(Reaction => $LoadingArray[0], definition => $LoadingArray[1], code => $LoadingArray[2], operator => $LoadingArray[3], pathway => $LoadingArray[4],keggMap => $LoadingArray[5], deltaG => $LoadingArray[6], MMdeltaG => $LoadingArray[7], deltaGerr => $LoadingArray[8], predictedReversibility => $LoadingArray[9]);  
             #Adding relationship between compounds and reactions  
             my ($Reactants,$Products) = $model->GetReactionSubstrateData($LoadingArray[0]);  
             if (! defined $Reactants) {  
                 $self->Add(missingReactants => 1);  
133              } else {              } else {
134                  for (my $j=0; $j < @{$Reactants}; $j++) {              $product = 1;
                     $self->PutR(Involves => $LoadingArray[0],$Reactants->[$j]->{"DATABASE"}->[0],product => 0,stoichiometry => $Reactants->[$j]->{"COEFFICIENT"}->[0],main => 1,compartment => $Reactants->[$j]->{"COMPARTMENT"}->[0],discriminator => $Count);  
                     #TODO:Properly identify main reactants in reactions  
                     $Count++;  
135                  }                  }
136            # Connect the reaction to the compound.
137            $self->PutR(Involves => $reaction, $compound, product => $product,
138                        stoichiometry => $stoich);
139        }
140        # Now we connect the reactions to roles.
141        $ih = $self->CheckFile("$dir/ReactionRole.txt", qw(ReactionID Role));
142        while (! eof $ih) {
143            # Get the next reaction/role pair. We don't need to create roles here because
144            # they are built in the Feature module.
145            my ($reaction, $role) = $self->ReadLine($ih);
146            # Insure the reaction exists.
147            $self->CreateReaction($reaction);
148            # Connect the reaction to the role.
149            $self->PutR(IsTriggeredBy => $reaction, $role);
150        }
151        # Now we create the models.
152        $ih = $self->CheckFile("$dir/ModelGenome.txt", qw(ModelID GenomeID));
153        while (! eof $ih) {
154            # Get the next model.
155            my ($model, $genome) = $self->ReadLine($ih);
156            # Create the model.
157            $self->PutE(Model => $model);
158            # Connect it to the genome. Again, the genomes are created elsewhere.
159            $self->PutR(IsModeledBy => $genome, $model);
160        }
161        # Next we connect the reactions to models.
162        $ih = $self->CheckFile("$dir/ModelReaction.txt", qw(ModelID ReactionID));
163        while (! eof $ih) {
164            # Get the next line.
165            my ($model, $reaction) = $self->ReadLine($ih);
166            # Only proceed if a reaction is present.
167            if ($reaction) {
168                # Insure the reaction exists.
169                $self->CreateReaction($reaction);
170                # Connect the reaction to the model.
171                $self->PutR(IsRequiredBy => $reaction, $model);
172              }              }
             if (! defined $Products) {  
                 $self->Add(missingProducts => 1);  
             } else {  
                 for (my $j=0; $j < @{$Products}; $j++) {  
                     $self->PutR(Involves => $LoadingArray[0],$Products->[$j]->{"DATABASE"}->[0],product => 1,stoichiometry => $Products->[$j]->{"COEFFICIENT"}->[0],main => 1,compartment => $Products->[$j]->{"COMPARTMENT"}->[0],discriminator => $Count);  
                     #TODO:Properly identify main reactants in reactions  
                     $Count++;  
173                  }                  }
174              }              }
175              #Adding relationship between EC number and reactions  
176              if (defined($Object->{"ENZYME"})) {  =head3 StartFile
177                  foreach my $Enzyme (@{$Object->{"ENZYME"}}) {  
178                     if ($Enzyme =~ /,/) {      my $ih = $sl->CheckFile($fileName, @fieldNames);
179                      #TODO:Invalid enzyme  
180    Read the header record of the specified file and verify that the field names match
181    the names in the input list. If they do not, an error will be thrown; if they do, an
182    open file handle will be returned, positioned on the first data record.
183    
184    =over 4
185    
186    =item fileName
187    
188    Name for the input file. The file is in standard tab-delimited format. The first record
189    contains the field names and the remaining records contain the data.
190    
191    =item fieldNames
192    
193    List of the field names expected, in order.
194    
195    =item RETURN
196    
197    Returns the open file handle if successful. If there is a mismatch, throws an error.
198    
199    =back
200    
201    =cut
202    
203    sub CheckFile {
204        # Get the parameters.
205        my ($self, $fileName, @fieldNames) = @_;
206        # Open the file.
207        my $retVal = Open(undef, "<$fileName");
208        $self->Add(files => 1);
209        # Read in the file header.
210        my @actualFields = Tracer::GetLine($retVal);
211        # This will be set to TRUE if there's a mismatch.
212        my $error = 0;
213        for (my $i = 0; $i <= $#fieldNames; $i++) {
214            if ($fieldNames[$i] ne $actualFields[$i]) {
215                Trace("Field match error: expected $fieldNames[$i], found $actualFields[$i].") if T(0);
216                $error = 1;
217            }
218        }
219        # Was there an error?
220        if ($error) {
221            # Yes, so abort.
222            Confess("Invalid field name header in $fileName.");
223                     } else {                     } else {
224                      $Enzyme =~ s/\s//g;          # No, so trace the open.
225                      $self->PutE(EcNumber => $Enzyme, obsolete => 0, replacedby => "NONE");          Trace("Processing $fileName.") if T(ERDBLoadGroup => 2);
                     $self->PutR(IsCategorizedInto => $LoadingArray[0],$Enzyme,source => "KEGG");  
                    }  
                 }  
             }  
             #Adding the relationship between reaction and alias type  
             if (defined($Object->{"NAME"})) {  
                 foreach my $Name (@{$Object->{"NAME"}}) {  
                     $self->PutR(HasReactionAliasOf => $LoadingArray[0],"NAME",alias => $Name);  
                 }  
             }  
             if (defined($Object->{"DBLINKS"})) {  
                 foreach my $Link (@{$Object->{"DBLINKS"}}) {  
                     my @TempArray = split(/:/,$Link);  
                     if (@TempArray >= 2) {  
                         if ($TempArray[0] ne "KEGG" && $TempArray[0] ne "ARGONNE") {  
                             $self->PutE(AliasType => $TempArray[0], source => "PUBLISHED MODEL");  
                         }  
                         if ($TempArray[0] eq "ARGONNE") {  
                             splice(@TempArray,1,1);  
                         }  
                         for (my $j=1; $j < @TempArray; $j++) {  
                             $self->PutR(HasReactionAliasOf => $LoadingArray[0],$TempArray[0],alias => $TempArray[$j]);  
                         }  
                     }  
                 }  
             }  
226          }          }
227        # Return the file handle.
228        return $retVal;
229      }      }
230    
231    =head3 ReadLine
232    
233        my @fields = $sl->ReadLine($ih);
234    
235    Read a line of data from an input file.
236    
237    =over 4
238    
239    =item ih
240    
241    Open file handle for the input file.
242    
243    =item RETURN
244    
245    Returns a list of the field values for the next record in the file.
246    
247    =back
248    
249    =cut
250    
251    sub ReadLine {
252        # Get the parameters.
253        my ($self, $ih) = @_;
254        # Read the line.
255        my @retVal = Tracer::GetLine($ih);
256        # Count this record.
257        $self->Track(records => $retVal[0], 1000);
258        # Return the data.
259        return @retVal;
260  }  }
261    
262    
263  sub FixDate {  =head3 CheckReaction
264      my ($date) = @_;  
265      my $retVal = ($date eq 'NA' ? 0 : $date);      $sl->CheckReaction($reaction);
266      return $retVal;  
267    Insure we have created a rectord for the specified reaction.
268    
269    =over 4
270    
271    =item reaction
272    
273    ID of the reaction in question.
274    
275    =back
276    
277    =cut
278    
279    sub CreateReaction {
280        # Get the parameters.
281        my ($self, $reaction) = @_;
282        # Get the reaction hash.
283        my $reactionH = $self->{reactions};
284        # See if this reaction is new.
285        if (! $reactionH->{$reaction}) {
286            # It is, so create it.
287            $self->PutE(Reaction => $reaction);
288            # Insure we don't create it again.
289            $reactionH->{$reaction} = 1;
290        }
291  }  }
292    
293  1;  1;

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.14

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3