-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_values_with_coded_values.pl
189 lines (162 loc) · 4.37 KB
/
replace_values_with_coded_values.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
#!/usr/bin/env perl
# Replaces non-empty values with coded values, e.g., Value 1 (for the most common value),
# Value 2 (for the second-most common value), Value 3, etc. Ties are broken alphabetically.
# Usage:
# perl replace_values_with_coded_values.pl [table] "[title of column to search]"
# "[optional code prefix]"
# Prints to console. To print to file, use
# perl replace_values_with_coded_values.pl [table] "[title of column to search]"
# "[optional code prefix]" > [output table path]
use strict;
use warnings;
my $table = $ARGV[0];
my $title_of_column_to_search = $ARGV[1];
my $code_prefix = $ARGV[2]; # optional
my $NEWLINE = "\n";
my $DELIMITER = "\t";
# 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;
}
# sets code prefix to default if not provided
if(!$code_prefix)
{
$code_prefix = "Value";
}
# reads in all values in input table
my %value_to_count = (); # key: non-empty value in column of interest -> value: number of times value appears
my $first_line = 1;
my $column_to_search = -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 include
my $column = 0;
foreach my $column_title(@items_in_line)
{
if(defined $column_title and $column_title eq $title_of_column_to_search)
{
if($column_to_search != -1)
{
print STDERR "Error: column to search ".$title_of_column_to_search
." encountered more than once. Exiting.\n";
die;
}
$column_to_search = $column;
}
$column++;
}
# verifies that we have found all column
if($column_to_search == -1)
{
print STDERR "Error: expected column title ".$title_of_column_to_search
." not found. Exiting.\n";
die;
}
$first_line = 0; # next line is not column titles
}
else # column values (not column titles)
{
my $value = $items_in_line[$column_to_search];
if(defined $value and length $value)
{
$value_to_count{$value}++;
}
}
}
}
close TABLE;
# codes values
my $value_count = 1;
my %value_to_coded_value = (); # key: value -> value: coded value
foreach my $value(sort {$value_to_count{$b} <=> $value_to_count{$a} || $a cmp $b}
keys %value_to_count)
{
my $coded_value = $code_prefix." ".$value_count;
$value_to_coded_value{$value} = $coded_value;
$value_count++;
}
# reads in input table and replaces values with coded values
$first_line = 1;
$column_to_search = -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 include
my $column = 0;
foreach my $column_title(@items_in_line)
{
if(defined $column_title and $column_title eq $title_of_column_to_search)
{
if($column_to_search != -1)
{
print STDERR "Error: column to search ".$title_of_column_to_search
." encountered more than once. Exiting.\n";
die;
}
$column_to_search = $column;
}
$column++;
}
# verifies that we have found all column
if($column_to_search == -1)
{
print STDERR "Error: expected column title ".$title_of_column_to_search
." not found. Exiting.\n";
die;
}
# print header line as is
print $line.$NEWLINE;
$first_line = 0; # next line is not column titles
}
else # column values (not column titles)
{
# prints all values, replacing values in columns to search
my $column = 0;
foreach my $value(@items_in_line)
{
# prints delimiter
if($column > 0)
{
print $DELIMITER;
}
# replaces values if this is a column to search
if($column == $column_to_search)
{
if(defined $value and length $value)
{
$value = $value_to_coded_value{$value};
}
}
# prints value
if(defined $value and length $value)
{
print $value;
}
$column++;
}
print $NEWLINE;
}
}
}
close TABLE;
# August 24, 2021
# December 22, 2021
# December 23, 2021