-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparseByTax.pl
executable file
·136 lines (126 loc) · 3.16 KB
/
parseByTax.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
#!/usr/bin/perl
# Script: parseByTax.pl
# Description: Trims an "out_table" file in accordance with a specific set of organisms
# Author: Steven Ahrendt
# email: [email protected]
# Date: 04.02.2014
##################################
use warnings;
use strict;
use Getopt::Long;
use Data::Dumper;
use lib '/rhome/sahrendt/Scripts/';
use SeqAnalysis;
#####-----Global Variables-----#####
my $input;
my ($help,$verb);
my %out_table;
my $sort;
my @master = rankMaster(); # array of refs: [0] is order (array ref) and [1] is org=>type pair (hash ref)
my @orgs = qw(Amac Bden Cang Clat Gpro Hpol OrpC PirE Rall Spun Ncra Spom Scer Pgra Umay Ccin Rory Crev Pbla Ccor);
my $other_tax; # additional taxa to add to @orgs
my $fullName; # displays full organism name in output (default is abbreviation)
my $selection; # user-provided subset of gene names (default is all)
GetOptions ('i|input=s' => \$input,
'other=s' => \$other_tax,
'select=s' =>\$selection,
'fullName' => \$fullName,
'h|help' => \$help,
'sort' => \$sort,
'v|verbose' => \$verb);
my $usage = "Usage: parseByTax.pl -i input\n";
die $usage if $help;
die "No input.\n$usage" if (!$input);
#####-----Main-----#####
open(my $fh,"<",$input) or die "Can't open $input: $!\n";
my $lc = 0;
my @gene_names;
while (my $line = <$fh>)
{
chomp $line;
my @data = split(/\s+/,$line);
if($lc == 0)
{
@gene_names = @data;
}
else
{
for(my $i=0;$i<scalar(@gene_names); $i++)
{
$out_table{$data[0]}{$gene_names[$i]} = $data[$i];
}
}
$lc++;
}
close($fh);
if ($selection)
{
my @new_gene_names = split(/,/,$selection);
@gene_names = @new_gene_names;
unshift (@gene_names,"Org");
}
#print join("-",@gene_names),"\n";
#print Dumper \%out_table;
my $master_rank = $master[1]; # hash ref
my $master_order = $master[0]; # array ref
if($other_tax)
{
my @tmp = split(/,/,$other_tax);
foreach my $tax (@tmp)
{
push @orgs,$tax;
}
}
@orgs = keys(%{$master_rank}) if ($sort);
print join("\t",@gene_names),"\n";
shift @gene_names;
#print $master_rank->{"Bden"},"\n";
#print indexOf($master_rank->{"Bden"},$master_order),"\n";
## Sorting/printing function
foreach my $org (sort {indexOf($master_rank->{$a}{"Group"},$master_order) <=> indexOf($master_rank->{$b}{"Group"},$master_order)} keys %{$master_rank})
{
# print "[$org]\n";
next if(!(isPresent(\@orgs,$org)));
#print indexOf($master_rank->{$org},$master_order),"\t";
#print $master_rank->{$org},"\t";
if($fullName)
{
print $master_rank->{$org}{"FullName"},"\t";
}
else
{
print $org,"\t";
}
foreach my $gene (@gene_names)
{
if(exists $out_table{$org})
{
print $out_table{$org}{$gene},"\t";
}
else
{
print "--\t";
}
}
print "\n";
}
warn "Done.\n";
exit(0);
#####-----Subroutines-----#####
sub isPresent {
my @ar = @{shift @_};
my $item = shift @_;
my $found = 0;
while(!$found && scalar(@ar))
{
my $tmp = shift @ar;
$found = 1 if ($item eq $tmp);
}
return $found;
}
sub indexOf {
my $search_for = shift @_;
my @array = @{shift @_};
my( $index )= grep { $array[$_] eq $search_for } 0..$#array;
return $index;
}