Data structures

Dr. Yu Cheng Hsu

Learning Objectives

  • Understand basic Python syntax

Review

In Session 1, we stored a single DNA sequence in a single variable (target_gene = "ATGC"). A standard RNA-seq experiment outputs expression data for 20,000+ genes simultaneously. To handle bulk data, Python uses Collections.

In this session, we will introduce the two most important collections in Python: Lists and Dictionaries.

List

Lists: Storing Multiple Items

A List is

  • An ordered collection of items.
  • In Python, you create a list using square brackets [], and you separate each item with a ,
# Storing multiple DNA motifs as Strings in a List
motifs = ["TATA", "ATG", "TAG"]

print("My motif list:", motifs)

# We can use the len() function to see how many motifs we are tracking
total_motifs = len(motifs)
print("Total motifs to scan:", total_motifs)
My motif list: ['TATA', 'ATG', 'TAG']
Total motifs to scan: 3

Indexing a List

Python lists are 0-indexed. We can grab specific genes out of our list using their position.

# Recalling that 
# motifs = ["TATA", "ATG", "TAG"]

# Grab the first motif in the list (Index 0)
first_motif = motifs[0]
print("The first motif is:", first_motif)

# Grab the last dna using a negative index
last_motif  = motifs[-1]
print("The last motif is:", last_motif)
The first motif is: TATA
The last motif is: TAG

Modifying the List

  • Use the .append() method to append a new item at the end of the list
# Add a new motif (CpG island) to the end of our list
motifs.append("CG")

print("Updated motif list:", motifs)
Updated motif list: ['TATA', 'ATG', 'TAG', 'CG']

For loop

For loop

for is the keyword for you to iterate over a designated list

# Our list of dna from earlier
motifs = [
    "TATA", 
    "ATG", 
    "TAG", 
    "CG"]
# Python reads this as: 
# "For every individual motif inside the motifs list, do this:"
for m in motifs:
    print("Currently analyzing:", m)
print("Loop finished! All motifs analyzed.")
Currently analyzing: TATA
Currently analyzing: ATG
Currently analyzing: TAG
Currently analyzing: CG
Loop finished! All motifs analyzed.

for INDEX in LIST:

  • for: Starting of a loop
  • INDEX: Index variable will iterate along the list order
  • in: Specify the list you want to iterate
  • LIST: Target list
  • :: Instruction start (all the instruction should be indented)

Example: Counting motifs

We want to calculate how many motifs are there in a designated dna_read

Without for loop

dna_read = "ATGCGTATATAATGCGCTAGCG"
motifs = ["TATA", "ATG", "TAG", "CG"]

occurrences = dna_read.count(motifs[0])
print("Motif:", motifs[0], " occurrs ",
 occurrences, " times")
occurrences = dna_read.count(motifs[1])
print("Motif:", motifs[1], " occurrs ", 
occurrences, " times") 
# and so on so forth ....
Motif: TATA  occurrs  1  times
Motif: ATG  occurrs  2  times

With for loop

dna_read = "ATGCGTATATAATGCGCTAGCG"

motifs = ["TATA", "ATG", "TAG", "CG"]

# The Automation (The Loop)
# Python reads this as: "For every individual
# sequence inside our motifs list..."
for m in motifs:
    # 2. Count how many times it appears in the DNA read
    occurrences = dna_read.count(m)
    # 3. Print the final report for this specific motif
    print("Motif:", m, "occurs", occurrences, "times")        
Motif: TATA occurs 1 times
Motif: ATG occurs 2 times
Motif: TAG occurs 1 times
Motif: CG occurs 3 times

Discussion of the loop

  • Reuse of the code and scalability
  • Construct a loop
    • Code without a loop
    • Identify the repeated pattern
    • Replace the changing part with an index variable

Example

# Without For loop

# occurrences = dna_read.count(motifs[0])
# print("Motif:", m, "occurs", occurrences, "times")        
# occurrences = dna_read.count(motifs[1])
# The Automation (The Loop)

# With for loop
for m in motifs:
    occurrences = dna_read.count(m)
    print("Motif:", m, "occurs", occurrences, "times")        
print("Motif scan complete!")

Dictionary

Dictionary

Dictionary links

  • a Key to
  • a Value

Dictionaries are created using curly braces {} and colons :.

# A dictionary mapping the Sequence (Key) to its Biological Label (Value)
motif_labels = {
    "TATA": "Promoter Box",
    "ATG": "Start Codon",
    "TAG": "Stop Codon",
    "CG": "CpG Island"
}

print("Full label dictionary:", motif_labels)
Full label dictionary: {'TATA': 'Promoter Box', 'ATG': 'Start Codon', 'TAG': 'Stop Codon', 'CG': 'CpG Island'}

Looking up a Label

To find the label for a specific sequence, you do not use a number. You just pass the Key into square brackets.

  • Key does not need to be a text, numeric value also works
# Look up the biological label for "ATG"
atg_label = motif_labels["ATG"]
print("The sequence ATG is a:", atg_label)

# Look up the biological label for "TAG"
tag_label = motif_labels["TAG"]
print("The sequence TAG is a:", tag_label)
The sequence ATG is a: Start Codon
The sequence TAG is a: Stop Codon

Updating dictionary

To add or update data, we use the exact same square bracket syntax we used for looking things up, but we use an equals sign = to assign the new label.

# 1. ADDING: We found a new motif we want to track
motif_labels["UAA"] = "Stop Codon"

# 2. UPDATING: We want to make our ATG label more specific
motif_labels["ATG"] = "Start Codon (Methionine)"

print("Updated Dictionary:", motif_labels)
Updated Dictionary: {'TATA': 'Promoter Box', 'ATG': 'Start Codon (Methionine)', 'TAG': 'Stop Codon', 'CG': 'CpG Island', 'UAA': 'Stop Codon'}

Checking if a Key Exists

If you ask Python for motif_labels[“CCC”], and “CCC” is not in your dictionary, Python will panic and throw a KeyError, crashing your entire script.

unknown_motif = "CCC"
# Do not run the code, as "CCC" is not a key
print("Unknown sequence", motif_labels[unknown_motif])

Instead, you can use a if ... else ... control to protect your code

unknown_motif = "CCC"

# Python evaluates this as True or False
if unknown_motif in motif_labels:
    print("Found it! The label is:", 
    motif_labels[unknown_motif])
else:
    print("Unknown sequence. No label exists for", 
    unknown_motif)
Unknown sequence. No label exists for CCC

Extracting All Keys

.keys()

  • To access all Keys in your dictionary
  • Give you a List of all keys
# Extract all the known sequences from our dictionary
known_sequences = motif_labels.keys()

print("Sequences we are currently tracking:", known_sequences)
Sequences we are currently tracking: dict_keys(['TATA', 'ATG', 'TAG', 'CG', 'UAA'])

Why this is powerful:

In the for loop section next, you will see how we can plug these extracted keys directly into an automated scanner, letting the dictionary do all the heavy lifting.

Exercise: The Motif Database Manager

Exercise: Background

Scenario:

You have received raw motif counting data from a patient’s sequencing run. It is structured as our “Dictionary of Lists”. You need to extract specific data, update the database with new findings, and write an automated loop to flag high-frequency motifs.

The Starting Data:

Copy this into your first Colab cell and run it to load the data into memory.

motif_table = {
    "Sequence": ["TATA", "ATG", "TAG", "CG"],
    "Label": ["Promoter", "Start Codon", "Stop Codon", "CpG Island"],
    "Counts": [42, 15, 15, 890]
}

Task: Extracting and Measuring

We want to know the highest motif count and the total number of motifs found. First, we extract the “Counts” list from the dictionary, and then we use Python’s built-in max()and sum() functions.

# 1. Grab just the list of counts using its Key
patient_counts = motif_table["Counts"]
print("All counts:", patient_counts)

# 2. Find the highest number and the total sum
highest_count = max(patient_counts)
total_motifs = sum(patient_counts)

print("Highest single motif count:", highest_count)
print("Total motifs found:", total_motifs)
All counts: [42, 15, 15, 890]
Highest single motif count: 890
Total motifs found: 962

Task: Updating the Database

The sequencer just finished another run and found a new motif: a “Poly-A Tail” (Sequence: “AAAA”, Count: 120).

To add this to our table, we must append the new data to ALL THREE lists. If we don’t, our “columns” will be uneven, and our data will be corrupted.

# Append the new data to each list inside the dictionary
motif_table["Sequence"].append("AAAA")
motif_table["Label"].append("Poly-A Tail")
motif_table["Counts"].append(120)

# Let's check if it worked
print("Updated Sequences:", motif_table["Sequence"])
print("Updated Counts:", motif_table["Counts"])
Updated Sequences: ['TATA', 'ATG', 'TAG', 'CG', 'AAAA']
Updated Counts: [42, 15, 15, 890, 120]

Task: Detect high frequency motif

We want to print an alert for any motif that appears more than 100 times. If the motif count is greater than 100, print print("ALERT: High frequency detected!")

# Find out how many 'rows' we have by checking the length of one of our lists
total_rows = len(motif_table["Sequence"])

# Loop through row 0, then row 1, then row 2...
for i in range(total_rows):
    
    # Use the 'i' index to extract the matching data across all three lists
    current_seq = motif_table["Sequence"][i]
    current_label = motif_table["Label"][i]
    current_count = motif_table["Counts"][i]
    
    # Apply our True/False Logic
    if current_count > 100:
        print("ALERT: High frequency detected!")
        print("--> Motif:", current_label, "(", current_seq, ") - Count:", current_count)
ALERT: High frequency detected!
--> Motif: CpG Island ( CG ) - Count: 890
ALERT: High frequency detected!
--> Motif: Poly-A Tail ( AAAA ) - Count: 120

Summary

  • Basic methods and usage of list and dictionary
  • For loop

What’s next

  • As we saw in the exercise, handling tabular data with multiple lists is not very convenient!
  • We need a more efficient way to do it

Bibliography Notes

Seurat, Georges. 1883. A Summer Landscape. Oil on wood. National Gallery of Art.