<jupyter_start><jupyter_text>Working with Python: functions and modules Session 4: Using third party libraries- [Matplotlib](Matplotlib)- [Exercise 4.1](Exercise-4.1)- [BioPython](BioPython)- [Working with sequences](Working-with-sequences)- [Connecting with biological databases](Connecting-with-biological-databases)- [Exercise 4.2](Exercise-4.2)  Matplotlib[matplotlib](http://matplotlib.org/) is probably the single most used Python package for graphics. It provides both a very quick way to visualize data from Python and publication-quality figures in many formats.matplotlib.pyplot is a collection of command style functions that make matplotlib work like MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc. Let's start with a very simple plot.<jupyter_code>import matplotlib.pyplot as mpyplot
mpyplot.plot([1,2,3,4])
mpyplot.ylabel('some numbers')
mpyplot.show()<jupyter_output><empty_output><jupyter_text>`plot()` is a versatile command, and will take an arbitrary number of arguments. For example, to plot x versus y, you can issue the command:<jupyter_code>mpyplot.plot([1,2,3,4], [1,4,9,16])<jupyter_output><empty_output><jupyter_text>For every x, y pair of arguments, there is an **optional third argument** which is the format string that indicates the color and line type of the plot. The letters and symbols of the format string are from MATLAB, and you concatenate a color string with a line style string. The default format string is `b-`, which is a solid blue line. For example, to plot the above with red circles, you would chose `ro`.<jupyter_code>import matplotlib.pyplot as mpyplot
mpyplot.plot([1,2,3,4], [1,4,9,16], 'ro')
mpyplot.axis([0, 6, 0, 20])
mpyplot.show()<jupyter_output><empty_output><jupyter_text>`matplotlib` has a few methods in the **`pyplot` module** that make creating common types of plots faster and more convenient because they automatically create a Figure and an Axes object. The most widely used are:- [mpyplot.bar](http://matplotlib.org/api/pyplot_api.htmlmatplotlib.pyplot.bar) – creates a bar chart.- [mpyplot.boxplot](http://matplotlib.org/api/pyplot_api.htmlmatplotlib.pyplot.boxplot) – makes a box and whisker plot.- [mpyplot.hist](http://matplotlib.org/api/pyplot_api.htmlmatplotlib.pyplot.hist) – makes a histogram.- [mpyplot.plot](http://matplotlib.org/api/pyplot_api.htmlmatplotlib.pyplot.plot) – creates a line plot.- [mpyplot.scatter](http://matplotlib.org/api/pyplot_api.htmlmatplotlib.pyplot.scatter) – makes a scatter plot.Calling any of these methods will automatically setup `Figure` and `Axes` objects, and draw the plot. Each of these methods has different parameters that can be passed in to modify the resulting plot.The [Pyplot tutorial](http://matplotlib.org/users/pyplot_tutorial.html) is where these simple examples above are coming from. More could be learn from it if you wish during your own time.Let's now try to plot the GC content along the chain we have calculated during the previous session, while solving the Exercises 3.3 and 3.4.<jupyter_code>seq = 'ATGGTGCATCTGACTCCTGAGGAGAAGTCTGCCGTTACTGCCCTGTGGGGCAAGGTG'
gc = [40.0, 60.0, 80.0, 60.0, 40.0, 60.0, 40.0, 40.0, 40.0, 60.0, 
      40.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 60.0, 
      60.0, 40.0, 40.0, 40.0, 40.0, 40.0, 60.0, 60.0, 80.0, 80.0, 
      80.0, 60.0, 40.0, 40.0, 20.0, 40.0, 60.0, 80.0, 80.0, 80.0, 
      80.0, 60.0, 60.0, 60.0, 80.0, 80.0, 100.0, 80.0, 60.0, 60.0, 
      60.0, 40.0, 60.0]
window_ids = range(len(gc))

import matplotlib.pyplot as mpyplot
mpyplot.plot(window_ids, gc, '--' )
mpyplot.xlabel('5 bases window id along the sequence')
mpyplot.ylabel('%GC')
mpyplot.title('GC plot for sequence\n' + seq)
mpyplot.show()<jupyter_output><empty_output><jupyter_text>Exercise 4.1Re-use the GapMinder dataset to plot, in Jupyter using Matplotlib, from the world data the life expectancy against GDP per capita for 1957 and 2007 using a scatter plot, add title to your graph as well as a legend.  BioPythonThe goal of Biopython is to make it as easy as possible to use Python for bioinformatics by creating high-quality, reusable modules and classes. Biopython features include parsers for various Bioinformatics file formats (BLAST, Clustalw, FASTA, Genbank,...), access to online services (NCBI, Expasy,...), interfaces to common and not-so-common programs (Clustalw, DSSP, MSMS...), a standard sequence class, various clustering modules, a KD tree data structure etc. and documentation as well as a tutorial: http://biopython.org/DIST/docs/tutorial/Tutorial.html.  Working with sequences We can create a sequence by defining a `Seq` object with strings. `Bio.Seq()` takes as input a string and converts in into a Seq object. We can print the sequences, individual residues, lengths and use other functions to get summary statistics.<jupyter_code># Creating sequence
from Bio.Seq import Seq
my_seq = Seq("AGTACACTGGT")
print(my_seq)
print(my_seq[10])
print(my_seq[1:5])
print(len(my_seq))
print(my_seq.count("A"))<jupyter_output><empty_output><jupyter_text>We can use functions from `Bio.SeqUtils` to get idea about a sequence<jupyter_code># Calculate the molecular weight
from Bio.SeqUtils import GC, molecular_weight
print(GC(my_seq))
print(molecular_weight(my_seq))<jupyter_output><empty_output><jupyter_text>One letter code protein sequences can be converted into three letter codes using `seq3` utility<jupyter_code>from Bio.SeqUtils import seq3
print(seq3(my_seq))<jupyter_output><empty_output><jupyter_text>Alphabets defines how the strings are going to be treated as sequence object. `Bio.Alphabet` module defines the available alphabets for Biopython. `Bio.Alphabet.IUPAC` provides basic definition for DNA, RNA and proteins.<jupyter_code>from Bio.Alphabet import IUPAC
my_dna = Seq("AGTACATGACTGGTTTAG", IUPAC.unambiguous_dna)
print(my_dna)
print(my_dna.alphabet)
my_dna.complement()
my_dna.reverse_complement()
my_dna.translate()<jupyter_output><empty_output><jupyter_text>Parsing sequence file format: FASTA files Sequence files can be parsed and read the same way we read other files.<jupyter_code>with open( "data/glpa.fa" ) as f:
    print(f.read())<jupyter_output><empty_output><jupyter_text>Biopython provides specific functions to allow parsing/reading sequence files.<jupyter_code># Reading FASTA files
from Bio import SeqIO

with open("data/glpa.fa") as f:
    for protein in SeqIO.parse(f, 'fasta'):
        print(protein.id)
        print(protein.seq)<jupyter_output><empty_output><jupyter_text>Sequence objects can be written into files using file handles with the function `SeqIO.write()`. We need to provide the name of the output sequence file and the sequence file format.<jupyter_code># Writing FASTA files
from Bio import SeqIO
from Bio.SeqRecord import SeqRecord
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC

sequence = 'MYGKIIFVLLLSEIVSISASSTTGVAMHTSTSSSVTKSYISSQTNDTHKRDTYAATPRAHEVSEISVRTVYPPEEETGERVQLAHHFSEPEITLIIFG'
 
seq = Seq(sequence, IUPAC.protein)
protein = [SeqRecord(seq, id="THEID", description='a description'),]

with open( "biopython.fa", "w") as f:
    SeqIO.write(protein, f, 'fasta')

with open( "biopython.fa" ) as f:
    print(f.read())<jupyter_output><empty_output><jupyter_text>Connecting with biological databases Sequences can be searched and downloaded from public databases.<jupyter_code># Read FASTA file from NCBI GenBank
from Bio import Entrez

Entrez.email = '<EMAIL>' # Always tell NCBI who you are
handle = Entrez.efetch(db="nucleotide", id="71066805", rettype="gb")
seq_record = SeqIO.read(handle, "gb")
handle.close()

print(seq_record.id, 'with', len(seq_record.features), 'features')
print(seq_record.seq)
print(seq_record.format("fasta"))
# Read SWISSPROT record
from Bio import ExPASy

handle = ExPASy.get_sprot_raw('HBB_HUMAN')
prot_record = SeqIO.read(handle, "swiss")
handle.close()

print(prot_record.description)
print(prot_record.seq)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10470164991936484, 'fraction_numerical': 0.028036223793573997, 'mean_word_length': 5.517380759902991, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '15255', 'license': 'Unlicense', 'n_tokens_mistral': 2868, 'n_tokens_neox': 2627, 'n_words': 877, 'path': 'python_fm_4.ipynb', 'repo_name': 'pycam/python-functions-and-modules', 'size': '8075'}