#For the impatient or expert user:
#
# You could run the entire workflow by running the following scripts in
# succession:
#
# 	./xpreparatory
# 	./xwork
# 	./xcleanup
# 	./xdettsr
#
# (in fact, you could run all the above in one script, provided you have
#  the appropriate definition of $rws as an environmental variable; see
#  message at the end of the xpreparatory script).
#
# More prudent and educational might to be read on below, where we have
# annotated the workflow steps.


#We need to get the genome files set up before we can do anything else:
#
cd HsGENOME
source 0README
cd ..

#Get the data:
#
#The file samples.tsv assumes implicitly that the read data are accessible
#in the download/stripeseq directory under the listed file name prefixes.
#If you have NCBI SRA Toolkit installed, the following will download the
#relevant STRIPE-seq read data:
cd downloads/stripeseq
./xgetdata
#If you done have the toolkit installed, follow the instructions below to
#define the singularity shortcut command as $rws, then run xgetdata from
#the toolkit installed in the container:
$rws ./xgetdata
cd ../..

#Let's set up a template Makefile to run the workflow:
#
sed -e "s#{RAWREAD1}.fq#{RAWREAD1}.fastq#; s#{RAWREAD2}.fq#{RAWREAD2}.fastq#;" ../GoSTRIPES/templates/Makefile_Sc >  Makefile_Hs
sed -i -e "s#ScGENOME#HsGENOME#; s#Saccharomyces_cerevisiae.R64-1-1.dna_sm.toplevel.fa#Homo_sapiens.GRCh38.dna_sm.primary_assembly.fa#; s#Saccharomyces_cerevisiae.R64-1-1.98.gtf#Homo_sapiens.GRCh38.98.chr.gtf#; s#Sc_rRNA.fa#Hs_rRNA.fa#; s#scrubSAMfile_OPTIONS  = -s 3 -i 50 -I 1000 -t 1500#scrubSAMfile_OPTIONS  = -s 3 -i 50 -I 100000 -t 100500#;" Makefile_Hs
sed -i -e '/samtools index ${SAMPLE}.bam/a \\tsamtools view -bh ${SAMPLE}.bam \\\n\t\t"1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" \\\n\t\t"13" "14" "15" "16" "17" "18" "19" "20" "21" "22" "X" "Y" \\\n\t\t> ${SAMPLE}.chr.bam\n\tsamtools index ${SAMPLE}.chr.bam' Makefile_Hs
#NOTE: The last edit sets up the *chr.bam files for TSS processing on
#      the assembled nuclear chromosomes only.


#And now we set up the sample directories to get the work done:
#
sed -e "s#EXPERIMENT=demo#EXPERIMENT=GSF2268#; s#{READFILESTEM\[i\]}_R1#{READFILESTEM[i]}_1#; s#{READFILESTEM\[i\]}_R2#{READFILESTEM[i]}_2#; s#Makefile_Sc#Makefile_Hs#; " ../GoSTRIPES/templates/xsetup_samples >  xsetup_samples
chmod a+x xsetup_samples
#NOTE: You should adjust the Makefile parameters to choices that are appropriate
#      for your system. As provided, the workflow will run with 8 cores.
./xsetup_samples

#Ok, one more absolutely necessary preparatory step:
#
echo "We must set the rws variable again, as we have moved the gostripes.simg images."
echo "Execute the following (or equivalent) in your working shell:"
echo ""
echo "source ../GoSTRIPES/bin/xworkStripes -b `pwd` -i `pwd`/../gostripes.simg" 
echo ""
