Skip to content

Commit

Permalink
give bioperlflattener a default subroutine to use for fetching featur…
Browse files Browse the repository at this point in the history
…e labels, which is actually the label-fetching code moved into there from flatfile-to-json.pl
  • Loading branch information
rbuels committed Feb 21, 2012
1 parent 4150791 commit c4a6a31
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
20 changes: 7 additions & 13 deletions bin/flatfile-to-json.pl
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,13 @@ =head2 BED-SPECIFIC
$nclChunk *= 4 if $compress;
}

#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]->load_id if $_[0]->can('load_id') && defined $_[0]->load_id;
return $_[0]->can('primary_id') ? $_[0]->primary_id : $_[0]->id;
};

my $stream;
my $labelStyle = 1;
if ($gff) {
my $io = Bio::FeatureIO->new(
-format => 'gff',
Expand All @@ -252,7 +243,7 @@ =head2 BED-SPECIFIC
($thinType ? ("-thin_type" => $thinType) : ()),
($thickType ? ("-thick_type" => $thickType) : ()),
);
$labelSub = sub {
$labelStyle = sub {
#label sub for features returned by Bio::FeatureIO::bed
return $_[0]->name;
};
Expand Down Expand Up @@ -283,7 +274,7 @@ =head2 BED-SPECIFIC
{
"idSub" => $idSub,
"label" => ($getLabel || ($autocomplete ne "none"))
? $labelSub : 0,
? $labelStyle : 0,
%style,
},
[], [] );
Expand Down Expand Up @@ -316,6 +307,9 @@ =head2 BED-SPECIFIC
},
);

use Data::Dump;
ddx( \@arrayrepr_classes );

# build a filtering subroutine for the features
my $filter = do {
my @filters;
Expand Down
29 changes: 27 additions & 2 deletions lib/BioperlFlattener.pm
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ sub new {
@curMapHeaders = (@curMapHeaders, @$extraHeaders);

if ($style{label}) {
push @curFeatMap, $style{label};
$style{label} = \&defaultLabelSub unless ref $style{label} eq 'CODE';
push @curFeatMap, $style{label};
push @curMapHeaders, "name";
}

Expand Down Expand Up @@ -156,7 +157,7 @@ sub new {

if ($self->{getLabel} || $self->{getAlias}) {
if ($self->{getLabel} && $self->{getAlias}) {
unshift @nameMap, sub {[unique($style{label}->($_[0]),
unshift @nameMap, sub {[unique( ($style{label} || \&defaultLabelSub)->($_[0]),
$_[0]->get_tag_values("Alias"))]};
} elsif ($self->{getLabel}) {
unshift @nameMap, sub {[$style{label}->($_[0])]};
Expand Down Expand Up @@ -257,4 +258,28 @@ sub unique {
return (grep(defined($_) && !$saw{$_}++, @_));
}


###################

sub defaultLabelSub {
my ( $f ) = @_;
if( $f->can('display_name') and defined( my $dn = $f->display_name )) {
return $dn
}
elsif( $f->can('get_tag_values') ) {
my $n = eval { ($f->get_tag_values('Name'))[0] };
return $n if defined $n;

my $a = eval { ($f->get_tag_values('Alias'))[0] };
return $a if defined $a;
}
elsif( $f->can('attributes') ) {
return $f->attributes('load_id') if defined $f->attributes('load_id');
return $f->attributes('Name') if defined $f->attributes('Name');
return $f->attributes('Alias') if defined $f->attributes('Alias');
}
return;
}


1;

0 comments on commit c4a6a31

Please sign in to comment.