#!/bin/bash

TEMP_DIR="/home/yan/TaRGET_II/Li_adt/LESS/temp_folder"
mkdir -p $TEMP_DIR

cat Candidate_list_BPA10mg.txt | while read file motif gene
do
    echo "Processing: FILE=${file}  MOTIF=${motif}  GENE=${gene}"

    out="${TEMP_DIR}/${file}_${gene}.txt"
    peak="${TEMP_DIR}/${file}_${gene}_peak.txt"

    motif_file="${file}/homerResults/${motif}.motif"

    if [[ ! -f "$motif_file" ]]; then
        echo "WARNING: Motif file not found: $motif_file"
        continue
    fi

    findMotifsGenome.pl ${file}.txt mm10 ${file} \
        -size given \
        -find "$motif_file" \
        -preparsedDir "$TEMP_DIR" \
        > "$out"

    cut -f1 "$out" | sed '1d' | sort -u > "$peak"

    echo "Generated: $out"
    echo "Generated: $peak"
    echo "----------------------------------------"

done
