-
Notifications
You must be signed in to change notification settings - Fork 199
/
NCFormatter.pm
96 lines (77 loc) · 2.93 KB
/
NCFormatter.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
package Bio::JBrowse::Cmd::NCFormatter;
use base 'Bio::JBrowse::Cmd';
use GenomeDB;
use Bio::JBrowse::ExternalSorter;
sub _format {
my ( $self, %args ) = @_;
my ( $trackLabel, $trackConfig, $feature_stream, $filter ) = @args{qw{ trackLabel trackConfig featureStream featureFilter }};
$filter ||= sub { @_ };
my $types = $self->opt('type');
@$types = split /,/, join ',', @$types;
# The Bio::JBrowse::ExternalSorter will get flattened [chrom, [start, end, ...]]
# arrays from the feature_stream
my $sorter = Bio::JBrowse::ExternalSorter->new(
do {
my $startIndex = $feature_stream->startIndex;
my $endIndex = $feature_stream->endIndex;
sub ($$) {
$_[0]->[0] cmp $_[1]->[0]
||
$_[0]->[1]->[$startIndex] <=> $_[1]->[1]->[$startIndex]
||
$_[1]->[1]->[$endIndex] <=> $_[0]->[1]->[$endIndex];
}
},
$self->opt('sortMem'),
);
my %featureCounts;
while ( my @feats = $feature_stream->next_items ) {
for my $feat ( $filter->( @feats ) ) {
my $chrom = $feat->{seq_id};
$featureCounts{$chrom} += 1;
$feat = $self->transform_feature( $feat );
my $row = [ $chrom,
$feature_stream->flatten_to_feature( $feat ),
$feature_stream->flatten_to_name( $feat ),
];
$sorter->add( $row );
}
}
$sorter->finish();
################################
my $gdb = GenomeDB->new( $self->opt('out') );
my $track = $gdb->getTrack( $trackLabel, { %$trackConfig, type => 'FeatureTrack' }, $trackConfig->{key} )
|| $gdb->createFeatureTrack( $trackLabel,
$trackConfig,
$trackConfig->{key},
);
my $curChrom = 'NONE YET';
my $totalMatches = 0;
while( my $feat = $sorter->get ) {
use Storable ();
unless( $curChrom eq $feat->[0] ) {
$curChrom = $feat->[0];
$track->finishLoad; #< does nothing if no load happening
$track->startLoad( $curChrom,
$self->opt('nclChunk'),
Storable::dclone( $feature_stream->arrayReprClasses ),
);
}
$totalMatches++;
$track->addSorted( $feat->[1] );
# load the feature's name record into the track if necessary
if( my $namerec = $feat->[2] ) {
$track->nameHandler->addName( $namerec );
}
}
$gdb->writeTrackEntry( $track );
# If no features are found, check for mistakes in user input
if( !$totalMatches && @$types ) {
warn "WARNING: No matching features found for @$types\n";
}
}
# stub
sub transform_feature {
return $_[1];
}
1;