##############
# Exercise 4#
##############

# The objective of this exercise is to understand how pipes work and how to 
# use them

# NOTE: We are using clustalw2_sleep in this exercise since is the one previously used. In the original directory you have the original clustalw without the sleep, You can use it instead if you want / can

# we go to the home directory
cd
# we go to the SEQS directory
cd PRACT_SO/SEQS/

# we remove LexA_all.fst
rm LexA_all.fst

# we create it again from the individual sequence files

# this is not needed, but it is good to do it. We list all *.fst files
ls *.fst
 
# we can see the contents of all the files that have ".fst" at the end of their names by sending the contents to the stdout (in this case the screen)
cat *.fst

# we send the contents of all the files that have ".fst" at the end of their names to the stdout and we redirect the stdout to a file
cat *.fst >LexA_all.fsta

# we take a look to the files
less LexA_all.fsta
less Eco.fst
less Det.fst

# now we are going to execute clustalw2_sleep in interactive mode
clustalw2_sleep
# we choose option 1 to enter the sequences from disk
1
# we type the file with our sequences:
LexA_all.fsta
#we choose option
2
# we do the multiple alignment choosing option
1
# we accept default alignment output file pressing ENTER

# we accept default tree output file pressing ENTER

# we press ENTER again to see the whole alignment

# ENTER again to continue

# to exit the menu we type
X
# and to exit the program
X


#we take a look to the output alignment
less LexA_all.aln

#we remove all output files
rm LexA_all.dnd LexA_all.aln

# now we take a look to this file $HOME/LINUX_CLI_EXERCISES/clustw_stdin.txt
cat $HOME/LINUX_CLI_EXERCISES/clustw_stdin.txt
#Does its contents rings a bell to you?

# we execute clustalw2_sleep redirecting stdin to this file
clustalw2_sleep <$HOME/LINUX_CLI_EXERCISES/clustw_stdin.txt

#we execute ls -ltr to see all files sorted by creation date
ls -ltr

#which new files have been created? what is inside them? What Just Happened?

#what is the output of this command
grep Bsu LexA_all.aln

#and this one:
ps -ef|grep $USER
#and this one:
ps -ef|less





# what this line does if you execute it:
grep ">" LexA_all.fsta |cut -d ">" -f 2|xargs -i bash -c "echo \>{};grep {} LexA_all.aln"|awk '{if (NF==2) print $2;if(NF==1) print $1}' > LexA_all.aln.fsta


# execute it command by command to understand what it does

grep ">" LexA_all.fsta 
grep ">" LexA_all.fsta |cut -d ">" -f 2
grep ">" LexA_all.fsta |cut -d ">" -f 2|xargs -i bash -c "echo \>{};grep {} LexA_all.aln"
grep ">" LexA_all.fsta |cut -d ">" -f 2|xargs -i bash -c "echo \>{};grep {} LexA_all.aln"|awk '{if (NF==2) print $2;if(NF==1) print $1}' 
grep ">" LexA_all.fsta |cut -d ">" -f 2|xargs -i bash -c "echo \>{};grep {} LexA_all.aln"|awk '{if (NF==2) print $2;if(NF==1) print $1}' > LexA_all.aln.fsta

