<commit_before>#!/usr/bin/env bash

# ------------------------------------------------------------------------------
# 
# Author: <NAME>
# Email: <EMAIL>
# Version: 1.0.1
# Description: calculates global centrality metrics
# 
# ------------------------------------------------------------------------------



# ENV VAR ======================================================================

export LC_ALL=C

# DEPENDENCIES =================================================================

function join_by { local IFS="$1"; shift; echo "$*"; }

# INPUT ========================================================================

# Help string
helps="
 usage: ./global_centrality.sh	[-hidf][-r regfile]
				-c csList -o outFile [BEDFILE]...

 Description:
  Calculate global centrality metrics.

 Mandatory arguments:
  BEDFILE	Bed file(s). Expected to be ordered per condition.
  -c csList	Cutsite list file.
  -o outFile	Output matrix file.

 Optional arguments:
  -h		Show this help page.
  -d		Debug mode: write out intermediate results.
  -r regfile	Path to bedfile, containing regions to be assigned to.
"

# Default options
debug=0

# Parse options
while getopts hdfr:c:o: opt "${bedfiles[@]}"; do
	case $opt in
		h)
			echo -e "$helps\n"
			exit 0
		;;
		d)
			debug=1
		;;
		r)
			if [ -e $OPTARG ]; then
				regFile=$OPTARG
			else
				msg="!!! Invalid -r option, file not found.\n    File: $OPTARG"
				echo -e "$helps\n$msg"
				exit 1
			fi
		;;
		c)
			if [ -e $OPTARG ]; then
				csList=$OPTARG
			else
				msg="!!! Invalid -c option, file not found.\n    File: $OPTARG"
				echo -e "$helps\n$msg"
				exit 1
			fi
		;;
		o)
			outFile=$OPTARG
		;;
	esac
done

# Check mandatory options
if [ -z "$csList" ]; then
	msg="!!! Missing mandatory -c option."
	echo -e "$helps\n$msg"
	exit
fi
if [ -z "$outFile" ]; then
	msg="!!! Missing mandatory -o option."
	echo -e "$helps\n$msg"
	exit
fi

# Read bedfile paths
shift $(($OPTIND - 1))
bedfiles=()
for bf in $*; do
	if [ -e $bf ]; then
		bedfiles+=("$bf")
	else
		msg="!!! Invalid bedfile, file not found.\n    File: $bf"
		echo -e " $helps\n$msg"
		exit 1
	fi
done

if [ 0 -eq ${#bedfiles[@]} ]; then
	msg="!!! No bedfile was specified!\n"
	echo -e " $helps\n$msg"
	exit 1
fi

# Library directory
libdir="`dirname ${BASH_SOURCE}`/../lib/"

# RUN ==========================================================================

# Assign to regions ------------------------------------------------------------
# If a list of regions is specified, assign them to the reads
# Then, treat them as "chromosome" and run the analysis on them

if [ -n "$regFile" ]; then
	echo -e " · Assigning to regions of interest..."

	rois2chr='{
		OFS=FS="\t"
		nrois = split($6, rois, " ");
		for (i = 1; i <= nrois; i++) {
			print rois[i] OFS $2 OFS $3 OFS $4 OFS $5 OFS
		}
	}'

	# Make temporary bedfiles
	for bfi in $(seq 0 `bc <<< "${#bedfiles[@]} - 1"`); do
		bf=${bedfiles[$bfi]}

		# Assign bedfile rows to ROIs
		echo -e " >> Working on $bf ..."

		# Use ROIs as chromosomes
		echo -e " >>> Using regions as chromosomes..."
		$libdir/bed-tools-gg/bed_addROIs.py $regFile $bf | awk "$rois2chr" \
			> $bf".regions.tmp"

		# Use tmp file with fake chromosomes
		bedfiles[$bfi]=$bf".regions.tmp"
	done

	# Make temporary cutsite list
	echo -e " >> Working on $csList ..."
	$libdir/bed-tools-gg/bed_addROIs.py $regFile $csList | awk "$rois2chr" \
		> $csList".regions.tmp"
	csList=$csList".regions.tmp"
fi

# Identify regions -------------------------------------------------------------
echo -e " · Identifying regions..."

chr_list=""
for bf in ${bedfiles[@]}; do
	chr_list=$chr_list" "`cat $bf | sed 1d | cut -f 1 | sort | uniq`
done
chr_list=`echo $chr_list | tr ' ' '\n' | sort | uniq | tr '\n' ' '`
IFS=' ' read -r -a chr_list <<< "$chr_list"

# Generate cumulative probability distribution matrix --------------------------
# One chromosome per row, one condition per column.
echo -e " · Preparing matrices..."

# Default empty matrix
matrix_crs=""
matrix_rcs=""

# Cycle over chromosomes
for chr in ${chr_list[@]}; do
	echo -e " >>> Working on $chr..."

	# Number of cutsite in the chromosome
	ncs=`cat $csList | awk -v chr=$chr '$1 == chr' | wc -l`

	if [ 0 -eq $ncs ]; then
		msg="!!! No cutsites found in $chr.\n    Please re-run without $chr."
		echo -e "$msg"
		exit 1
	fi

	# Array of normalized counts
	# prs: ProbabilitieS
	# crs: Cumulative of the RatioS
	# rcs: Ratio of the CumulativeS
	# std: STandard Deviation
	# ffs: Fano FactorS
	# cov: Coefficients Of Variation
	prs=()
	crs=(0)
	rcs=(0)
	std=()
	ffs=()
	cov=()
	counts=(0)
	totals=(0)
	for i in $(seq 0 `expr ${#bedfiles[@]} - 1`); do
		bf=${bedfiles[$i]}

		# Select only current chromosome rows
		bfchr=`cat $bf | sed 1d | awk -v chr=$chr '$1 == chr'`
		
		# Number of reads in the condition
		tot=`cat $bf | sed 1d | cut -f 5 | paste -sd+ | bc`
		
		if [ 0 -eq $tot ]; then
			msg="!!! No reads found in condition #$i.\n"
			msg="$msg    Please re-run without the following bedFile: $bf"
			echo -e "$msg"
			exit 1
		fi

		# Number of reads in the condition in the chromosome
		n=`echo "$bfchr" | cut -f 5 | paste -sd+ | bc`
		if [ -z "$n" ]; then
			n=0
		fi

		# if [ -z "$n" ]; then
		# 	msg="!!! No reads found in condition #$i on $chr.\n"
		# 	msg="$msg    Please re-run without the following bedFile: $bf\n"
		# 	msg="$msg    Or without $chr."
		# 	echo -e "$msg"
		# 	exit 1
		# fi
		
		# Probability ----------------------------------------------------------

		r=`bc -l <<< "$n / $tot / $ncs"`
		prs+=($r)

		# Ratio of the sums ----------------------------------------------------

		# Prepare ratios
		prev_n=${counts[${#counts[@]}-1]}
		prev_t=${totals[${#totals[@]}-1]}
		r=`bc -l <<< "($n + $prev_n) / ($tot + $prev_t) / $ncs"`
<commit_msg>Compatible with updated bed-tools-gg library.<commit_after>#!/usr/bin/env bash

# ------------------------------------------------------------------------------
# 
# Author: <NAME>
# Email: <EMAIL>
# Version: 1.0.1
# Description: calculates global centrality metrics
# 
# ------------------------------------------------------------------------------



# ENV VAR ======================================================================

export LC_ALL=C

# DEPENDENCIES =================================================================

function join_by { local IFS="$1"; shift; echo "$*"; }

# INPUT ========================================================================

# Help string
helps="
 usage: ./global_centrality.sh	[-hidf][-r regfile]
				-c csList -o outFile [BEDFILE]...

 Description:
  Calculate global centrality metrics.

 Mandatory arguments:
  BEDFILE	Bed file(s). Expected to be ordered per condition.
  -c csList	Cutsite list file.
  -o outFile	Output matrix file.

 Optional arguments:
  -h		Show this help page.
  -d		Debug mode: write out intermediate results.
  -r regfile	Path to bedfile, containing regions to be assigned to.
"

# Default options
debug=0

# Parse options
while getopts hdfr:c:o: opt "${bedfiles[@]}"; do
	case $opt in
		h)
			echo -e "$helps\n"
			exit 0
		;;
		d)
			debug=1
		;;
		r)
			if [ -e $OPTARG ]; then
				regFile=$OPTARG
			else
				msg="!!! Invalid -r option, file not found.\n    File: $OPTARG"
				echo -e "$helps\n$msg"
				exit 1
			fi
		;;
		c)
			if [ -e $OPTARG ]; then
				csList=$OPTARG
			else
				msg="!!! Invalid -c option, file not found.\n    File: $OPTARG"
				echo -e "$helps\n$msg"
				exit 1
			fi
		;;
		o)
			outFile=$OPTARG
		;;
	esac
done

# Check mandatory options
if [ -z "$csList" ]; then
	msg="!!! Missing mandatory -c option."
	echo -e "$helps\n$msg"
	exit
fi
if [ -z "$outFile" ]; then
	msg="!!! Missing mandatory -o option."
	echo -e "$helps\n$msg"
	exit
fi

# Read bedfile paths
shift $(($OPTIND - 1))
bedfiles=()
for bf in $*; do
	if [ -e $bf ]; then
		bedfiles+=("$bf")
	else
		msg="!!! Invalid bedfile, file not found.\n    File: $bf"
		echo -e " $helps\n$msg"
		exit 1
	fi
done

if [ 0 -eq ${#bedfiles[@]} ]; then
	msg="!!! No bedfile was specified!\n"
	echo -e " $helps\n$msg"
	exit 1
fi

# Library directory
libdir="`dirname ${BASH_SOURCE}`/../lib/"

# RUN ==========================================================================

# Assign to regions ------------------------------------------------------------
# If a list of regions is specified, assign them to the reads
# Then, treat them as "chromosome" and run the analysis on them

if [ -n "$regFile" ]; then
	echo -e " · Assigning to regions of interest..."

	rois2chr='{
		OFS=FS="\t"
		nrois = split($6, rois, " ");
		for (i = 1; i <= nrois; i++) {
			print rois[i] OFS $2 OFS $3 OFS $4 OFS $5 OFS
		}
	}'

	# Make temporary bedfiles
	for bfi in $(seq 0 `bc <<< "${#bedfiles[@]} - 1"`); do
		bf=${bedfiles[$bfi]}

		# Assign bedfile rows to ROIs
		echo -e " >> Working on $bf ..."

		# Use ROIs as chromosomes
		echo -e " >>> Using regions as chromosomes..."
		$libdir/bed-tools-gg/bed_add_rois.py $regFile $bf | awk "$rois2chr" \
			> $bf".regions.tmp"

		# Use tmp file with fake chromosomes
		bedfiles[$bfi]=$bf".regions.tmp"
	done

	# Make temporary cutsite list
	echo -e " >> Working on $csList ..."
	$libdir/bed-tools-gg/bed_add_rois.py $regFile $csList | awk "$rois2chr" \
		> $csList".regions.tmp"
	csList=$csList".regions.tmp"
fi

# Identify regions -------------------------------------------------------------
echo -e " · Identifying regions..."

chr_list=""
for bf in ${bedfiles[@]}; do
	chr_list=$chr_list" "`cat $bf | sed 1d | cut -f 1 | sort | uniq`
done
chr_list=`echo $chr_list | tr ' ' '\n' | sort | uniq | tr '\n' ' '`
IFS=' ' read -r -a chr_list <<< "$chr_list"

# Generate cumulative probability distribution matrix --------------------------
# One chromosome per row, one condition per column.
echo -e " · Preparing matrices..."

# Default empty matrix
matrix_crs=""
matrix_rcs=""

# Cycle over chromosomes
for chr in ${chr_list[@]}; do
	echo -e " >>> Working on $chr..."

	# Number of cutsite in the chromosome
	ncs=`cat $csList | awk -v chr=$chr '$1 == chr' | wc -l`

	if [ 0 -eq $ncs ]; then
		msg="!!! No cutsites found in $chr.\n    Please re-run without $chr."
		echo -e "$msg"
		exit 1
	fi

	# Array of normalized counts
	# prs: ProbabilitieS
	# crs: Cumulative of the RatioS
	# rcs: Ratio of the CumulativeS
	# std: STandard Deviation
	# ffs: Fano FactorS
	# cov: Coefficients Of Variation
	prs=()
	crs=(0)
	rcs=(0)
	std=()
	ffs=()
	cov=()
	counts=(0)
	totals=(0)
	for i in $(seq 0 `expr ${#bedfiles[@]} - 1`); do
		bf=${bedfiles[$i]}

		# Select only current chromosome rows
		bfchr=`cat $bf | sed 1d | awk -v chr=$chr '$1 == chr'`
		
		# Number of reads in the condition
		tot=`cat $bf | sed 1d | cut -f 5 | paste -sd+ | bc`
		
		if [ 0 -eq $tot ]; then
			msg="!!! No reads found in condition #$i.\n"
			msg="$msg    Please re-run without the following bedFile: $bf"
			echo -e "$msg"
			exit 1
		fi
		
		# Number of reads in the condition in the chromosome
		n=`echo "$bfchr" | cut -f 5 | paste -sd+ | bc`
		if [ -z "$n" ]; then
			n=0
		fi

		# if [ -z "$n" ]; then
		# 	msg="!!! No reads found in condition #$i on $chr.\n"
		# 	msg="$msg    Please re-run without the following bedFile: $bf\n"
		# 	msg="$msg    Or without $chr."
		# 	echo -e "$msg"
		# 	exit 1
		# fi
		
		# Probability ----------------------------------------------------------

		r=`bc -l <<< "$n / $tot / $ncs"`
		prs+=($r)

		# Ratio of the sums ----------------------------------------------------

		# Prepare ratios
		prev_n=${counts[${#counts[@]}-1]}
		prev_t=${totals[${#totals[@]}-1]}
		r=`bc -l <<< "($n + $prev_n) / ($tot + $prev_t) / $ncs"`

{'fraction_non_alphanumeric': 0.26605108733172245, 'fraction_numerical': 0.007939247497411116, 'mean_word_length': 2.3397694524495676, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '627975', 'n_tokens_mistral': 4430, 'n_tokens_neox': 3947, 'n_words': 1308}