<jupyter_start><jupyter_text>biopython IntroductionFor the whole documentation, see http://biopython.org/wiki/Biopython, for the long tutorial see http://biopython.org/DIST/docs/tutorial/Tutorial.html, for the cookbook see http://biopython.org/wiki/Category%3ACookbook.biopython is a universal bioinformatic Python package, coordinated by the Open Bioinformatics Foundation (https://www.open-bio.org/wiki/Main_Page, also coordinates - amongst other projects - BioPerl http://bioperl.org/ and BioJava http://biojava.org/). If you use the Anaconda python distribution, the easiest way to install biopython is by using the Anaconda Navigator. If you use pip, you can install biopython with the command "pip install biopython".Once you have installed biopython, you can import it to a Python script like this (not that you may have to import submodules of biopython to access all of its function):<jupyter_code>import Bio<jupyter_output><empty_output><jupyter_text>1. Storing DNA, RNA and Amino Acid Sequences<jupyter_code>from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
# An unambiguous DNA sequence
unambiguous = Seq("ACGT", IUPAC.unambiguous_dna)
print(unambiguous)
print(unambiguous.alphabet)
print(unambiguous.count("AC")) # Returns only non-overlapping occurences
print(len(unambiguous))
# An ambiguous DNA sequence
ambiguous = Seq("ACGT", IUPAC.ambiguous_dna)
ambiguous
# An RNA sequence
# An ambiguous DNA sequence
ambiguous = Seq("ACGU", IUPAC.ambiguous_rna)
ambiguous
# An amino acid sequence
protein = Seq("TRUMP", IUPAC.protein)
protein<jupyter_output><empty_output><jupyter_text>2. Translation and transcription<jupyter_code># Complementary DNA
sequence = Seq("AGCCCTYA", IUPAC.ambiguous_dna)
sequence.complement()
# Reverse-complementary DNA
sequence.reverse_complement()
# DNA->RNA
dna = Seq("AAATTTCCCGGG", IUPAC.unambiguous_dna)
rna = dna.transcribe()
rna
# RNA->Proteins
print(dna.translate()) # The DNA's or RNA's length be a multiple of 3
print(rna.translate())<jupyter_output>KFPG
KFPG<jupyter_text>3. Access to common databases<jupyter_code># NCBI Entrez...
from Bio import Entrez
# ...get all databases...
handle = Entrez.einfo()
result = handle.read()
handle.close()
result
# ...look up in pubmed
Entrez.email = ""
handle = Entrez.esearch(db="pubmed", term="Vibrio")
record = Entrez.read(handle)
record
# KEGG...
from Bio.KEGG import REST
from Bio.KEGG import Enzyme
# ...get pathways for Vibrio alginolyticus...
pathways = REST.kegg_list("pathway", "vag").read()
# ...select a pathway...
pathway_line = pathways.rstrip().split("\n")[0]
entry, description = pathway_line.split("\t")
entry
# ...and analyze this pathway
pathway_file = REST.kegg_get(entry).read()<jupyter_output><empty_output><jupyter_text>4. Sequence Alignments<jupyter_code># Simple Needleman-Wunsch (note: two cells below, a more complex alternative is shown)
from Bio import pairwise2
from Bio import SeqIO

seq1 = Seq("AAATTGGGCCGG", IUPAC.protein)
seq2 = Seq("AAAGGTTAAA", IUPAC.protein)

alignments = pairwise2.align.globalxx(seq1, seq2)
print(alignments[0]) # First optimal alignment
# Simple Smith-Watherman (note: two cells below, a more complex alternative is shown)
alignments2 = pairwise2.align.localxx(seq1, seq2)
print(alignments2[0]) # First optimal alignment
# Better global alignment alternative with penalizing options
from Bio.SubsMat.MatrixInfo import blosum62

alignments3 = pairwise2.align.globalds(seq1, seq2, blosum62, -10, -0.5)
print(alignments3[0])
# Better local alignment alternative with penalizing options
alignments4 = pairwise2.align.localds(seq1, seq2, blosum62, -10, -1)
print(alignments4[0])<jupyter_output>('AAATTGGGCCGG', '--AAAGGTTAAA', 16.0, 2, 7)<jupyter_text>5. BLAST<jupyter_code># The biopython option is not reliable. Use the command line instead, e.g.
blastn_output = run("blastn", "-query", "temp_in.txt", "-out", "temp_out.txt", "-db", "nt", "-outfmt", '6 sacc pident qstart qend evalue bitscore stitle sscinames sskingdoms', "-max_target_seqs", "5")<jupyter_output><empty_output><jupyter_text>6. Sequence Motif Analysis<jupyter_code>from Bio import motifs
# Generate nucleotide count report
seqset = [Seq("ACGTG"),
          Seq("ATGTT"),
          Seq("TTTTT")]
motif = motifs.create(seqset)
motif.counts
# Access a line
motif.counts["T"]
# Get consensus
motif.consensus
# Get anticonsensus
motif.anticonsensus
# Get degenerate consensus
motif.degenerate_consensus
# Create a logo (using a web service)
motif.weblogo("./biopython/motif.png")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11497563136907399, 'fraction_numerical': 0.011962782454585734, 'mean_word_length': 5.935483870967742, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '220619', 'license': 'MIT', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1500, 'n_words': 419, 'path': 'python_module_tutorials/biopython.ipynb', 'repo_name': 'Paulocracy/python_tutorials', 'size': '4514'}