User Tools

Site Tools


assignments:ex3

Exercise 3


Introduction to Scripting

In this exercise, we will introduce scripting in Bash.

Script: A series of commands or instructions to automate a task. The commands are written in a text file that is then executed by a program without being first compiled (converted into the binary machine code).

Scripting language: A computer programming language that supports scripts. The scripts are typically interpreted by the program and do not have to be compiled.

★ Interactive Exercise: Prerequisite to scripting

In this exercise, we’ll discuss variable assignment and manipulation. The goal is to demonstrate the need to automate repeated tasks, which will be done in the next exercise in which we introduce scripts.

1. Open a shell or terminal window.

2. Assign a DNA sequence to a variable (sequence):

$ sequence=ACTGTACGGTACAC

3. To recall a variable we place a $ in front of it. It is stored until the terminal is closed. Complement the sequence using tr:

$ echo $sequence | tr [ACTGactg] [TGACtgac]

4. Reverse the sequence using rev:

$ echo $sequence | rev

5. Reverse complement the sequence using tr and rev:

$ echo $sequence | rev | tr [ACTGactg] [TGACtgac]

6. Calculate the length of the sequence using wc -m:

$ echo -n $sequence | wc -m 

This exercise required multiple steps and if it is something we wanted to repeat, it could be easily automated with a script, as demonstrated in the exercise below.

Text Editors

A good text editor is essential for writing scripts. Microsoft Word and Mac TextEdit should not be used for writing scripts.

Common Free Text Editors


★ Interactive Exercise: First script

1. Open a shell or terminal window.

2. Create a new directory named Bash_Scripts using mkdir.

3. Change into the Bash_Scripts directory using cd.

4. Open a text editor, such as TextWrangler or Notepad++, using open and use it to create a new file called hello.sh within the Bash_Scripts directory. In the next steps, we will write a bash script by adding commands to the file.

5. Confirm that you are in the Bash_Scripts directory using pwd and that the hello.sh script is in the directory using ls.

6. Insert a shebang (#!) that directs the terminal to bash within the hello.sh file: #!/bin/bash

7. read is a command that takes input from a user and stores it as a variable. First, we'll prompt the user for their name and store it as a variable using the read command:

read -p "What is your name? " name

*the -p option allows us to include a text message. The variable name is the argument. Note that although it looks a little more complex, the read command has a familiar format.

8. Print a personalized greeting to the terminal:

echo "Hello, $name!"

9. Save the file and return to the terminal.

10. The program may by default not be executable, as this poses a security threat. To make the program executable, we'll use the command chmod:

$ chmod 755 hello.sh

*755 specifies a certain set of permissions we're assigning to the file. See unix_commands.pdf file for details on what this code means.

11. Execute the program:

$ ./hello.sh


★ Exercise 3

Write a bash script to identify the length, the complement, the reverse, and the reverse complement of a DNA sequence.

1. Using TextWrangler, create a new file called iseq.sh within the Bash_Scripts directory.

2. Insert a shebang that directs the terminal to bash within the iseq.sh file: #!/bin/bash

3. Prompt the user for a sequence and store it as a variable (seq) using read -p.

4. Complement the sequence using tr and store it as new variable (comp).

5. Reverse the sequence using rev and store it as a new variable (reverse).

6. Reverse complement a sequence using tr and rev and store it as a new variable (revcomp).

7. Calculate the length of the sequence using wc -c and store as a new variable (length).

8. Print to the shell the output of each of the above steps along with a brief description. For example:

echo ""
echo "Original sequence: $seq"
echo "Complement: $comp"
echo "Reverse: $reverse"
echo "Reverse complement: $revcomp"
echo ""

9. Return to your terminal window and execute the script using ./iseq.sh.

10. Edit iseq.sh to repeat indefinitely using a while loop as follows.

a. Add while before the read command.

while read -p "Enter a sequence: " seq

b. Insert a new line and the command do directly after the previous line containing while:

do

c. At the end of the script add a new line and the command done:

done

11. Return to your terminal window and execute the script again.

12. Submit your iseq.sh script on Canvas.

assignments/ex3.txt · Last modified: 2018/09/03 21:51 by dokuroot