Dominion: Doctor + Rats
Dominion cards are always processed from the top down:


  To play an Action, the player takes an Action card from his hand
  and lays it face-up in his play area. He announces which card he is
  playing and follows the instructions written on that card from top
  to bottom.  


-- Base rules PDF

However, "when you trash" cards interrupt the existing Action:


  Many cards in Dark Ages do something "when you trash" that card. These abilities function no matter whose turn the card is trashed on. The player that a "when you trash this" ability functions for is the player that had that card, regardless of whose card trashed the card. These abilities happen directly after the card is put into the trash, and can function in the middle of resolving effects for an Action card; for example, if a player plays Graverobber and uses it to trash a Cultist, he first draws three cards for trashing Cultist, then continues with resolving Graverobber and gains a card costing up to $8.


-- Dark Ages rules PDF

Therefore, you draw the card from trashing the Rats before the cards would be put back on top of your deck because Doctor has "Trash the matches." before "Put the rest back on top in any order." and the Rats trashing resolves during the "Trash the matches" section of Doctor.