Guided project
Module 3 · Coding with AI assistants · Self-checked
Use an assistant to help build a small command-line tool, and keep full control of it. Work through the six tasks, check each against its checkpoint, and tick it off. Nothing is submitted.
The task
Build fasta_stats.py: given a FASTA file, print one line per sequence with its ID, length and GC%, with an option to skip short sequences. Run it as python fasta_stats.py practice_sequences.fasta --min-length 25.
How to approach this
This project puts the whole of Module 3 into one small tool. The order of the tasks is the point: you work out the answers before you ask for any code, you read and test what comes back, and only then do you share it. If you reverse that order, you end up checking the tool against itself.
First, be clear about the pieces in the command you will run.
- A FASTA file is a plain-text file of sequences. Each record starts with a header line beginning with
>, followed by the sequence ID, and the sequence letters follow on the next line or lines. practice_sequences.fasta holds four records, seq1 to seq4. - Length is the number of letters in a sequence. GC% is the share of those letters that are G or C, as a percentage: (number of G + number of C) ÷ length × 100.
python fasta_stats.pyruns your script.practice_sequences.fastais a positional argument: a value the script expects in a fixed place, here the file to read.--min-length 25is an option: it starts with two dashes, it can be left out, and when present it carries a value, here 25.- argparse is the part of Python's standard library that reads these arguments from the command line, checks them, and writes a
--helpmessage for you.
The figure shows what the finished tool should do with the practice file, using the numbers from the checkpoints. Use it to picture the goal, but still work out task 1 yourself before you look at the checkpoint.
Every step reuses something you have already built. The GC calculation is the tested gc_content from Testing AI-written code, the prompt follows the template from Asking well: prompts for code, and the README disclosure follows Working responsibly. The new parts are reading a FASTA file and reading options from the command line, which is where you should expect most of the problems.
Expected answers first
Asking well: prompts for code · Read before you run
- Before asking anything, work out by hand the length and GC% of seq2 and seq3 in practice_sequences.fasta.
Hint. Open the file in a text editor, not in Python. For each sequence, count the letters, then count only the G and C letters. Divide the second number by the first, multiply by 100 and round to one decimal place. Sequences with very low or very high GC are useful checks, because a bug that swaps or ignores letters shows up clearly.
Two counting mistakes are common. Do not count the header line: the letters of an ID such as seq2 are not bases. And check whether any sequence runs over more than one line. FASTA files often wrap long sequences every 60 or 80 letters, and the length is the total across all of a record's lines. Your hand count is the standard the tool will be judged against, so it is worth doing twice.
Checkpoint: seq2 has 23 bases and 8.7% GC; seq3 has 21 bases and 95.2% GC.
Prompt
Asking well: prompts for code
- Write a specific prompt: Python version, standard library only (argparse), reuse
gc_contentfrom your seqtools.py, the output format, the option, and your expected answers.
Hint. "Standard library only" means nothing that needs a separate install, so the tool runs on any computer with Python. Asking it to reuse your own gc_content keeps one tested version of that calculation instead of two. Give the output format as an example line, and say exactly what the option should do with a sequence whose length equals the cut-off. Test your prompt by reading it as a stranger would: is any decision left open?
"Skip short sequences" sounds clear but is not: with --min-length 25, is a sequence of exactly 25 bases kept or skipped? Choose, and write the choice into the prompt as a rule and as an example. The same goes for the output format. Say whether the columns are separated by tabs or spaces and how many decimal places GC% should have, so that your hand-worked values and the tool's output can be compared directly.
Checkpoint: a reader could write the tool from your prompt alone.
Review and run
Read before you run
- Read every line of what you get back and run it.
- Check the output against task 1, and try
--help.
Hint. Run the tool twice: once with the file name alone, and once with the option added. Compare every printed number with your hand-worked values, not only the first row. --help is written by argparse from the descriptions in the code, so if the help text is vague or wrong, that is a review finding too.
While reading, look for three things in particular. Does the code import your gc_content from seqtools.py, or has the assistant quietly written a new, untested copy? Does it handle a sequence spread over several lines, or does it treat each line as a separate sequence? And is the option compared with the length using the rule you chose for a sequence exactly at the cut-off? A wrapped sequence read line by line gives more rows than there are records, which is easy to spot if you know there should be four.
Checkpoint: four rows with lengths 36, 23, 21 and 27 and GC% 44.4, 8.7, 95.2 and 48.1; with --min-length 25, only seq1 and seq4.
Tests
Testing AI-written code · Debugging with AI
- Write pytest tests with your own expected values, including a FASTA file with a sequence before the first header, and an empty file.
- Fix anything that fails, and add a test for each fix.
Hint. pytest runs every function whose name starts with test_. For the awkward files, decide first what the right behaviour is: an empty file might print nothing, while sequence letters before any header line have no ID to belong to, so the tool should stop with a message that says so. pytest's built-in tmp_path gives each test a temporary folder where you can write these small files. A test added for each fix is called a regression test: it stops the same bug coming back later.
Two more tests are worth adding because they test your decisions, not just the code. One uses a sequence exactly as long as the cut-off, so the rule you chose in task 2 is fixed in a test. The other uses a lower-case sequence, the edge case that broke the first draft in Read before you run. If the tool reuses your tested gc_content, that test should pass at once, which confirms the reuse is real.
Checkpoint: all tests pass, and a malformed file gives a clear error message rather than a crash deep inside the code.
Explain it back
Explaining, documenting and translating
- Without the assistant, write a comment above each function saying what it does and why.
Hint. For the argparse part, trace one value through the code: the text "25" typed after --min-length, how it becomes a whole number, the name it is stored under (argparse turns the dash into an underscore, so --min-length is read back as min_length), and the line where it is compared with each sequence's length. If you cannot follow a line, look it up in the Python documentation before you ask the assistant.
Writing the comments yourself is the test of whether the tool is really yours. If a comment comes out as a restatement of the code, such as "loops over lines", ask yourself why the loop is there and what would break without it. Any line you still cannot explain after looking it up is a line to simplify or remove, because you would not be able to defend it if a result were questioned.
Checkpoint: you can explain every line, including how argparse reads --min-length.
Disclose and share
Privacy, security and licences · Working responsibly
- Write a README with usage, the tests, and a “Use of AI tools” section.
- Check that no secrets or private data are in the folder before sharing it.
Hint. A useful README answers three questions in order: how do I run it, how do I test it, and how was it made. The disclosure from Working responsibly is a good model for the last part. "Secrets" means anything that gives access to something: passwords, API keys and token files. Look in every file, including hidden ones whose names start with a dot, and leave out any real sequence data you are not free to share.
If the folder is a git repository, also list secrets and private files in .gitignore, and run git status before you commit to see exactly which files would be included. Remember from Privacy, security and licences that removing a file in a later commit does not remove it from the history, so the check has to happen before the first commit, not after.
Checkpoint: someone else could install, run and test the tool from the README, and knows exactly how AI was used.
Go deeper: make the command line testable
A script that reads sys.argv directly is awkward to test. A common pattern is to put the work in a function, def main(argv=None):, that starts with args = parser.parse_args(argv). When argv is None, argparse reads the real command line as usual. In a test you can instead call main(["tiny.fasta", "--min-length", "25"]) with a file written to tmp_path.
To check what the tool printed, add pytest’s built-in capsys fixture to the test’s arguments and read capsys.readouterr().out. One more detail: declare the option with type=int. Without it, argparse keeps "25" as text, and comparing a whole number with text raises a TypeError, which is exactly the kind of crash deep in the code that task 4 asks you to avoid.
Take it further
Put the tool on GitHub and set up a GitHub Actions workflow that runs your tests on every change. Then continue with Module 4: machine learning or the AI, Machine Learning & Data Science diploma.
GitHub Actions is GitHub's service for running commands on its own computers each time you push a change. Running your tests this way is called continuous integration: a change that breaks a test is flagged at once, before anyone relies on it. It is the same idea as this project, made automatic.
Module 4 turns to machine learning, and the habits from this project carry straight over. Working out expected answers before asking for code becomes deciding, before you train anything, what a useful result would look like and what a simple baseline achieves. Testing against values you know becomes evaluating a model on data it has never seen. And reading every line before you trust it becomes checking each step of a model pipeline for data leakage, where information from the test data slips into training and makes a model look better than it is. A model's score, like a passing test, is only as trustworthy as the way it was checked.
Further reading · Module 3Wilson et al. 2014: testing and review habits that apply to AI-written code too. · All reading for this course
Up next
Lesson 1
What machine learning is