A lethal drug-resistant bug that spread rapidly around the world and killed tens of thousands of people has been traced to hospitals in the US and Canada.

British researchers used powerful genetic techniques to reconstruct Clostridium difficile's route as it circled the globe, and identified four separate waves that brought the bug to Britain. In all but one instance, the pathogen crossed the Atlantic.

The detailed map of the epidemic shows that two highly virulent strains emerged independently in North America after the pathogens evolved resistance to a frontline antibiotic in wide use at the time.

The genetic sleuthing demonstrates the extraordinary information that scientists can glean from the DNA of infectious organisms. Within the next few years, rapid and real-time surveillance of pathogens is expected to become standard practice.

C difficile became the most feared hospital bug in the developed world after the number and severity of infections among patients soared to record levels in a series of countries in the early 2000s.

Normal strains of C difficile are found in the guts of a few per cent of adults, but they rarely cause problems because other gut microbes keep them in check. But if a patient with the drug-resistant strain is given antibiotics, it multiplies and makes toxins that cause diarrhoea and other illnesses.

Unlike many other bacteria that cause infections in hospitals, C difficile produces hardy, infectious spores that help it spread rapidly and that survive on surfaces for long periods, even when they are cleaned with alcohol.

C difficile brought havoc to UK hospitals where poor hygiene and cleaning practices left scores of mostly older patients vulnerable to the infection. The bad handling of an outbreak in 2007 led to the death of 90 patients at Maidstone and Tunbridge Wells NHS trust. More than 30 patients died in two earlier outbreaks at Stoke Mandeville hospital between 2003 and 2006.

Although the global rise in cases has abated, the bug has not been beaten. Last year, C difficile killed more than 2,000 patients in England and Wales, and in the US deaths from the infection remain historically high, at 14,000 a year.

"This organism went from relative obscurity to the most common and feared hospital pathogen we have within 10 years," said Trevor Lawley, a geneticist at the Wellcome Trust Sanger Institute in Cambridge, which conducted the research. "We wanted to understand what had happened."

The scientists created a family tree for C difficile after sequencing the whole genomes of 151 bugs isolated from patients in 19 countries who fell ill or died from the infection between 1985 and 2010. From this, they identified different strains and traced their movements. They tested a further 145 bugs from UK patients to understand how the organism spread once it arrived in Britain.

One dangerous strain originated in or near Pittsburgh in 2001 and jumped quickly to South Korea and Switzerland. The other drug-resistant strain appears to have come from the Montreal area in 2003 and spread swiftly to continental Europe, the UK and Australia. Both had gained resistance to fluoroquinolone antibiotics.

"Since these strains acquired resistance to this frontline antibiotic, not only is it now virtually useless against this organism, but resistance seems to have been a major factor in the continued evolution and persistence of these strains in hospitals and clinical settings," said Brendan Wren, a professor at the London School of Hygiene and Tropical Medicine.

Having arrived in Britain, the bug triggered outbreaks around the country. According to the scientists, the resistant strain from Canada arrived in three waves that struck Ayrshire, Birmingham and Exeter. The same strain also reached Maidstone from mainland Europe, and went on to cause a large outbreak in London and Cambridge. The details are reported in the journal Nature Genetics.

"This gives us a much wider understanding of how these organisms are spreading, not only in this country but around the world," said another of the paper's authors, Nicholas Brown, of the Health Protection Agency. One problem scientists faced before was that their techniques were too blunt to reveal precisely which strains were driving the epidemic.

The plummeting cost of whole-genome sequencing means scientists will soon track the spread of scores of different diseases as standard practice. Such up-to-date surveillance will help hospitals prepare for new strains of pathogens, and contain outbreaks more effectively.

"Over the next few years we want to do real-time sequencing for the detection of C difficile in hospitals, as well as MRSA and other infections," said Julian Parkhill, another author at the Sanger Institute.

"If we can do this work in real time, we can spot the difference between a rise in sporadic cases of an infection, and transmission of a strain within a hospital, which could indicate the start of a new outbreak."

Last month Parkhill and others used the same methods to trace the source of an outbreak of the hospital superbug MRSA in a baby unit at a hospital in Cambridge.

The team compared the entire genetic code of MRSA bacteria from each baby and found them all related and part of a single outbreak. Tests on hospital staff then found one employee who carried the MRSA strain. Treating them for the infection ended the outbreak.

Parkhill said scientists want to build databases that hold up-to-date details of emerging strains of C difficile and other pathogens, so that dangerous new strains can be spotted early on, before they spread through hospitals and around the world.