-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_values_with_column_proportions.pl
121 lines (106 loc) · 2.65 KB
/
replace_values_with_column_proportions.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
#!/usr/bin/env perl
# Calculates sum of each column containing numerical values. Replaces values in column
# with the proportion of its sum.
# Usage:
# perl replace_values_with_column_proportions.pl [tab-separated table]
# Prints to console. To print to file, use
# perl replace_values_with_column_proportions.pl [tab-separated table] > [output table path]
use strict;
use warnings;
my $table = $ARGV[0];
my $NEWLINE = "\n";
my $DELIMITER = "\t";
# reads in table and calculates sum of each column
my %column_is_not_numerical = (); # key: column number (0-indexed) -> value: 1 if column does not contain only numerical values
my %column_sum = (); # key: column number (0-indexed) -> value: sum of values in column
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
{
$first_line = 0; # next line is not column titles
}
else # column values (not column titles)
{
my $column = 0;
foreach my $value(@items_in_line)
{
# checks if value is numerical
if($value =~ /^[\d.-]+$/)
{
# column is numerical
# adds value to column sum
if(!$column_sum{$column})
{
$column_sum{$column} = 0;
}
$column_sum{$column} = $column_sum{$column} + $value;
}
else
{
# column is not numerical
$column_is_not_numerical{$column} = 1;
}
$column++;
}
}
}
}
close TABLE;
# reads in table and replaces each numerical column value with proportion
$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
{
$first_line = 0; # next line is not column titles
# prints column titles line as is
print $line.$NEWLINE;
}
else # column values (not column titles)
{
my $column = 0;
foreach my $value(@items_in_line)
{
if($column > 0)
{
print $DELIMITER;
}
if($column_is_not_numerical{$column})
{
# column is not numerical--print value as is
print $value;
}
else # column is numerical
{
# column is numerical--print proportion
if($column_sum{$column})
{
my $proportion = $value / $column_sum{$column};
print $proportion;
}
else
{
print "0";
}
}
$column++;
}
print $NEWLINE;
}
}
}
close TABLE;
# April 29, 2023