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

# ------------------------------------------------------------------------------
# 
# Author: <NAME>
# Email: <EMAIL>
# Version: 0.1.1
# Description: module for read alignment.
# 
# ------------------------------------------------------------------------------



# START ========================================================================

function alignment() {
	echo -e 'Alignment\n=====================\n'

	new_fields="\tmapped\tmapped/prefix\tproperly_paired"
	new_fields="$new_fields\tproperly_paired/prefix\tinter_chr"
	head -n 1 $outcontrol/summary_pattern | \
		awk -v nf="$new_fields" "{ print \$0 nf }" - \
		> $outcontrol/summary_align

	patfiles="$indir/pat_files"
	for condition in "${condv[@]}"; do
		echo -e "Aligning reads from condition '$condition'..."

		# Run trimmer ----------------------------------------------------------
		$scriptdir/reads_trim.sh -o $cout -c "$condition" -p $patfiles

		# Run aligner ----------------------------------------------------------
		if [ $numb_of_files -eq 2 ]; then
			# Paired-end
			$scriptdir/reads_align.sh -t $threads -o "$cout/$condition" \
				-c "$condition" -p -r $refGenome -a $aligner
		else
			# Single-end
			$scriptdir/reads_align.sh -t $threads -o "$cout/$condition" \
				-c "$condition" -r $refGenome -a $aligner
		fi

		# Update summary -------------------------------------------------------
		echo -e " · Retrieving flagstats ..."
		samtools flagstat $cout/"$condition"/"$condition".sorted.bam \
			> $cout/"$condition"/"$condition".bam_notes.txt

		# Mapped reads
		count=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'mapped' | head -n 1 | cut -d ' ' -f 1`
		perc=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'mapped' | head -n 1 | cut -d '(' -f 2 | cut -d ':' -f 1`

		# Properly paired reads
		count2=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'properly paired' | head -n 1 | cut -d ' ' -f 1`
		perc2=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'properly paired' | head -n 1 | cut -d '(' -f 2 | \
			cut -d ':' -f 1`
		
		# Chimeric reads	
		count3=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'with mate mapped to a different chr' | head -n 1 | \
			cut -d ' ' -f 1`

		# Add to summary
		new_fields="\t$count\t$perc\t$count2\t$perc2\t$count3"
		grep "$condition" "$outcontrol/summary_pattern" | \
			awk -v nf="$new_fields" "{ print \$0 nf }" - \
			>> $outcontrol/summary_align

		# Add back the UMIs to the SAM file ------------------------------------
		echo -e " · Adding linkers to SAM file ..."

		awkprogram='@include "join";
		FNR==NR{a[$1]=$0;next} ($1 in a) { OFS="\t";

		split(a[$1], t1, OFS);
		split($0, t2, OFS);

		t2[10]=t1[2] "\t" t[10];
		t2[11]=t1[4] "\t" t[11];


		print join(t2, 1, 13, OFS) }'
		awk "$awkprogram" \
			<(cat "$cout/$condition/filtered.r1.linkers.oneline.fq") \
			<(cat "$cout/$condition/$condition.sam" | \
				grep -v "^\@" | tr -s ' ') \
			> "$cout/$condition/$condition.linkers.sam"
	done

	cp $outcontrol/summary_align $out/summary
}

################################################################################<commit_msg>Fixed awk script generating the linkers.sam file.<commit_after>#!/usr/bin/env bash

# ------------------------------------------------------------------------------
# 
# Author: <NAME>
# Email: <EMAIL>
# Version: 0.1.1
# Description: module for read alignment.
# 
# ------------------------------------------------------------------------------



# START ========================================================================

function alignment() {
	echo -e 'Alignment\n====================='

	new_fields="\tmapped\tmapped/prefix\tproperly_paired"
	new_fields="$new_fields\tproperly_paired/prefix\tinter_chr"
	head -n 1 $outcontrol/summary_pattern | \
		awk -v nf="$new_fields" "{ print \$0 nf }" - \
		> $outcontrol/summary_align

	patfiles="$indir/pat_files"
	for condition in "${condv[@]}"; do
		echo -e "\nAligning reads from condition '$condition'..."

		# Run trimmer ----------------------------------------------------------
		$scriptdir/reads_trim.sh -o $cout -c "$condition" -p $patfiles

		# Run aligner ----------------------------------------------------------
		if [ $numb_of_files -eq 2 ]; then
			# Paired-end
			$scriptdir/reads_align.sh -t $threads -o "$cout/$condition" \
				-c "$condition" -p -r $refGenome -a $aligner
		else
			# Single-end
			$scriptdir/reads_align.sh -t $threads -o "$cout/$condition" \
				-c "$condition" -r $refGenome -a $aligner
		fi

		# Update summary -------------------------------------------------------
		echo -e " · Retrieving flagstats ..."
		samtools flagstat $cout/"$condition"/"$condition".sorted.bam \
			> $cout/"$condition"/"$condition".bam_notes.txt

		# Mapped reads
		count=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'mapped' | head -n 1 | cut -d ' ' -f 1`
		perc=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'mapped' | head -n 1 | cut -d '(' -f 2 | cut -d ':' -f 1`

		# Properly paired reads
		count2=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'properly paired' | head -n 1 | cut -d ' ' -f 1`
		perc2=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'properly paired' | head -n 1 | cut -d '(' -f 2 | \
			cut -d ':' -f 1`
		
		# Chimeric reads	
		count3=`cat $cout/"$condition"/"$condition".bam_notes.txt | \
			grep 'with mate mapped to a different chr' | head -n 1 | \
			cut -d ' ' -f 1`

		# Add to summary
		new_fields="\t$count\t$perc\t$count2\t$perc2\t$count3"
		grep "$condition" "$outcontrol/summary_pattern" | \
			awk -v nf="$new_fields" "{ print \$0 nf }" - \
			>> $outcontrol/summary_align

		# Add back the UMIs to the SAM file ------------------------------------
		echo -e " · Adding linkers to SAM file ..."

		awkprogram='@include "join";
		FNR==NR{a[$1]=$0;next} ($1 in a) { OFS="\t";

		split(a[$1], t1, OFS);
		split($0, t2, OFS);

		t2[10]=t1[2] "\t" t2[10];
		t2[11]=t1[4] "\t" t2[11];

		print join(t2, 1, 11, OFS) }'
		awk "$awkprogram" \
			<(cat "$cout/$condition/filtered.r1.linkers.oneline.fq") \
			<(cat "$cout/$condition/$condition.sam" | \
				grep -v "^\@" | tr -s ' ') \
			> "$cout/$condition/$condition.linkers.sam"
	done

	cp $outcontrol/summary_align $out/summary
}

################################################################################
{'fraction_non_alphanumeric': 0.3248715153402897, 'fraction_numerical': 0.016196854072574367, 'mean_word_length': 2.114451988360815, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, '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': '2336071', 'n_tokens_mistral': 2442, 'n_tokens_neox': 2129, 'n_words': 498}