Doctors at Zuckerberg San Francisco General Hospital could not figure out what was wrong with the 29-year-old man sitting before them. An otherwise healthy construction worker from Nicaragua, the patient was suffering from a splitting headache, double vision and ringing in his ears. Part of his face was also numb. The cause could have been anything—from an infection to a stroke, a tumor or some kind of autoimmune disease. The Emergency Department (ED) staff took a magnetic resonance imaging scan of the man’s brain, performed a spinal tap and completed a series of other tests that did not turn up any obvious reason for the swelling in his brain—a condition that is formally known as encephalitis.

Most likely, it was some kind of infection. But what kind? Nineteen standard tests are available to help clinicians try to pin down the source of encephalitis, but they test for the presence of only the most common infections; more than 60 percent of cases go unsolved each year. Physicians looked in the patient’s cerebrospinal fluid (which surrounds the brain and protects it) for evidence of Lyme disease, syphilis and valley fever, among other things. Nothing matched. So the S.F. General ED staff settled on the most likely culprit as a diagnosis: a form of tuberculosis (TB) that causes brain inflammation but cannot always be detected with typical tests. Doctors gave the man a prescription for some steroids to reduce the swelling plus some anti-TB drugs and sent him home.

Soon he was back, however, with the same symptoms. This time the physicians assumed the man, whose life was chaotic, had not been taking his drugs properly. (Even people with regular jobs and schedules often find taking TB medications fairly difficult.) The ED staff sent him away with another prescription but he returned again and again—even after he could prove that he was taking his medication correctly and on time. The drugs helped briefly after each visit, but the symptoms always returned. During that year his medical bills reached $580,000. Finally, S.F. General turned to an experimental test that is designed to uncover the source of virtually any neurological infection.

The test is the brainchild of researchers at the nearby University of California, San Francisco, led by neurologist Michael Wilson, biochemist Joseph DeRisi and infectious disease expert Charles Chiu. The group uses genetic-sequencing technology to identify mystery illnesses in people with encephalitis or meningitis (inflammation of the meninges, the membranes around the brain and spinal cord). This so-called metagenomic test analyzes all the DNA and RNA found in a sample of cerebrospinal fluid (meta means “beyond” in Greek). So any DNA or RNA that does not belong to the patient—including that from viruses, bacteria, parasites or fungi—shows up in the results.

Done correctly, metagenomic testing could radically change the way infections of the brain are diagnosed. An element of circular logic underlies most standard infectious disease tests. Doctors order individual tests for each bug they suspect might be causing the problem. But how do they know what is causing the problem if they have not yet done the test? Metagenomic sequencing, in contrast, casts the broadest possible net, which allows it to pick up unexpected or previously unknown pathogens. “We’re looking at everything at once, which has the potential of replacing the myriad of lab tests with a single test,” Chiu says.

The U.C. San Francisco team ran a sample of the man’s cerebrospinal fluid through their investigative diagnostic procedure. “That’s when we found the worm,” Wilson says. Genetic sequencing and analysis revealed DNA from the kind of tapeworm found in pigs. The patient did not get better on TB drugs because he did not have TB—he had tapeworms living in his brain.

In Nicaragua and other developing countries tapeworm infections are common, and the larvae can migrate into the brain—a condition called neurocysticercosis. Usually the infection causes seizures as well as large brain cysts that are obvious on an MRI. This man had neither, however, which had led the medical staff to dismiss neurocysticercosis as a possibility. “When this guy first presented, they absolutely thought of it,” Wilson says. “But when they scanned him in his brain and spinal cord, there were no cysts.” He would improve briefly, because the steroids they gave him temporarily reduced the swelling in his brain, but the TB drugs were useless, so he would soon relapse again. Now that they knew the cause, the team started him right away on anti-worm drugs, which cannot eliminate the infection but will keep it under control. “It’s very treatable,” Wilson says. “He’s doing great.”

The construction worker is one of nearly 300 patients who has participated since June 2016 in a metagenomic sequencing study based at U.C. San Francisco. Participants consist of anyone who has been admitted to one of eight medical centers, mostly in California, with an apparent neurological infection and no clear diagnosis. Metagenomic sequencing is done alongside traditional testing, for a head-to-head comparison.

Wilson and colleagues hope to prove their test, which will be more widely available this summer for about $2,400, can be an efficient and reliable solution for the sorts of medical mysteries that befuddle doctors, aggravate patients and can run up huge medical bills. The challenge is in making sense of the output, Chiu says. Sequencing does not produce a yes or no answer. The group developed a standard set of procedures to make it easy for an infectious disease doctor to pinpoint what the most likely bug might be.

On July 1 the U.C. San Francisco group will begin offering this test as a custom-ordered service to a broader group of people so that hospitals and labs in the U.S., and eventually anywhere in the world, can send cerebrospinal fluid for analysis. They plan to eventually expand their metagenomic testing to include pneumonia (infection of the lungs) and sepsis (infection of the bloodstream)—two other ailments that often cause diagnostic dilemmas.

A similar metagenomic test for pneumonia, developed by researchers at the University of Utah, Arup Laboratories and start-up IDbyDNA, is also expected to debut this summer. In both cases the teams first do extensive work to make it easier for physicians to understand the complex and ambiguous genomic information garnered by the test.

These two tests will be the first commercially available metagenomic tests for infection in the U.S., but they are part of a growing trend. Two academic groups in Europe recently introduced tests for sepsis. And researchers at computational geneticist Pardis Sabeti’s lab at Harvard University are currently running a study on a similar test for encephalitis, says Anne Piantadosi, an infectious disease physician at Massachusetts General Hospital and a postdoctoral research fellow with the Sabeti group. She envisions a time in the next few years when future clinicians will be able to look back say, “That was a really big improvement in how we diagnose infections.”