diff --git a/docs/05-writing-scripts.md b/docs/05-writing-scripts.md index 4ea0ae7..a451a6d 100644 --- a/docs/05-writing-scripts.md +++ b/docs/05-writing-scripts.md @@ -13,39 +13,20 @@ - How can we automate a commonly used set of commands? - ## Writing files We've been able to do a lot of work with files that already exist, but what if we want to write our own files? We're not going to type in a FASTA file, but we'll see as we go through other tutorials, there are a lot of reasons we'll want to write a file, or edit an existing file. -To add text to files, we're going to use a text editor called Nano. We're going to create a file to take notes about what we've been doing with the data files in `~/obss_2023/commandline/shell_data/untrimmed_fastq`. +To add text to files, we're going to use a text editor called Nano. We're going to create a file to take notes about what we've been doing with the data files in `~/shell_data/untrimmed_fastq`. This is good practice when working in bioinformatics. We can create a file called `README.txt` that describes the data files in the directory or documents how the files in that directory were generated. As the name suggests, it's a file that we or others should read to understand the information in that directory. -Let's change our working directory to `~/obss_2023/commandline/shell_data/untrimmed_fastq` using `cd`, +Let's change our working directory to `~/shell_data/untrimmed_fastq` using `cd`, then run `nano` to create a file called `README.txt`: ```bash -$ cd ~/obss_2023/commandline/shell_data/untrimmed_fastq +$ cd ~/shell_data/untrimmed_fastq $ nano README.txt ``` @@ -55,7 +36,7 @@ You should see something like this: The text at the bottom of the screen shows the keyboard shortcuts for performing various tasks in `nano`. We will talk more about how to interpret this information soon. -::::::::::::::::::::::::::::::::::::::::: callout + ## Which Editor? @@ -80,7 +61,7 @@ your computer's start menu, the editor may want to save files in your desktop or documents directory instead. You can change this by navigating to another directory the first time you "Save As..." -:::::::::::::::::::::::::::::::::::::::::::::::::: + Let's type in a few lines of text. Describe what the files in this directory are or what you've been doing with them. @@ -91,7 +72,7 @@ press Return to accept the suggested default of `README.txt`. Once our file is saved, we can use Ctrl\-X to quit the `nano` editor and return to the shell. -::::::::::::::::::::::::::::::::::::::::: callout + ## Control, Ctrl, or ^ Key @@ -111,26 +92,19 @@ In `nano`, along the bottom of the screen you'll see `^G Get Help ^O WriteOut`. This means that you can use Ctrl\-G to get help and Ctrl\-O to save your file. -:::::::::::::::::::::::::::::::::::::::::::::::::: - -Now you've written a file. You can take a look at it with `less` or `cat`, or open it up again and edit it with `nano`. - -::::::::::::::::::::::::::::::::::::::: challenge -## Exercise -Open `README.txt` and add the date to the top of the file and save the file. +Now you've written a file. You can take a look at it with `less` or `cat`, or open it up again and edit it with `nano`. -::::::::::::::: solution -## Solution +!!! dumbbell "Exercise" -Use `nano README.txt` to open the file. -Add today's date and then use Ctrl\-X followed by `y` and Enter to save. + Open `README.txt` and add the date to the top of the file and save the file. -::::::::::::::::::::::::: +??? success "Solution" -:::::::::::::::::::::::::::::::::::::::::::::::::: + Use `nano README.txt` to open the file. + Add today's date and then use Ctrl\-X followed by `y` and Enter to save. ## Writing scripts @@ -140,92 +114,97 @@ One thing we will commonly want to do with sequencing results is pull out bad re We're going to create a new file to put this command in. We'll call it `bad-reads-script.sh`. The `sh` isn't required, but using that extension tells us that it's a shell script. -```bash -$ nano bad-reads-script.sh -``` +!!! terminal "code" + + ```bash + $ nano bad-reads-script.sh + ``` Bad reads have a lot of N's, so we're going to look for `NNNNNNNNNN` with `grep`. We want the whole FASTQ record, so we're also going to get the one line above the sequence and the two lines below. We also want to look in all the files that end with `.fastq`, so we're going to use the `*` wildcard. -```bash -grep -B1 -A2 -h NNNNNNNNNN *.fastq | grep -v '^--' > scripted_bad_reads.txt -``` +!!! terminal "code" + + ```bash + grep -B1 -A2 -h NNNNNNNNNN *.fastq | grep -v '^--' > scripted_bad_reads.txt + ``` + -::::::::::::::::::::::::::::::::::::::::: callout ## Custom `grep` control We introduced the `-v` option in [the previous episode](04-redirection.md), now we are using `-h` to "Suppress the prefixing of file names on output" according to the documentation shown by `man grep`. -:::::::::::::::::::::::::::::::::::::::::::::::::: + Type your `grep` command into the file and save it as before. Be careful that you did not add the `$` at the beginning of the line. Now comes the neat part. We can run this script. Type: -```bash -$ bash bad-reads-script.sh -``` - -It will look like nothing happened, but now if you look at `scripted_bad_reads.txt`, you can see that there are now reads in the file. +!!! terminal "Code" -::::::::::::::::::::::::::::::::::::::: challenge + ```bash + $ bash bad-reads-script.sh + ``` -## Exercise +It will look like nothing happened, but now if you look at `scripted_bad_reads.txt`, you can see that there are now reads in the file. -We want the script to tell us when it's done. -1. Open `bad-reads-script.sh` and add the line `echo "Script finished!"` after the `grep` command and save the file. -2. Run the updated script. +!!! dumbbell "Exercise" -::::::::::::::: solution + We want the script to tell us when it's done. + + 1. Open `bad-reads-script.sh` and add the line `echo "Script finished!"` after the `grep` command and save the file. + 2. Run the updated script. -## Solution + ??? success "Solution" -``` -$ bash bad-reads-script.sh -Script finished! -``` + ``` + $ bash bad-reads-script.sh + Script finished! + ``` -::::::::::::::::::::::::: -:::::::::::::::::::::::::::::::::::::::::::::::::: ## Making the script into a program We had to type `bash` because we needed to tell the computer what program to use to run this script. Instead, we can turn this script into its own program. We need to tell the computer that this script is a program by making the script file executable. We can do this by changing the file permissions. We talked about permissions in [an earlier episode](03-working-with-files.md). -First, let's look at the current permissions. +!!! terminal-2 "First, let's look at the current permissions." -```bash -$ ls -l bad-reads-script.sh -``` + ```bash + $ ls -l bad-reads-script.sh + ``` -```output --rw-rw-r-- 1 dcuser dcuser 0 Oct 25 21:46 bad-reads-script.sh -``` + ```output + -rw-rw-r-- 1 dcuser dcuser 0 Oct 25 21:46 bad-reads-script.sh + ``` We see that it says `-rw-r--r--`. This shows that the file can be read by any user and written to by the file owner (you). We want to change these permissions so that the file can be executed as a program. We use the command `chmod` like we did earlier when we removed write permissions. Here we are adding (`+`) executable permissions (`+x`). -```bash -$ chmod +x bad-reads-script.sh -``` +!!! terminal "code" -Now let's look at the permissions again. + ```bash + $ chmod +x bad-reads-script.sh + ``` -```bash -$ ls -l bad-reads-script.sh -``` +!!! terminal-2 "Now let's look at the permissions again." -```output --rwxrwxr-x 1 dcuser dcuser 0 Oct 25 21:46 bad-reads-script.sh -``` + ```bash + $ ls -l bad-reads-script.sh + ``` + + ```output + -rwxrwxr-x 1 dcuser dcuser 0 Oct 25 21:46 bad-reads-script.sh + ``` Now we see that it says `-rwxr-xr-x`. The `x`'s that are there now tell us we can run it as a program. So, let's try it! We'll need to put `./` at the beginning so the computer knows to look here in this directory for the program. -```bash -$ ./bad-reads-script.sh -``` +!!! terminal "code" + + ```bash + $ ./bad-reads-script.sh + ``` The script should run the same way as before, but now we've created our very own computer program! @@ -314,133 +293,10 @@ command line belongs to. So, if you are logged into AWS on the command line and the `curl` command above in the AWS terminal, the file will be downloaded to your AWS machine, not your local one. -### Moving files between your laptop and NeSI with Jupyterhub - -With Jupyterhub on NeSI, one of the easiest way to move small-medium sized files is to use the upload option on the file explorer panel - -![](fig/nesi_images/upload.png) - -And to download a file, you can right click on it in the explorer panel and select "Download" - -![](fig/nesi_images/download.png) - -::::::::::::::::::::::::::::::::::::::::: callout - -**Original instructions for downloading data from AWS** - -### Moving files between your laptop and your instance - AWS - -What if the data you need is on your local computer, but you need to get it _into_ the -cloud? There are also several ways to do this, but it's _always_ easier -to start the transfer locally. **This means if you're typing into a terminal, the terminal -should not be logged into your instance, it should be showing your local computer. If you're -using a transfer program, it needs to be installed on your local machine, not your instance.** - -## Transferring Data Between your Local Machine and the Cloud - -If you're using Windows with PuTTY instead of Git Bash, please select the alternative option here: - - -