-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotate_parser.pl
More file actions
180 lines (148 loc) · 4.43 KB
/
annotate_parser.pl
File metadata and controls
180 lines (148 loc) · 4.43 KB
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
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long qw(GetOptions);
use Pod::Usage;
# VERSION
=head1 SYNOPSIS
annotate_parser.pl -i kobas_output.txt -o kobas_tabular_output.txt
=head1 DESCRIPTION
Perl file to convert KOBAS annotate output to tabular format
=head1 OPTIONS
=over 8
=item I<-i>
Input file (original KOBAS annotate file)
=item I<-o>
Output file to store converted KOBAS file. If no output path is provided, results will be printed on the console.
=item I<--tsv>
Output file type (tab delimited)
=item I<--csv>
Output file type (comma delimited)
=back
=cut
my $help = 0;
my ($in, $out);
my ($tsv, $csv);
my $state = 0;
my ($Path_state, $GO_state, $GO_slim_state) = (0)x3;
my ($Query, $Gene_id, $Gene_name, $Entrez_id) = ("NA")x4;
my (@All_pathway_ids, @All_GO_ids, @All_GO_slim_ids);
# handle command line options
GetOptions('i=s' => \$in,
'o=s' => \$out,
'tsv' => \$tsv,
'csv' => \$csv,
q(help) => \$help) or pod2usage(q(-verbose) => 1);
# show help
pod2usage(q(-verbose) => 1) if $help;
# check command line options
check_arguments($in, $out);
# input files can be provided comma separated
# all files will be parsed into single output file
my @samples = (split(/,/,$in));
foreach(@samples) {
parse($_);
}
sub parse {
# open file and parse content using flip flop principle
open(my $filehandle,'<',$_[0]) || die "Can´t open $in: $!";
while(my $line = <$filehandle>) {
chomp($line);
if($line =~ m!^/{4}! and $state == 0) {
$state = 1;
}
elsif($line =~ m!^/{4}! and $state == 1) {
# concate final information of arrays
my $Pathway = @All_pathway_ids ? join(";", @All_pathway_ids) : "NA";
my $GO = @All_GO_ids ? join(";", @All_GO_ids) : "NA";
my $GO_slim = @All_GO_slim_ids ? join(";", @All_GO_slim_ids) : "NA";
# output information
# if output path is set, write to file
# otherwise print in console
my $delimiter = $csv ? "," : "\t";
if(defined $out) {
write_file($out, join($delimiter, $Query, $Gene_id, $Gene_name, $Entrez_id, $Pathway, $GO, $GO_slim));
}
else {
print(join($delimiter, $Query, $Gene_id, $Gene_name, $Entrez_id, $Pathway, $GO, $GO_slim) . "\n");
}
# reset variables for next annotation
($Query, $Gene_id, $Gene_name, $Entrez_id) = ("NA")x4;
@All_pathway_ids = ();
@All_GO_ids = ();
@All_GO_slim_ids = ();
# set state
$state = 1;
}
elsif($state) {
# get query id
if($line =~ /^Query:/) {
$Query = (split(/\t/,$line))[1] // "NA";
}
# get gene id
if($line =~ /^Gene:/) {
$Gene_id = (split(/\t/,$line))[1] // "NA";
$Gene_name = (split(/\t/,$line))[2] // "NA";
}
# get entrez gene id
if($line =~ /^Entrez/) {
$Entrez_id = (split(/\t/,$line))[1] // "NA";
}
# get pathway ids
if($line =~ /^Pathway:/ and $Path_state == 0) {
push(@All_pathway_ids, (split(/\t/,$line))[-1] // "NA");
$Path_state = 1
}
elsif($line !~ /^\s/ and $Path_state == 1) {
$Path_state = 0;
}
elsif($Path_state) {
push(@All_pathway_ids, (split(/\t/,$line))[-1] // "NA");
}
# get GO ids
if($line =~ /^GO:/ and $GO_state == 0) {
push(@All_GO_ids, (split(/\t/,$line))[-1] // "NA");
$GO_state = 1
}
elsif($line !~ /^\s/ and $GO_state == 1) {
$GO_state = 0;
}
elsif($GO_state) {
push(@All_GO_ids, (split(/\t/,$line))[-1] // "NA");
}
# get GO slim ids
if($line =~ /^GOslim:/ and $GO_slim_state == 0) {
push(@All_GO_slim_ids, (split(/\t/,$line))[-1] // "NA");
$GO_slim_state = 1
}
elsif($line !~ /^\s/ and $GO_slim_state == 1) {
$GO_slim_state = 0;
}
elsif($GO_slim_state) {
push(@All_GO_slim_ids, (split(/\t/,$line))[-1] // "NA");
}
}
}
close $filehandle;
}
sub check_arguments {
# check if input ('-i') file is provided, otherwise exit program
if(!$_[0]) {
print("No input file provided. Use '-i' tag.\n");
exit;
}
# check if output ('-o') parameter is provided
if(defined $_[1]) {
# check if output file already exists
if(-e $_[1]) {
print("Output file already exists, please provide different output path.\n");
exit;
}
}
}
sub write_file {
# append parsed data to file
open(FH, ">>", $_[0]) || die $!;
print(FH $_[1] . "\n");
close(FH);
}