forked from rec3141/ddmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilization.pl
More file actions
executable file
·242 lines (221 loc) · 7.9 KB
/
utilization.pl
File metadata and controls
executable file
·242 lines (221 loc) · 7.9 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/perl
use warnings;
use strict;
# use diagnostics;
use Encode;
use open qw(:std :utf8);
use Data::Dumper;
# use Bio::DB::Taxonomy;
# my $idx_dir = '/work/bergeys/tables/idx/';
# my ( $nodefile, $namesfile ) = ( "nodes.dmp", "names.dmp" );
# my $db = new Bio::DB::Taxonomy( -source => 'flatfile',
# -nodesfile => $nodefile,
# -namesfile => $namesfile,
# -directory => $idx_dir
# );
my %tablehash;
my %traithash;
my %taxahash;
my %footnotes;
my %caphash;
my $new=0;
my $title;
#read tables into %tablehash
while (<>) {
next if m/^\s*$/; #skip blank lines
chomp; #remove newline
s/\b\s+$//; #remove trailing spaces
my $str = $_;
if ($str =~ m/\#\#\#\#\#/) {
$new=1;
} elsif ($new==1) {
(my @desc) = split(/\s/,$str);
shift(@desc);
$title = shift(@desc);
my $caption = join(" ",@desc);
$caphash{$title} = $caption;
$new=2;
} elsif ($new==2) {
$str =~ s/^\s*\b//; #remove leading spaces
my @taxa;
if ($str =~ m/\t/) {
@taxa = split(/\t/,$str);
} else {
@taxa = split(/ {2,}/,$str);
}
push(@{ $taxahash{$title} },@taxa);
$new=0;
} elsif ($str=~ m/footnote ([a-z][a-z]?): (.*)/) {
# print "footnote $1: $2\n";
$footnotes{$title}{$1} = $2;
} else {
# print nice_string($str),"\n";
push(@{ $tablehash{$title} },$str);
}
}
#find tables which match 'Utilization of:' etc.
#and parse data into new hash of traits
TABLE: foreach my $table (keys %tablehash) {
my $trait;
LINE: foreach my $line (@{ $tablehash{$table} }) {
# print "$line\n";
if ($line =~ m/^(.*?)\:\s*$/) {
$trait = lc($1);
$trait =~ s/\s/_/g;
# print $trait,"\n";
} elsif ($line =~ m/requirement/i) {
push @{ $traithash{$table}{'requirement'} }, $line;
} elsif ($line =~ m/growth/i) {
push @{ $traithash{$table}{'growth'} }, $line;
} elsif ($line =~ m/reduction/i) {
push @{ $traithash{$table}{'reduction_of'} }, $line;
} elsif (defined($trait)) {
if ($line =~ m/^ /) {
$line =~ s/^\s*\b//; #remove leading spaces
push @{ $traithash{$table}{$trait} }, $line;
} else {
undef $trait;
push @{ $traithash{$table}{'characteristic'} }, $line;
}
} else {
push @{ $traithash{$table}{'characteristic'} }, $line;
}
} #end LINE
} #end TABLE
# parse hash of traits into exportable data
my %bytrait; # $bytrait{'fermentation_of'}{'glucose'}{'Table 1'}[0] = '+';
my %byorg; # $byorg{'Table 1'}{'organism'}{'fermentation_of'}{'glucose'} = '+';
foreach my $table (keys %traithash) {
foreach my $trait (keys %{$traithash{$table}}) {
foreach my $line (@{ $traithash{$table}{$trait} }) {
my @traitvals;
if ($line =~ m/\t/) {
@traitvals = split(/\t/,lc($line));
} else {
@traitvals = split(/ {2,}/,lc($line));
}
# print(join("~",@traitvals),"\n");
my $chem = shift(@traitvals);
print nice_string($chem),"\n";
$chem =~ s/[\x{0251}\x{03B1}]/alpha/g; #replace alpha
$chem =~ s/[\x{00DF}\x{03B2}]/beta/g; #replace beta
#deal with commas
my @simtrait = ();
if ($chem =~ m/, /) {
@simtrait = split(/, /,$chem);
foreach my $subchem (@simtrait) {
# $subchem =~ s/ //g;
$subchem =~ s/^\s*\b//; #remove leading spaces
$subchem =~ s/\b\s*$//; #remove trailing spaces
push @{ $bytrait{$trait}{$subchem}{$table} }, @traitvals;
# print "$trait\t$chem\t",join("\t",@traitvals),"\n";
if (scalar(@{ $bytrait{$trait}{$subchem}{$table} }) == scalar(@{$taxahash{$table}})) {
foreach my $taxon (@{$taxahash{$table}}) {
my $val = shift(@{ $bytrait{$trait}{$subchem}{$table} });
if ($val =~ m/([+\x{2013}\x{2212}])([a-z][a-z]?)/) {
$val =~ s/([+\x{2013}\x{2212}])([a-z][a-z]?)/$1!$2/ if defined($footnotes{$table}{$2});
}
# print nice_string($val),"\n";
# $val =~ s/^[\x{2013}\x{2212}]([ ]*)/0$1/; #replace leading -
# $val =~ s/([ ]*)[\x{2013}\x2212]$/0$1/; #replace trailing -
# $val =~ s/^\+([ ]*)/1$1/; #replace leading +
# $val =~ s/([ ]*)\+$/0$1/; #replace trailing +
$byorg{$table}{$taxon}{$trait}{$subchem} = "'$val'";
}
} else {die("lengths don't match in $trait \'$subchem\' ",scalar(@traitvals)," vs ",scalar(@{$taxahash{$table}}),"\n")}
}
} else {
# $chem =~ s/ //g;
push(@{ $bytrait{$trait}{$chem}{$table} }, @traitvals);
# print "$trait\t$chem\t",join("\t",@traitvals),"\n";
if (scalar(@{ $bytrait{$trait}{$chem}{$table} }) == scalar(@{$taxahash{$table}})) {
foreach my $taxon (@{$taxahash{$table}}) {
my $val = shift(@{ $bytrait{$trait}{$chem}{$table} });
if ($val =~ m/([+\x{2013}\x{2212}])([a-z][a-z]?)/) {
$val =~ s/([+\x{2013}\x{2212}])([a-z][a-z]?)/$1!$2/ if defined($footnotes{$table}{$2});
}
# print nice_string($val),"\n";
# $val =~ s/^[\x{2013}\x{2212}](\s*)/neg$1/; #replace leading -
# $val =~ s/(\s*)[\x{2013}\x2212]$/neg$1/; #replace trailing -
# $val =~ s/^\+(\s*)/pos$1/; #replace leading +
# $val =~ s/(\s*)\+$/pos$1/; #replace trailing +
$byorg{$table}{$taxon}{$trait}{$chem} = "'$val'";
}
} else {die("lengths don't match in $trait \'$chem\' ",scalar(@traitvals)," vs ",scalar(@{$taxahash{$table}}),"\n")}
}
#deal with multi-lines
} #end foreach my $line
} #end foreach my $trait
} #end foreach my $table
# no need to do it so it matches import.csv
# print out clean table
# foreach my $trait (keys %bytrait) {
# foreach my $chem (keys %{ $bytrait{$trait} }) {
# foreach my $table (keys %{ $bytrait{$trait}{$chem} }) {
# print "taxonomy\t\t",join("\t",@{ $taxahash{$table} }),"\n";
# print "$trait\t",join("\t",@{ $bytrait{$trait}{$chem}{$table} }),"\n";
# }
# }
# }
# print out clean table
# "taxonomy","taxonomy","taxonomy","bergeys","growth","growth","growth","growth","growth","characteristic","reduction_of","characteristic","characteristic","characteristic","hydrolysis_of","hydrolysis_of","utilization_of","utilization_of","utilization_of","utilization_of","utilization_of","fatty_acid","characteristic"
# "ncbi_taxid","genus","species","BXCII.a.92.","0%_nacl","10%_nacl","4°c","25°c","35°c","barophilic","nitrate","denitrification","fermentation","anaerobic_respiration","gelatin","starch","d-glucose","maltose","salicin","galacturonate","dl-glycerate","polyunsaturated_fatty_acid","g+c_content"
foreach my $table (keys %caphash) {
print "ddmp_src\t$table\t$caphash{$table}\n";
} print "\n";
foreach my $table (keys %footnotes) {
foreach my $fn (sort keys %{$footnotes{$table}}) {
print "ddmp_fntext\t$table\t$fn\t$footnotes{$table}{$fn}\n";
}
} print "\n";
print "ddmp_class\t\t";
foreach my $trait (sort keys %bytrait) {
foreach my $chem (sort keys %{$bytrait{$trait}}) {
print "\t",$trait;
}
} print "\n";
print "ddmp_prop\t\t";
foreach my $trait (sort keys %bytrait) {
foreach my $chem (sort keys %{$bytrait{$trait}}) {
print "\t",$chem;
}
} print "\n";
foreach my $table (sort keys %byorg) {
foreach my $org (sort keys %{ $byorg{$table} }) {
print "ddmp_data\t$table\t$org";
foreach my $trait (sort keys %bytrait) {
foreach my $chem (sort keys %{$bytrait{$trait}}) {
if (defined($byorg{$table}{$org}{$trait}{$chem})) {
if ($byorg{$table}{$org}{$trait}{$chem} eq 'nd') {
print "\tNULL";
} else {
print "\t",$byorg{$table}{$org}{$trait}{$chem};
}
} else {
print "\tNULL";
}
}
}
print "\n";
}
}
sub nice_string {
join("",
map { $_ > 255 ? # if wide character...
sprintf("\\x{%04X}", $_) : # \x{...}
chr($_) =~ /[[:cntrl:]]/ ? # else if control character ...
sprintf("\\x%02X", $_) : # \x..
chr($_) # else as themselves
} unpack("U*", $_[0])); # unpack Unicode characters
}
# print Dumper(%bytrait);
# print Dumper(%taxahash);
# -2 NO growth
# -1 not determined/no data
# 0 no (most not)/minus
# 1 yes (most do)/plus
# 2 variable
# 3
# 4
# 5
# ! footnote