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

Annotation of /Sprout/AnnotationGroup.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : parrello 1.1 # -*- perl -*-
2 :     #
3 :     # Copyright (c) 2003-2011 University of Chicago and Fellowship
4 :     # for Interpretations of Genomes. All Rights Reserved.
5 :     #
6 :     # This file is part of the SEED Toolkit.
7 :     #
8 :     # The SEED Toolkit is free software. You can redistribute
9 :     # it and/or modify it under the terms of the SEED Toolkit
10 :     # Public License.
11 :     #
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 :     package AnnotationGroup;
20 :    
21 :     =head1 Annotation Group
22 :    
23 :     An I<annotation group> is a list of related annotations. Each annotation in
24 :     the group relates to a single feature and in general they are all within the
25 :     same small timespan.
26 :    
27 :     This object allows the client to create an annotation group by placing annotation
28 :     tuples into it and then perform useful operations on the group.
29 :    
30 :     The fields of this object are as follows:
31 :    
32 :     =over 4
33 :    
34 :     =item fid
35 :    
36 :     ID of the feature to which the annotation group applies, or C<undef> if the
37 :     group is empty.
38 :    
39 :     =item assignment
40 :    
41 :     Text of any assignment made by an annotation in the group, or C<undef> if no
42 :     assignment is present.
43 :    
44 :     =item list
45 :    
46 :     List of the annotations in the group.
47 :    
48 :     =item time0
49 :    
50 :     Earliest timestamp in the group, or C<0> if the group is empty.
51 :    
52 :     =back
53 :    
54 :     =cut
55 :    
56 :     use strict;
57 :    
58 :     =head2 Special Methods
59 :    
60 :     =head3 new
61 :    
62 :     my $group = AnnotationGroup->new();
63 :    
64 :     Create a new, empty annotation group.
65 :    
66 :     =cut
67 :    
68 :     sub new {
69 :     # Get the parameters.
70 :     my ($class) = @_;
71 :     # Create the object.
72 :     my $retVal = {
73 :     fid => undef,
74 :     assignment => undef,
75 :     time0 => 0,
76 :     list => []
77 :     };
78 :     # Bless and return it.
79 :     bless $retVal, $class;
80 :     return $retVal;
81 :     }
82 :    
83 :     =head2 Public Methods
84 :    
85 :     =head3 Read
86 :    
87 :     my ($fid, $time, $user, $data) = AnnotationGroup::Read($ih);
88 :    
89 :     Read an annotation from an annotation file. The annotation consists of four parts.
90 :     Each of the first three components is on a line by itself. The data component is
91 :     terminated by a line containing only a pair of forward slashes (C<//>).
92 :    
93 :     =over 4
94 :    
95 :     =item ih
96 :    
97 :     Open handle for the input annotation file.
98 :    
99 :     =item RETURN
100 :    
101 :     Returns a four-element list consisting of the feature ID, the time stamp (in seconds),
102 :     the user name, and the data string.
103 :    
104 :     =back
105 :    
106 :     =cut
107 :    
108 :     sub Read {
109 :     # Handle an object-oriented call.
110 :     shift if UNIVERSAL::isa($_[0],__PACKAGE__);
111 :     # Get the parameters.
112 :     my ($ih) = @_;
113 :     # Declare the return variable.
114 :     my @retVal;
115 :     # Read the first three items.
116 :     for (my $i = 0; $i < 3; $i++) {
117 :     my $item = <$ih>;
118 :     chomp $item;
119 :     push @retVal, $item;
120 :     }
121 :     # Assemble the lines of the data item.
122 :     my @lines;
123 :     my $line = <$ih>;
124 :     while (defined $line && $line ne "//\n") {
125 :     push @lines, $line;
126 :     $line = <$ih>;
127 :     }
128 :     push @retVal, join("", @lines);
129 :     # Return the pieces of the annotation.
130 :     return @retVal;
131 :     }
132 :    
133 :     =head3 Add
134 :    
135 :     $group->Add($fid, $time, $user, $data);
136 :    
137 :     Add the specified annotation to this annotation group. If this is the first
138 :     annotation in the group, the feature ID will be memorized. If it is a function
139 :     assignment, the function text will be memorized.
140 :    
141 :     =over 4
142 :    
143 :     =item fid
144 :    
145 :     ID of the feature being annotated.
146 :    
147 :     =item time
148 :    
149 :     Time stamp for the annotation.
150 :    
151 :     =item user
152 :    
153 :     Name of the user who made the annotation.
154 :    
155 :     =item data
156 :    
157 :     Data string of the annotation.
158 :    
159 :     =back
160 :    
161 :     =cut
162 :    
163 :     sub Add {
164 :     # Get the parameters.
165 :     my ($self, $fid, $time, $user, $data) = @_;
166 :     # Find out if we need to memorize the feature ID.
167 :     if (! defined $self->{fid}) {
168 :     $self->{fid} = $fid;
169 :     }
170 :     # Is this an assignment?
171 :     if ($data =~ /^Set\s+(master|FIG)\s+function\s+to\n(.+)/) {
172 :     # Yes. Memorize the assignment text.
173 :     $self->{assignment} = $2;
174 :     }
175 :     # Merge in the time. We want to keep the earliest.
176 :     my $oldTime = $self->{time0};
177 :     if (! $oldTime || $oldTime > $time) {
178 :     $self->{time0} = $time;
179 :     }
180 :     # Store the annotation in the annotation list.
181 :     push @{$self->{list}}, [$fid, $time, $user, $data];
182 :     }
183 :    
184 :     =head3 fid
185 :    
186 :     my $fid = $group->fid;
187 :    
188 :     Return the feature ID relevant to this annotation group.
189 :    
190 :     =cut
191 :    
192 :     sub fid {
193 :     return $_[0]->{fid};
194 :     }
195 :    
196 :     =head3 assignment
197 :    
198 :     my $assignment = $group->assignment;
199 :    
200 :     Return the assignment (if any) for this annotation group.
201 :    
202 :     =cut
203 :    
204 :     sub assignment {
205 :     return $_[0]->{assignment};
206 :     }
207 :    
208 :     =head3 time0
209 :    
210 :     my $time0 = $group->time0;
211 :    
212 :     Return the earliest time for any annotation in the group.
213 :    
214 :     =cut
215 :    
216 :     sub time0 {
217 :     return $_[0]->{time0};
218 :     }
219 :    
220 :     =head3 count
221 :    
222 :     my $count = $group->count;
223 :    
224 :     Return the number of annotations in this group.
225 :    
226 :     =cut
227 :    
228 :     sub count {
229 :     return scalar @{$_[0]->{list}};
230 :     }
231 :    
232 :     =head3 annotation
233 :    
234 :     my ($fid, $time, $user, $data) = $group->annotation($idx);
235 :    
236 :     Return the annotation in the specified position of the annotation group.
237 :    
238 :     =over 4
239 :    
240 :     =item idx
241 :    
242 :     Index (0-based) of the desired annotation to retrieve from the annotation group's
243 :     annotation list.
244 :    
245 :     =item RETURN
246 :    
247 :     Returns a four-element list consisting of the feature ID, the time stamp (in seconds),
248 :     the user name, and the data string. If the index is out of bounds, will return an
249 :     empty list.
250 :    
251 :     =back
252 :    
253 :     =cut
254 :    
255 :     sub annotation {
256 :     # Get the parameters.
257 :     my ($self, $idx) = @_;
258 :     # Declare the return variable.
259 :     my @retVal;
260 :     # Get the annotation list.
261 :     my $list = $self->{list};
262 :     # Is the index in range?
263 :     if ($idx >= 0 && $idx < @$list) {
264 :     # Extract the annotation requested.
265 :     @retVal = @{$list->[$idx]};
266 :     }
267 :     # Return the annotation found.
268 :     return @retVal;
269 :     }
270 :    
271 :     1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3