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

Add query to ref coord mapping #120

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add query to ref coord mapping
  • Loading branch information
chilampoon committed Oct 10, 2023
commit 5b55e6b4efc96d5392fb494409af5571d9bd4db5
28 changes: 26 additions & 2 deletions src/remora/data_chunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
)
QUERY_OPS = np.array([True, True, False, False, True, False, False, True, True])
REF_OPS = np.array([True, False, True, True, False, False, False, True, True])
REFCOORD_OPS = np.array([True, False, True, False, False, False, False, True, True])
CIGAR_CODES = ["M", "I", "D", "N", "S", "H", "P", "=", "X"]
CODE_TO_OP = {
"M": 0,
Expand Down Expand Up @@ -59,10 +60,16 @@ def map_ref_to_signal(*, query_to_signal, ref_to_query_knots):
query_to_signal (np.array): Query to signal coordinate mapping
ref_to_query_knots (np.array): Reference to query coordinate mapping
"""
query_to_ref_coords = np.interp(
np.arange(query_to_signal.size),
np.arange(ref_to_query_knots.size),
ref_to_query_knots
).astype(int)

return np.floor(
np.interp(
ref_to_query_knots,
np.arange(query_to_signal.size),
query_to_ref_coords,
query_to_signal,
)
).astype(int)
Expand Down Expand Up @@ -98,11 +105,28 @@ def make_sequence_coordinate_mapping(cigar):
query_knots = np.concatenate(
[[0], (query_knots[is_match] - offsets).T.flatten(), [query_knots[-1]]]
)
knots = np.interp(np.arange(ref_knots[-1] + 1), ref_knots, query_knots)
ref_coords = get_ref_coords(ops, lens)
knots = np.interp(np.concatenate([[0], ref_coords]), ref_knots, query_knots)

return knots


def get_ref_coords(ops, lens):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dumb version of this function is:

def get_ref_coords(ops, lens):
    ref_coords = []
    s = 1

    for op, l in zip(ops, lens):
        if op in [3]:
            s += l
        elif op in [1,4,5]:
            continue
        elif op in [0,2,7,8]:
            # mapped
            pos_list = list(range(s, s + l))
            ref_coords.extend(pos_list)
            s += l
        
    return ref_coords

'''
Map query base to reference coordinates (1-based).
Returns
array shape (ref_len+1,); ref_len = len(aln.get_reference_sequences())
chilampoon marked this conversation as resolved.
Show resolved Hide resolved
'''
starts = np.cumsum(np.where(REF_OPS[ops], lens, 0)) - np.where(REF_OPS[ops], lens, 0)
ranges = [
np.arange(start, start + l)
for op, start, l in zip(ops, starts, lens)
if REFCOORD_OPS[op]
]
ref_coords = np.concatenate(ranges) + 1
Copy link
Collaborator

@marcus1487 marcus1487 Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still feel like there is extra logic here. I think the fix here might be as simple as completely dropping the skip cigar ops. Do you get the same results if you switch the REF_OPS skip code value to False without any other code changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about the same thing I think you're right - just changing the intron operation to False in REF_OPS can work as well... got the same results for genomic reads and didn't raise errors for transcriptomic reads

return ref_coords


def compute_ref_to_signal(query_to_signal, cigar):
ref_to_read_knots = make_sequence_coordinate_mapping(cigar)
assert query_to_signal.size == ref_to_read_knots[-1].astype(int) + 1, (
Expand Down