Skip to content

Commit

Permalink
Merge pull request #524 from mgcam/upstreamtags_tidyup
Browse files Browse the repository at this point in the history
remove vestiges of decoding with BamIndexDecoder.jar
  • Loading branch information
dozy authored Jun 13, 2018
2 parents 454b0bd + 66ecbe8 commit 817a418
Showing 1 changed file with 56 additions and 91 deletions.
147 changes: 56 additions & 91 deletions lib/npg_qc/autoqc/checks/upstream_tags.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use Carp;
use English qw(-no_match_vars);
use Fatal qw(open close);
use File::Basename;
use File::Spec::Functions qw(catfile catdir);
use List::MoreUtils qw { any };
use File::Spec::Functions qw(catfile);
use List::MoreUtils qw(any);
use Readonly;
use FindBin qw($Bin);

use npg_tracking::Schema;
use npg_qc::autoqc::checks::tag_metrics;
Expand All @@ -23,10 +22,8 @@ with qw(npg_tracking::data::reference::find

our $VERSION = '0';

Readonly::Scalar my $SAMTOOLS_NAME => q[samtools];
Readonly::Scalar my $EXT => q[bam];
Readonly::Scalar my $BARCODE_FILENAME => q[sanger168.tags];
Readonly::Scalar my $BID_JAR_NAME => q[BamIndexDecoder.jar];
Readonly::Scalar my $NUM_BACK_RUNS => 5;
Readonly::Scalar my $MAX_MISMATCHES_DEFAULT => 1;
Readonly::Scalar my $MAX_NO_CALLS_DEFAULT => 0;
Expand All @@ -44,8 +41,6 @@ Readonly::Scalar my $ID_INSTRUMENT_POS => 2;
Readonly::Scalar my $INSTRUMENT_NAME_POS => 3;
Readonly::Scalar my $SLOT_POS => 4;

Readonly::Scalar my $DEFAULT_JAVA_XMX => q{-Xmx1000m};

has '+file_type' => (default => $EXT,);

#####################################################################################################
Expand Down Expand Up @@ -142,15 +137,15 @@ has 'num_back_runs' => ( isa => 'NpgTrackingNonNegativeInt',
);

###############################################################
# max_mismatches: BamIndexDecoder parameter
# max_mismatches: index decoder parameter
###############################################################
has 'max_mismatches' => ( isa => 'NpgTrackingNonNegativeInt',
is => 'rw',
default => $MAX_MISMATCHES_DEFAULT,
);

###############################################################
# max_no_calls: BamIndexDecoder parameter
# max_no_calls: index decoder parameter
###############################################################
has 'max_no_calls' => ( isa => 'NpgTrackingNonNegativeInt',
is => 'rw',
Expand All @@ -169,7 +164,7 @@ sub _build_total_tag0_reads {
my $self = shift;

my $total_reads = 0;
for my $read_count (values %{$self->tag0_BamIndexDecoder_metrics->result->reads_pf_count}) {
for my $read_count (values %{$self->tag0_metrics->result->reads_pf_count}) {
$total_reads += $read_count;
}

Expand All @@ -189,8 +184,8 @@ sub _build_total_tag0_perfect_matches_reads {

my $total_reads = 0;
my $phix_tag_indices = $self->_run_info_data->{runs_info}->[0]->{phix_tagidx};
my $pmc = $self->tag0_BamIndexDecoder_metrics->result->perfect_matches_pf_count;
for my $perfect_matches_tagidx (keys %{$self->tag0_BamIndexDecoder_metrics->result->perfect_matches_pf_count}) {
my $pmc = $self->tag0_metrics->result->perfect_matches_pf_count;
for my $perfect_matches_tagidx (keys %{$self->tag0_metrics->result->perfect_matches_pf_count}) {
next if($phix_tag_indices->{$perfect_matches_tagidx});
$total_reads += $pmc->{$perfect_matches_tagidx};
}
Expand Down Expand Up @@ -290,7 +285,7 @@ sub _build_tag0_index_length {
## no critic (ProhibitTwoArgOpen ErrorHandling::RequireCheckingReturnValueOfEval
my $index_length = 0;
my $bfile = $self->tag0_bam_file;
my $command = q[/bin/bash -c "set -o pipefail && ] . $self->samtools . qq[ view $bfile" ];
my $command = q[/bin/bash -c "set -o pipefail && ] . $self->samtools_cmd . qq[ view $bfile" ];
open my $ph, q(-|), $command or croak qq[Cannot fork '$command', error $ERRNO];
while (my $line = <$ph>) {
if($line =~ /\t(BC|RT):Z:(\S+)/smx) {
Expand Down Expand Up @@ -368,12 +363,6 @@ has 'db_lookup' => ( isa => 'Bool',
default => 1,
);

has 'bid_jar_path' => ( is => 'ro',
isa => 'NpgCommonResolvedPathJarFile',
coerce => 1,
default => $BID_JAR_NAME,
);

has 'run_rows' => ( isa => 'ArrayRef',
is => 'ro',
lazy_build => 1,
Expand Down Expand Up @@ -412,72 +401,45 @@ sub _build__run_info_data {
return $rid;
}

has 'java_xmx_flag' => ( is => 'ro',
isa => 'Str',
default => $DEFAULT_JAVA_XMX,
);

has 'maskflags_name' => ( is => 'ro',
isa => 'NpgCommonResolvedPathExecutable',
coerce => 1,
default => 'bammaskflags',
);

has 'maskflags_cmd' => ( is => 'ro',
isa => 'Str',
lazy_build => 1,
);
sub _build_maskflags_cmd {
my $self = shift;
return $self->maskflags_name . q[ maskneg=107];
}

## no critic qw(NamingConventions::Capitalization NamingConventions::ProhibitMixedCaseSubs)
has 'BamIndexDecoder_cmd' => ( isa => 'Str',
is => 'ro',
required => 0,
lazy_build => 1,
);
sub _build_BamIndexDecoder_cmd {
has 'index_decoder_cmd' => ( isa => 'Str',
is => 'ro',
required => 0,
lazy_build => 1,
);
sub _build_index_decoder_cmd {
my $self = shift;

# my $bid_cmd_template = q[java %s -jar %s COMPRESSION_LEVEL=0 INPUT=%s OUTPUT=/dev/null BARCODE_FILE=%s METRICS_FILE=%s MAX_MISMATCHES=%d MAX_NO_CALLS=%d VALIDATION_STRINGENCY=LENIENT];
#
# my $bid_cmd = sprintf $bid_cmd_template,
# $self->java_xmx_flag,
# $self->bid_jar_path,

my $bid_cmd_template = q[bambi decode --compression-level 0 --output /dev/null --barcode-file %s --metrics-file %s --max-mismatches %d --max-no-calls %d %s];

my $bid_cmd = sprintf $bid_cmd_template,
$self->barcode_filename,
$self->metrics_output_file,
$self->max_mismatches,
$self->max_no_calls,
$self->tag0_bam_file;
$self->barcode_filename,
$self->metrics_output_file,
$self->max_mismatches,
$self->max_no_calls,
$self->tag0_bam_file;

$bid_cmd .= q[ > /dev/null 2>&1]; # all useful output goes to metrics_file

return $bid_cmd;
}

##################################################################################
# tag0_BamIndexDecoder_metrics: tag metrics check object - created with the output
# from a BamIndexDecoder run on the tag#0 bam file
# tag0_metrics: tag metrics check object - created with the output
# from a index decoder run on the tag#0 bam file
##################################################################################
has 'tag0_BamIndexDecoder_metrics' => ( isa => 'Object',
is => 'ro',
lazy_build => 1,
);
sub _build_tag0_BamIndexDecoder_metrics {
has 'tag0_metrics' => ( isa => 'Object',
is => 'ro',
lazy_build => 1,
);
sub _build_tag0_metrics {
my ($self) = @_;

my $bid_cmd = $self->BamIndexDecoder_cmd;
my $bid_cmd = $self->index_decoder_cmd;

system($bid_cmd) == 0 or croak qq[Failed to execute BamIndexDecoder command: $bid_cmd];
system($bid_cmd) == 0 or croak qq[Failed to execute index decoder command: $bid_cmd];

#############################################################
# Read BamIndexDecoder output into a tag_metrics check object
# Read index decoder output into a tag_metrics check object
# for easier manipulation of values
#############################################################
my $tmc=npg_qc::autoqc::checks::tag_metrics->new(
Expand All @@ -488,10 +450,9 @@ sub _build_tag0_BamIndexDecoder_metrics {

return $tmc;
}
## use critic

#############################################################################################
# _tmc_tag_indexes: maps tag sequences to tag indices for tag set used in BamIndexDecoder run
# _tmc_tag_indexes: maps tag sequences to tag indices for tag set used in index decoder run
#############################################################################################
has '_tmc_tag_indexes' => ( isa => 'HashRef',
is => 'ro',
Expand All @@ -500,7 +461,7 @@ has '_tmc_tag_indexes' => ( isa => 'HashRef',
sub _build__tmc_tag_indexes {
my ($self) = @_;
# tag_seq -> tag_idx map for sanger168 tag set
return { reverse %{$self->tag0_BamIndexDecoder_metrics->result->tags} };
return { reverse %{$self->tag0_metrics->result->tags} };
}

## no critic qw(BuiltinFunctions::ProhibitReverseSortBlock)
Expand All @@ -515,7 +476,7 @@ sub _build_unexpected_tags {
my ($self) = @_;

my @ret = ();
my $bid_metrics = $self->tag0_BamIndexDecoder_metrics;
my $bid_metrics = $self->tag0_metrics;
my $tti = $self->_tmc_tag_indexes;

my $tag_seq_runs = $self->_run_info_data->{tag_seq_runs};
Expand Down Expand Up @@ -550,30 +511,12 @@ sub _build_prev_runs {

for my $i (0..($self->num_back_runs - 1)) {
my $tti = $self->_tmc_tag_indexes;
push @ret, _generate_run_info_row($run_info_list->[$i], $self->composition->get_component(0)->position, $self->tag0_BamIndexDecoder_metrics, $tti);
push @ret, _generate_run_info_row($run_info_list->[$i], $self->composition->get_component(0)->position, $self->tag0_metrics, $tti);
}

return \@ret;
}

########################################################################################
# you can override the executable name. May be useful for variants like "samtools_irods"
########################################################################################
has 'samtools_name' => ( is => 'ro',
isa => 'Str',
default => $SAMTOOLS_NAME,
);

has 'samtools' => ( is => 'ro',
isa => 'NpgCommonResolvedPathExecutable',
lazy_build => 1,
coerce => 1,
);
sub _build_samtools {
my ($self) = @_;
return $self->samtools_name;
}

override 'can_run' => sub {
my $self = shift;

Expand Down Expand Up @@ -848,8 +791,30 @@ npg_qc::autoqc::checks::upstream_tags
=over
=item Moose
=item namespace::autoclean
=item Carp
=item English
=item Fatal
=item File::Basename
=item File::Spec::Functions
=item List::MoreUtils
=item Readonly
=item npg_tracking::Schema
=item npg_tracking::data::reference::find
=item npg_common::roles::software_location
=back
=head1 AUTHOR
Expand All @@ -858,7 +823,7 @@ Kevin Lewis, kl2
=head1 LICENSE AND COPYRIGHT
Copyright (C) 2016 GRL
Copyright (C) 2018 GRL
This file is part of NPG.
Expand Down

0 comments on commit 817a418

Please sign in to comment.