-
Notifications
You must be signed in to change notification settings - Fork 0
/
listed_columns.pl
51 lines (47 loc) · 1.47 KB
/
listed_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
#!/usr/bin/perl
##v0.1.0 Robert R Butler III on 4/25/2017
##grabbing a set of columns from a tab separated text file(s)
##columns defined by text file with list of column headers
##assumes first uncommented row is headers, and asks for first header
use strict;
use warnings;
use Array::Utils qw(:all);
my $usage = 'USAGE = perl listed_columns.pl column.list *.txt'; #gimme the right inputs
my $listfile = shift@ARGV or die "$usage";
open(LIST, "<$listfile") or die "Couldn't open $listfile\n$usage";
my @list;
while (my $line = <LIST>){
chomp $line;
push @list, $line;
}
close LIST;
my $firstcol = $list[0] or die "Unable to read first column";
while (my $file = shift@ARGV){
open(IN, "<$file");
$file =~ s/.txt$//;
open(OUT, ">$file.filtered.txt");
my @headers;
my @row;
my @sublist;
my %probeset;
while (my $line = <IN>){
chomp $line;
if ($line =~ /^#/){
print OUT "$line\n";
} elsif ($line =~ /^$firstcol/){
@headers = split(/\t/, $line); #defining column names
for (@headers){s/ /_/g}; #changing " " to "_"
for (@headers){s/"//g}; #changing " to nothing
@sublist = intersect(@headers, @list);
print OUT join("\t", @sublist), "\n"; #print header to file
} else {
@row = split(/\t/, $line); # breaking up columns in row
for (@row){s/ /_/g}; #changing " " to "_"
for (@row){s/"//g}; #changing " to nothing
@probeset{@headers} = @row; #assigning keys to row values
print OUT join("\t", @probeset{@sublist}), "\n";
}
}
close IN;
close OUT;
}