(It's going to be tough to explain all of this in a single answer. If you're interested in this, I strongly recommend finding a music theory text, either online or in hard copy. But I'll do my best to address it all here!)
When it comes to major and minor keys, the best way to determine tonality, in my opinion, is to determine the location of half steps. (You can also determine the location of the tritone, but really that's just a fancier way of determining the half steps.)
Major scales have a pattern of WWHWWWH, where H is a half step and W is a whole step (two half steps). Minor scales are a bit trickier, because there are three uses of minor—natural minor, harmonic minor, and melodic minor—but we can skip that for this answer.
Looking at your example, there are half steps between E
and D♯
, between G♯
and A
, and between A♯
and B
. (There's also a half step between A
and A♯
, but we'll address that in the next paragraph.) If we try to compare this to our WWHWWWH pattern, we see that the best fit starts on E
. E
to F♯
is a whole step, F♯
to G♯
is a whole step, G♯
to A
is a half step, and so on.
The only trouble is the appearance of A♯
. Here's where it gets tricky, but not too tricky, because we have a rule in tonal music: each note name will only appear once in a major scale. If another version of that note name appears, it will be a chromatic pitch. So since A
makes sense in our E-major scale, we can view the A♯
as a chromatic pitch. All of this tells us that these pitches are likely in E major, with a brief move towards (what we call a tonicization of) B.
(Note: B major fits just as well as E major, and we could call the A
a chromatic pitch in B major. My decision of E major comes from years of experience with tonal music, where it's a very common move for music to begin in the original key and move to the fifth scale degree. This is also more common than the ♭7 that would be the A
at the beginning of a B-major excerpt.)
As for transposition, the quickest method (again, just my opinion) is to think in terms of scale degrees. In E major, the pitches you wrote are 1 7 1 7 1 1 7 1 2 3 2 3 4 5 ♯4 5 ♯4 5
. Now we can just think in D major—D E F♯ G A B C♯ D
—and write out those scale degrees in D to transpose. This results in D C♯ D ♯ D D C♯ D E F♯ E F♯ G A G♯ A G♯ A
.