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

Annotation of /Sprout/ERDBLoadFormat.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 #!/usr/bin/perl -w
2 :    
3 :     #
4 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
5 :     # for Interpretations of Genomes. All Rights Reserved.
6 :     #
7 :     # This file is part of the SEED Toolkit.
8 :     #
9 :     # The SEED Toolkit is free software. You can redistribute
10 :     # it and/or modify it under the terms of the SEED Toolkit
11 :     # Public License.
12 :     #
13 :     # You should have received a copy of the SEED Toolkit Public License
14 :     # along with this program; if not write to the University of Chicago
15 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
16 :     # Genomes at veronika@thefig.info or download a copy from
17 :     # http://www.theseed.org/LICENSE.TXT.
18 :     #
19 :    
20 :     use strict;
21 :     use Tracer;
22 :     use ERDB;
23 :     use ERDBExtras;
24 :    
25 :    
26 :     =head1 ERDBLoadFormat Script
27 :    
28 :     ERDBLoadFormat [options] <database>
29 :    
30 :     ERDB Database Load Format Display
31 :    
32 :     =head2 Introduction
33 :    
34 :     This script displays in the form of a text file the information needed to
35 :     create load files for the specified ERDB database.
36 :    
37 :     =head2 Positional Parameters
38 :    
39 :     =over 4
40 :    
41 :     =item database
42 :    
43 :     Name of the ERDB database. This should be the class name for the subclass used
44 :     to access the database.
45 :    
46 :     =back
47 :    
48 :     =head2 Command-Line Options
49 :    
50 :     =over 4
51 :    
52 :     =item trace
53 :    
54 :     Specifies the tracing level. The higher the tracing level, the more messages
55 :     will appear in the trace log. Use E to specify emergency tracing.
56 :    
57 :     =item user
58 :    
59 :     Name suffix to be used for log files. If omitted, the PID is used.
60 :    
61 :     =item background
62 :    
63 :     Save the standard and error output to files. The files will be created
64 :     in the FIG temporary directory and will be named C<err>I<User>C<.log> and
65 :     C<out>I<User>C<.log>, respectively, where I<User> is the value of the
66 :     B<user> option above.
67 :    
68 :     =item help
69 :    
70 :     Display this command's parameters and options.
71 :    
72 :     =item warn
73 :    
74 :     Create an event in the RSS feed when an error occurs.
75 :    
76 :     =item phone
77 :    
78 :     Phone number to message when the script is complete.
79 :    
80 :     =item DBD
81 :    
82 :     Fully-qualified name of the DBD file. This option allows the use of an alternate
83 :     DBD during load so that access to the database by other processes is not
84 :     compromised.
85 :    
86 :     =item dbName
87 :    
88 :     SQL name of the target database. If not specified, the default name is used.
89 :     This option allows you to specify a backup or alternate database that can
90 :     be loaded without compromising the main database.
91 :    
92 :     =item dbhost
93 :    
94 :     Name of the MySQL database host. If not specified, the default host is used.
95 :    
96 :     =item entities
97 :    
98 :     If specified, name of a file containing a list of entities. Only tables
99 :     related to the entities will be displayed.
100 :    
101 :     =back
102 :    
103 :     =cut
104 :    
105 :     # Get the command-line options and parameters.
106 :     my ($options, @parameters) = StandardSetup([qw(ERDB) ],
107 :     {
108 :     dbName => ["", "if specified, the SQL name of the target database"],
109 :     dbhost => ["", "if specified, the name of the target database"],
110 : parrello 1.2 port => ["", "if specified, the port on which to connect to the target database"],
111 : parrello 1.1 trace => ["2-", "tracing level"],
112 :     DBD => ["", "if specified, the name of a DBD file in the FIG directory"],
113 :     entities => ["", "if specified, the name of a file containing the entities of interest"]
114 :     },
115 :     "<database>",
116 :     @ARGV);
117 :     # Connect to the database.
118 : parrello 1.3 my $erdb = ERDB::GetDatabase($parameters[0], undef, %$options, externalDBD => 1, offline => 1);
119 : parrello 1.1 # Get the hash of entities.
120 :     my $entityHash = $erdb->GetObjectsTable('entity');
121 :     # Get the list of entities of interest.
122 :     my %entities;
123 :     if ($options->{entities}) {
124 :     %entities = map { $_ => $entityHash->{$_} } Tracer::GetFile($options->{entities});
125 :     } else {
126 :     %entities = %$entityHash;
127 :     }
128 :     # Loop through the list of entities.
129 :     for my $entity (sort keys %entities) {
130 :     # Display the entity description.
131 :     DisplayObject($entity, \%entities);
132 :     # Space before the next entity.
133 :     print "\n";
134 :     }
135 :     # Loop through the list of relationships.
136 :     my $relationshipHash = $erdb->GetObjectsTable('relationship');
137 :     for my $relationship (sort keys %$relationshipHash) {
138 :     # Get the FROM and TO entites.
139 :     my $from = $relationshipHash->{$relationship}->{from};
140 :     my $to = $relationshipHash->{$relationship}->{to};
141 :     # Only display this relationship if both ends are in our
142 :     # list of entities.
143 :     if (exists $entities{$from} && exists $entities{$to}) {
144 :     DisplayObject($relationship, $relationshipHash);
145 :     # Space before the next relationship.
146 :     print "\n";
147 :     }
148 :     }
149 :    
150 :     # Display the data about an object and its relations.
151 :     sub DisplayObject {
152 :     my ($object, $objectHash) = @_;
153 :     FormatNotes($object, $objectHash->{$object}->{Notes}->{content});
154 :     print "\n";
155 :     # Loop through its relations.
156 :     my $relHash = $objectHash->{$object}->{Relations};
157 :     for my $table (sort keys %$relHash) {
158 :     print " Table: $table\n";
159 :     # Get this table's fields.
160 :     my $relData = $relHash->{$table};
161 :     # Loop through them.
162 :     for my $fieldData (@{$relData->{Fields}}) {
163 :     # Get the field's name.
164 :     my $name = $fieldData->{name};
165 :     # Get the field's type.
166 :     my $type = $fieldData->{type};
167 :     # Display this field's information.
168 :     FormatNotes(" $name ($type)", $fieldData->{Notes}->{content});
169 :     }
170 :     # Space before the next table.
171 :     print "\n";
172 :     }
173 :     }
174 :    
175 :     # Display an object with its formatted notes.
176 :     sub FormatNotes {
177 :     my ($heading, $notes) = @_;
178 :     # Create the display prefix from the heading.
179 :     my $prefix = "$heading:";
180 :     # Compute the length of the prefix.
181 :     my $length = length $prefix;
182 :     # Create the prefix for secondary lines.
183 :     my $spacer = " " x $length;
184 :     # Delete all the markers from the notes.
185 :     $notes =~ s/\[[^\]]+\]//g;
186 :     # Break the notes into words.
187 :     my @words = split /(?:\s|\n)+/, $notes;
188 :     # Form the words into lines.
189 :     my @line = $prefix;
190 :     my $lineLength = $length;
191 :     for my $word (@words) {
192 :     push @line, $word;
193 :     $lineLength += 1 + length $word;
194 :     if ($lineLength >= 75) {
195 :     print join(" ", @line) . "\n";
196 :     @line = ($spacer);
197 :     $lineLength = $length;
198 :     }
199 :     }
200 :     if (scalar @line > 1) {
201 :     print join(" ", @line) . "\n";
202 :     }
203 :     }

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3