Unit 2 / 11

Biological Data Analysis Fundamentals with Python

Gains:

  • Ability to trust deterministic output by writing the code that reads and cleans biological data to artificial intelligence and running it itself with Pandas, NumPy and Biopython
  • Being able to avoid the risk of 'wrong code working without errors' by testing the code with a small situation of which the result is known and an assert test.
  • Ability to establish a repeatable analysis with version pinning, randomness seeding and raw data preservation habits

The language of modern biology is increasingly becoming Python. Manual processing in a lab notebook now turns into lines of code processing tens of thousands of lines of tables per second. In this unit, we will learn to use AI as a co-programmer that prints Python code that reads, cleans, and summarizes your biological data. The important thing is to write the code to the artificial intelligence, run it yourself and verify the result; This is because it does not rely on the verbal prediction of the model, but on the deterministic (providing the same result in every run) output of the code.

You don't need to know how to code in this unit; You will learn to express the intention correctly and provide the output.

Why Python and which libraries?

The most used Python libraries (library: package of ready-made functions) in biology are:

  • pandas: To read tabular data (CSV, Excel) and perform row-column operations. Basic tool to filter, group, merge a gene expression table.
  • NumPy: For numeric arrays and matrix operations; It runs under pandas.
  • Biopython: For working with DNA/RNA/protein sequences, reading FASTA files, translation (translating DNA into protein).
  • matplotlib / seaborn: For plotting plots.
  • SciPy/statsmodels: For statistics tests.

Artificial intelligence knows these libraries very well. Your job is to clearly state what you want to do with which library and to run and verify the generated code.

Hint: The model can sometimes "make up" (hallucinate) a library function that does not exist. If the code gives an error, don't panic; pasting the error back into the model as is usually fixes it. If it still doesn't work, check the official documentation.

Step by step: clearing a counting table

Let's say you have counts.csv: rows are genes, columns are samples, cells are raw read counts. Typical first steps:

  1. Loading: Read the table with pandas.
  2. Discovery: Check size (how many genes, how many samples), missing values, duplicate gene names.
  3. Filtering: Discard genes that are not read in any sample (total count 0); these are noise.
  4. Summarize: Calculate the total number of reads per sample (library size); The sample that is too low may have failed.

You can outsource this workflow to artificial intelligence as follows:

Role: You are a Python assistant focused on bioinformatics. Task: Read the counts.csv file with pandas. Data: rows are gene (index=gene_id), columns are 24 samples, values are integer raw counts. I want: (1) print the size, (2) discard the genes that were never read, (3) show the total reads per sample in a bar graph. Add short Turkish comments to each line. Just give working code.

Generates model code; you run it. If you see 24 columns and a reasonable number of genes (e.g. 15,000-25,000) in the output, you're on track. If one sample contains one-tenth as many readings as the others, write that sample down.

three mini cases

Case 1 — Missing value trap: A student had the mean calculated in a 30-sample metabolomics table; The result was absurd. Problem: the missing cells were filled with the text "ND" instead of NaN (not a number), so the column was read as text. It was fixed when I made the artificial intelligence say "Make ND values ​​NaN and convert the column to numbers". Lesson: always explore raw data first.

Case 2 — Merge error: A researcher merged two tables (expression and gene annotation) but 2,000 genes were lost. Cause: in one table the IDs were "ENSG00000141510", in the other they were "ENSG00000141510.14" (with version number). Model wrote a single line of code that cleared the version number; The loss was reduced to 40 genes. Lesson: align ID formats before merging them.

Case 3 — Silent data loss: A technician did not notice that after filtering, the number of genes dropped from 22,000 to 8,000; the threshold was set incorrectly (>10 total instead of >10 readings in each sample). A known gene (housekeeping gene: genes such as GAPDH that are constantly expressed in every cell) was ultimately missing. Lesson: check for a "must have" gene post-filter.

Testing with known situation (most important habit)

The surest way to trust the accuracy of the code written by artificial intelligence is to test it with a small sample whose result you know in advance. For example, give a dummy table with 5 rows; calculate the total manually; See if the code gives the same result.

Add a test to the filtering code you wrote: Generate a small DataFrame consisting of 5 genes, 3 samples, deliberately set 2 genes to zero, verify with assert that the filter discards exactly these 2 genes. Make the test executable.

assert warns you if the code deviates from expected behavior. This is the strongest shield against the risk of the "silent false conclusion".

Weak prompt / Strong prompt

Weak: "Clean my chart."

Powerful: "counts.csv: rows gene (gene_id index), 24 columns sample, values ​​raw integer. Do the following: report missing values, discard genes that sum to 0 across all samples, print total reads for each sample, compare gene count before/after filter. Just give working, commented Python code."

Difference: Strong prompt specifies the data structure, steps, and validation output (before/after comparison). The model doesn't have to guess.

Comparison chart: AI or manual?

transaction

Print to artificial intelligence

verify it yourself

CSV reading, format conversion

Yes

Check size and types

Filtering, grouping

Yes

Count before/after

Statistics test

Yes (code)

Confirm assumptions and test

"How many lines are left?"

No (let the code count)

Read the output

Biological meaning of the result

partially

Expert comment is required

Common mistakes

  • Relying on the number the model produces: “What is the average expression?” Ask the question to the code, not the model.
  • Not checking data types: Columns of numbers read like text silently return incorrect results.
  • Not checking post-filter: Verify that an expected gene is still there.
  • Forgetting the seed of randomness: If the seed is not fixed in the code containing random operations, the result changes every time; repeatability is impaired.
  • Running the code without reading it: At least read the comments and follow the logic.
Attention: Just because the code works does not mean that the code is correct. "Wrong code that works without errors" is the most dangerous situation in biology; because the wrong result is produced silently. Testing with a known condition eliminates this risk.

Reproducibility: scientific value of the code

In biology, the scientific value of a result depends on the ability of others (and your future self) to reproduce it. Manual table operations are not recorded; No one knows which cell changes and how. The code documents each step. Therefore, think of the analysis you produce with artificial intelligence as a stored and shared record, not as a one-time box.

Three habits are important for a repeatable analysis. The first is version pinning: note which library version you are using (e.g. pandas 2.2); Different version may give different results. The second is the randomness seed: fix the seed in every code that contains random operations so that the result is the same in every run. Third, never change the raw data: do not touch the original file, do all transformations in code so that it can be rolled back.

Add lines that print the versions of the libraries used at the beginning of the analysis code you wrote, and if there is a random process, fix the seed with sanp.random.seed(42). Do not change the raw CSV at all, save all output in a separate file.

Jupyter notebook: combination of analysis and narrative

The most used environment in bioinformatics is the Jupyter notebook (notebook: tool that combines code, output and description in the same document). Having the AI ​​generate the code according to the notebook cells, with each step separated by a Markdown explanation, makes it easier for both you and your colleagues to follow the analysis. This makes the analysis a readable laboratory notebook, not a "black box".

Recognizing biological file formats

When processing biological data with Python, you will constantly encounter certain file formats. Before the model can read a file correctly, it must know what format it is in; If you get the format wrong, you'll fall into the "wrong code that works without errors" trap. The most common are:

format

Content

suitable vehicle

CSV/TSV

Table data (expression, measurement)

pandas

FASTA (.fa/.fasta)

DNA/RNA/protein sequences

biopython

FASTQ (.fq)

Raw sequencing reads + quality

Biopython, custom tools

VCF

Variant (mutation) list

pandas/pysam

GFF/GTF

Genome annotation (gene positions)

pandas, gffutils

If you don't recognize a format, first have the model identify it by showing a few sample lines, then ask for the read code:

I am giving the first 5 lines of the file below. What biofile format is this? Explain the meaning of the columns/fields, then give code that safely reads (format checks) this file in Python. First 5 lines: [paste]

This approach prevents silent errors arising from the assumption of form in the first place.

In summary

Python is the main processing language for biological data; pandas, NumPy and Biopython are the basic tools. The AI ​​writes this code quickly, but you run it and verify it. The most critical habit is to test the code with a small sample whose result you know and embed the expectation into the code with assert. Rely on the deterministic output of the code you run, not verbal guesswork.

Application task

Print a code that has the AI read the CSV table you have (or a sample one), print its size, and filter out empty genes. Then add an assert test from the model with 5 lines of dummy data. Run the code; Note the number of genes before and after the filter. Check that a housekeeping gene (e.g. GAPDH/ACTB) is still present in the result.

checklist

  • [ ] I checked the size and types of the data before processing it.
  • [ ] I have explicitly handled missing values.
  • [ ] I compared the number of rows before/after filter.
  • [ ] I added an assert test with a known condition.
  • [ ] I left the counting/calculation to the code, not the model.
  • [ ] I read the comments of the code and followed the logic.