-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_shared_values_summary_column.pl
197 lines (167 loc) · 5.66 KB
/
add_shared_values_summary_column.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
#!/usr/bin/env perl
# Summarizes all values appearing in columns to summarize (sample ids and dates, for
# example) for each shared value (patient id, for example). Adds summary in new column.
# Usage:
# perl add_shared_values_summary_column.pl [tab-separated table]
# "[title of column containing values shared by rows]"
# "[title of column to include in summary of shared values]"
# "[title of another column to include in summary of shared values]" [etc.]
# Prints to console. To print to file, use
# perl add_shared_values_summary_column.pl [tab-separated table]
# "[title of column containing values shared by rows]"
# "[title of column to include in summary of shared values]"
# "[title of another column to include in summary of shared values]" [etc.]
# > [output table path]
use strict;
use warnings;
my $table = $ARGV[0];
my $title_of_column_with_shared_values = $ARGV[1];
my @titles_of_columns_to_include_in_summary = @ARGV[2..$#ARGV];
my $NEWLINE = "\n";
my $DELIMITER = "\t";
my $NO_DATA = "";
# 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 merge on are provided and make sense
if(!defined $title_of_column_with_shared_values or !length $title_of_column_with_shared_values)
{
print STDERR "Error: title of column with shared values not provided. Exiting.\n";
die;
}
if(!scalar @titles_of_columns_to_include_in_summary)
{
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 %summary_column_title_to_column = ();
foreach my $column_title(@titles_of_columns_to_include_in_summary)
{
$summary_column_title_to_column{$column_title} = -1;
}
my $column_with_shared_values = -1;
my %shared_value_to_summary = (); # key: value in column with shared values -> value: summary to print in new 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
{
# identifies columns to merge
my $column = 0;
foreach my $column_title(@items_in_line)
{
if(defined $summary_column_title_to_column{$column_title})
{
if($summary_column_title_to_column{$column_title} == -1)
{
$summary_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";
}
}
elsif($column_title eq $title_of_column_with_shared_values)
{
$column_with_shared_values = $column;
}
$column++;
}
# verifies that all columns have been found
foreach my $column_title(keys %summary_column_title_to_column)
{
if($summary_column_title_to_column{$column_title} == -1)
{
print STDERR "Error: expected column title ".$column_title
." not found in table ".$table
."\nExiting.\n";
die;
}
elsif($column_with_shared_values == -1)
{
print STDERR "Error: expected column title "
.$title_of_column_with_shared_values." not found in table "
.$table."\nExiting.\n";
die;
}
$column++;
}
$first_line = 0; # next line is not column titles
}
else # column values (not titles)
{
# retrieves shared value to generate summary for
my $shared_value = $items_in_line[$column_with_shared_values];
# retrieves summary values without duplicates
my %summary_values = (); # key: value in a summary column -> value: 1 (to eliminate duplicates)
foreach my $summary_value_column(values %summary_column_title_to_column)
{
if(defined $items_in_line[$summary_value_column] and length $items_in_line[$summary_value_column])
{
$summary_values{$items_in_line[$summary_value_column]} = 1;
}
}
# generates this row's addition to shared value's summary
my $summary_contribution = join(" ", sort keys %summary_values);
# adds this rows addition to shared value's summary
if($shared_value_to_summary{$shared_value})
{
$shared_value_to_summary{$shared_value} .= ", ";
}
$shared_value_to_summary{$shared_value} .= $summary_contribution;
}
}
}
close TABLE;
# prints table with added summary column
$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
{
# generates summary column title
my $summary_column_title = join("/", @titles_of_columns_to_include_in_summary)
."_by_".$title_of_column_with_shared_values;
# prints existing column titles
print $line.$DELIMITER;
# prints summary column
print $summary_column_title.$NEWLINE;
$first_line = 0; # next line is not column titles
}
else # column values (not titles)
{
# retrieves shared value we generated summary for
my $shared_value = $items_in_line[$column_with_shared_values];
# prints existing column values
print $line.$DELIMITER;
# prints summary column
if(defined $shared_value_to_summary{$shared_value} and length $shared_value_to_summary{$shared_value})
{
print $shared_value_to_summary{$shared_value};
}
print $NEWLINE;
}
}
}
close TABLE;
# August 26, 2021