NitRobe

How the model was trained

A logistic regression model is trained to recognise nosZ, the one gene on Earth that destroys nitrous oxide. It is built from 3,994 real copies of nosZ, 2,977 copies of 2 genes that look like it, nirK and coxB, and 8,804 ordinary bacterial genes. Here is every step, with the real numbers and the real code.

3,994real nosZ genes it learned from
99.6%correct on 3,155 genes it never saw
13 of 40genomes it found the gene in
3 of 3commercial strains it got right

Run it on a genome →

1 · what goes in

A gene is just four letters, over and over

CTGGGGCCGCTGCACACCGAGTTTGACGCAAACGGAAACGCATATACTTCATTCTTCGTATCATCCGAGATTGTAAAGTGGAACGTTCAAAAACTCGAAGTTCTTGACAGGGTTCCTACTTATTATTCTATCGGTCACTTGAGCGTACCTGGTGGTCCGACGAAAAAACCCCACGGCAAA…
2 · the idea

Recognise it by its spelling habits

You can tell English from Spanish without reading a word. English is full of th and ing; Spanish is full of ll and ción. You are not reading it, you are noticing its habits.

A nosZ gene has habits too, so the model never has to read it. It chops the DNA into short runs of letters and counts them. Here are 24 letters out of a nosZ gene, and 24 out of an ordinary one.

from a nosZ gene

TCTGCAAGTGGAACATCGACAAGG

20 runs, and all 20 of them different.

from an ordinary gene

ATCCGCGGCGGCGGCGGCGGCGGC

20 runs, but only 7 different ones. GCGGC alone appears 6 times.

See every run counted
RunHow many
AACAT1
AAGTG1
ACAAG1
ACATC1
AGTGG1
ATCGA1
CAAGG1
CAAGT1
CATCG1
CGACA1
CTGCA1
GAACA1
GACAA1
GCAAG1
GGAAC1
GTGGA1
TCGAC1
TCTGC1
TGCAA1
TGGAA1
RunHow many
GCGGC6
CGGCG5
GGCGG5
ATCCG1
CCGCG1
CGCGG1
TCCGC1
3 · teaching it

Show it thousands of each, and it works out the difference

The model is given the real genes labelled yes and everything else labelled no. It counts the runs in all 15,775 of them and works out which runs tell the two apart. That is what the 3 lines below do, and it takes 26 seconds.

# what to learn from genes = fetch_from_ena('nosZ') # -> 3,994 others = fetch_from_ena('nirK') + ordinary_genes() # -> 11,781 sequences = genes + others labels = [1] * len(genes) + [0] * len(others) # the learning, 3 lines of scikit-learn model = make_pipeline( TfidfVectorizer(analyzer="char", ngram_range=(5, 8)), LogisticRegression()) model.fit(sequences, labels)

Now ask it about anything. 1.0 means certainly the gene, 0.0 certainly not.

model.predict_proba([a_nosZ_gene]) # -> 0.9778 model.predict_proba([an_ordinary_gene]) # -> 0.0061

Of 3,155 sequences held back and never shown to it, it got 3,142 right.

See what it actually learned, run by run

says nosZ

atcgaca+2.25
gacagcca+2.19
acagccag+2.07
acagcca+2.01
tcgacag+1.95
tactgcac+1.95
gacagcc+1.95
acaagat+1.91

says not nosZ

gcggc-1.62
ggcgc-1.51
gcgct-1.41
gcgcg-1.26
ggcgcg-1.21
cgcgg-1.21
ccgcc-1.18
cgcgc-1.16

The right-hand runs are nearly all G and C, which is what ordinary Bradyrhizobium DNA is made of. Nobody told it that.

4 · finding it in a genome

Walk along and score every slice

The model scores a short stretch of DNA, not a whole genome. So walk the genome 2,000 letters at a time and score each slice. That is 9,104 slices across 9,105,828 letters.

genome = read_fasta('GCA_000011365.fna') # -> 9,105,828 letters starts = range(0, len(genome), 1000) slices = [genome[s : s + 2000] for s in starts] scores = model.predict_proba(slices)[:, 1] starts[scores.argmax()] # -> 327,000 # the gene is annotated at 326,883. It found it.
0.00 0.25 0.50 0.75 1.00 we say the gene is here above 0.90 0.99, and the gene is right here annotated at 326,883 0M 2M 4M 6M 8M position along the genome, in thousands of letters

Out of 9,104 slices, 2 cross the line, and both sit on the gene. The dark mark is where the database says the gene is. Nobody told the computer that.

5 · what it found

13 of the 40 bacteria carry the gene

Every genome in the genus with a published assembly was read end to end. The scores split cleanly: the 13 that carry it score above 0.99, and the 27 that do not stay below 0.56. Nothing lands in between, which is what you want to see. A model that was guessing would smear species across the middle.

It was also checked against 3 strains a farmer can order, where microbiology already knew the answer and the model had never seen any of them. It got all 3 right, including CPAC 15, which has been sold across millions of hectares and is the one that cannot destroy N₂O.

40genomes read
13carry the gene
0that landed in between
See all 40
#SpeciesScoreCarries the gene
1Bradyrhizobium guangdongense0.9974yes
2Bradyrhizobium forestalis0.9963yes
3Bradyrhizobium nanningense0.9959yes
4Bradyrhizobium nitroreducens0.9957yes
5Bradyrhizobium frederickii0.9954yes
6Bradyrhizobium neotropicale0.9951yes
7Bradyrhizobium diazoefficiens0.9948yes
8Bradyrhizobium ottawaense0.9945yes
9Bradyrhizobium shewense0.9939yes
10Bradyrhizobium stylosanthis0.9939yes
11Bradyrhizobium oligotrophicum S580.9925yes
12Bradyrhizobium lablabi0.9919yes
13Bradyrhizobium centrolobii0.9901yes
14Bradyrhizobium manausense0.6580no
15Bradyrhizobium retamae0.6366no
16Bradyrhizobium symbiodeficiens0.5538no
17Bradyrhizobium cajani0.4967no
18Bradyrhizobium huanghuaihaiense0.4917no
19Bradyrhizobium zhanjiangense0.4803no
20Bradyrhizobium sacchari0.4670no
21Bradyrhizobium cosmicum0.4553no
22Bradyrhizobium macuxiense0.4186no
23Bradyrhizobium guangzhouense0.4081no
24Bradyrhizobium valentinum0.3984no
25Bradyrhizobium canariense0.3872no
26Bradyrhizobium pachyrhizi0.3822no
27Bradyrhizobium australiense0.3776no
28Bradyrhizobium hipponense0.3760no
29Bradyrhizobium erythrophlei0.3749no
30Bradyrhizobium daqingense0.3660no
31Bradyrhizobium betae0.3542no
32Bradyrhizobium amphicarpaeae0.3481no
33Bradyrhizobium cytisi0.3456no
34Bradyrhizobium jicamae0.3446no
35Bradyrhizobium rifense0.3421no
36Bradyrhizobium uaiense0.3323no
37Bradyrhizobium niftali0.3246no
38Bradyrhizobium ivorense0.3011no
39Bradyrhizobium icense0.2939no
40Bradyrhizobium archetypum0.2885no