#!bin/bash
##########################################
# Written by Dr. Bryan Venters and Irina Popova, EpiCypher Inc. 
# Updated 05 FEB 2024
#
# 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 because the barcoded side of nucleosome may be ligated in either direction relative to the P5 or P7 adapters.
# 
# Caution: avoid spaces in file paths and filenames because it is a frequent source of syntax errors.
##########################################

## Instructions ##
# In Finder (Mac):
# 1. Duplicate this template shell script to create an experiment-specific copy. Save to a desired folder. 
# 2. Unzip (extract) all fastq.gz to fastq on Mac or Linux by double-clicking on *.gz files. Save these files to the same folder containing shell script.

# In TextEdit:
# 3. Copy/Paste the lines between "# template loop begin ##" and "# template loop end ##" (below; from the first "echo" to the last "done") in this shell script as many times as needed (1 template loop per paired-end R1 & R2 data set). This is the script needed to align sequencing reads to the spike-in barcodes.
# 4. Add sample filenames (eg: DYKDDDDK_R1_100k.fastq) in place of "reaction1_R1.fastq" in copied/pasted loops. Each loop should contain R1 and R2 files for the matched reaction.

# In Terminal:
# 5. In Terminal, cd (change directory) to directory (folder) containing unzipped_fastq files (eg: cd path_to_fastq). This can also be done in a Mac by dragging your file onto Terminal.
# 6. To execute shell script in the Terminal application type "sh" followed by the file path to your saved shell script, or by dragging shell script file into Terminal: sh path_to_shell.sh
#7: Press enter. The shell script will output the barcode counts in the order listed under "# Barcode identities" (below) and datasets will be annotated based on the filenames. For each annotated loop (R1 and R2 sample set), it will generate all R1 counts and then all R2 counts.

# In Excel:
# Note: The Excel template provides space to copy in read count data for the IgG negative control and 6 additional reactions. Copy and make additional analyses as needed.
# 8. Copy and paste R1 and R2 barcode counts for IgG control reaction from Terminal to yellow highlighted cells.  
# 9. Copy and paste R1 and R2 barcode counts for reactions containing DYKDDDDK-Tag generated from running the script and paste into the appropriate highlighted yellow cells. Make sure the R1 and R2 files are matched (i.e. from the same reaction).
# 10. The Excel file will automatically generate a heatmap visualization of results (see "Output Table" tab), where read counts are normalized relative to DYKDDDDK Tag. Condensed results, summarizing antibody binding data is provided to right.
# 11. Enter the total "Unique align reads" from your sequencing reaction at the bottom of each table. The total barcode reads will auto-fill. The target for "% total barcode reads" is 1-10%. Lower than 1% and there may be insufficient reads to determine specificity. Greater than 10% indicates spike-ins should be diluted further in future experiments.
# 12. Go to the "Output Table" sheet in the Excel Workbook for a combined heat map showing antibody specificity data for all reactions.

# Notes:
# EpiCypher considers a DYKDDDDK antibody with <20% binding to unmodified nucleosome suitable for downstream data analysis. 
# For IgG, data is normalized to the sum of total barcode reads. % binding should be approximately 50% for unmodified and 50% for DYKDDDDK Tag nucleosomes. 



# Barcode identities
# Unmodified (A & B)
# ACCGCGATACGCGCGCGATATG
# ACGACGTACGGCGCGCTATACG

# DYKDDDDK (A & B)
# ATCGTACGCCGCGTCGCTCGAA
# TAGCGTACCGACGATACGCGTT


# template loop begin ##
echo "R1.fq 250bp 22nt barcodes = 8 counts"
for barcode in ACCGCGATACGCGCGCGATATG ACGACGTACGGCGCGCTATACG ATCGTACGCCGCGTCGCTCGAA TAGCGTACCGACGATACGCGTT ;
do
	grep -c $barcode reaction1_R1.fastq
done

echo "R2.fq 250bp 22nt barcodes = 8 counts"
for barcode in ACCGCGATACGCGCGCGATATG ACGACGTACGGCGCGCTATACG ATCGTACGCCGCGTCGCTCGAA TAGCGTACCGACGATACGCGTT ;
do
	grep -c $barcode reaction1_R2.fastq
done
# template loop end ##




