forked from GMOD/jbrowse
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflatfile-to-json.pl
executable file
·247 lines (210 loc) · 8.78 KB
/
flatfile-to-json.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
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
243
244
245
246
247
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin qw($Bin);
use lib "$Bin/../lib";
use Getopt::Long;
use Bio::DB::SeqFeature::Store;
use Bio::DB::GFF;
use Bio::FeatureIO;
use JsonGenerator;
use JSON 2;
my $hasSamTools = 1;
eval { require Bio::DB::Sam; };
if ($@) {
$hasSamTools = 0;
}
my ($gff, $gff2, $bed, $bam,
$trackLabel, $key,
$urlTemplate, $subfeatureClasses, $arrowheadClass, $clientConfig, $extraData,
$thinType, $thickType,
$types);
my $autocomplete = "none";
my $outdir = "data";
my $cssClass = "feature";
my $nclChunk = 1000;
my ($getType, $getPhase, $getSubs, $getLabel) = (0, 0, 0, 0);
GetOptions("gff=s" => \$gff,
"gff2=s" => \$gff2,
"bed=s" => \$bed,
"bam=s" => \$bam,
"out=s" => \$outdir,
"tracklabel=s" => \$trackLabel,
"key=s" => \$key,
"cssclass=s" => \$cssClass,
"autocomplete=s" => \$autocomplete,
"getType" => \$getType,
"getPhase" => \$getPhase,
"getSubs" => \$getSubs,
"getLabel" => \$getLabel,
"urltemplate=s" => \$urlTemplate,
"extraData=s" => \$extraData,
"arrowheadClass=s" => \$arrowheadClass,
"subfeatureClasses=s" => \$subfeatureClasses,
"clientConfig=s" => \$clientConfig,
"thinType=s" => \$thinType,
"thicktype=s" => \$thickType,
"type=s@" => \$types,
"nclChunk=i" => \$nclChunk);
my $trackDir = "$outdir/tracks";
my @refSeqs = @{JsonGenerator::readJSON("$outdir/refSeqs.js", [], 1)};
die "run prepare-refseqs.pl first to supply information about your reference sequences" if $#refSeqs < 0;
if (!(defined($gff) || defined($gff2) || defined($bed) || defined($bam)) || !defined($trackLabel)) {
print "The --tracklabel parameter is required\n"
unless defined($trackLabel);
print "You must supply either a --gff, -gff2, --bed, or --bam parameter\n"
unless (defined($gff) || defined($gff2) || defined($bed) || defined($bam));
print <<USAGE;
USAGE: $0 [--gff <gff3 file> | --gff2 <gff2 file> | --bed <bed file> | --bam <bam file>] [--out <output directory>] --tracklabel <track identifier> --key <human-readable track name> [--cssclass <CSS class for displaying features>] [--autocomplete none|label|alias|all] [--getType] [--getPhase] [--getSubs] [--getLabel] [--urltemplate "http://example.com/idlookup?id={id}"] [--extraData <attribute>] [--subfeatureClasses <JSON-syntax subfeature class map>] [--clientConfig <JSON-syntax extra configuration for FeatureTrack>]
--out: defaults to "data"
--cssclass: defaults to "feature"
--autocomplete: make these features searchable by their "label", by their "alias"es, both ("all"), or "none" (default).
--getType: include the type of the features in the json
--getPhase: include the phase of the features in the json
--getSubs: include subfeatures in the json
--getLabel: include a label for the features in the json
--urltemplate: template for a URL that clicking on a feature will navigate to
--arrowheadClass: CSS class for arrowheads
--subfeatureClasses: CSS classes for each subfeature type, in JSON syntax
e.g. '{"CDS": "transcript-CDS", "exon": "transcript-exon"}'
--clientConfig: extra configuration for the client, in JSON syntax
e.g. '{"css": "background-color: black;", "histScale": 5}'
--type: only process features of the given type
--nclChunk: NCList chunk size; if you get "json text or perl structure exceeds maximum nesting level" errors, try setting this lower (default: $nclChunk)
--extraData: a map of feature attribute names to perl subs that extract information from the feature object
e.g. '{"protein_id" : "sub {shift->attributes(\"protein_id\");} "}'
USAGE
exit(1);
}
#default label-extracting function, for GFF
my $labelSub = sub {
return $_[0]->display_name if ($_[0]->can('display_name') && defined($_[0]->display_name));
if ($_[0]->can('attributes')) {
return $_[0]->attributes('load_id') if $_[0]->attributes('load_id');
return $_[0]->attributes('Alias') if $_[0]->attributes('Alias');
}
#return eval{$_[0]->primary_tag};
};
my $idSub = sub {
return $_[0]->load_id if ($_[0]->can('load_id') && defined($_[0]->load_id));
return $_[0]->can('primary_id') ? $_[0]->primary_id : $_[0]->id;
};
my $streaming = 0;
my $shareSubs = 0;
my ($db, $stream);
if ($gff) {
$db = Bio::DB::SeqFeature::Store->new(-adaptor => 'memory',
-dsn => $gff);
} elsif ($gff2) {
$db = Bio::DB::GFF->new(-adaptor => 'memory',
-gff => $gff2);
} elsif ($bed) {
$stream = Bio::FeatureIO->new(-format => 'bed', -file => $bed,
($thinType ? ("-thin_type" => $thinType) : ()),
($thickType ? ("-thick_type" => $thickType) : ()) );
$streaming = 1;
$shareSubs = 1;
$labelSub = sub {
#label sub for features returned by Bio::FeatureIO::bed
return $_[0]->name;
};
} elsif ($bam){
if (! $hasSamTools) {
die "install Bio::DB::Sam in order to use BAM files";
}
$db = Bio::DB::Sam->new('-bam' => $bam);
} else {
die "please specify -gff, -gff2, -bed or -bam";
}
mkdir($outdir) unless (-d $outdir);
mkdir($trackDir) unless (-d $trackDir);
my %style = ("autocomplete" => $autocomplete,
"type" => $getType,
"phase" => $getPhase,
"subfeatures" => $getSubs,
"class" => $cssClass,
"label" => ($getLabel || ($autocomplete ne "none")) ?
$labelSub : 0,
"idSub" => $idSub,
"key" => defined($key) ? $key : $trackLabel,
"urlTemplate" => $urlTemplate,
"arrowheadClass" => $arrowheadClass,
"clientConfig" => $clientConfig);
if ($bam) {
$style{noId} = 1;
}
$style{subfeature_classes} = JSON::from_json($subfeatureClasses)
if defined($subfeatureClasses);
$style{clientConfig} = JSON::from_json($clientConfig)
if defined($clientConfig);
$style{extraData} = JSON::from_json($extraData)
if defined($extraData);
my %perChromGens;
foreach my $seqInfo (@refSeqs) {
$perChromGens{$seqInfo->{"name"}} = JsonGenerator->new($trackLabel,
$seqInfo->{"name"},
\%style, [], [],
$shareSubs);
}
if ($streaming) {
my $jsonGen;
while (my $feat = $stream->next_feature()) {
$jsonGen = $perChromGens{$feat->seq_id};
#ignore feature unless we already know about its ref seq
next unless $jsonGen;
$jsonGen->addFeature($feat);
}
}
my $totalMatches = 0;
foreach my $seqInfo (@refSeqs) {
my $seqName = $seqInfo->{"name"};
mkdir("$trackDir/$seqName") unless (-d "$trackDir/$seqName");
my $jsonGen = $perChromGens{$seqName};
unless ($streaming) {
print "\nworking on seq $seqName\n";
my @queryArgs = ("-seq_id" => $seqName);
if (defined($types)) {
@queryArgs = (@queryArgs, "-types" => $types);
}
if ($bam) {
$db->fetch($seqName, sub {$jsonGen->addFeature($_[0])});
} else {
my @features = $db->features(@queryArgs);
$jsonGen->addFeature($_) foreach (@features);
}
}
next if $jsonGen->featureCount == 0;
$totalMatches += $jsonGen->featureCount;
print $seqName . "\t" . $jsonGen->featureCount . "\n";
$jsonGen->generateTrack("$trackDir/$seqName/$trackLabel/", 1000, $nclChunk, $seqInfo->{"start"}, $seqInfo->{"end"});
JsonGenerator::modifyJSFile("$outdir/trackInfo.js", "trackInfo",
sub {
my $trackList = shift;
my $i;
for ($i = 0; $i <= $#{$trackList}; $i++) {
last if ($trackList->[$i]->{'label'} eq $trackLabel);
}
$trackList->[$i] =
{
'label' => $trackLabel,
'key' => $style{"key"},
'url' => "$trackDir/{refseq}/$trackLabel/trackData.json",
'type' => "FeatureTrack",
};
return $trackList;
});
delete $perChromGens{$seqName};
}
# If no features are found, check for mistakes in user input
if(!$totalMatches && defined($types)) {
print STDERR "No matches found for types\n";
exit(1);
}
=head1 AUTHOR
Mitchell Skinner E<lt>mitch_skinner@berkeley.eduE<gt>
Copyright (c) 2007-2009 The Evolutionary Software Foundation
This package and its accompanying libraries are free software; you can
redistribute it and/or modify it under the terms of the LGPL (either
version 2.1, or at your option, any later version) or the Artistic
License 2.0. Refer to LICENSE for the full license text.
=cut