Generally it is advisable to mention what country you're asking about, as tax laws differ.
To the best of my knowledge, however, this particular issue is handled consistently in every tax jurisdiction I'm familiar with.
You invested X, it appreciated and is now worth X + Y. In your example, X = $10,000 and Y = $40,000. Total X + Y = $50,000.
When you withdraw an amount, say A (in your example A = $10,000), it is considered a withdrawal of both the earnings and the original capital, in proportion to the total of your account.
Taxable portion of the withdrawal is proportional to the earnings. Lets mark it T.
T = X - A * X/(X+Y)
In your example, T = $10,000 - $10,000 * $10,000/$50,000 = $8,000. I.e.: 80% of the withdrawal will be attributed to earnings and would be taxable (short term in your case, if you're in the US), and 20% to the original capital.
This will keep the proportion of the remained the same - 20% of the remaining amount will be attributed to the original capital (accidentally, it will be $8,000), and the remaining 80% will be attributed to the earnings. The withdrawn amount attributed to the capital ($2,000), and the remaining amount attributed to the capital ($8,000) will equal exactly to the invested amount.