I write music the same way, hear it in my head fist then get it down. Easiest way is to break it down bar by bar and record it into a sequencer. When it's all in there you can then work out how to play it fluidly and get it up to the desired speed. A good way of developing this ability is to record yourself whistling, humming or singing the part you want to play - then loop each section you are working on. Then it's just a matter of training your ear over time to pick out the notes on the fretboard.