For the answer, remember the fact that Walter's brain works much in analytical fashion. He deduces, assumes or implies even the tiniest possibilities for any given situation.
It was obvious that Lionel and Marco Salamanca were cousins of Tuco, this part must have been told (or at the best heard while stories were being passed around between colleagues and friends of Hank from DEA) by DEA friends of Hank waiting in hospital. So Walter figured out that whole thing was revolving around Tuco's death.
Also, before Tuco himself had mentioned existence of his 'cousins'.
(General Implications: Hank likes to brag about the case he works on especially to the White Family. Also, when Skyler tells him in hospital that Hank was in shootout, Walter immediately shows concern towards the events. So we can easily imply that Hank must have told Walter about the shootout, Hector and the fact that DEA must have figured out some way to communicate with him, Ouija or Alphabet board. Also, how Salamancas were part of cartel and so on.)
So basically he deduced that Hector must have somehow narrated the whole incident to the cousins. He doesn't know that only name was provided.
Walter doesn't know who the cousins were, he realizes who there are once he visits the hospital and meets Hanks friends and colleagues.
So it was just his analytical brain that works out that at least for the slightest moment, the cousins must have known his existence and involvement in Tuco's death. Though not exactly, but yes for the fact that Walter was present there and also attempted poisoning.
So basically Walter realizes/assumes that cousins were after everybody who were involved in Tuco's death, including Jesse. After that Walter must have assumed that cousins must have targeted the nearest victim first, that is Hank. Or more dangerous victim first. (Hank being DEA with field experience). They must have been planning to kill him or Jesse after Hank. Also, killing Walter or Jesse first would have put Hank in more active state and thus leading to them being tracked down.
(General Implications: While working with Gus for brief period, he must have easily realized that Gus and Cartels were enemies. There are many ways to know it, especially Gale Botechar. Gale has been working with Gus for long time, so it can be implied that since Gale becomes close to Walter, he must have discussed the relations of Gus and Cartel at least once.)
But later, I think Gomez tells Walter how Hank received call about the attack a minute before. Thus, Walter deduces that it must have been Gus one way or other who made the call. But he still doesn't conclude this possibility. The point where Gus comes in to inquire about Hank, makes Walter conclude that it was Gus.
Also, when he sees the injured cousin looking in his eyes and crawling towards the door, he realizes that he too was their target.
Till this point Walter realizes that cousins intended to kill everyone involved, including Jesse. Had they got chance, they would have killed Walter first.
But after Gus leaves and the injured cousin dies as Gus had hinted, Walter realizes that the whole thing was planned from the start.
It wouldn't have taken much time for Walter to deduce the whole scenario. Also, the time frame between Gus leaving hospital and and Walter having meeting with him was never suggested. So it could have been days. This much time was enough for Walter to figure out the scenario.
Also, as a viewer we are never shown other possibilities of event that Walter thought about. When Walter discusses the event with Gus, Gus never shows interest in the matter and just lets Walter think that events happened as he is thinking, thus giving us viewers the only outlook from Walter's view.