#!bin/bash
##########################################
# Written by Dr. Bryan Venters (EpiCypher 2020)
#
# Purpose of script: Use "grep -c" to count exact match to CUTANA spike-in nucleosome barcodes from unzipped paired-end (R1 & R2) fastqs. Both R1 and R2 are searched b/c the barcoded side of nucleosome may be ligated in either direction to P5 or P7 adapters.
# 
# Caution: avoid spaces in file paths and filenames b/c it is a frequent source of syntax errors.
##########################################

## Instructions ##
# In Finder:
# 1. Unzip (extract) all fastq.gz to fastq on Mac or Linux by double-clicking on *.gz file.

# In TextEdit:
# 2. Copy/Paste template loop below in this shell script as many times as needed (1 template loop / PE R1 & R2 data set).
# 3. Add sample filenames (eg: BV813_H3K4me3_dNuc_R1_100k.fastq) in place of "unzipped_fastq_filename_R1-2.fastq" to copied/pasted loops.

# In Terminal:
# 4. In Terminal, cd (change directory) to directory containing unzipped_fastq files (eg: cd path_to_fastq).
# 5. To execute shell scipt in the Terminal application: sh path_to_shell.sh
# 6. Copy/Paste barcode counts from Terminal to the provided Excel file to record the barcode identities.

# In Excel:
# 7. The "on-target" (H3K4me3) reads counts for a CUTANA certified antibody will typically have an on target read count of >80% of the total. 
# 8. The provided excel file will automatically generate heatmap visualization of results, where read counts are normalized relative to on-target (H3K4me3, set to 100). For IgG, data is normalized to the sum of total reads.


# Barcode identities
# H3K4me0 (C058 & C097)
# TTCGCGCGTAACGACGTACCGT 
# CGCGATACGACCGCGTTACGCG 

# H3K4me1 (C017 & C096)
# CGACGTTAACGCGTTTCGTACG 
# CGCGACTATCGCGCGTAACGCG 

# H3K4me2 (C079 & C019)
# CCGTACGTCGTGTCGAACGACG 
# CGATACGCGTTGGTACGCGTAA 

# H3K4me3 (C038 & C041)
# TAGTTCGCGACACCGTTCGTCG 
# TCGACGCGTAAACGGTACGTCG


# template loop begin ##
echo "R1.fq 250bp 22nt barcodes = 8 counts"
for barcode in TTCGCGCGTAACGACGTACCGT CGCGATACGACCGCGTTACGCG CGACGTTAACGCGTTTCGTACG CGCGACTATCGCGCGTAACGCG CCGTACGTCGTGTCGAACGACG CGATACGCGTTGGTACGCGTAA TAGTTCGCGACACCGTTCGTCG TCGACGCGTAAACGGTACGTCG ;
do
	grep -c $barcode unzipped_fastq_filename_R1-2.fastq
done

echo "R2.fq 250bp 22nt barcodes = 8 counts"
for barcode in TTCGCGCGTAACGACGTACCGT CGCGATACGACCGCGTTACGCG CGACGTTAACGCGTTTCGTACG CGCGACTATCGCGCGTAACGCG CCGTACGTCGTGTCGAACGACG CGATACGCGTTGGTACGCGTAA TAGTTCGCGACACCGTTCGTCG TCGACGCGTAAACGGTACGTCG ;
do
	grep -c $barcode unzipped_fastq_filename_R1-2.fastq
done
# template loop end ##




