Homework 03
Due Date: Tuesday, February 3 by 11:00am CST
Life in Many Formats
Our in-class examples so far have used a simplified Uniprot protein dataset in multiple formats. Your task for the third homework is to work with a more realistic protein dataset from Uniprot. You will read data from a Uniprot JSON file and perform a few exploratory analyses on the data. You will also convert this JSON to CSV, XML, and YAML formats.
This assignment will help you practice working with different data formats commonly used in bioinformatics.
Input File: This JSON file will be your input for each exercise below.
Exercise 1: Reading and Analyzing JSON Data
Create a Python script called exercise1.py that:
Defines a ProteinEntry model with Pydantic
Reads in the input JSON file as a Python Dictionary
Converts the Python Dictionary into a list of ProteinEntry objects
Then,
Write a function called
find_total_mass()that prints the total combined mass of all proteins in the dataset.Write a function called
find_large_proteins()that prints a list of protein names of any proteins with a sequence length greater than or equal to 1000.Write a function called
find_non_eukaryotes()that prints a list of protein names of any non-eukaryotic proteins in the dataset. (Hint: A protein is considered non-eukaryotic if “Eukaryota” does not appear inprotein.organism['lineage'])
After defining your functions, call each one so the results are printed to the terminal when the script runs.
Requirements:
Use the
jsonmodule from Python’s standard libraryUse the
BaseModelclass from thepydanticPython library to define aProteinEntrymodelUse type hints when defining your functions.
Exercise 2: Converting JSON to CSV
Create a Python script called exercise2.py that:
Reads the input JSON file into a Python dictionary
Converts the data to CSV format with the following columns:
primaryAccessionproteinNamegeneNameorganism_scientificName(extract from nestedorganismobject)sequence_length(extract from nestedsequenceobject)sequence_mass(extract from nestedsequenceobject)function
Writes the output to a file called
proteins.csv
Requirements:
Use the
csvmodule from Python’s standard libraryInclude a header row with flattened field names (using underscores to indicate nesting)
Iterate through entries and extract nested values to create each row
Write each flattened row to the CSV file
Exercise 3: Converting JSON to XML
Create a Python script called exercise3.py that:
Reads the input JSON file
Converts the data to XML format
Writes the output to a file called
proteins.xml
Requirements:
Import the
xmltodictmoduleRemember that XML requires exactly one root element
Use the
pretty=Trueoption when writing XML for readability
Exercise 4: Converting JSON to YAML
Create a Python script called exercise4.py that:
Reads the
uniprot_protein_list.jsonfileConverts the data to YAML format
Writes the output to a file called
proteins.yaml
Requirements:
Import the
yamlmoduleThe YAML file should maintain the original order within the JSON file
The YAML file should start with
---and end with...
What to Turn In
Create a
homework03directory in your Git repository (on your VM)Add all four Python scripts (
exercise1.pythroughexercise4.py) to this directoryAdd a
README.mdfile in thehomework03directory that:Describes what each script does
Includes a section on AI usage (if applicable - see note below)
Commit and push your work to GitHub
Expected Output Files:
my-mbs337-repo/
├── homework03/
│ ├── exercise1.py
│ ├── exercise2.py
│ ├── exercise3.py
│ ├── exercise4.py
| |── proteins.csv
| |── proteins.xml
| |── proteins.yaml
│ └── README.md # specifically describes the contents of the homework03 directory
Note on Using AI
The use of AI to complete this assignment is not recommended, but it is permitted with the following restrictions:
The use of LLMs (like ChatGPT, Copilot, etc) or any other AI must be rigorously cited. Any code blocks or text that are generated by an AI model should be clearly marked as such with in-code comments describing what was generated, how it was generated, and why you chose to use AI in that instance. The homework README must also contain a section that summarizes where AI was used in the assignment.
Additional Resources
UniProt — protein sequence and functional annotation database
Please find us in the class Slack channel if you have any questions!