Debian Codegraph @

texmf-dist/scripts/pedigree-perl/Pedigree/Parser.pm

raw
1 =pod
2
3 =head1 NAME
4
5 Pedigree::Parser - parser for the input file
6
7 =head1 SYNOPSIS
8
9 use Pedigree::Parser;
10
11 $parser = new Pedigree::Parser(I{$inputline>, I<$lang>)
12
13 $parser->Parse($inputline);
14
15
16 =head1 DESCRIPTION
17
18 This package parses input for the pedigree library and is used to
19 define nodes.
20
21
22 =cut
23
24 ####################################################################
25 # Define the package #
26 ####################################################################
27
28 package Pedigree::Parser;
29 use strict;
30
31 ####################################################################
32 # package variables #
33 ####################################################################
34
35 #
36 # %fields_to_convert: the hash of fields that contain limited
37 # number of values and the default value for such field
38 #
39
40 my %fields_to_convert = (
41 'Sex'=>'unknown',
42 'Proband'=>0,
43 'Condition'=>'normal',
44 'Type' => ''
45 );
46
47
48 ####################################################################
49 # And package methods #
50 ####################################################################
51
52 ####################################################################
53 # new #
54 ####################################################################
55
56 =pod
57
58 =over 4
59
60 =item B<new>(I<$inputline>, I<$lang>);
61
62 Construct a new parser from the pipe-separated line at input
63
64 =cut
65
66 sub new {
67 my ($class,$inputline,$lang)=@_;
68 my $self={};
69
70 #
71 # The hash $self->{fields} is the main stored data structure.
72 # The key is the field, the value is the number of the field
73 # in the input lines.
74 #
75 my %fieldnames=%{$lang->GetFieldNames()};
76 chomp $inputline;
77 $inputline =~ s/^\s+//;
78 $inputline =~ s/\s+$//;
79 my @input = split /\s*\|\s*/, $inputline;
80 for (my $i=0; $i<scalar @input; $i++) {
81 my $name=$input[$i];
82 if (exists $fieldnames{$name}) {
83 my $field=$fieldnames{$name};
84 $self->{fields}->{$field}=$i;
85 } else {
86 print STDERR "Warning: unknown field $name\n";
87 }
88 }
89
90 if ($main::DEBUG) {
91 print STDERR "Field names:\n";
92 foreach my $key (keys %fieldnames) {
93 my $field=$fieldnames{$key};
94 my $pos=$self->{fields}->{$field};
95 print STDERR "\t$key\t$field\t$pos\n";
96 }
97 }
98
99 #
100 # The hash $self->{values} contains values for fields
101 # with closed sets of values.
102 #
103 my %values=%{$lang->GetValues()};
104 $self->{values}=\%values;
105
106 if ($main::DEBUG) {
107 print STDERR "Field values:\n";
108 foreach my $key (keys %values) {
109 my $value=$values{$key};
110 print STDERR "\t$key\t$value\n";
111 }
112 }
113
114
115 #
116 # The hash $self->{special_names} contains special values
117 # for the 'Name' field
118 #
119 my %special=%{$lang->GetSpecialNames()};
120 $self->{'special_names'}=\%special;
121
122 if ($main::DEBUG) {
123 print STDERR "Special names:\n";
124 foreach my $key (keys %special) {
125 my $value=$special{$key};
126 print STDERR "\t$key\t$value\n";
127 }
128 }
129
130 if ($main::DEBUG) {
131 print STDERR "Special fields:\n";
132 foreach my $key (keys %fields_to_convert) {
133 my $value=$fields_to_convert{$key};
134 print STDERR "\t$key\t$value\n";
135 }
136 }
137
138 bless ($self,$class);
139 return $self;
140 }
141
142
143 ####################################################################
144 # Parse #
145 ####################################################################
146
147 =pod
148
149 =item B<Parse>(I<$inputline>);
150
151 Take a line of comma-separated values; return a reference to a
152 hash of parsed values
153
154 =cut
155
156 sub Parse {
157 my ($self,$inputline)=@_;
158 chomp $inputline;
159 $inputline =~ s/^\s+//;
160 $inputline =~ s/\s+$//;
161 my @input = split /\s*\|\s*/, $inputline;
162 if ($main::DEBUG) {
163 print STDERR "Parsing line:$inputline\n";
164 }
165
166 my %result;
167
168 foreach my $field (keys %{$self->{fields}}) {
169 my $i=$self->{fields}->{$field};
170 my $value=$input[$i];
171 #
172 # Special fields...
173 #
174 if (exists $self->{values}->{$value}) {
175 $value=$self->{values}->{$value};
176 }
177 if (exists $fields_to_convert{$field}) {
178 if (length($value) == 0 ) {
179 $value=$fields_to_convert{$field};
180 }
181 }
182 #
183 # Dropping empty fields
184 #
185 if (length($value) == 0 ) {
186 next;
187 }
188
189 #
190 # Converting Name field
191 #
192 if (($field eq 'Name') && ($value =~ /^\#/)) {
193 foreach my $regexp (keys %{$self->{'special_names'}}) {
194 my $name=$self->{'special_names'}->{$regexp};
195 $value =~ s/^(\#$regexp.*)/\#$name/i;
196 }
197 }
198
199 #
200 # And finishing
201 #
202 $result{$field}=$value;
203 if ($main::DEBUG) {
204 print STDERR "\t$field\t$value\n";
205 }
206 }
207
208 return \%result;
209
210 }
211
212 ####################################################################
213 # THE END #
214 ####################################################################
215
216 =pod
217
218 =back
219
220 =head1 ENVIRONMENT
221
222 The calling program should define B<$main::DEBUG> and set it to 0
223 or 1.
224
225 =head1 SEE ALSO
226
227 pedigree(1), Pedigree(3)
228
229 =head1 AUTHOR
230
231 Boris Veytsman, Leila Akhmadeeva, 2006, 2007
232
233
234
235 =cut
236
237 1;
238