<jupyter_start><jupyter_text>Python packages like Biopython and Squiggle will be used when dealing with biological sequence data in Python. Biopython installationpip install biopython<jupyter_code>!pip install biopython<jupyter_output>Collecting biopython
[?25l  Downloading https://files.pythonhosted.org/packages/a8/66/134dbd5f885fc71493c61b6cf04c9ea08082da28da5ed07709b02857cbd0/biopython-1.77-cp36-cp36m-manylinux1_x86_64.whl (2.3MB)
[K     |████████████████████████████████| 2.3MB 4.9MB/s 
[?25hRequirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from biopython) (1.18.5)
Installing collected packages: biopython
Successfully installed biopython-1.77<jupyter_text>Squiggle installationpip install Squiggle<jupyter_code>!pip install Squiggle<jupyter_output>Collecting Squiggle
  Downloading https://files.pythonhosted.org/packages/cb/a8/072034a88835017246761eeaa587022483895e7bcdd2402260180c0fe1a3/squiggle-0.3.1-py2.py3-none-any.whl
Requirement already satisfied: click in /usr/local/lib/python3.6/dist-packages (from Squiggle) (7.1.2)
Requirement already satisfied: bokeh>=0.13.0 in /usr/local/lib/python3.6/dist-packages (from Squiggle) (2.1.1)
Requirement already satisfied: tqdm in /usr/local/lib/python3.6/dist-packages (from Squiggle) (4.41.1)
Requirement already satisfied: numpy in /usr/local/lib/python3.6/dist-packages (from Squiggle) (1.18.5)
Collecting pyfaidx>=0.5.4.2
  Downloading https://files.pythonhosted.org/packages/09/91/10e16d419aa1fd8d2889b70843989640f9b88f10a792b6c73da0ebfd1966/pyfaidx-0.5.9.1.tar.gz
Collecting python-box
  Downloading https://files.pythonhosted.org/packages/ea/1b/c9808413d5e9a4cb68b177cab85ea7ed143670517f4650bfcad0d9834c50/python_box-5.1.0-py3-none-any.whl
Requirement already satisfied: PyYAML>=3.10 in /usr/l[...]<jupyter_text>DNA sequence data usually are contained in a file format called “fasta” format. Fasta format is simply a single line prefixed by the greater than symbol that contains annotations and another line that contains the sequence:“AAGGTGAGTGAAATCTCAACACGAGTATGGTTCTGAGAGTAGCTCTGTAACTCTGAGG”The file can contain one or many DNA sequences. There are lots of other formats, but fasta is the most common. We will use Bio.SeqIO from Biopython for parsing DNA sequence data(fasta) as it provides a simple uniform interface to input and output assorted sequence file formats.<jupyter_code>from Bio import SeqIO
for sequence in SeqIO.parse('/sample.fa', "fasta"):
    print(sequence.id)
    print(sequence.seq)
    print(len(sequence))<jupyter_output>ENST00000435737.5
ATGTTTCGCATCACCAACATTGAGTTTCTTCCCGAATACCGACAAAAGGAGTCCAGGGAATTTCTTTCAGTGTCACGGACTGTGCAGCAAGTGATAAACCTGGTTTATACAACATCTGCCTTCTCCAAATTTTATGAGCAGTCTGTTGTTGCAGATGTCAGCAACAACAAAGGCGGCCTCCTTGTCCACTTTTGGATTGTTTTTGTCATGCCACGTGCCAAAGGCCACATCTTCTGTGAAGACTGTGTTGCCGCCATCTTGAAGGACTCCATCCAGACAAGCATCATAAACCGGACCTCTGTGGGGAGCTTGCAGGGACTGGCTGTGGACATGGACTCTGTGGTACTAAATGAAGTCCTGGGGCTGACTCTCATTGTCTGGATTGACTGA
390
ENST00000419127.5
ATGTTTCGCATCACCAACATTGAGTTTCTTCCCGAATACCGACAAAAGGAGTCCAGGGAATTTCTTTCAGTGTCACGGACTGTGCAGCAAGTGATAAACCTGGTTTATACAACATCTGCCTTCTCCAAATTTTATGAGCAGTCTGTTGTTGCAGATGTCAGCAACAACAAAGGCGGCCTCCTTGTCCACTTTTGGATTGTTTTTGTCATGCCACGTGCCAAAGGCCACATCTTCTGTGAAGACTGTGTTGCCGCCATCTTGAAGGACTCCATCCAGACAAGCATCATAAACCGGACCTCTGTGGGGAGCTTGCAGGGACTGGCTGTGGACATGGACTCTGTGGTACTAAATGACAAAGGCTGCTCTCAGTACTTCTATGCAGAGCATCTGTCTCTCCACTACCCGCTGGAGATTTCTGCAGCCTCAGGGAGGCTGATGTGTCACTTCAAGCTGGTGGCCATAGTGGGCTACCTGATTCGTCTCTCAATCAAGTCCATCCAAATCGAAGCCGACAACTGTGTCACTGACTCCCTGACCATTTACGACTCCCTTTTGCCCATCCGGAGCAG[...]<jupyter_text>There are **3 general approaches** to encode sequence data:1.Ordinal encoding DNA Sequence2.One-hot encoding DNA Sequence3.DNA sequence as a “language”, known as k-mer counting  **Ordinal encoding DNA sequence data**We need to encode each nitrogen bases as an ordinal value. For example “ATGC” becomes [0.25, 0.5, 0.75, 1.0]. Any other base such as “N” can be a 0.<jupyter_code>#Ordinal encoding DNA sequence data¶
# function to convert a DNA sequence string to a numpy array
# converts to lower case, changes any non 'acgt' characters to 'n'
import numpy as np
import re
def string_to_array(seq_string):
    seq_string = seq_string.lower()
    seq_string = re.sub('[^acgt]', 'z', seq_string)
    seq_string = np.array(list(seq_string))
    return seq_string
    
# create a label encoder with 'acgtn' alphabet
from sklearn.preprocessing import LabelEncoder
label_encoder = LabelEncoder()
label_encoder.fit(np.array(['a','c','g','t','z']))<jupyter_output><empty_output><jupyter_text>Here is a function to encode a DNA sequence string as an ordinal vector. It returns a NumPy array with A=0.25, C=0.50, G=0.75, T=1.00, n=0.00.<jupyter_code># function to encode a DNA sequence string as an ordinal vector
# returns a numpy vector with a=0.25, c=0.50, g=0.75, t=1.00, n=0.00
def ordinal_encoder(my_array):
    integer_encoded = label_encoder.transform(my_array)
    float_encoded = integer_encoded.astype(float)
    float_encoded[float_encoded == 0] = 0.25 # A
    float_encoded[float_encoded == 1] = 0.50 # C
    float_encoded[float_encoded == 2] = 0.75 # G
    float_encoded[float_encoded == 3] = 1.00 # T
    float_encoded[float_encoded == 4] = 0.00 # anything else, lets say z
    return float_encoded

seq_test = 'TTCAGCCAGTG'
ordinal_encoder(string_to_array(seq_test))<jupyter_output><empty_output><jupyter_text>**One-hot encoding DNA Sequence**It is widely used in deep learning methods and lends itself well to algorithms like CNN.For instance, "ATGC" will become [0,0,0,1], [0,0,1,0], [0,1,0,0], [1,0,0,0]. Then, it is turned into a 2-D Array.<jupyter_code>#One-hot encoding DNA sequence data¶
# function to one-hot encode a DNA sequence string
# non 'acgt' bases (n) are 0000
from sklearn.preprocessing import OneHotEncoder
def one_hot_encoder(seq_string):
    int_encoded = label_encoder.transform(seq_string)
    onehot_encoder = OneHotEncoder(sparse=False, dtype=int)
    int_encoded = int_encoded.reshape(len(int_encoded), 1)
    onehot_encoded = onehot_encoder.fit_transform(int_encoded)
    onehot_encoded = np.delete(onehot_encoded, -1, 1)
    return onehot_encoded
#let’s try it out with a simple short sequence:

seq_test = 'GAATTCTCGAA'
one_hot_encoder(string_to_array(seq_test))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09027336300063572, 'fraction_numerical': 0.057692307692307696, 'mean_word_length': 6.759556103575832, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '200139', 'license': 'MIT', 'n_tokens_mistral': 2598, 'n_tokens_neox': 2293, 'n_words': 480, 'path': 'DNA_Sequencing.ipynb', 'repo_name': 'Abivarsh/DNA-Sequence-Analysis', 'size': '6292'}