"Phantasm" was the 6th episode of the last season and was written by Brannon Braga.
In this episode, it is Data who can sense creatures on a subconscious level instead of Troi and has dreams that save the crew from certain death.
The story begins with Data walking along the corridor when he sees Geordi standing along the wall who tells him that they will be working on the plasma conduit for the warp engines soon. Then he sees three strange men breaking down the wall with sledgehammers and Data asks them if they are authorized to work in the area. They keep working and when Data tries to insist they stop, a high pitched whistle noise comes out of his mouth and the workmen attack Data, tearing him apart, literally limb to limb.
Then we see him sit up with a confused look on his face. He had activated his dream program (the one introduced in "Birthright"). He goes to work with Geordi in engineering while telling him about his strange dream with disturbing images. Later, he has another dream where he's in Ten Forward and sees Worf eating something. He asks him "what are you eating?" and Worf says "It's a cellular peptide cake--with mint frosting. Delicious." Then he sees Dr. Crusher drinking from a straw out of Riker's temple. There's ringing. Riker tells Data to answer it. He walks toward a group of workmen and they show Data a cake shaped like an upper torso with a blue Starfleet uniform. It has Counselor Troi's head and she pleads with Data to stop as he proceeds to cut with a knife the workmen gave him into the cake's shoulder area. Troi yells "Data!" as Data wakes up with her and Worf looking on. Data is confused as to how he could have "overslept."
He creates a counseling session with Sigmund Freud in the holodeck to tell him about his dream. Freud tells Data that he has a desire to possess his mother and has anger issues with his father. Not being satisfied with this, Data ends the holodeck program. He goes back to engineering to assist Geordi with the warp drive, which is malfunctioning. Geordi hands Data a brace coil that resembles a carving knife with a handle and tells him to check it for fractures. When Geordi turns his back, Data sees something like a mouth on his neck.
Now, Data is back in a dream, a waking dream, and uses the brace coil as a phone where he hears Freud tell him to "Kill them all." Data visits Counselor Troi and she suggests Data may be experiencing some sort of neurosis like a human would. She also tells Data to come to her next time instead of Sigmund.
As Troi heads to the turbolife, she has a sense of being followed. She turns around to find Data with the brace coil, who proceeds to stab her with it in her shoulder. Worf and Riker find Troi unconscious in the turbolift and get her to sick bay.
Data tells the crew in the conference room about his dreams and he is confined to quarters until they figure out what's going on with him.
Meanwhile, Dr. Crusher is puzzled why Troi's would still appears red after treatment. She investigates and sees, with an interphasic scanner, a parasite on the wounded shoulder. She also informs the captain that rest of the crew is also infected. During a conference, she tells them that the parasites are eating tissue on the cellular peptide level and that they will eventually kill everyone. They reason that Data isn't affected by the parasites on a biological level, but he is able to detect them on a subconscious level.
To get to the bottom of what's happening and how to get rid of them, the captain and Geordi try an experiment to hook up Data's neural net to the hologram to see his dream. Data has another dream with the same images and the captain and Geordi witness the dream along with Data. They hear a phone ringing and Riker orders them to answer it. Picard does and now they are in the holodeck and Freud tells them they must kill them all. They discover that the high pitched noise Data does in the dream causes the "workmen" pain. Upon waking, Data has Geordi rig up his positronic sub processor to emit a pulse on that frequency that will eliminate the bugs.
The crew figures that the planet they got the new core conduit from was carrying the creatures. Counselor Troi pays Data a visit with a cake she made---in the shape of a certain Lt. Commander in a gold uniform.
I like this episode because it deals with a subject matter of dreams of which I'm interested in. I've kept a dream journal for years and I remember being highly impressed with this episode when it first aired. I like the symbolism of the "cellular peptide cake--with mint frosting" and Freud representing Data's subconscious to warn the crew about the danger they are in. The show also has humorous side stories as well. Captain Picard is being hounded by an insistent admiral to attend a formal dinner that evening on the nearby star base. Picard is relieved to learn that getting the plasma conduit back on line will take longer than the time allotted to make the dinner. Geordi is annoyed by a flirtatious ensign who has the hots for him. And Data asks Worf to look after Spt while he's under house arrest. "Your animal?" Worf says and gives a loud sneeze on his way out the door with Spot.