A federal district court judge in Boston ruled on Monday that the National Institutes of Health acted illegally in terminating about 800 research grants.

The terminated NIH grants focused on topics related to diversity, transgender issues, and other areas of research disfavored by the Trump administration.

The ruling came in a hearing that combined several lawsuits over NIH grants, including one filed by the American Public Health Association and another by a coalition of states led by Massachusetts.

A separate lawsuit is pending in New York over terminations of grants by the National Science Foundation. Massachusetts is also a party to that case. And Harvard University has filed its own suit over the termination of billions of dollars in federal funding for research on topics that range far beyond the ideological scope targeted by the lawsuits considered in court on Monday.

U.S. District Court Judge William Young ruled the NIH’s terminations of those grants were “arbitrary and capricious,” and he ordered the agency to immediately resume payments for the affected grants.

Attorney Rachel Meeropol of the ACLU represents some of the plaintiffs and spoke after the ruling.

“The judge looked at the agency’s explanation for why it would not fund these categories of research and said that they were wholly without reason and without reasoning,” Meeropol said.

NIH had come forward with no explanation as to why important medical research that seemed like it would be essential to protect the health of people in this country, why it is all of the sudden on the chopping block. The agency completely failed to explain what it was doing adequately to the American people.”

“I’m very grateful that the judge understood both the urgency and severity of what was at stake,” said Kenneth Parreno, an attorney with the group Protect Democracy, which also represented plaintiffs.

“Our clients, who are researchers, have dedicated their lives to furthering the scientific endeavor to just bettering public health trying to save lives out there and grateful that the judge recognized that the sheer disruption and unlawfulness of what the government has done.”

Parreno said he’s grateful to those researchers for their role in the lawsuit.

“This is an existential crisis here for science and public health,” he said. “They put a lot on the line, they were part of this case, and we’re just grateful that today we could be here because of their bravery.”

After issuing his verbal ruling, the judge went on to address what he described as a “darker aspect” of the case that he said he felt compelled to address. He went on to say the government’s actions in the case amounted to racial discrimination and discrimination against the LGBTQ population.

Young said the “extirpation of affirmative action” is a legitimate government policy, but warned that “it is not a license to discriminate on the basis of color.”

“I’ve never seen a record where racial discrimination is so palpable,” Judge Young said from the bench. He said it was clear that the termination of some grants were designed to stop research on matters related to the health of LGBTQ Americans.

“That’s appalling,” he said, adding that he had never seen anything like this in his 40 years on the bench. He invited the plaintiffs attorneys to offer evidence of the harm to those populations — so he could weigh in on that.

“Have we fallen so low?” Judge Young closed by asking. “Have we no shame?”

Attorneys for the U.S. Department of Justice have not yet said if they plan to appeal the judge’s ruling. In Monday’s hearing, Judge Young asked an attorney for the government if he expected the government to comply with the ruling. The attorney said yes.