# Extract start and end positions from a PairwiseAlignment

**URL:** https://discourse.julialang.org/t/extract-start-and-end-positions-from-a-pairwisealignment/77610
**Category:** Biology, Health, and Medicine
**Tags:** question, biology
**Created:** [March 8, 2022, 10:56pm UTC](https://discourse.julialang.org/t/extract-start-and-end-positions-from-a-pairwisealignment/77610 "2022-03-08T22:56:56Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![rohan.m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohan.m/32/26450_2.png) [@rohan.m](https://discourse.julialang.org/u/rohan.m)
#### Post date: [March 8, 2022, 10:56pm UTC](https://discourse.julialang.org/t/extract-start-and-end-positions-from-a-pairwisealignment/77610/1 "2022-03-08T22:56:56Z")

</div>

Hello, I am dealing with PairwiseAlignments of the form:

PairwiseAlignment{LongDNASeq, LongDNASeq}:  
seq: 2 TGTCTTTCGCTGCTGAGGGTAGA 24  
| | | | | | | | | | | | | | | | | | | | | |  
ref: 307 TGTCTTTCGCTGCTGAGGGT-GA 328

I just want to extract the start and end coordinates for the sequence (2, 24) and reference (307, 328), but this is surprising challenging. As far as I can tell, the current API for BioAlignments.jl does not cover the case of extracting locations. Although if I have one pair, then I can trivially extract the other pair using the seq2ref and ref2seq functions.

Does anybody know how I can extract the at least one of the seq and ref pairs, like (2, 24) and (307, 328) in the above example, from a PairwiseAlignment? Thanks in advance!

---

<div class="post-metadata">

### Author: ![rohan.m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohan.m/32/26450_2.png) [@rohan.m](https://discourse.julialang.org/u/rohan.m)
#### Post date: [March 9, 2022, 5:13am UTC](https://discourse.julialang.org/t/extract-start-and-end-positions-from-a-pairwisealignment/77610/2 "2022-03-09T05:13:39Z")

</div>

OK. I wrote the following one-line functions to solve my own problem. Hopefully helpful for others as well:

function GetPairwiseAlignmentSeqStart(aln::PairwiseAlignment)  
return aln.a.aln.anchors[1].seqpos + 1  
end

function GetPairwiseAlignmentRefStart(aln::PairwiseAlignment)  
return aln.a.aln.anchors[1].refpos + 1  
end

function GetPairwiseAlignmentSeqEnd(aln::PairwiseAlignment)  
return aln.a.aln.anchors[end].seqpos  
end

function GetPairwiseAlignmentRefEnd(aln::PairwiseAlignment)  
return aln.a.aln.anchors[end].refpos  
end

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [March 9, 2022, 10:46am UTC](https://discourse.julialang.org/t/extract-start-and-end-positions-from-a-pairwisealignment/77610/3 "2022-03-09T10:46:27Z")

</div>

> [@rohan.m](#):
>
> function GetPairwiseAlignmentSeqStart(aln::PairwiseAlignment)  
> return aln.a.aln.anchors[1].seqpos + 1  
> end

I don’t know that package, or the field, but it looks like you are reaching deep into the internals of the type. That is normally _not_ recommended practice in Julia.

If the internal fields (several layers deep) are not part of the API, you risk that your function suddenly stops working (or gives wrong answers) after a package update.

---

<div class="post-metadata">

### Author: ![jgreener64](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jgreener64/32/2483_2.png) [@jgreener64](https://discourse.julialang.org/u/jgreener64)
#### Post date: [March 9, 2022, 11:29am UTC](https://discourse.julialang.org/t/extract-start-and-end-positions-from-a-pairwisealignment/77610/4 "2022-03-09T11:29:45Z")

</div>

I recall having to do something similar in BioStructures: [https://github.com/BioJulia/BioStructures.jl/blob/master/src/spatial.jl#L155-L157](https://github.com/BioJulia/BioStructures.jl/blob/master/src/spatial.jl#L155-L157). It would be nice if there was a better way.

---

<div class="post-metadata">

### Author: ![rohan.m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohan.m/32/26450_2.png) [@rohan.m](https://discourse.julialang.org/u/rohan.m)
#### Post date: [March 9, 2022, 3:33pm UTC](https://discourse.julialang.org/t/extract-start-and-end-positions-from-a-pairwisealignment/77610/5 "2022-03-09T15:33:27Z")

</div>

I agree. To my knowledge-- and as you can tell, I dug into the internals of this package-- the current API does not allow the client to get this information _at all_. If I were more adept, I would submit a pull request to update the API to have some getters like this. I feel that I would probably create more work for the maintainers if I tried to make a “contribution”.

In the meantime, this solution works for me, and it should work for others too. I hope it can help more adept contributors to BioJulia to better understand clients’ use cases as they improve the API over time 🙏🏿

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [March 9, 2022, 4:45pm UTC](https://discourse.julialang.org/t/extract-start-and-end-positions-from-a-pairwisealignment/77610/6 "2022-03-09T16:45:10Z")

</div>

I think you could open an issue, explaining the use case. That could be helpful, and doesn’t require much expertise to do.

---

<div class="post-metadata">

### Author: ![rohan.m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rohan.m/32/26450_2.png) [@rohan.m](https://discourse.julialang.org/u/rohan.m)
#### Post date: [March 10, 2022, 3:02am UTC](https://discourse.julialang.org/t/extract-start-and-end-positions-from-a-pairwisealignment/77610/7 "2022-03-10T03:02:03Z")

</div>

I’ll do that, thanks.
