-
Notifications
You must be signed in to change notification settings - Fork 199
/
FormatSequences.pm
332 lines (277 loc) · 11.1 KB
/
FormatSequences.pm
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package Bio::JBrowse::Cmd::FormatSequences;
=head1 NAME
Bio::JBrowse::Cmd::FormatSequences - script module to format reference
sequences (backend module for prepare-refseqs.pl)
=cut
use strict;
use warnings;
use base 'Bio::JBrowse::Cmd';
use Pod::Usage;
use File::Spec::Functions qw/ catfile catdir /;
use File::Path 'mkpath';
use POSIX;
use JSON 2;
use JsonFileStorage;
use FastaDatabase;
sub option_defaults {(
out => 'data',
chunksize => 20_000
)}
sub option_definitions {(
"out=s",
"conf=s",
"noseq",
"gff=s",
"chunksize=s",
"fasta=s@",
"refs=s",
"refids=s",
"compress",
"help|h|?",
"nohash"
)}
sub run {
my ( $self ) = @_;
my $seqTrackName = "DNA";
my $refs = $self->opt('refs');
my $compress = $self->opt('compress');
$self->{storage} = JsonFileStorage->new( $self->opt('out'), $self->opt('compress'), { pretty => 0 } );
pod2usage( 'must provide either a --fasta, --gff, or --conf option' )
unless defined $self->opt('gff') || $self->opt('conf') || $self->opt('fasta');
my $chunkSize = $self->opt('chunksize');
$chunkSize *= 4 if $compress;
my %refSeqs;
my $db;
if ( $self->opt('fasta') && @{$self->opt('fasta')} ) {
$db = FastaDatabase->from_fasta( @{$self->opt('fasta')});
die "IDs not implemented for FASTA database" if defined $self->opt('refids');
if ( ! defined $refs && ! defined $self->opt('refids') ) {
$refs = join (",", $db->seq_ids);
}
die "found no sequences in FASTA file" if "" eq $refs;
}
elsif ( $self->opt('gff') ) {
my $gff = $self->opt('gff');
open my $fh, '<', $gff or die "$! reading GFF file $gff";
while ( <$fh> ) {
if ( /^\#\#\s*sequence-region\s+(\S+)\s+(-?\d+)\s+(-?\d+)/i ) { # header line
$refSeqs{$1} = {
name => $1,
start => $2 - 1,
end => int($3),
length => ($3 - $2 + 1)
};
}
elsif( /^##FASTA\s*$/ ) {
# start of the sequence block, pass the filehandle to our fasta database
$db = FastaDatabase->from_fasta( $fh );
last;
}
elsif( /^>/ ) {
# beginning of implicit sequence block, need to seek
# back
seek $fh, -length($_), SEEK_CUR;
$db = FastaDatabase->from_fasta( $fh );
last;
}
}
if ( $db && ! defined $refs && ! defined $self->opt('refids') ) {
$refs = join (",", $db->seq_ids);
}
} elsif ( $self->opt('conf') ) {
my $config = decode_json( do {
local $/;
open my $f, '<', $self->opt('conf') or die "$! reading ".$self->opt('conf');
scalar <$f>
});
eval "require $config->{db_adaptor}; 1" or die $@;
$db = eval {$config->{db_adaptor}->new(%{$config->{db_args}})}
or warn $@;
die "Could not open database: $@" unless $db;
if (my $refclass = $config->{'reference class'}) {
eval {$db->default_class($refclass)};
}
$db->strict_bounds_checking(1) if $db->can('strict_bounds_checking');
}
unless ( defined $self->opt('refids') || defined $refs ) {
die "please specify which sequences to process using the --refs"
." or --refids command line parameters\n";
}
if ( defined $self->opt('refids') ) {
foreach my $refid (split ",", $self->opt('refids')) {
my $seg = $db->segment(-db_id => $refid);
unless( $seg ) {
warn "WARNING: Reference sequence with -db_id '$refid' not found in input.\n";
next;
}
my $refInfo = {
name => $self->refName($seg),
id => $refid, #keep ID for later querying
start => $seg->start - 1,
end => $seg->end,
length => $seg->length
};
unless( $self->opt('noseq') ) {
$self->exportSeqChunks( $refInfo, $chunkSize, $db,
[-db_id => $refid],
$seg->start, $seg->end);
$refInfo->{"seqChunkSize"} = $chunkSize;
}
$refSeqs{ $refInfo->{name} } = $refInfo;
}
}
if ( defined $refs ) {
foreach my $ref (split ",", $refs) {
my ($seg) = my @segments = $db->segment(-name => $ref);
if (! @segments ) {
warn "WARNING: Reference sequence '$ref' not found in input.\n";
next;
} elsif ( @segments > 1 ) {
warn "WARNING: multiple matches for '$ref' found in input, using only the first one.\n";
}
my $refInfo = {
name => $self->refName($seg),
start => $seg->start - 1,
end => $seg->end,
length => $seg->length,
( $compress ? ( 'compress' => 1 ) : () ),
};
unless ($self->opt('noseq')) {
$self->exportSeqChunks( $refInfo, $chunkSize, $db,
[-name => $ref],
$seg->start, $seg->end);
$refInfo->{"seqChunkSize"} = $chunkSize;
}
$refSeqs{ $refInfo->{name} } = $refInfo;
}
}
unless( %refSeqs ) {
warn "No reference sequences found, exiting.\n";
exit;
}
$self->{storage}->modify( 'seq/refSeqs.json',
sub {
#add new ref seqs while keeping the order
#of the existing ref seqs
my $old = shift || [];
my %refs = %refSeqs;
for (my $i = 0; $i < @$old; $i++) {
if( $refs{$old->[$i]->{name}} ) {
$old->[$i] = delete $refs{$old->[$i]->{name}};
}
}
foreach my $name (sort keys %refs) {
push @{$old}, $refs{$name};
}
return $old;
});
if ( $compress ) {
# if we are compressing the sequence files, drop a .htaccess file
# in the seq/ dir that will automatically configure users with
# Apache (and AllowOverride on) to serve the .txt.gz files
# correctly
require GenomeDB;
my $hta = catfile( $self->opt('out'), 'seq', '.htaccess' );
open my $hta_fh, '>', $hta or die "$! writing $hta";
$hta_fh->print( GenomeDB->precompression_htaccess('.txtz','.jsonz') );
}
unless( $self->opt('noseq') ) {
$self->{storage}->modify( 'trackList.json',
sub {
my $trackList = shift;
unless (defined($trackList)) {
$trackList =
{
'formatVersion' => 1,
'tracks' => []
};
}
my $tracks = $trackList->{'tracks'};
my $i;
for ($i = 0; $i <= $#{$tracks}; $i++) {
last if ($tracks->[$i]->{'label'}
eq
$seqTrackName);
}
$tracks->[$i] =
{
'label' => $seqTrackName,
'key' => $seqTrackName,
'type' => "SequenceTrack",
'chunkSize' => $chunkSize,
'urlTemplate' => $self->seqUrlTemplate,
( $compress ? ( 'compress' => 1 ): () ),
};
return $trackList;
});
}
return;
}
###########################
sub refName {
my ( $self, $seg ) = @_;
my $segName = $seg->name;
$segName = $seg->{'uniquename'} if $seg->{'uniquename'};
$segName =~ s/:.*$//; #get rid of coords if any
return $segName;
}
sub openChunkFile {
my ( $self, $refInfo, $chunkNum ) = @_;
my $compress = $self->opt('compress');
my ( $dir, $file ) = $self->opt('nohash')
# old style
? ( catdir( $self->opt('out'), 'seq',
$refInfo->{name}
),
"$chunkNum.txt"
)
# new hashed structure
: ( catdir( $self->opt('out'), 'seq',
$self->_crc32_path( $refInfo->{name} )
),
"$refInfo->{name}-$chunkNum.txt"
);
$file .= 'z' if $compress;
mkpath( $dir );
open my $fh, '>'.($compress ? ':gzip' : ''), catfile( $dir, $file )
or die "$! writing $file";
return $fh;
}
sub _crc32_path {
my ( $self, $str ) = @_;
my $crc = ( $self->{crc} ||= do { require Digest::Crc32; Digest::Crc32->new } )
->strcrc32( $str );
my $hex = lc sprintf( '%08x', $crc );
return catdir( $hex =~ /(.{1,3})/g );
}
sub seqUrlTemplate {
my ( $self ) = @_;
return $self->opt('nohash')
? "seq/{refseq}/" # old style
: "seq/{refseq_dirpath}/{refseq}-"; # new hashed structure
}
sub exportSeqChunks {
my ( $self, $refInfo, $chunkSize, $db, $segDef, $start, $end ) = @_;
$start = 1 if $start < 1;
$db->absolute( 1 ) if $db->can('absolute');
my $chunkStart = $start;
while( $chunkStart <= $end ) {
my $chunkEnd = $chunkStart + $chunkSize - 1;
my $chunkNum = floor( ($chunkStart - 1) / $chunkSize );
my ($seg) = $db->segment( @$segDef,
-start => $chunkStart,
-end => $chunkEnd,
-absolute => 1,
);
unless( $seg ) {
die "Seq export query failed, please inform the developers of this error"
}
$seg->start == $chunkStart
or die "requested $chunkStart .. $chunkEnd; got " . $seg->start . " .. " . $seg->end;
$chunkStart = $chunkEnd + 1;
next unless $seg && $seg->seq && $seg->seq->seq;
$self->openChunkFile( $refInfo, $chunkNum )
->print( $seg->seq->seq );
}
}
1;