Unix capstone exercise¶
Ths capstone exercise covers essentially all of the Unix commands you need to develop your bioinformatics piepliens. If you can do this exercise without too much difficulty, you are ready to process raw RNA-seq data!
But don’t be discouraged if you find this difficutl - unless you have quite a lot of previous programming experience, this exerise is likely to be challenging.
General hints:
Use
pwdliberally to check that you are in the right directoryEach command should execute almost instantly - if not, something is problay wrong with your command
Commands in Jupyter can appear to “hang” when there is an error - try the command in a terminal to see what’s wrong
Remember to configure so that you get a warning with unbound variabels
Use variables liberally - if a name is used more than once, it should probably be assinged to a variable
There is no restriction on using any search engine or reviewing previous notebooks
Make two directories
dataandresults. Check that the directories were created.
Hint:
You can list just directories with
ls -d */
[1]:
pwd
/home/jovyan/work/Duke_HTS2019/cliburn
[2]:
if [ -d "data" ]
then
echo "Directory data exists."
else
echo "Create folder data."
echo mkdir data
fi
Directory data exists.
[3]:
if [ -d "results" ]
then
echo "Directory data exists."
else
echo "Create folder results."
echo mkdir results
fi
Directory data exists.
[4]:
ls -d */
data/ figs/ results/ seqs/
In
data, create the filesexpt-1,expt-2,epxt-3, each with 5 rows of 3 random numbers between 0 and 9 (inclusive) separated by tabs. When done, display the contents of each file.
Hint:
Create 3 for loops
the first to create a file,
the next to append a row of numbers, and add a newline
and the third to generate the numbers in the row.
You can generate random integers in Unix by repeatedly capturing the value of the special variable
$RANDOM.Initialize the
RANDOMvariable with the seed42.The arithmetic modulo opertor is
%(gives remainder after diviison)The tab character is represented by the string
\tThe argument
-netoechomeans do not emit a newline, evaluate specail characaters like\tYou can generate sequences of numbers using brace expansion or
seqArithmetic operatinons are evaluated like this $((1+2))
[5]:
cd data
[6]:
RANDOM=42
for FILE in expt-{1..3}
do
rm $FILE
touch $FILE
for row in {1..5}
do
for col in {1..3}
do
echo -ne $(($RANDOM % 10))'\t' >> $FILE
done
echo >> $FILE
done
done
rm: cannot remove 'expt-1': No such file or directory
rm: cannot remove 'expt-2': No such file or directory
rm: cannot remove 'expt-3': No such file or directory
[7]:
for FILE in expt-{1..3}
do
echo $FILE
cat $FILE
done
expt-1
6 1 1
3 8 7
9 0 6
3 4 9
2 5 2
expt-2
0 4 6
8 4 3
6 2 7
3 2 9
1 5 9
expt-3
0 2 5
7 8 5
4 5 3
5 8 7
8 3 1
Create an
MD5SUMfile containing the checksums of the contents withinexpt-1,expt-2, andepxt-3. Show the contents of theMD5SUMfile.
Hint:
md5sumneeds a list of files as arguments
[8]:
ls
expt-1 expt-2 expt-3 gene_counts_raw.txt gene_counts.txt
[9]:
md5sum * > MD5SUM
[10]:
cat MD5SUM
72e21e4726baa4296d0722161c92d19b expt-1
c98f878743247e78e2dc5dacfb0e3b7f expt-2
0bd25d1865615e48cc55d8f7ad88c440 expt-3
a61102c86253625061a43f7a56e3131c gene_counts_raw.txt
a71c7c2c2b84cc10d9c1519d5cac5bc5 gene_counts.txt
Create a
data.tar.gzfile containing all the contents of thedatadirectory
[11]:
cd ..
[12]:
tar -czvf data.tar.gz data
data/
data/expt-1
data/expt-2
data/gene_counts.txt
data/expt-3
data/gene_counts_raw.txt
data/MD5SUM
Move
data.tar.gzto theresultsdirectory and recreate the original files
[13]:
mv data.tar.gz results
[14]:
cd results
[15]:
tar -xzvf data.tar.gz
data/
data/expt-1
data/expt-2
data/gene_counts.txt
data/expt-3
data/gene_counts_raw.txt
data/MD5SUM
[16]:
ls
data data.tar.gz
Create a new file
expt-sum.txtinresultswith 3 lines showing the sum of the numbers in the 1st, 2nd and 3rd columns over all 3expt-xfiles. That is, the first row ofexpt-sum.txtcontains the sum of the 1st column ofexpt-1,expt-2, andexpt-3and so on. Use globbing to get the filesexpt-1,expt-2, andexpt-3.
Hint:
Remember the
cutcommandUse a double for loop
The outer loop selects a file
The inner loop sums up the numbers in the appropriate column of the file
[17]:
DEST_FILE='expt-sum.txt'
touch $DEST_FILE
COL=1
for FILE in $(ls data/expt*)
do
SUM=0
NS=$(cat $FILE | cut -f$COL)
for N in $NS
do
SUM=$((SUM + N))
done
COL=$((COL + 1))
echo $SUM >> $DEST_FILE
done
[18]:
cat $DEST_FILE
23
17
21
Delete the
dataandreultsdirectory recursively
[19]:
pwd
/home/jovyan/work/Duke_HTS2019/cliburn/results
[20]:
cd ..
[21]:
ls -d */
data/ figs/ results/ seqs/
[22]:
rm -r data results
[23]:
ls -d */
figs/ seqs/