<jupyter_start><jupyter_text>DNA Sequence Statistics   Reading Data<jupyter_code>import screed # A Python library for reading FASTA and FASQ file format.
def readFastaFile(inputfile):
    """
    Reads and returns file as FASTA format with special characters removed.
    """
    with screed.open(inputfile) as seqfile:
        for read in seqfile:
            seq = read.sequence
    return seq<jupyter_output><empty_output><jupyter_text>Read FASTA/FASQ File<jupyter_code>den1 = readFastaFile("../data/den1.fasta")
den2 = readFastaFile("../data/den2.fasta")
den3 = readFastaFile("../data/den3.fasta")
den4 = readFastaFile("../data/den4.fasta")<jupyter_output><empty_output><jupyter_text>Statistical Aanalysis  Length of a DNA sequence<jupyter_code># length of DEN1 
len(den1)
# length of DEN2
len(den2)
# length of DEN3
len(den3)
len(den4)<jupyter_output><empty_output><jupyter_text>Base composition of a DNA sequence¶<jupyter_code>def count_base(seq): 
    base_counts = {} 
    for base in seq: 
        if base in base_counts: 
            base_counts[base] += 1 
        else: 
            base_counts[base] = 1 
    return base_counts
# Let's call the function count_base
counts = count_base(den1)
print(counts)
from collections import Counter
def count_base(seq): 
    base_counts = Counter(seq)
    return base_counts
bases = count_base(den1)
print(bases)<jupyter_output>Counter({'A': 3426, 'G': 2770, 'T': 2299, 'C': 2240})<jupyter_text>Frequency with Counter<jupyter_code>from collections import Counter
# Base composition of DEN1
freq1 = Counter(den1)
print(freq1)
# Base composition of DEN2
freq2 = Counter(den2)
print(freq2)
# Base composition of DEN3
freq3 = Counter(den3)
print(freq3)
# Base composition of DEN4
freq4 = Counter(den4)
print(freq2)<jupyter_output>Counter({'A': 3553, 'G': 2713, 'T': 2257, 'C': 2200})<jupyter_text>GC Content of DNA<jupyter_code>import screed # A Python library for reading FASTA and FASQ file format.
def readFastaFile(inputfile):
    """
    Reads and returns file as FASTA format with special characters removed.
    """
    with screed.open(inputfile) as seqfile:
        for read in seqfile:
            seq = read.sequence
    return seq

def calculate_gc_content(seq): 
    """
    Take DNA sequence as input and calculate the GC content.
    """
    no_of_g = seq.count("G")
    no_of_c = seq.count("C")
    total = no_of_g + no_of_c 
    gc = total/len(seq) * 100
    
    return gc 
# Calculate GC Content of DEN1
den1 = readFastaFile("../data/den1.fasta")
result1 = calculate_gc_content(den1)
print(result1) 
# Calculate GC Content of DEN2
den2 = readFastaFile("../data/den2.fasta")
result2 = calculate_gc_content(den2)
print(result2)
# Calculate GC Content of DEN3
den3 = readFastaFile("../data/den3.fasta")
result3 = calculate_gc_content(den3)
print(result3)
# Calculate GC Content of DEN4
den4 = readFastaFile("../data/den4.fasta")
result4 = calculate_gc_content(den4)
print(result4)<jupyter_output>47.1217954737534<jupyter_text>AT Content of DNA<jupyter_code>import screed # A Python library for reading FASTA and FASQ file format.
def readFastaFile(inputfile):
    """
    Reads and returns file as FASTA format with special characters removed.
    """
    with screed.open(inputfile) as seqfile:
        for read in seqfile:
            seq = read.sequence
    return seq

def calculate_at_content(seq): 
    """
    Take DNA sequence as input and calculate the AT content.
    """
    no_of_a = seq.count("A")
    no_of_t = seq.count("T")
    total = no_of_a + no_of_t
    at = total/len(seq) * 100
    
    return at 
# Calculate AT Content of DEN1
den1 = readFastaFile("../data/den1.fasta")
result1 = calculate_at_content(den1)
print(result1)
# Calculate AT Content of DEN2
den2 = readFastaFile("../data/den2.fasta")
result2 = calculate_at_content(den2)
print(result2)
# Calculate AT Content of DEN3
den3 = readFastaFile("../data/den3.fasta")
result3 = calculate_at_content(den3)
print(result3)
# Calculate AT Content of DEN4
den4 = readFastaFile("../data/den4.fasta")
result4 = calculate_at_content(den4)
print(result4)<jupyter_output>52.8782045262466<jupyter_text>A sliding window analysis of GC content<jupyter_code>import screed # A Python library for reading FASTA and FASQ file format.
def readFastaFile(inputfile):
    """
    Reads and returns file as FASTA format with special characters removed.
    """
    with screed.open(inputfile) as seqfile:
        for read in seqfile:
            seq = read.sequence
    return seq

def calculate_gc_content(seq): 
    """
    Take DNA sequence as input and calculate the GC content.
    """
    no_of_g = seq.count("G")
    no_of_c = seq.count("C")
    total = no_of_g + no_of_c 
    gc = total/len(seq) * 100
    
    return gc 
# Calculate the GC content of nucleotides 1-2000 of the Dengue genome(DEN-1)
seq = readFastaFile("../data/den1.fasta")
calculate_gc_content(seq[1:2001])
# Calculate the GC content of nucleotides 2001-4000 of the Dengue genome(DEN-1)
seq = readFastaFile("../data/den1.fasta")
calculate_gc_content(seq[2001:4001])
# Calculate the GC content of nucleotides 4001-6000 of the Dengue genome(DEN-1)
seq = readFastaFile("../data/den1.fasta")
calculate_gc_content(seq[4001:6001])
# Calculate the GC content of nucleotides 6001-8000 of the Dengue genome(DEN-1)
seq = readFastaFile("../data/den1.fasta")
calculate_gc_content(seq[6001:8001])
# Calculate the GC content of nucleotides 8001-10000 of the Dengue genome(DEN-1)
seq = readFastaFile("../data/den1.fasta")
calculate_gc_content(seq[8001:10001])
# Calculate the GC content of nucleotides 10001-10735 of the Dengue genome(DEN-1)
seq = readFastaFile("../data/den1.fasta")
calculate_gc_content(seq[10001:10735])<jupyter_output><empty_output><jupyter_text>GC content of the non-overlapping sub-sequences of size k<jupyter_code>def calculate_gc(seq):
    """ 
    Returns percentage of G and C nucleotides in a DNA sequence.
    """
    gc = 0 
    for base in seq:
        if base in "GCgc":
            gc += 1 
        else:
            gc = 1 
    return gc/len(seq) * 100

def gc_subseq(seq, k=2000):
    """
    Returns GC content of non − overlapping sub− sequences of size k.
    The result is a list.
    """
    res = [] 
    for i in range(0, len(seq)-k+1, k):
        subseq = seq[i:i+k]
        gc = calculate_gc(subseq)
        res.append(gc)
    return gc 
    
seq = readFastaFile("../data/den1.fasta")
gc_subseq(seq, 2000)<jupyter_output><empty_output><jupyter_text>K-mer Analysis<jupyter_code>def build_kmers(sequence, ksize):
    kmers = []
    n_kmers = len(sequence) - ksize + 1

    for i in range(n_kmers):
        kmer = sequence[i:i + ksize]
        kmers.append(kmer)

    return kmers
seq = readFastaFile("../data/den1.fasta")
# Dimer
km2 = build_kmers(seq, 2)
# Count dimer
from collections import Counter
dimer = Counter(km2)
print(dimer)
# Trimer 
km3 = build_kmers(seq, 3)
# Count trimer
trimer = Counter(km3)
print(trimer)<jupyter_output>Counter({'AAA': 388, 'GGA': 354, 'GAA': 346, 'AGA': 334, 'TGG': 321, 'ACA': 294, 'ATG': 292, 'AAG': 280, 'CAA': 273, 'GAG': 249, 'AAC': 237, 'AGG': 237, 'CCA': 236, 'CAG': 227, 'TGA': 218, 'AAT': 203, 'CAT': 203, 'TCA': 202, 'CAC': 198, 'GAC': 197, 'GTG': 192, 'CTG': 186, 'AGC': 184, 'GAT': 184, 'ACC': 176, 'GCA': 169, 'TTG': 162, 'TGT': 160, 'GGG': 160, 'ACT': 156, 'GCT': 144, 'GGT': 141, 'ATA': 141, 'TTT': 139, 'ATT': 138, 'ATC': 137, 'GTT': 136, 'AGT': 135, 'TCT': 134, 'TAG': 133, 'TTC': 133, 'TGC': 133, 'GGC': 132, 'CTA': 131, 'GCC': 129, 'CTC': 121, 'CCT': 121, 'TAT': 118, 'CTT': 116, 'TCC': 112, 'GTC': 106, 'CCC': 106, 'TAA': 101, 'TTA': 95, 'ACG': 94, 'TAC': 88, 'GTA': 73, 'CGT': 71, 'CGA': 70, 'CGG': 69, 'CCG': 60, 'GCG': 58, 'CGC': 51, 'TCG': 49})
{'fraction_non_alphanumeric': 0.13268608414239483, 'fraction_numerical': 0.060582524271844664, 'mean_word_length': 3.9557408595253367, 'pattern_counts': {'":': 1, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '51823', 'license': 'CC0-1.0', 'n_tokens_mistral': 3233, 'n_tokens_neox': 2782, 'n_words': 760, 'path': 'book/_build/html/_sources/notebooks/07-DNA Sequence Statistics.ipynb', 'repo_name': 'hossainlab/PY4B', 'size': '7725'}