☰ Menu

      Bioinformatics: Command Line/R Prerequisites 2020

Home
Introduction
Intro to the Workshop and Core
Schedule
Resources
Zoom help
Slack help
Software and Links
Cheat Sheets
Intro CLI
Logging in
Introduction to the Command Line part 1
Introduction to the Command Line part 2
The cluster and modules
Introduction to Python
Python Part1, Background
Python Part2, Data Types
Python Part2, Solutions
Python Part3, Flow Control
Python Part3, Solutions
Python Part4, Working with Files
Python Part4, Solutions
Advanced CLI
Advanced Command Line
Advanced Challenge Solutions
A Simple Bioinformatics Workflow
Software Installation
Using make and cmake
Using Conda
Getting set up with AWS
AWS Event Engine
Starting and Stopping your AWS Instance
Introduction to R
Introduction to R
Introduction to tidyverse
Organizing, manipulating, and visualizing data in the tidyverse
Advanced R
Linear models in R
ETC
Closing thoughts
Github page
Biocore website

Intro to Python

Python

Schedule

Part 1: Intro to Python

Why learn Python?

Python

https://businessoverbroadway.com/2019/01/13/programming-languages-most-used-and-recommended-by-data-scientists/


Goals for this section


Question

1) Click “YES” if you have ever programmed in Python before.

2) Click “YES” if you have ever programmed in any language before.


Background

What is a programming language and why do we need it?

Speaking to a computer in its native language is tedious and complicated. A programming language is a way for humans to describe a set of operations to a computer in a more abstract and understandable way. A helper program then translates our description of the operations into a set of instructions (machine code) for the computer to carry out.

Some day we may develop a programming language that allows us to communicate our instructions to the computer in our native language (Alexa, turn on the TV). Except for simple cases, this option doesn’t exist yet, largely because human languages are complicated and instructions can be difficult to understand (even for other humans).

In order for the helper program to work properly, we need to use a concise language:

Specifically in Python:

PythonInterpreter


A brief history of Python


Interesting features of Python


Base Python and the extensive package ecosystem


Writing Python Code

You can work directly on the interactive Python terminal, but eventually you might want a record of your work. One option is to edit your code in a text editor, then run it in the terminal. This is also a great way to test your code piece by piece as you work through developing or editing your program.

Lots of tools have been developed to help with writing computer code. Two popular options are Visual Studio Code, and Atom Editor. Both work well and are available for free. Download and install either one of these if you want, or use a text editor you already have.

When working directly on the server, Nano, Vim, and Emacs are all popular editors.


Your first Python program

Hello, World! is traditionally the first program to write in any new programming language.

Connect to the server

ssh <user>@tadpole.genomecenter.ucdavis.edu

Then follow this example:

export PATH=/share/biocore/shunter/opt/bin/:$PATH
alias ll='ls -alFh --color'

mkdir -p  /share/workshop/prereq_workshop/$USER/python

cd /share/workshop/prereq_workshop/$USER/python

ipython3

https://ipython.readthedocs.io/en/stable/index.html

Python 3.8.2 (default, May 5 2020, 12:05:43) Type 'copyright', 'credits' or 'license' for more information IPython 7.14.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]:

Note that IPython is an enhanced version of the Python shell. Among many other things, it has better support for pasting Python code into the interactive terminal without messing up indentation. Some systems don’t have ipython installed, in which case you can usually run “python” or “python3”.

Use the interactive Python interpreter to run a short program:

>>> print("Hello World!")
Hello World!

Once you have successfully run `python3` and printed "Hello World!", mark "Yes" in zoom. Post questions or problems to the Slack channel.

Now we need to exit the Python interpreter. This can be done by typing exit() or CTRL+D.

Next, lets try writing a “Hello World” Python script.

nano helloworld.py

Enter the same print statement we used above, press Ctrl+O to write the changes, then Ctrl+X to exit the file.

Check that the file you created is present and that your changes were saved successfully, then run the script:

ll

cat helloworld.py

python3 helloworld.py
shunter@tadpole:python$ ll total 5.0K drwxrwxr-x 2 shunter shunter 2.0K Oct 4 20:11 ./ drwxrwx--- 26 shunter shunter 2.0K Oct 4 20:14 ../ -rw-rw-r-- 1 shunter shunter 23 Oct 4 20:09 helloworld.py
shunter@tadpole:python$ cat helloworld.py print("Hello, World!") shunter@tadpole:python$ python3 helloworld.py Hello, World!

Once you have successfully created your Python script and run it, mark "Yes" in zoom. Post questions or problems to the Slack channel.


This is the end of the Intro section. Next we will talk about data types and do more hands-on exercies.