In my collaboration we had agreed that I would be second author, but in the end I did almost all of the work. My collaborator does not want to give up position as first author. What should I do
The authorship on a paper is a mutual agreement between the authors. In this case, the solution would be joint first authorship, which is quite common in biomedical sciences. This is usually indicated by an asterisk in the authors’ byline (*Joint first authors or *Both authors contributed equally to the work and should be considered first authors). If you have good arguments and evidence for joint authorship, this would be the solutions. But be aware that not all journals allow this practice, however common it is in the research field.
The order of authors in a scientific paper should always be determined by an evaluation of the amount of each author’s scientific contributions; the planning of the study, data collection and interpretation, drafting of paper etc. This implies that such a decision should be made at the time of submission of a paper, not at an earlier stage, since it is not possible to know who has contributed sufficiently to be author - and who should be ranked first, second, third etc before the paper is ready for submission. Hierarchical position should never be allowed to determine the order of aurhorship, only scientific contribution.
This is a difficult case because you are seeking to change a prior agreement. Unless the other author agrees, then the stark choice is between accepting the status quo or not publishing. I do agree that the suggestion by Ana of “joint first authorship” could be a good compromise. A likely outcome of such a disagreement is that the collaboration ends. Is that what the other author wants?