Importing .py Python Files

You can create your own modules in python.

Hey bioinformatics enthusiasts!

Welcome back to another post in the Python for Bioinformatics series. In today’s discussion, we’ll learn how to import Python functions from custom .py files. This approach is super useful for structuring your bioinformatics projects efficiently by reusing code.

If you’ve watched our previous video on importing modules from Python libraries, you already know how convenient it is to use prebuilt functions. But what if you need to create and import your own? That’s exactly what we’ll cover today.

Step 1: Creating a Python File

First, we need a Python file that contains our reusable functions. Let’s create a file called functions.py and populate it with some functions we’ve used previously.

Here’s the code for functions.py:

import random

def random_base(RNAFlag=False):
    """Return a random base. Use 'U' instead of 'T' if RNAFlag is True."""
    return ("UCGA" if RNAFlag else "TCGA")[random.randint(0, 3)]

def random_codon(RNAFlag=False):
    """Return a random codon (three random bases)."""
    return random_base(RNAFlag) + random_base(RNAFlag) + random_base(RNAFlag)

def replace_base_randomly_using_names(base_seq):
    """
    Replace a random base in the sequence with another randomly chosen base.
    """
    position = random.randint(0, len(base_seq) - 1)
    bases = "TCGA"
    bases = bases.replace(base_seq[position], "")
    new_base = bases[random.randint(0, 2)]
    return base_seq[:position] + new_base + base_seq[position + 1:]

Save this file in a specific folder, such as your Desktop.

Step 2: Importing Functions

Now that we have functions.py, let’s create another Python file to test our imports. Save this file in the same folder as functions.py. For this example, we’ll name it testing.py.

Here’s the code for testing.py:

from functions import random_base, random_codon, replace_base_randomly_using_names

# Testing `random_base`
print(random_base(True))  # Example: 'U'

# Testing `random_codon`
print(random_codon(True))  # Example: 'UCC'

# Testing `replace_base_randomly_using_names`
print(replace_base_randomly_using_names("ATGCTA"))  # Example: 'ATGCCA'

Step 3: Running the Code

When running testing.py, ensure that it’s in the same folder as functions.py. If everything is set up correctly, you’ll see output similar to the following:

U
UCC
ATGCCA

Common Errors and Fixes

Spelling Errors: Double-check function names during import. For instance, importing random_base as random will cause errors.

Why This Matters in Bioinformatics

Organizing your code into separate files promotes modularity and reusability. Instead of duplicating code, you can focus on analyzing genetic sequences, building pipelines, or processing biological datasets more efficiently.

Join the Community

If you enjoyed this tutorial and want to connect with like-minded bioinformatics enthusiasts, join our Facebook group: Bioinformatics Guys. For more programming and personal content, check out my vlogging channel!

Thanks for reading, and I’ll see you in the next post of the series. Until then, keep coding and exploring the vast world of bioinformatics! 🚀

← Previous Next →