<commit_before>package Sanger::CGP::Star::Implement;
##########LICENCE ##########
#Copyright (c) 2015 Genome Research Ltd.
###
#Author: Cancer Genome Project <<EMAIL>>
###
#This file is part of cgpRna.
###
#cgpRna is free software: you can redistribute it and/or modify it under
#the terms of the GNU Affero General Public License as published by the
#Free Software Foundation; either version 3 of the License, or (at your
#option) any later version.
###
#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
#General Public License for more details.
###
#You should have received a copy of the GNU Affero General Public
#License along with this program. If not, see
#<http://www.gnu.org/licenses/>.
###
#1. The usage of a range of years within a copyright statement contained
#within this distribution should be interpreted as being equivalent to a
#list of years including the first and last year specified and all
#consecutive years between them. For example, a copyright statement that
#reads ‘Copyright (c) 2005, 2007- 2009, 2011-2012’ should be interpreted
#as being identical to a statement that reads ‘Copyright (c) 2005, 2007,
#2008, 2009, 2011, 2012’ and a copyright statement that reads ‘Copyright
#(c) 2005-2012’ should be interpreted as being identical to a statement
#that reads ‘Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010, 2011,
#2012’."
##########LICENCE ##########
##########
use strict;
use warnings FATAL => 'all';
use autodie qw(:all);
use English qw( -no_match_vars );
use Const::Fast qw(const);
use File::Which qw(which);
use File::Spec;
use File::Path qw(remove_tree make_path);
use File::Basename;
use File::Copy;
use Cwd;
use FindBin qw($Bin);
use Capture::Tiny qw(capture capture_stdout);
use PCAP::Cli;
use PCAP::Threaded;
use PCAP::Bwa::Meta;
use PCAP::Bam;
use Sanger::CGP::CgpRna;

use Data::Dumper;

const my $BAMFASTQ => q{ exclude=QCFAIL,SECONDARY,SUPPLEMENTARY T=%s S=%s O=%s O2=%s gz=1 level=1 F=%s F2=%s filename=%s};
const my $BAMFASTQ_ANALYSIS => q{ exclude=SECONDARY,SUPPLEMENTARY T=%s S=%s O=%s O2=%s gz=1 level=1 F=%s F2=%s filename=%s};
const my $FUSIONS_FILTER => q{ -i %s -s %s -n %s -o %s -p star};
const my $STAR_MAX_CORES => 16;
const my $STAR_DEFAULTS_SECTION => 'star-parameters';
const my $STAR_FUSION_SECTION => 'star-fusion-parameters';
const my $STAR => q{ %s %s --readFilesIn %s };
const my $STAR_FUSION => q{ %s --chimeric_out_sam %s --chimeric_junction %s --ref_GTF %s --min_novel_junction_support 10 --min_alt_pct_junction 10.0 --out_prefix %s };
const my $SAMTOBAM => q{ view -bS %s > %s };
const my $BAMSORT => q{ I=%s fixmate=1 inputformat=bam level=1 tmpfile=%s/tmp O=%s inputthreads=%s outputthreads=%s};
const my $HD_LINE => '@HD	VN:1.4	SO:unsorted';

sub check_input {
	my $options = shift;

  my $fusion_mode;
	
	if(exists $options->{'fusion_mode'}){
	  $fusion_mode = $options->{'fusion_mode'};
	}

	my $ref_data = $options->{'refdataloc'};
	my $species = $options->{'species'};
	my $ref_build = $options->{'referencebuild'};
	my $gene_build = $options->{'genebuild'};
	my $ref_build_loc = File::Spec->catdir($ref_data, $species, $ref_build);

	# Check the gtf and normal fusions files exist
	PCAP::Cli::file_for_reading('gtf-file', File::Spec->catfile($ref_build_loc, 'star', $gene_build, $options->{'gtffilename'}));

	if($fusion_mode){
	  PCAP::Cli::file_for_reading('normals-list',File::Spec->catfile($ref_build_loc,'cgpRna',$options->{'normalfusionslist'}));
	}
	
	my $input_meta = PCAP::Bwa::Meta::files_to_meta($options->{'tmp'}, $options->{'raw_files'}, $options->{'sample'});
	
	$options->{'meta_set'} = $input_meta;
	
	# Check the input data.
	$options->{'max_split'} = scalar @{$input_meta};
	
	my $fq = 0;
	my $fqgz = 0;
	my $bam = 0;
	
	# If the input is fastq need to check whether there is a mixture of gzipped and uncompressed fastqs.
	
	for my $input(@{$input_meta}) {
		if($input->fastq) {
			if($input->fastq =~ m/\.gz$/) {
				$fqgz = 1;
			}
			else{
				$fq = 1;
			}
		}
		else{
			$bam = 1;
		}
	}
	
	# If BAMs have been detected set flag so that they will be converted to pairs of fastq files in the prepare subroutine
	if($bam == 1){
		$options->{'bam'} = 1;
	}
	else{
		# If a mixture of gzipped and uncompressed fastqs, set flag to gzip the plain fastq/fq files in the prepare subroutine
		if($fq == $fqgz){
			$options->{'mixedfq'} = 1;
		}
	}
	
	return 1;
}

sub filter_fusions {
	my $options = shift;
	
	my $tmp = $options->{'tmp'};
	return 1 if PCAP::Threaded::success_exists(File::Spec->catdir($tmp, 'progress'), 0);
	
	my $sample = $options->{'sample'};
	my $star_outdir = File::Spec->catdir($options->{'tmp'}, 'star');
	my $fusions_file = File::Spec->catfile($star_outdir, "$sample.fusion_candidates.txt");
	die "The star fusion output files are missing, please run the starfusion step prior to filter.\n" unless(-e $fusions_file);

	my $normals_file = File::Spec->catfile($options->{'refdataloc'},$options->{'species'},$options->{'referencebuild'},'cgpRna',$options->{'normalfusionslist'});

	my $command = "$^X ";
	$command .= _which('filter_fusions.pl');
	$command .= sprintf $FUSIONS_FILTER, 	$fusions_file,
						$sample,
						$normals_file,
						$options->{'outdir'};

	PCAP::Threaded::external_process_handler(File::Spec->catdir($tmp, 'logs'), $command, 0);
	PCAP::Threaded::touch_success(File::Spec->catdir($tmp, 'progress'), 0);
	return 1;
}

sub format_rg_tags {
  my $options = shift;

  my $sample = $options->{'sample'};
  
  # Format the PU RG tag if the npg_run and lane_pos parameters have been provided
  $options->{'PU'} = $options->{'npg'}."_".$options->{'lane_pos'} if(defined $options->{'npg'} && $options->{'lane_pos'});
  
  # Check the input data
	my $input_meta = $options->{'meta_set'};<commit_msg>easier to set PU tag when using star_mapping.pl<commit_after>package Sanger::CGP::Star::Implement;
##########LICENCE ##########
#Copyright (c) 2015 Genome Research Ltd.
###
#Author: Cancer Genome Project <<EMAIL>>
###
#This file is part of cgpRna.
###
#cgpRna is free software: you can redistribute it and/or modify it under
#the terms of the GNU Affero General Public License as published by the
#Free Software Foundation; either version 3 of the License, or (at your
#option) any later version.
###
#This program is distributed in the hope that it will be useful, but
#WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
#General Public License for more details.
###
#You should have received a copy of the GNU Affero General Public
#License along with this program. If not, see
#<http://www.gnu.org/licenses/>.
###
#1. The usage of a range of years within a copyright statement contained
#within this distribution should be interpreted as being equivalent to a
#list of years including the first and last year specified and all
#consecutive years between them. For example, a copyright statement that
#reads ‘Copyright (c) 2005, 2007- 2009, 2011-2012’ should be interpreted
#as being identical to a statement that reads ‘Copyright (c) 2005, 2007,
#2008, 2009, 2011, 2012’ and a copyright statement that reads ‘Copyright
#(c) 2005-2012’ should be interpreted as being identical to a statement
#that reads ‘Copyright (c) 2005, 2006, 2007, 2008, 2009, 2010, 2011,
#2012’."
##########LICENCE ##########
##########
use strict;
use warnings FATAL => 'all';
use autodie qw(:all);
use English qw( -no_match_vars );
use Const::Fast qw(const);
use File::Which qw(which);
use File::Spec;
use File::Path qw(remove_tree make_path);
use File::Basename;
use File::Copy;
use Cwd;
use FindBin qw($Bin);
use Capture::Tiny qw(capture capture_stdout);
use PCAP::Cli;
use PCAP::Threaded;
use PCAP::Bwa::Meta;
use PCAP::Bam;
use Sanger::CGP::CgpRna;

use Data::Dumper;

const my $BAMFASTQ => q{ exclude=QCFAIL,SECONDARY,SUPPLEMENTARY T=%s S=%s O=%s O2=%s gz=1 level=1 F=%s F2=%s filename=%s};
const my $BAMFASTQ_ANALYSIS => q{ exclude=SECONDARY,SUPPLEMENTARY T=%s S=%s O=%s O2=%s gz=1 level=1 F=%s F2=%s filename=%s};
const my $FUSIONS_FILTER => q{ -i %s -s %s -n %s -o %s -p star};
const my $STAR_MAX_CORES => 16;
const my $STAR_DEFAULTS_SECTION => 'star-parameters';
const my $STAR_FUSION_SECTION => 'star-fusion-parameters';
const my $STAR => q{ %s %s --readFilesIn %s };
const my $STAR_FUSION => q{ %s --chimeric_out_sam %s --chimeric_junction %s --ref_GTF %s --min_novel_junction_support 10 --min_alt_pct_junction 10.0 --out_prefix %s };
const my $SAMTOBAM => q{ view -bS %s > %s };
const my $BAMSORT => q{ I=%s fixmate=1 inputformat=bam level=1 tmpfile=%s/tmp O=%s inputthreads=%s outputthreads=%s};
const my $HD_LINE => '@HD	VN:1.4	SO:unsorted';

sub check_input {
	my $options = shift;

  my $fusion_mode;
	
	if(exists $options->{'fusion_mode'}){
	  $fusion_mode = $options->{'fusion_mode'};
	}

	my $ref_data = $options->{'refdataloc'};
	my $species = $options->{'species'};
	my $ref_build = $options->{'referencebuild'};
	my $gene_build = $options->{'genebuild'};
	my $ref_build_loc = File::Spec->catdir($ref_data, $species, $ref_build);

	# Check the gtf and normal fusions files exist
	PCAP::Cli::file_for_reading('gtf-file', File::Spec->catfile($ref_build_loc, 'star', $gene_build, $options->{'gtffilename'}));

	if($fusion_mode){
	  PCAP::Cli::file_for_reading('normals-list',File::Spec->catfile($ref_build_loc,'cgpRna',$options->{'normalfusionslist'}));
	}
	
	my $input_meta = PCAP::Bwa::Meta::files_to_meta($options->{'tmp'}, $options->{'raw_files'}, $options->{'sample'});
	
	$options->{'meta_set'} = $input_meta;
	
	# Check the input data.
	$options->{'max_split'} = scalar @{$input_meta};
	
	my $fq = 0;
	my $fqgz = 0;
	my $bam = 0;
	
	# If the input is fastq need to check whether there is a mixture of gzipped and uncompressed fastqs.
	
	for my $input(@{$input_meta}) {
		if($input->fastq) {
			if($input->fastq =~ m/\.gz$/) {
				$fqgz = 1;
			}
			else{
				$fq = 1;
			}
		}
		else{
			$bam = 1;
		}
	}
	
	# If BAMs have been detected set flag so that they will be converted to pairs of fastq files in the prepare subroutine
	if($bam == 1){
		$options->{'bam'} = 1;
	}
	else{
		# If a mixture of gzipped and uncompressed fastqs, set flag to gzip the plain fastq/fq files in the prepare subroutine
		if($fq == $fqgz){
			$options->{'mixedfq'} = 1;
		}
	}
	
	return 1;
}

sub filter_fusions {
	my $options = shift;
	
	my $tmp = $options->{'tmp'};
	return 1 if PCAP::Threaded::success_exists(File::Spec->catdir($tmp, 'progress'), 0);
	
	my $sample = $options->{'sample'};
	my $star_outdir = File::Spec->catdir($options->{'tmp'}, 'star');
	my $fusions_file = File::Spec->catfile($star_outdir, "$sample.fusion_candidates.txt");
	die "The star fusion output files are missing, please run the starfusion step prior to filter.\n" unless(-e $fusions_file);

	my $normals_file = File::Spec->catfile($options->{'refdataloc'},$options->{'species'},$options->{'referencebuild'},'cgpRna',$options->{'normalfusionslist'});

	my $command = "$^X ";
	$command .= _which('filter_fusions.pl');
	$command .= sprintf $FUSIONS_FILTER, 	$fusions_file,
						$sample,
						$normals_file,
						$options->{'outdir'};

	PCAP::Threaded::external_process_handler(File::Spec->catdir($tmp, 'logs'), $command, 0);
	PCAP::Threaded::touch_success(File::Spec->catdir($tmp, 'progress'), 0);
	return 1;
}

sub format_rg_tags {
	my $options = shift;

	my $sample = $options->{'sample'};

	# Format the PU RG tag if the npg_run and lane_pos parameters have been provided
	if(defined $options->{'npg'} && defined $options->{'lane_pos'}) {
		$options->{'PU'} = $options->{'npg'}."_".$options->{'lane_pos'};
	} elsif (defined $options->{'npg'}) { # Use ngp_run alone if lane_pos is not defined, which allows users to set PU tag more easily.
		$options->{'PU'} = $options->{'npg'};
	}

	# Check the input data
	my $input_meta = $options->{'meta_set'};
{'fraction_non_alphanumeric': 0.16592901878914404, 'fraction_numerical': 0.0207098121085595, 'mean_word_length': 4.949329359165425, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 120, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4894969', 'n_tokens_mistral': 4622, 'n_tokens_neox': 4183, 'n_words': 1291}