#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 ScGENOME
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_Sc

#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#; " ../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 ""

#Now for the real work:
#
sed -e "s#EXPERIMENT=demo#EXPERIMENT=GSF2268#; " ../GoSTRIPES/templates/xrun_samples >  xrun_samples
chmod a+x xrun_samples
#NOTE: This should take several hours.
./xrun_samples

#Cleaning up:
#
sed -e "s#EXPERIMENT=demo#EXPERIMENT=GSF2268#; " ../GoSTRIPES/templates/xcleanup_samples >  xcleanup_samples
chmod a+x xcleanup_samples
#
#NOTE: This will remove temporary files and archive others, greatly reducing
#      disk space usage.
#
./xcleanup_samples
../xsummarize_alignments

#NOW:
#
# - you will have records of what was done in the sample directories (see err
#   and logfiles/*).
#
# - having run the xsummarize_alignments script should have created the
#   tab-delimited file ProcessingAndAlignmentResults.tsv, which summarizes
#   the numbers of reads and alignments at the various stages of processing,
#   from initial input to final alignments used for the next workflow step
#
# - you should find in this directory bamfiles.tsv, which tabulates the samples
#   and location of the generated *.bam alignment files. bamfiles.tsv can be
#   used as input to TSRchitect for the next workflow step: identification of
#   TSS and TSR; see directory tsr and instructions tsr/0Record.
