Repository navigation
Expand file tree
/
Copy pathelastic_schema_parse.pl
More file actions
executable file
·174 lines (153 loc) · 4.86 KB
/
Copy pathelastic_schema_parse.pl
File metadata and controls
executable file
·174 lines (153 loc) · 4.86 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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
use JSON;
use File::Slurp;
use POSIX qw(strftime);
sub TO_JSON { return { %{ shift() } }; }
sub usage {
print "es_schema_parse.pl >>> parse elasticsearch schema to create perl hash of fields\n";
print "es_schema_parse.pl -input <path to input schema file> -output <path to output file>\n";
}
# read in parameters
my $input = '';
my $output = '';
GetOptions(
'input=s' => \$input,
'output=s' => \$output
);
unless ($input and $output) {
&usage();
exit 0;
}
my $json = JSON->new();
$json->max_size(0);
$json->allow_nonref;
my $schema_str = read_file($input);
my $schema_obj = $json->decode($schema_str);
my $properties = $schema_obj->{mappings}{metagenome}{properties};
my @prefix = ("job_info_", "job_stat_", "project_", "sample_", "env_package_", "library_", "pipeline_parameters_");
open(OUTF, ">$output") or die "could not open outfile: $output";
# header
print OUTF "###################################\n";
print OUTF "# this is an auto-generated package\n";
print OUTF "# created by: /MG-RAST/bin/elastic_schema_parse.pl\n";
print OUTF "# created on: ".strftime("%Y-%m-%dT%H:%M:%S", gmtime)."\n";
print OUTF "###################################\n";
print OUTF "package ElasticSearch;\n\n";
print OUTF "use strict;\n";
print OUTF "use warnings;\n";
print OUTF "no warnings('once');\n\n";
# ontology terms
print OUTF "our \$ontology = {\n";
print OUTF "\t'sample_biome' => 'biome',\n";
print OUTF "\t'sample_feature' => 'feature',\n";
print OUTF "\t'sample_material' => 'material',\n";
print OUTF "\t'sample_metagenome_taxonomy' => 'metagenome_taxonomy'\n";
print OUTF "};\n\n";
# mixs list
print OUTF "our \$mixs = [\n";
print OUTF "\t'project_project_name',\n";
print OUTF "\t'sample_biome',\n";
print OUTF "\t'sample_feature',\n";
print OUTF "\t'sample_material',\n";
print OUTF "\t'sample_latitude',\n";
print OUTF "\t'sample_longitude',\n";
print OUTF "\t'sample_country',\n";
print OUTF "\t'sample_location',\n";
print OUTF "\t'sample_env_package',\n";
print OUTF "\t'sample_collection_date',\n";
print OUTF "\t'library_investigation_type',\n";
print OUTF "\t'library_seq_meth'\n";
print OUTF "];\n\n";
# id prefixes
print OUTF "our \$ids = {\n";
print OUTF "\t'id' => 'mgm',\n";
print OUTF "\t'project_project_id' => 'mgp',\n";
print OUTF "\t'sample_sample_id' => 'mgs',\n";
print OUTF "\t'library_library_id' => 'mgl',\n";
print OUTF "\t'env_package_env_package_id' => 'mge'\n";
print OUTF "};\n\n";
# field map
print OUTF "our \$fields = {\n";
print OUTF "\tall => 'all_metadata',\n";
print OUTF "\tall_project => 'all_project',\n";
print OUTF "\tall_sample => 'all_sample',\n";
print OUTF "\tall_library => 'all_library',\n";
print OUTF "\tall_env_package => 'all_env_package',\n";
print OUTF "\tmetagenome_id => 'id',\n";
foreach my $pf (@prefix) {
foreach my $prop (keys %$properties) {
if ($prop =~ /^$pf(.*)/) {
my $name = $1;
if (exists($properties->{$prop}{fields}) && exists($properties->{$prop}{fields}{keyword})) {
print OUTF "\t$name => '$prop.keyword',\n";
} else {
print OUTF "\t$name => '$prop',\n";
}
}
}
}
print OUTF "};\n\n";
# prefix map
print OUTF "our \$prefixes = {\n";
foreach my $pf (@prefix) {
print OUTF "\t'$pf' => [\n";
foreach my $prop (keys %$properties) {
if ($prop =~ /^$pf(.*)/) {
my $name = $1;
print OUTF "\t\t'$name',\n";
}
}
print OUTF "\t],\n";
}
print OUTF "};\n\n";
# type map
print OUTF "our \$types = {\n";
print OUTF "\tall => 'text',\n";
print OUTF "\tall_project => 'text',\n";
print OUTF "\tall_sample => 'text',\n";
print OUTF "\tall_library => 'text',\n";
print OUTF "\tall_env_package => 'text',\n";
print OUTF "\tmetagenome_id => 'keyword',\n";
foreach my $pf (@prefix) {
foreach my $prop (keys %$properties) {
if ($prop =~ /^$pf(.*)/ && exists($properties->{$prop}{type})) {
my $name = $1;
print OUTF "\t$name => '".$properties->{$prop}{type}."',\n";
}
}
}
print OUTF "};\n\n";
# taxonomy range numbers
my $taxa_properties = $schema_obj->{mappings}{taxonomy}{properties};
my @nums = ();
foreach my $prop (keys %$taxa_properties) {
my @parts = split(/_/, $prop);
if (scalar(@parts) == 2) {
push @nums, int($parts[1]);
}
}
@nums = sort { $a <=> $b } @nums;
print OUTF "our \$taxa_num = [\n";
foreach my $n (@nums) {
print OUTF "\t$n,\n";
}
print OUTF "];\n\n";
# function range numbers
my $func_properties = $schema_obj->{mappings}{function}{properties};
@nums = ();
foreach my $prop (keys %$func_properties) {
my @parts = split(/_/, $prop);
if (scalar(@parts) == 2) {
push @nums, int($parts[1]);
}
}
@nums = sort { $a <=> $b } @nums;
print OUTF "our \$func_num = [\n";
foreach my $n (@nums) {
print OUTF "\t$n,\n";
}
print OUTF "];\n\n";
print OUTF "1;\n";