0

Please take a look at the image A below. Image of a linear DNA strand, coolwarm colormap Some introduction to my question: My goal is to obtain an accurate coordinate trace of the illustrated DNA molecule. The coordinates of the trace are represented by the blue dots in the image and are represented as a 2-d numpy array in Python, i.e.: trace: nd-array; shape (N, 2) where N is the number of trace points. The plots was done using plt.scatter(trace[:, 1], trace[:, 0]).

Now, please take a closer look at the following function:

def rel_coords_to_trace(trace, distance_limit=5.0):
    """
    Finds the pixels in the image that are within the 'distance_limit' of the 'trace' points. For those pixels the
    relative coordinates to the closest trace point is calculated.

    Args:
        trace ([N, 2] array): Initial trace of the DNA strand
        distance_limit (float): Maximum distance a pixel can have from the trace to be taken into account
    Returns:

        pixels: Array with row/column coordinates of the pixels within the distance limit from the trace
        trace_id: Int relating each pixel from the 'pixels' array to the point in the 'trace' it is closest to
        relative_coords ([N, 2] array): Relative x and y distances of all pixels from the closest point of the trace
        heights([N, ] array): Height of the image at the position of the pixel
    """

    min_r, min_c = np.floor(trace.min(axis=0) - distance_limit).astype(int).clip(min=0)
    max_r, max_c = np.ceil(trace.max(axis=0) + distance_limit).astype(int).clip(max=mol_filtered.shape)
    pixels_pos = np.mgrid[min_r:max_r, min_c:max_c].reshape([2, -1]).T      # all potential pixels

    # kdTree finds the nearest neighbour between a specific pixel and all trace points
    # Returns distances between pixels and nn and the id of the nn. Distances are inf if bigger than distance_limit
    kdtree = cKDTree(trace)
    distances, trace_id = kdtree.query(pixels_pos, k=1, distance_upper_bound=distance_limit)
    pixels = pixels_pos[distances != np.inf]
    trace_id = trace_id[distances != np.inf]
    rel_coords = pixels - trace[trace_id]

    return rel_coords, pixels, trace_id

Its execution is illustrated in Image BBlack points are pixels of the image. White arrows indicate the nearest neighbors of each pixel in trace. My question: Now, when I have a sharp turn in my coordinate trace, I get comparatively many white arrows that point to a specific trace point from more or less one direction. My goal is to quantify how many more white arrows are pointing from one side of the trace (in a direction normal to the trace) compared to the other side of the trace. This quantification doesn't have to be exact, I just somehow want to add a respective weight into the mix.

How can I achieve this quantification?

LionCereals
  • 171
  • 1
  • 10
  • Also, provide a reproducible trace, such as: `np.random.seed(0); x = sorted(np.random.randint(0, 100, 10)); np.random.seed(42); y = sorted(np.random.randint(0, 100, 10))` or something like that – Max Pierini Apr 02 '21 at 15:32
  • Please, fix your `def`: where is `mol_filtered` defined? – Max Pierini Apr 02 '21 at 15:38

1 Answers1

1

I don't understand what you need to quantify exactly.

For example, how do you define in this image, whether pixel 8,7 is normal to segment AB or to BC?

enter image description here

I mean, cKDTree is from point to point and you want neighbor points to be aligned to the grid (but they could be anywhere else)

enter image description here

how did you define the pixel to line relationship?

Max Pierini
  • 2,027
  • 11
  • 17