AI prompt for writing code from scratch

Drashti Shah
Bioinformatics with Rust
3 min readDec 8, 2023
Generated with AI

Let’s say we are trying to solve a Rosalind challenge where we have to calculate transition/transversion ratio in a DNA sequence.

Prompt Template

My strategy is to give a detailed description of the function and a long list of rules of programming. Most of these rules are based on a book by the same name by Chris Zimmerman.

Assume you are a bioinformatics expert with extensive Rust programming knowledge. You are especially good at writing simple code. I will give you a description of the problem including the inputs and the outputs. Your task is to solve this problem with simple code that is easy-to-understand for beginner Rust programmers. You have to write one function. Use Rust style guide for formatting your code. In order to write simple code, you must follow these rules of programming:

  • Simple code is easy to read. The simplest code can be read straight through, top to bottom, just like reading a book.
  • Don’t mix and match conventions. For example, consistently use the same naming convention.
  • Solve only the problem and nothing else. Do not unnecessarily write a function that generalises to cover more use cases.
  • Avoid too many parameters/arguments.
  • Use descriptive and useful names for variables and functions.
  • Use comments to complement the code instead of repeating what the code does without adding any value
  • Try to use stack memory instead of heap memory whenever possible

This is the problem you need to solve:

[the problem]

Your function must take these inputs:

[list of inputs]

Your function must result in this output:

[description of output]

Prompt Example

I replace [] with the following parts. Note that I didn’t define what transition and transversion are. GPT-4 is able to generate the correct code with an incomplete problem statement. If it can access information about the missing elements, we should be okay.

The problem (copied from Rosalind)

For DNA strings s1 and s2 having the same length, their transition/transversion ratio R(s1,s2) is the ratio of the total number of transitions to the total number of transversions, where symbol substitutions are inferred from mismatched corresponding symbols as when calculating Hamming distance.

Given: Two DNA strings s1 and s2 of equal length (at most 1 kbp)

Return: The transition/transversion ratio R(s1,s2)

List of inputs

2 DNA sequences as strings of equal length

Description of output

Transition/transversion as a decimal number

Sample Output

The code works in the first iteration.

// A function that takes two DNA sequences as strings of equal length and returns the transition/transversion ratio
fn transition_transversion_ratio(dna1: &str, dna2: &str) -> f64 {
// Initialize the counts of transitions and transversions to zero
let mut transitions = 0;
let mut transversions = 0;

// Define the sets of purines and pyrimidines
let purines = ['A', 'G'];
let pyrimidines = ['C', 'T'];

// Iterate over the characters of both DNA sequences
for (c1, c2) in dna1.chars().zip(dna2.chars()) {
// If the characters are different, check if they are a transition or a transversion
if c1 != c2 {
// A transition occurs when a purine is replaced by another purine or a pyrimidine by another pyrimidine
if (purines.contains(&c1) && purines.contains(&c2)) || (pyrimidines.contains(&c1) && pyrimidines.contains(&c2)) {
transitions += 1;
} else {
// A transversion occurs when a purine is replaced by a pyrimidine or vice versa
transversions += 1;
}
}
}

// Return the ratio of transitions to transversions as a decimal number
transitions as f64 / transversions as f64
}

You can play with the code on Rust Playground.

More things you can try

It is very likely that the output doesn’t meet your expectation. When that happens, try these:

  • If the code results in an error, copy & paste the error and ask the model to correct the code.
  • If the code works but the output is wrong, give the model some examples of inputs and outputs. And ask it to correct the logic.
  • If you don’t understand a part of the code, copy & paste that part and ask the model to explain that part as if they are explaining to a high school kid with no prior programming knowledge.

Writer’s Note: I am not a prompt engineering expert. But I know enough to generate useful output (after a lot of trial and error). You can learn more about how to write good prompts here.

--

--

Drashti Shah
Bioinformatics with Rust

ESG & climate data scientist by day. Aspiring computational biologist and game designer by night. A Rust fan who believes in an "open career".