-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarize_numerical_columns.pl
290 lines (244 loc) · 6.7 KB
/
summarize_numerical_columns.pl
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env perl
# Summarizes selected numerical columns. Adds new columns with: mean, standard deviation,
# median, min, max, range, and all values sorted in a comma-separated list. If a column
# value contains a comma-separated list, includes all values from list.
# Usage:
# perl summarize_numerical_columns.pl [tab-separated table] "[column title]"
# "[another column title]" [etc.]
# Prints to console. To print to file, use
# perl summarize_numerical_columns.pl [tab-separated table] "[column title]"
# "[another column title]" [etc.] > [output table path]
use strict;
use warnings;
my $table = $ARGV[0];
my @titles_of_columns_to_summarize = @ARGV[1..$#ARGV];
my $NEWLINE = "\n";
my $DELIMITER = "\t";
my $NO_DATA = "NA";
my $CONCATENATED_COLUMN_TITLE_SEPARATOR = "_";
my $COLUMN_VALUES_SEPARATOR = ", ";
# verifies that input file exists and is not empty
if(!$table or !-e $table or -z $table)
{
print STDERR "Error: table not provided, does not exist, or empty:\n\t"
.$table."\nExiting.\n";
die;
}
# verifies that titles of column to summarize are provided and make sense
if(!scalar @titles_of_columns_to_summarize)
{
print STDERR "Error: title of columns to summarize not provided. Exiting.\n";
die;
}
# reads in and processes input table
my %column_to_summarize = (); # key: column (0-indexed) -> value: 1 if column will be summarized
my %column_title_to_column = ();
foreach my $column_title(@titles_of_columns_to_summarize)
{
$column_title_to_column{$column_title} = -1;
}
my $first_line = 1;
open TABLE, "<$table" || die "Could not open $table to read; terminating =(\n";
while(<TABLE>) # for each row in the file
{
chomp;
my $line = $_;
if($line =~ /\S/) # if row not empty
{
my @items_in_line = split($DELIMITER, $line, -1);
if($first_line) # column titles
{
# identifies columns to merge
my $column = 0;
foreach my $column_title(@items_in_line)
{
if(defined $column_title_to_column{$column_title})
{
if($column_title_to_column{$column_title} == -1)
{
$column_title_to_column{$column_title} = $column;
$column_to_summarize{$column} = 1;
}
else
{
print STDERR "Warning: column ".$column_title." encountered more "
."than once in table ".$table."\n";
}
}
$column++;
}
# verifies that all columns have been found
foreach my $column_title(keys %column_title_to_column)
{
if($column_title_to_column{$column_title} == -1)
{
print STDERR "Error: expected column title ".$column_title
." not found in table ".$table
."\nExiting.\n";
die;
}
$column++;
}
# generates concatenated column title
my $concatenated_column_title = join($CONCATENATED_COLUMN_TITLE_SEPARATOR, @titles_of_columns_to_summarize);
# prints existing column titles
print $line.$DELIMITER;
# prints new column titles
print "mean_".$concatenated_column_title.$DELIMITER; # mean
print "std_dev_".$concatenated_column_title.$DELIMITER; # standard deviation,
print "median_".$concatenated_column_title.$DELIMITER; # median
print "min_".$concatenated_column_title.$DELIMITER; # min
print "max_".$concatenated_column_title.$DELIMITER; # max
print "range_".$concatenated_column_title.$DELIMITER; # range
print "all_values_sorted_".$concatenated_column_title.$NEWLINE; # all values sorted in a comma-separated list
$first_line = 0; # next line is not column titles
}
else # column values (not titles)
{
# retrieves column values to summarize
# if a column value contains a comma-separated list, includes all values from list
my @column_values = ();
foreach my $column_title(@titles_of_columns_to_summarize)
{
my $column = $column_title_to_column{$column_title};
my $value = $items_in_line[$column];
my @values = ($value);
if($value =~ /, /)
{
@values = split(", ", $value);
}
elsif($value =~ /,/)
{
@values = split(",", $value);
}
foreach my $value(@values)
{
if($value ne $NO_DATA)
{
push(@column_values, $value);
}
}
}
# calculates summary values
my $mean = mean(@column_values);
my $std_dev = std_dev(@column_values);
my $median = median(@column_values);
my $min = min(@column_values);
my $max = max(@column_values);
my $range = $min."-".$max;
my $all_values_sorted = join($COLUMN_VALUES_SEPARATOR, sort(@column_values));
# prints existing column values
print $line.$DELIMITER;
# prints new column values
print $mean.$DELIMITER; # mean
print $std_dev.$DELIMITER; # standard deviation,
print $median.$DELIMITER; # median
print $min.$DELIMITER; # min
print $max.$DELIMITER; # max
print $range.$DELIMITER; # range
print $all_values_sorted.$NEWLINE; # all values sorted in a comma-separated list
}
}
}
close TABLE;
# returns minimum value in input array
sub min
{
my @values = @_;
# returns if we don't have any input values
if(scalar @values < 1)
{
return $NO_DATA;
}
# retrieves minimum value
my $min_value = $values[0];
foreach my $value(@values)
{
if($value < $min_value)
{
$min_value = $value;
}
}
return $min_value;
}
# returns maximum value in input array
sub max
{
my @values = @_;
# returns if we don't have any input values
if(scalar @values < 1)
{
return $NO_DATA;
}
# retrieves maximum value
my $max_value = $values[0];
foreach my $value(@values)
{
if($value > $max_value)
{
$max_value = $value;
}
}
return $max_value;
}
# returns mean of input array
sub mean
{
my @values = @_;
# returns if we don't have any input values
if(scalar @values < 1)
{
return $NO_DATA;
}
# sums all values
my $sum = 0;
foreach my $value(@values)
{
$sum += $value;
}
return $sum/scalar @values;
}
# returns standard deviation of input array
sub std_dev
{
my @values = @_;
# returns if we don't have enough input values
if(scalar @values < 2)
{
return $NO_DATA;
}
# calculates mean
my $mean = mean(@values);
# calculates sum of squared differences from the mean
my $sum_of_squared_differences = 0;
foreach my $value(@values)
{
my $difference = $mean - $value;
$sum_of_squared_differences += $difference * $difference;
}
# calculates and returns standard deviation
return sqrt($sum_of_squared_differences / (scalar @values - 1))
}
# returns median of input array
sub median
{
my @values = @_;
# returns if we don't have any input values
if(scalar @values < 1)
{
return $NO_DATA;
}
# sorts values
@values = sort @values;
# returns center
if(scalar @values % 2 == 0) # even number of values
{
return ($values[scalar @values/2] + $values[scalar @values/2-1])/2;
}
else # odd number of values
{
return $values[scalar @values/2];
}
}
# August 26, 2021
# September 26, 2021