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

Annotation of /Sprout/BaseSaplingLoader.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (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 :     package BaseSaplingLoader;
21 :    
22 :     use strict;
23 :     use Tracer;
24 :     use ERDB;
25 :     use FIG;
26 :     use Time::HiRes;
27 :     use base 'ERDBLoadGroup';
28 :    
29 :     # Name of the global section
30 :     use constant GLOBAL => 'Globals';
31 :    
32 :     =head1 Sapling Load Group Base Class
33 :    
34 :     =head2 Introduction
35 :    
36 :     This is the base class for all the Sapling loaders. It performs common tasks
37 :     required by multiple load groups.
38 :    
39 :     =head3 new
40 :    
41 :     my $sl = BaseSaplingLoader->new($erdb, $options, @tables);
42 :    
43 :     Construct a new BaseSaplingLoader object.
44 :    
45 :     =over 4
46 :    
47 :     =item erdb
48 :    
49 : parrello 1.4 L<Sapling> object for the database being loaded.
50 : parrello 1.1
51 :     =item source
52 :    
53 : parrello 1.4 L<FIG> object used to access the source data.
54 : parrello 1.1
55 :     =item options
56 :    
57 :     Reference to a hash of command-line options.
58 :    
59 :     =item tables
60 :    
61 :     List of tables in this load group.
62 :    
63 :     =back
64 :    
65 :     =cut
66 :    
67 :     sub new {
68 :     # Get the parameters.
69 :     my ($class, $erdb, $options, @tables) = @_;
70 :     # Create the base load group object.
71 :     my $retVal = ERDBLoadGroup::new($class, $erdb, $options, @tables);
72 :     # Return it.
73 :     return $retVal;
74 :     }
75 :    
76 :    
77 :     =head2 Public Methods
78 :    
79 :     =head3 global
80 :    
81 :     my $flag = $sl->global();
82 :    
83 :     Return TRUE if the current section is the global section.
84 :    
85 :     =cut
86 :    
87 :     sub global {
88 :     my ($self) = @_;
89 :     # Get the database.
90 :     my $sapling = $self->db();
91 :     # Get the section ID.
92 :     my $section = $self->section();
93 :     # Ask the DB object if this is the global section.
94 :     return $sapling->GlobalSection($section);
95 :     }
96 :    
97 :     =head3 Starless
98 :    
99 : parrello 1.2 my $adjusted = BaseSaplingLoader::Starless($codeString);
100 : parrello 1.1
101 :     Remove any spaces and leading or trailing asterisks from the incoming string and
102 :     return the result.
103 :    
104 :     =over 4
105 :    
106 :     =item codeString
107 :    
108 :     Input string that needs to have the asterisks trimmed.
109 :    
110 :     =item RETURN
111 :    
112 :     Returns the incoming string with spaces and leading and trailing asterisks
113 :     removed.
114 :    
115 :     =back
116 :    
117 :     =cut
118 :    
119 :     sub Starless {
120 :     # Get the parameters.
121 : parrello 1.2 my ($codeString) = @_;
122 : parrello 1.1 # Declare the return variable.
123 :     my $retVal = $codeString;
124 :     # Remove the spaces.
125 : parrello 1.2 $retVal =~ s/\s+//g;
126 : parrello 1.1 # Trim the asterisks.
127 :     $retVal =~ s/^\*+//;
128 :     $retVal =~ s/\*+$//;
129 :     # Return the result.
130 :     return $retVal;
131 :     }
132 :    
133 : parrello 1.3 =head3 LoadFromFile
134 : parrello 1.1
135 : parrello 1.3 $sl->LoadFromFile($tableName => $fileName, @fieldNames);
136 : parrello 1.1
137 : parrello 1.3 This method loads the specified table from the specified tab-delimited
138 :     file. The list of field names indicates the order in which the fields are
139 :     present in the input file.
140 : parrello 1.1
141 :     =over 4
142 :    
143 : parrello 1.3 =item tableName
144 : parrello 1.1
145 : parrello 1.3 Name of the table to load.
146 : parrello 1.1
147 : parrello 1.3 =item fileName
148 : parrello 1.1
149 : parrello 1.3 Name of the file containing the data for the table.
150 :    
151 :     =item fieldNames
152 :    
153 :     List of the names of the fields found in the file, in the order they are
154 :     found in the load file.
155 : parrello 1.1
156 :     =back
157 :    
158 :     =cut
159 :    
160 : parrello 1.3 sub LoadFromFile {
161 : parrello 1.1 # Get the parameters.
162 : parrello 1.3 my ($self, $tableName, $fileName, @fieldNames) = @_;
163 :     # Open the input file.
164 :     my $ih = Open(undef, "<$fileName");
165 :     # We'll use this to count the number of records read.
166 :     my $count = 0;
167 :     # Loop through the file.
168 :     while (! eof $ih) {
169 :     # Get the next input record.
170 :     my @inFields = Tracer::GetLine($ih);
171 :     $self->Track(FileRecords => $fileName . " line " . ++$count, 1000);
172 : parrello 1.5 # Insure we have any blank fields truncated from the end.
173 :     while (scalar(@inFields) <= $#fieldNames) {
174 :     push @inFields, "";
175 :     }
176 : parrello 1.3 # Create a map from field names to values.
177 :     my %map = map { $fieldNames[$_] => $inFields[$_] } 0 .. $#fieldNames;
178 :     # Insert it into the table.
179 :     $self->Put($tableName, %map);
180 :     }
181 : parrello 1.1 }
182 :    
183 :    
184 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3