LONDON (Reuters) - Charlie Gard will spend his final hours in a hospice before the ventilator that keeps him alive is turned off, a judge ruled on Thursday, after a harrowing legal battle that prompted a debate over who has the moral right to decide the fate of a sick child.

Charlie’s distraught parents had been trying to find a medical team that could look after him in a hospice for several days so that they could bid farewell to him just days before his first birthday, which is due on Aug. 4.

A judge had given the parents until noon to reach an agreement with Great Ormond Street Hospital about spending more time in a hospice, but no compromise was reached so a judge ruled that Charlie’s artificial ventilation should be turned off.

“It is not in Charlie’s best interests for artificial ventilation to continue to be provided to him, and it is therefore lawful and in his best interests for it to be withdrawn,” High Court judge Nicholas Francis said in an order.

Francis ruled that Charlie be transferred to a hospice and that his ventilation be withdrawn, according to a copy of the order seen by Reuters.

The tragedy for Charlie and his family has snowballed into a global debate, partly fueled by social media, about the ethical dilemma of whether parents, doctors or the state should decide Charlie’s fate.

The case even drew comment from U.S. President Donald Trump and Pope Francis and involved months of legal wrangling about whether Charlie should be taken to the United States for experimental treatment.

Charlie suffers from an extremely rare genetic condition causing progressive brain damage and muscle weakness. A ventilator keeps him alive, he cannot move his arms or legs, and he cannot see, hear or swallow.

After reluctantly accepting that there was no hope for Charlie, his parents, Connie Yates and Chris Gard, had sought to take their son home to die.

But Great Ormond Street Hospital, where Charlie is being treated, said that would not be possible due to the invasive ventilation equipment needed to keep Charlie alive.

His parents then tried to find an intensive care doctor to oversee a plan that would allow Charlie to be ventilated in a hospice for several days.

A lawyer for Charlie’s court-appointed guardian had told the High Court that no hospice could provide care for intensively ventilated children for a long time, so the parents’ wish to spend several days with him could not be fulfilled.

Slideshow ( 4 images )

CHARLIE’S DEATH

The judge ruled that the name of the hospice and details about the arrangements for Charlie’s death should not be published.

Charlie’s parents fundamentally believed only they had the right to decide what medical treatment Charlie received, prompting a legal battle with Great Ormond Street Hospital, one of the world’s most prestigious children’s hospitals.

At one point on Wednesday, Yates shouted in court, apparently at Charlie’s guardian: “What if it was your child? I hope you are happy with yourself.” She then left the courtroom in tears.

Slideshow ( 4 images )

The guardian was appointed to represent Charlie’s interests as relations between his parents and Great Ormond Street Hospital, a pioneering pediatric center, soured.

“We deeply regret that profound and heartfelt differences between Charlie’s doctors and his parents have had to be played out in court over such a protracted period,” the hospital said.

“We will arrange for Charlie to be transferred to a specialist children’s hospice, whose remarkable and compassionate staff will support his family at this impossible time.”

His parents had wanted to take him to the United States to undergo an experimental treatment never before tried on anyone with his condition, against the advice of Great Ormond Street doctors, who said it would needlessly prolong his suffering.

The treatment they wanted for Charlie, called nucleoside therapy, had not even been tried on mice with the same disease which Charlie had, an extremely rare mutation of an inherited disease called infantile onset encephalomyopathic mitochondrial DNA depletion syndrome.

Such is the obscurity of the disease that the mutation Charlie suffers from is called simply “RRM2B”.

Charlie started having seizures before Christmas and his clinicians concluded that he had suffered irreversible brain damage by early this year.

His parents insisted he could have made a significant recovery if he had been treated differently.

Britain’s courts, backed by the European Court of Human Rights, refused permission for him to travel to the United States, saying it would prolong his suffering without any realistic prospect of helping him.

“Put simply, this is about a sweet, gorgeous, innocent little boy who was born with a rare disease, who had a real, genuine chance at life and a family who love him so very dearly and that’s why we fought so hard for him,” his parents said in a statement earlier this week.

“We are struggling to find any comfort or peace with all this, but one thing that does give us the slightest bit of comfort, is that we truly believe that Charlie may have been too special for this cruel world.”