summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/pedigree-perl/Pedigree/Parser.pm
blob: 7978064f47324249a1be9c8e805c5958a22f9aa5 (plain)
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
=pod

=head1 NAME

Pedigree::Parser - parser for the input file

=head1 SYNOPSIS

use Pedigree::Parser;

$parser = new Pedigree::Parser(I{$inputline>, I<$lang>)

$parser->Parse($inputline);


=head1 DESCRIPTION

This package parses input for the pedigree library and is used to
define nodes.

=over 4

=cut

####################################################################
# Define the package                                               #
####################################################################

package Pedigree::Parser;
use strict;

####################################################################
#    package variables                                             #
####################################################################

#
# %fields_to_convert:  the hash of fields that contain limited 
# number of values and the default value for such field
#

my %fields_to_convert = (
        'Sex'=>'unknown', 
        'Proband'=>0,
        'Condition'=>'normal',
        'Type' => ''
);


####################################################################
# And package methods                                              #
####################################################################

####################################################################
#    new                                                           #
####################################################################

=pod

=item B<new>(I<$inputline>, I<$lang>);

Construct a new parser from the pipe-separated line at input

=cut

sub new {
    my ($class,$inputline,$lang)=@_;
    my $self={};

    #
    # The hash $self->{fields} is the main stored data structure.  
    # The key is the field, the value is the number of the field 
    # in the input lines. 
    #
    my %fieldnames=%{$lang->GetFieldNames()};
    chomp $inputline;
    $inputline =~ s/^\s+//;
    $inputline =~ s/\s+$//;
    my @input = split /\s*\|\s*/, $inputline;
    for (my $i=0;  $i<scalar @input; $i++) {
	my $name=$input[$i];
	if (exists $fieldnames{$name}) {
	    my $field=$fieldnames{$name};
	    $self->{fields}->{$field}=$i;
	} else {
	    print STDERR "Warning:  unknown field $name\n";
	}
    }

    if ($main::DEBUG) {
	print STDERR "Field names:\n";
	foreach my $key (keys %fieldnames) {
	    my $field=$fieldnames{$key};
	    my $pos=$self->{fields}->{$field};
	    print STDERR "\t$key\t$field\t$pos\n";
	}
    }

    #
    # The hash $self->{values} contains values for  fields
    # with closed sets of values.
    #
    my %values=%{$lang->GetValues()};
    $self->{values}=\%values;

    if ($main::DEBUG) {
	print STDERR "Field values:\n";
	foreach my $key (keys %values) {
	    my $value=$values{$key};
	    print STDERR "\t$key\t$value\n";
	}
    }


    #
    # The hash $self->{special_names} contains special values
    # for the 'Name' field
    #
    my %special=%{$lang->GetSpecialNames()};
    $self->{'special_names'}=\%special;

    if ($main::DEBUG) {
	print STDERR "Special names:\n";
	foreach my $key (keys %special) {
	    my $value=$special{$key};
	    print STDERR "\t$key\t$value\n";
	}
    }

    if ($main::DEBUG) {
	print STDERR "Special fields:\n";
	foreach my $key (keys %fields_to_convert) {
	    my $value=$fields_to_convert{$key};
	    print STDERR "\t$key\t$value\n";
	}
    }

    bless ($self,$class);
    return $self;
}


####################################################################
#    Parse                                                         #
####################################################################

=pod

=item B<Parse>(I<$inputline>);

Take a line of comma-separated values;  return a reference to a 
hash of parsed values

=cut

sub Parse  {
    my ($self,$inputline)=@_;
    chomp $inputline;
    $inputline =~ s/^\s+//;
    $inputline =~ s/\s+$//;
    my @input = split /\s*\|\s*/, $inputline;
    if ($main::DEBUG) {
	print STDERR "Parsing line:$inputline\n";
    }

    my %result;

    foreach my $field (keys %{$self->{fields}})  {
	my $i=$self->{fields}->{$field};
	my $value=$input[$i];
	#
	# Special fields...
	#
	if (exists $self->{values}->{$value}) {
	    $value=$self->{values}->{$value};
	}
	if (exists $fields_to_convert{$field}) {
	    if (length($value) == 0 ) {
		$value=$fields_to_convert{$field};
	    }
	}
	#
	#  Dropping empty fields
	#
	if (length($value) == 0 ) {
	    next;
	}

	#
	# Converting Name field
	#
	if (($field eq 'Name') && ($value =~ /^\#/)) {
	    foreach my $regexp (keys %{$self->{'special_names'}}) {
		my $name=$self->{'special_names'}->{$regexp};
		$value =~ s/^(\#$regexp.*)/\#$name/i;
	    }
	}

	#
	# And finishing
	#
	$result{$field}=$value;
	if ($main::DEBUG) {
	    print STDERR "\t$field\t$value\n";
	}
    }

    return \%result;

}

####################################################################
#    THE END                                                       #
####################################################################

=pod

=back

=head1 ENVIRONMENT

The calling program should define B<$main::DEBUG> and set it to 0
or 1.

=head1 SEE ALSO

pedigree(1), Pedigree(3)

=head1  AUTHOR

Boris Veytsman, Leila Akhmadeeva, 2006, 2007



=cut

1;