#!/bin/bash
#SBATCH --cpus-per-task=8
#SBATCH --time=00:45:00
#SBATCH --account=csd772
#SBATCH --partition=condo
#SBATCH --qos=condo
#SBATCH -e /tscc/projects/ps-epigen/users/kdang/ASAP/Aging_Figures/Aging_WWN_nodoub/240730_NOISeq/eo_script/240924_homerNOISeq_q0.99.e
#SBATCH -o /tscc/projects/ps-epigen/users/kdang/ASAP/Aging_Figures/Aging_WWN_nodoub/240730_NOISeq/eo_script/240924_homerNOISeq_q0.99.o

## gets a particular bed file in the input directory and runs it through homer
## usage sbatch --array=1-4 240514_02_homer_fm_heps_comb_02.sh  where 1-4 is the number of files in the directory

#inp_bed_dir="/tscc/projects/ps-epigen/users/cmiciano/Liver/NASH_NAFLD_pooling/outputs/sandbox/fm_test/FM_output/05_test_heps_sc/res_de_logfc15_bed/"
#inp_bed_dir="/tscc/projects/ps-epigen/users/cmiciano/Liver/NASH_NAFLD_pooling/outputs/atac/fm_atac/FM_output/02_test_heps_sc_comb_02/res_de_logfc15_bed/"
inp_bed_dir="/tscc/projects/ps-epigen/users/kdang/ASAP/Aging_Figures/Aging_WWN_nodoub/240730_NOISeq/CN_normalize/NOISeq_out_denorm_padj0.01/sig_bed/"

#homer_out="/tscc/projects/ps-epigen/users/cmiciano/Liver/NASH_NAFLD_pooling/outputs/atac/fm_atac/homer_out/"
#homer_out="/tscc/projects/ps-epigen/users/cmiciano/Liver/NASH_NAFLD_pooling/outputs/sandbox/homer_out_fm/"
homer_out="/tscc/projects/ps-epigen/users/kdang/ASAP/Aging_Figures/Aging_WWN_nodoub/240730_NOISeq/CN_normalize/NOISeq_out_denorm_padj0.01/homer_out/"

# Name of subfolder of input bed files, homer out directory will be named the same
subfolder_name="sig_bed"

# HOMER output folders
homer_out_sub=$homer_out$subfolder_name

[[ -d $homer_out ]] || mkdir $homer_out
[[ -d $homer_out_sub ]] || mkdir $homer_out_sub

bed_file=$( ls $inp_bed_dir | head -$SLURM_ARRAY_TASK_ID | tail -1)

echo "inp bed file"
echo $bed_file
bed_file_fn="${bed_file##*/}" #takes basename (will be name of directory for that clust)

echo "$bed_file_fn"
hout_file_dir="${bed_file_fn%.*}/" #removes .bed
echo "$hout_file_dir"

hout_bdir=$homer_out_sub"/"$hout_file_dir #creating a homer directory for each bed filer
echo "hout_bdir"
echo $hout_bdir

fp_bed=$inp_bed_dir$bed_file

date
findMotifsGenome.pl $fp_bed mm10 $hout_bdir -size 200 -p 8  #include -nomotif option if you only want to run knownmotifs
date

