#4C data preprocessing

##various PATH defs
PYTPATH=/PATH/TO/PYTHON/SCRIPTS
FASPATH=PATH/FASTA/FILE
BOWTIEPATH=/PATH/TO/Indexes/bowtieIndex
RcountCLVPATH=/PATH/TO/Rcount/linux_64bit/Rcount_CLV
RESPATH_LIB1=/PATH/TO/DATA/4C_results/LIB1res/merged_files_LIB1
FRAGPATH_LIB1=/PATH/TO/DATA/4C_results/LIB1res/fragments_LIB1
RESPATH_LIB2=/PATH/TO/DATA/4C_results/LIB2res/merged_files_LIB2
FRAGPATH_LIB2=/PATH/TO/DATA/4C_results/LIB2res/fragments_LIB2
RESPATH_LIB3=/PATH/TO/DATA/4C_results/LIB3res/merged_files_LIB3
FRAGPATH_LIB3=/PATH/TO/DATA/4C_results/LIB3res/fragments_LIB3
RESPATH_LIB4=/PATH/TO/DATA/4C_results/LIB4res/merged_files_LIB4
FRAGPATH_LIB4=/PATH/TO/DATA/4C_results/LIB4res/fragments_LIB4
RESPATH_LIB5=/PATH/TO/DATA/4C_results/LIB5res/merged_files_LIB5
FRAGPATH_LIB5=/PATH/TO/DATA/4C_results/LIB5res/fragments_LIB5
RESPATH_LIB6=/PATH/TO/DATA/4C_results/LIB6res/merged_files_LIB6
FRAGPATH_LIB6=/PATH/TO/DATA/4C_results/LIB6res/fragments_LIB6
RESPATH_LIB7=/PATH/TO/DATA/4C_results/LIB7res/merged_files_LIB7
FRAGPATH_LIB7=/PATH/TO/DATA/4C_results/LIB7res/fragments_LIB7
RESPATH_LIB8=/PATH/TO/DATA/4C_results/LIB8res/merged_files_LIB8
FRAGPATH_LIB8=/PATH/TO/DATA/4C_results/LIB8res/fragments_LIB8
RESPATH_LIB9=/PATH/TO/DATA/4C_results/LIB9res/merged_files_LIB9
FRAGPATH_LIB9=/PATH/TO/DATA/4C_results/LIB9res/fragments_LIB9
RESPATH_LIB10=/PATH/TO/DATA/4C_results/LIB10res/merged_files_LIB10
FRAGPATH_LIB10=/PATH/TO/DATA/4C_results/LIB10res/fragments_LIB10
RESPATH_LIB11=/PATH/TO/DATA/4C_results/LIB11res/merged_files_LIB11
FRAGPATH_LIB11=/PATH/TO/DATA/4C_results/LIB11res/fragments_LIB11



for lib in LIB1 LIB2 LIB3 LIB4 LIB5 LIB6 LIB7 LIB8 LIB9 LIB10 LIB11
do mkdir /PATH/TO/DATA/4C_extracted/"$lib"
done
cut -f1-4 /PATH/TO/DATA/HiCDat_data/HindIII_fragments.txt > /PATH/TO/DATA/HiCDat_data/HindIII_fragments_only.txt

#merge files from each lane..
for lib in LIB1 LIB2 LIB3 LIB4 LIB5 LIB6 LIB7 LIB8 LIB9 LIB10 LIB11
for lib in LIB2
do echo "$lib"
cat /PATH/TO/DATA/4C_extracted_2015/"$lib"/*.fastq > /PATH/TO/DATA/4C_extracted_2015/"$lib"/"$lib"_merged/"$lib"_merged.fastq
done

for lib in LIB1 LIB2 LIB3 LIB4 LIB5 LIB6 LIB7 LIB8 LIB9 LIB10 LIB11
do mkdir /PATH/TO/DATA/4C_results/"$lib"res
done


#split by primers....

for lib in LIB3 LIB4 LIB5 LIB6 LIB7 LIB8 LIB9 LIB10 LIB11
do echo "$lib"
python "$PYTPATH"/SplitPrimer.py "$lib" 0 -noprimersequence
done

##LIB1 and for other libraries accordingly...
mkdir /PATH/TO/DATA/4C_results/LIB11res/merged_files_LIB11
for PSET in pKEE6 pSG310 pSG330
do cat "/PATH/TO/DATA/4C_results/LIB11res/$PSET"Dpn_library_NPS.txt "/PATH/TO/DATA/4C_results/LIB11res/$PSET"Hind_library_NPS.txt >"/PATH/TO/DATA/4C_results/LIB11res/merged_files_LIB11/$PSET"_library_NPS_LIB11.txt
done

for PSET in pSG307
do cat "/PATH/TO/DATA/4C_results/LIB11res/$PSET"Nla_library_NPS.txt "/PATH/TO/DATA/4C_results/LIB11res/$PSET"Hind_library_NPS.txt >"/PATH/TO/DATA/4C_results/LIB11res/merged_files_LIB11/$PSET"_library_NPS_LIB11.txt
done

for PSET in pSG298 pSG333_1_
do cat "/PATH/TO/DATA/4C_results/LIB11res/$PSET"Taq_library_NPS.txt "/PATH/TO/DATA/4C_results/LIB11res/$PSET"Hind_library_NPS.txt >"/PATH/TO/DATA/4C_results/LIB11res/merged_files_LIB11/$PSET"_library_NPS_LIB11.txt
done

##do the alignment
for PSET in pKEE6 pSG298 pSG307 pSG310 pSG330 pSG333_1_
do echo "$PSET"
bowtie -a -v 0 -m 25 --sam --chunkmbs 1024 -p 7 -t "$BOWTIEPATH/At_genome" -q "$RESPATH_LIB11/$PSET"_library_NPS_LIB11.txt "$RESPATH_LIB11/$PSET"_alignment.sam
done

####MultiReads Flagger
for PSET in pSG330
do echo "$PSET"
python "$PYTPATH/stefanMultiSam.py" "$RESPATH_LIB2/$PSET"_alignment.sam "$RESPATH_LIB2/$PSET"_alignment.bam
done

####sort bam files and index

for PSET in pSG330
do echo "$PSET"
samtools sort -m 12000000000 "$RESPATH_LIB2/$PSET"_alignment.bam "$RESPATH_LIB2/$PSET"_alignment_sorted
samtools index "$RESPATH_LIB2/$PSET"_alignment_sorted.bam
done


for PSET in pSG314
do echo "$PSET"
"$RcountCLVPATH/Rcount-multireads_CLV" -c "$RESPATH_LIB6/$PSET"_alignment_sorted.bam,"$RESPATH_LIB6/$PSET"_alignment_multiout.bam,y,100
done

for LIB in LIB1 LIB2 LIB3 LIB4 LIB5 LIB6 LIB7 LIB8 LIB9 LIB10 LIB11
do mkdir /PATH/TO/DATA/4C_fragments/100kb/"$LIB"
done


