“I remember walking to my adviser’s office and telling her my Ph.D. was doomed because I couldn’t figure it out,” Mr. Li said in an interview.

But just as Mr. Li was despairing, a team of scientists at the University of Alberta unveiled a new database of DNA from hundreds of plant species. Mr. Li renewed his search, searching this new cache of genes for a neochrome-like gene.

He found one. To his surprise, however, the gene was not in a fern. Instead, it belonged to a hornwort. These primitive plants, which lack roots or stems, grow in mats on damp banks or on trees. It was a strange connection to find because hornworts are only distantly related to ferns.

“The first thing that came to my mind was that this must be a contamination,” Mr. Li said. A neochrome gene must have somehow been mixed into a sample of hornwort DNA.

The only way to know for sure was to look at more hornwort DNA. Mr. Li obtained hornwort tissue from other scientists and gathered some of his own from a roadside ditch near Duke. In all five species, he found variants of the neochrome gene.

Comparing all the data, Mr. Li and his colleagues came up with an unexpected hypothesis for how ferns got their neochromes. Neochromes did not gradually evolve in ancient ferns. Instead, a single lineage of ferns picked up the neochrome gene from hornworts about 180 million years ago.

Mr. Li speculates that the transfer took place between a hornwort and a fern growing in intimate contact. Once a fern picked up the neochrome gene, his research indicates, it moved into other fern species as well. It’s possible that acquiring this gene enabled ferns to thrive in dark forests.