The doctor took a sperm sample from him; does he have to do ghusl?
Firstly:
Extracting semen by hand and the like comes under the heading of masturbation which is haraam, but if that is for the purpose of medical tests and treatment there is nothing wrong with it, as is explained in the answer to question no. 27112. See also question no. 329 for more information on the evidence that masturbation is haraam.
Secondly:
If the semen is extracted by means of pressing on the prostate or by some other method, then one of two scenarios must apply. Either it is emitted with feelings of pleasure, which is what usually happens, in which case ghusl is required, according to scholarly consensus; or it is emitted without any feelings of pleasure, which usually happens as the result of sickness, and without deliberately making it happen. In this case there is a difference of opinion among the fuqaha’. The majority of Hanafis, Maalikis and Hanbalis are of the view that it does not mean that ghusl is required, and this is the correct view.
This applies to one who is awake. As for one who is sleeping, if semen is emitted he must do ghusl in all cases, whether it came out with feelings of pleasure or not.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) said: The apparent meaning of the hadeeth (“Water is for water”) is that ghusl is required whether it comes out gushing and with feelings of pleasure or not. This is the view of al-Shaafa’i (may Allaah have mercy on him), that the emission of semen in all cases means that ghusl is required, even if it is without desire and by any means, because of the general meaning of the hadeeth. The majority of scholars are of the view that the obligation of ghusl is dependent on whether it comes out gushing with feelings of pleasure.
Some scholars referred only to feelings of pleasure and omitting the idea of gushing, and said: If it is with feelings of pleasure then it will inevitably be gushing. The mention of gushing is in accordance with the verse in which Allaah says (interpretation of the meaning):
“So let man see from what he is created!
6. He is created from a water gushing forth”
[al-Taariq 86:5, 6]
If it is emitted without feelings of pleasure when one is awake, then ghusl is not required. This is the correct view.
If it is said: What is the answer to the hadeeth “Water is for water”?
We say: This applies to the usual case, which is emission with feelings of pleasure, which leads to relaxation and tiredness. As for that which is not like that, it does not lead to relaxation and tiredness. End quote from al-Sharh al-Mumti’.
See also the answer to question no. 12317.
And Allaah knows best.