File: SOIHandler.pm

package info (click to toggle)
libchado-perl 1.23-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,976 kB
  • ctags: 10,378
  • sloc: xml: 192,540; sql: 165,945; perl: 28,339; sh: 101; python: 73; makefile: 46
file content (220 lines) | stat: -rw-r--r-- 4,589 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package SOI::SOIHandler;

=head1 NAME

SOI::SOIHandler

=head1 SYNOPSIS

perlSAX handler to parse soi xml

=head1 USAGE

=begin

my $handler = SOI::SOIHandler->new([qw(your feature type list here)]);
my $parser = XML::Parser::PerlSAX->new(Handler=>$handler);
$parser->parse(Source => { SystemId =>$soixml_file});
my $feature = $handler->feature; #get SOI::Feature obj (feature tree) from soi xml

=end

=cut

=head1 FEEDBACK

Email sshu@fruitfly.org

=cut

use strict;
use SOI::Feature;
use FileHandle;
use Carp;

=head1 FUNCTIONS

=cut

sub new {
    my $class = shift;
    my $self = {};

#    my $outfile = shift || ">-";
#    $self->{fh} = new FileHandle ">$outfile";
    bless $self, $class;
    #for now one arg (non-optional)
    my $types = shift || confess("must pass in feature types (arrayref)");
    $types = [$types] unless (ref($types) eq 'ARRAY');
    $self->{feature_types} = $types;
    return $self;
}

=head2 start_element

Called directly by SAX Parser.
Do NOT call this directly.

Adds element name string to stack.

=cut

sub start_element {
    my ($self, $element) = @_;

    my $name = $element->{Name};
    my $feature = $self->feature;
    my $method = "add_$name";
    if (grep{$name eq $_}@{$self->{feature_types} || []}) {
        my $feature = SOI::Feature->new({type=>$element->{Name}});
        $feature->depth(scalar(@{$self->{feature_stack} || []}));
        push @{$self->{feature_stack}}, $feature;
        push @{$self->{level_stack}}, "feature";
    }
#    elsif (grep{$name eq $_}qw(property dbxref ontology secondary_location)) {
    elsif ($feature && $feature->can($method)) {
        push @{$self->{level_stack}}, $name;
        $self->start_auxillary($element);
    }
    else {
        undef $self->{cur_e_char};
    }

    return 1;
}

=head2 end_element

Called directly by SAX Parser.
Do NOT call this directly.

Removes element name string from stack.

=cut


sub end_element {
    my ($self, $element) = @_;

    my $name = $element->{Name};
    my $feature = $self->feature;
    my $method = "add_$name";
    my $level = $self->{level_stack}->[-1];
    if (grep{$name eq $_}@{$self->{feature_types} || []}) {
        pop @{$self->{level_stack}};
        unless (@{$self->{feature_stack}} == 1) {
            my $subf = pop @{$self->{feature_stack}};
            my $curf = $self->feature;
            $curf->add_node($subf);
        }
    }
#    elsif (grep{$name eq $_}qw(property dbxref ontology secondary_location)) {
    elsif ($feature && $feature->can($method)) {
        pop @{$self->{level_stack}};
        my $el_name = "end_auxillary";
        $self->$el_name($element);
    }
    else {
        my $e_val = $self->{cur_e_char};
        if ($e_val) {
            $e_val =~ s/^\s*//g;
            $e_val =~ s/\s*$//g;
        }
        if ($level eq 'feature') {
            $feature->hash->{$name} = $e_val;
        }
        else {
            if ($level eq 'secondary_location') {
                $self->{hash}->{$name} = $e_val;
            } else {
                $self->{hash} = {type=>$name, value=>$e_val};
            }
        }
    }
    return 1;
}

sub start_auxillary {
    my ($self, $element) = @_;
    $self->{hash} = {};
}

sub end_auxillary {
    my ($self, $element) = @_;

    my $name = $element->{Name};
    $name =~ tr/A-Z/a-z/;
    my $feature = $self->feature;
    my $method = "add_$name";
    confess("unsupported auxillary: $name") unless ($feature->can($method));
    my $h = $self->{hash};
    if ($name eq 'secondary_location') {
        my $locf = SOI::Feature->new($h);
        $feature->add_secondary_node($locf);
    } else {
        $feature->$method($h);
    }
}

sub start_document {
    my $self = shift;
    undef $self->{feature_stack};
    undef $self->{level_stack};
}

=head2 end_element

Called directly by SAX Parser.
Do NOT call this directly.

The last method called by the
Sax Parser.  This returns the
data in $self->data.

=cut


sub end_document {
  my $self = shift;

#  if ($self->{fh}) {
#      $self->{fh}->close;
#  }
#  return $self->{data};
}

=head2 characters

Called directly by SAX Parser.
Do NOT call this directly.

Calls method with the name of the
current element with text as the 
first argument.

=cut

sub characters {
  my ($self, $characters) = @_;

  my $data = $characters->{Data};
  return $self->{cur_e_char} .= $data;
}


=head2 feature

 usage:  my $feature = $self->feature;

 returns: top node feature

=cut

sub feature {
    my $self = shift;
    return unless (@{$self->{feature_stack} || []});
    return $self->{feature_stack}->[-1];
}


1;