line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::Bayon; |
2
|
11
|
|
|
11
|
|
6286
|
use strict; |
|
11
|
|
|
|
|
18
|
|
|
11
|
|
|
|
|
339
|
|
3
|
11
|
|
|
11
|
|
51
|
use warnings; |
|
11
|
|
|
|
|
17
|
|
|
11
|
|
|
|
|
296
|
|
4
|
11
|
|
|
11
|
|
16860
|
use File::Temp qw(tempdir tempfile); |
|
11
|
|
|
|
|
338227
|
|
|
11
|
|
|
|
|
844
|
|
5
|
11
|
|
|
11
|
|
99
|
use Carp; |
|
11
|
|
|
|
|
21
|
|
|
11
|
|
|
|
|
668
|
|
6
|
11
|
|
|
11
|
|
93
|
use base qw(Class::Accessor::Fast); |
|
11
|
|
|
|
|
22
|
|
|
11
|
|
|
|
|
10591
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors($_) for qw(bayon_path dry_run io_files); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.00002'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
15
|
|
|
15
|
1
|
162
|
my $class = shift; |
14
|
15
|
|
|
|
|
174
|
my $self = $class->SUPER::new( {@_} ); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub clustering { |
18
|
2
|
|
|
2
|
1
|
72
|
my $self = shift; |
19
|
2
|
|
|
|
|
5
|
my $input = shift; |
20
|
2
|
|
|
|
|
4
|
my $args_options = shift; |
21
|
2
|
|
|
|
|
4
|
my $args_outfiles = shift; |
22
|
|
|
|
|
|
|
|
23
|
2
|
|
|
|
|
8
|
my $cmd = $self->_generate_command( 'clustering', $input, $args_options, |
24
|
|
|
|
|
|
|
$args_outfiles ); |
25
|
2
|
50
|
|
|
|
9
|
return $cmd if $self->dry_run; |
26
|
0
|
|
|
|
|
0
|
system($cmd); |
27
|
0
|
0
|
0
|
|
|
0
|
if ( !$args_outfiles or $args_outfiles->{return_force} ) { |
28
|
0
|
|
|
|
|
0
|
my @ret = $self->_build_return_data( $self->io_files, $cmd ); |
29
|
0
|
0
|
|
|
|
0
|
if (wantarray) { |
30
|
0
|
|
|
|
|
0
|
return @ret; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
else { |
33
|
0
|
|
|
|
|
0
|
return $ret[0]; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub classify { |
39
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
40
|
0
|
|
|
|
|
0
|
my $input = shift; |
41
|
0
|
|
|
|
|
0
|
my $args_options = shift; |
42
|
0
|
|
|
|
|
0
|
my $args_outfiles = shift; |
43
|
|
|
|
|
|
|
|
44
|
0
|
|
|
|
|
0
|
my $cmd = $self->_generate_command( 'classify', $input, $args_options, |
45
|
|
|
|
|
|
|
$args_outfiles ); |
46
|
0
|
0
|
|
|
|
0
|
return $cmd if $self->dry_run; |
47
|
0
|
|
|
|
|
0
|
system($cmd); |
48
|
0
|
0
|
0
|
|
|
0
|
if ( !$args_outfiles or $args_outfiles->{return_force} ) { |
49
|
0
|
|
|
|
|
0
|
my @ret = $self->_build_return_data( $self->io_files, $cmd ); |
50
|
0
|
0
|
|
|
|
0
|
if (wantarray) { |
51
|
0
|
|
|
|
|
0
|
return @ret; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
else { |
54
|
0
|
|
|
|
|
0
|
return $ret[0]; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub _generate_command { |
60
|
2
|
|
|
2
|
|
3
|
my $self = shift; |
61
|
2
|
|
|
|
|
4
|
my $method_name = shift; |
62
|
2
|
|
|
|
|
4
|
my $input = shift; |
63
|
2
|
|
|
|
|
3
|
my $args_options = shift; |
64
|
2
|
|
|
|
|
4
|
my $args_outfiles = shift; |
65
|
|
|
|
|
|
|
|
66
|
2
|
|
100
|
|
|
10
|
my $bayon_path = $self->bayon_path || 'bayon'; |
67
|
2
|
|
|
|
|
30
|
my $io_files = $self->_io_file_names( $input, $args_outfiles ); |
68
|
2
|
|
|
|
|
10
|
$self->io_files($io_files); |
69
|
2
|
|
|
|
|
21
|
my $options = $self->_option( $method_name, $args_options, $io_files ); |
70
|
|
|
|
|
|
|
|
71
|
2
|
|
|
|
|
4
|
my $cmd; |
72
|
2
|
|
|
|
|
3
|
my $infile = $io_files->{input}; |
73
|
2
|
|
|
|
|
4
|
my $outfile = $io_files->{output}; |
74
|
2
|
|
|
|
|
9
|
$cmd = "$bayon_path $infile $options > $outfile"; |
75
|
2
|
|
|
|
|
7
|
return $cmd; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _io_file_names { |
79
|
13
|
|
|
13
|
|
1481
|
my $self = shift; |
80
|
13
|
|
|
|
|
25
|
my $input = shift; |
81
|
13
|
|
|
|
|
23
|
my $args_outfiles = shift; |
82
|
|
|
|
|
|
|
|
83
|
13
|
|
|
|
|
20
|
my %io_files; |
84
|
|
|
|
|
|
|
|
85
|
13
|
100
|
100
|
|
|
168
|
if ( ref $input eq 'HASH' ) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
86
|
1
|
|
|
|
|
6
|
my $dir = tempdir(CLEANUP => 1); |
87
|
1
|
|
|
|
|
733
|
my ( $fh, $fname ) = tempfile(DIR => $dir); |
88
|
1
|
|
|
|
|
305
|
while ( my ( $key, $val ) = each %$input ) { |
89
|
6
|
|
|
|
|
14
|
print $fh $key, "\t"; |
90
|
6
|
|
|
|
|
292
|
print $fh join( "\t", (%$val) ), "\n"; |
91
|
|
|
|
|
|
|
} |
92
|
1
|
|
|
|
|
45
|
close($fh); |
93
|
1
|
|
|
|
|
6
|
$io_files{input} = $fname; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
elsif ( ref $input eq 'GLOB' ) { |
96
|
1
|
|
|
|
|
7
|
my $dir = tempdir(CLEANUP => 1); |
97
|
1
|
|
|
|
|
753
|
my ( $fh, $fname ) = tempfile(DIR => $dir); |
98
|
1
|
|
|
|
|
331
|
while ( my $rec = <$input> ) { |
99
|
6
|
|
|
|
|
48
|
print $fh $rec; |
100
|
|
|
|
|
|
|
} |
101
|
1
|
|
|
|
|
54
|
close($fh); |
102
|
1
|
|
|
|
|
7
|
$io_files{input} = $fname; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
elsif ( $input and ref $input eq '' ) { |
105
|
9
|
100
|
|
|
|
362
|
croak("can't find input file $input") unless -e $input; |
106
|
8
|
|
|
|
|
23
|
$io_files{input} = $input; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
else { |
109
|
2
|
|
|
|
|
360
|
croak("wrong input"); |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
10
|
|
|
|
|
34
|
for (qw( output clvector )) { |
113
|
20
|
100
|
100
|
|
|
128
|
if ( $args_outfiles and $args_outfiles->{$_} ) { |
114
|
13
|
|
|
|
|
39
|
$io_files{$_} = $args_outfiles->{$_}; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
else { |
117
|
7
|
|
|
|
|
36
|
my $dir = tempdir(CLEANUP => 1); |
118
|
7
|
|
|
|
|
3817
|
my ( $fh, $fname ) = tempfile(DIR => $dir); |
119
|
7
|
|
|
|
|
282859
|
close($fh); |
120
|
7
|
|
|
|
|
55
|
$io_files{$_} = $fname; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
} |
123
|
10
|
|
|
|
|
47
|
return \%io_files; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub _build_return_data { |
127
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
128
|
0
|
|
|
|
|
0
|
my $io_files = shift; |
129
|
0
|
|
|
|
|
0
|
my $cmd = shift; |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
0
|
my @ret; |
132
|
0
|
|
|
|
|
0
|
for (qw(output clvector)) { |
133
|
0
|
|
|
|
|
0
|
my $data; |
134
|
0
|
|
|
|
|
0
|
open( FILE, "<", $io_files->{$_} ); |
135
|
0
|
|
|
|
|
0
|
while ( my $line = ) { |
136
|
0
|
|
|
|
|
0
|
chomp $line; |
137
|
0
|
|
|
|
|
0
|
my @f = split( "\t", $line ); |
138
|
0
|
|
|
|
|
0
|
my $label = shift @f; |
139
|
0
|
0
|
0
|
|
|
0
|
if ( $cmd =~ / -p / || $cmd =~ /--classify/ ) { |
140
|
0
|
|
|
|
|
0
|
my @array; |
141
|
0
|
|
|
|
|
0
|
while ( @f > 0 ) { |
142
|
0
|
|
|
|
|
0
|
my $key = shift @f; |
143
|
0
|
|
|
|
|
0
|
my $val = shift @f; |
144
|
0
|
|
|
|
|
0
|
push @array, { $key => $val }; |
145
|
|
|
|
|
|
|
} |
146
|
0
|
|
|
|
|
0
|
$data->{$label} = \@array; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
else { |
149
|
0
|
|
|
|
|
0
|
$data->{$label} = \@f; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
} |
152
|
0
|
|
|
|
|
0
|
close(FILE); |
153
|
0
|
|
|
|
|
0
|
push @ret, $data; |
154
|
|
|
|
|
|
|
} |
155
|
0
|
|
|
|
|
0
|
return @ret; |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub _option { |
159
|
7
|
|
|
7
|
|
28
|
my $self = shift; |
160
|
7
|
|
|
|
|
14
|
my $method_name = shift; |
161
|
7
|
|
|
|
|
11
|
my $args_options = shift; |
162
|
7
|
|
|
|
|
9
|
my $io_files = shift; |
163
|
|
|
|
|
|
|
|
164
|
7
|
|
|
|
|
10
|
my $option; |
165
|
|
|
|
|
|
|
|
166
|
7
|
100
|
|
|
|
26
|
if ( $method_name eq 'clustering' ) { |
|
|
100
|
|
|
|
|
|
167
|
5
|
|
66
|
|
|
35
|
my $number |
168
|
|
|
|
|
|
|
= $args_options->{number} |
169
|
|
|
|
|
|
|
|| $args_options->{num} |
170
|
|
|
|
|
|
|
|| $args_options->{n}; |
171
|
5
|
|
66
|
|
|
34
|
my $limit |
172
|
|
|
|
|
|
|
= $args_options->{limit} |
173
|
|
|
|
|
|
|
|| $args_options->{lim} |
174
|
|
|
|
|
|
|
|| $args_options->{l}; |
175
|
5
|
|
66
|
|
|
22
|
my $point = $args_options->{point} || $args_options->{p}; |
176
|
5
|
|
|
|
|
9
|
my $clvector = $args_options->{clvector}; |
177
|
5
|
|
|
|
|
16
|
my $clvector_size = $args_options->{clvector_size}; |
178
|
5
|
|
|
|
|
9
|
my $method = $args_options->{method}; |
179
|
5
|
|
|
|
|
8
|
my $seed = $args_options->{seed}; |
180
|
5
|
|
|
|
|
16
|
my $idf = $args_options->{idf}; |
181
|
|
|
|
|
|
|
|
182
|
5
|
100
|
66
|
|
|
21
|
if ( !$number && !$limit ) { |
183
|
1
|
|
|
|
|
2
|
$limit = 1.5; |
184
|
|
|
|
|
|
|
} |
185
|
5
|
100
|
|
|
|
10
|
if ($number) { |
186
|
2
|
|
|
|
|
93
|
$option .= '-n ' . $number . ' '; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
else { |
189
|
3
|
50
|
|
|
|
10
|
if ($limit) { |
190
|
3
|
|
|
|
|
28
|
$option .= '-l ' . $limit . ' '; |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
5
|
100
|
|
|
|
13
|
if ($point) { |
194
|
3
|
|
|
|
|
5
|
$option .= '-p '; |
195
|
|
|
|
|
|
|
} |
196
|
5
|
100
|
|
|
|
12
|
if ($clvector) { |
197
|
3
|
|
|
|
|
11
|
$option .= '-c ' . $io_files->{clvector} . ' '; |
198
|
3
|
50
|
|
|
|
10
|
if ($clvector_size) { |
199
|
0
|
|
|
|
|
0
|
$option .= '--clvector-size=' . $clvector_size . ' '; |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
} |
202
|
5
|
50
|
|
|
|
19
|
if ($method) { |
203
|
0
|
|
|
|
|
0
|
$option .= '--method=' . $method . ' '; |
204
|
|
|
|
|
|
|
} |
205
|
5
|
50
|
|
|
|
10
|
if ($seed) { |
206
|
0
|
|
|
|
|
0
|
$option .= '--seed=' . $seed . ' '; |
207
|
|
|
|
|
|
|
} |
208
|
5
|
100
|
|
|
|
20
|
if ($idf) { |
209
|
2
|
|
|
|
|
5
|
$option .= '--idf '; |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
} |
212
|
|
|
|
|
|
|
elsif ( $method_name eq 'classify' ) { |
213
|
1
|
|
|
|
|
4
|
my $classify = $args_options->{'classify'}; |
214
|
1
|
|
50
|
|
|
9
|
my $inv_keys = $args_options->{'inv-keys'} || 20; |
215
|
1
|
|
50
|
|
|
38
|
my $inv_size = $args_options->{'inv-size'} || 100; |
216
|
1
|
|
50
|
|
|
6
|
my $classify_size = $args_options->{'classify-size'} || 20; |
217
|
1
|
|
|
|
|
10
|
$option |
218
|
|
|
|
|
|
|
.= '--classify=' |
219
|
|
|
|
|
|
|
. $classify |
220
|
|
|
|
|
|
|
. ' --inv-keys=' |
221
|
|
|
|
|
|
|
. $inv_keys |
222
|
|
|
|
|
|
|
. ' --inv-size=' |
223
|
|
|
|
|
|
|
. $inv_size |
224
|
|
|
|
|
|
|
. ' --classify-size=' |
225
|
|
|
|
|
|
|
. $classify_size; |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
else { |
228
|
1
|
|
|
|
|
207
|
croak("wrong method name"); |
229
|
|
|
|
|
|
|
} |
230
|
6
|
|
|
|
|
34
|
$option =~ s/ $//; |
231
|
6
|
|
|
|
|
17
|
return $option; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
1; |
235
|
|
|
|
|
|
|
__END__ |