Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About how wavefront offsets are saved #94

Closed
shenwei356 opened this issue May 29, 2024 · 5 comments
Closed

About how wavefront offsets are saved #94

shenwei356 opened this issue May 29, 2024 · 5 comments

Comments

@shenwei356
Copy link

Hi @smarco ,

I'm trying to understand the source code about how wavefront offsets are saved.

It looks like they are saved in a list,

https://github.com/smarco/WFA2-lib/blob/main/wavefront/wavefront.h#L62

  wf_offset_t* offsets;                // Offsets (k-centered)

which is accessed by the k as the index.

https://github.com/smarco/WFA2-lib/blob/main/wavefront/wavefront_backtrace.c#L73

mwavefront->offsets[k]

However, the k might be negative. How is this possible for a negative list index?

My guess is all k values are added by the length of the query sequence to make them >=0. But I can't find the code and this would occupy a lot of space, especially for long sequences.

In my implementation, to support negative k values, I use the layout below. But it requires bound checking in every reading/writing.

  index: 0,  1,  2,  3,  4,  5,  6
  k:     0, -1,  1, -2,  2, -3,  3

Best,
Wei.

@shenwei356
Copy link
Author

My guess is all k values are added by the length of the query sequence to make them >=0

It seems to be true. https://github.com/smarco/WFA2-lib/blob/main/wavefront/wavefront.c#L89

@RagnarGrootKoerkamp
Copy link
Contributor

Yes indeed, my understanding is that mwavefront->offsets is simply a pointer to the middle of a sufficiently large array so it can be indexed by the (possibly negative) diagonal directly.

@shenwei356
Copy link
Author

Thank you Ragnar. I just searched and found C++ does support a negative index !!!! https://stackoverflow.com/questions/47170740/c-negative-array-index

But Go don't allow that. So I'll still use the previous layout but grow the the array less frequently.

@smarco
Copy link
Owner

smarco commented May 29, 2024

Hi,

Yes. I know it can be a little bit weird at first. But, usually, the convention is to index the main diagonal as $k=0$, diagonals above as $k>0$, and below $k<0$. To be able to index negative $k$, I have to shift the mwavefront->offsets to point to the middle of the allocated region of memory.

But this is just a convention, and you may as well map the wavefront's indexes (i.e., diagonals) to any positive range.

Do not hesitate to contact me if you have more questions.

@smarco smarco closed this as completed May 29, 2024
@shenwei356
Copy link
Author

I see, thank you every much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants