def track(params): # Read in the image data image_data = images.ImageData() image_data.read(params.name + ".tif", params) if params.ALEX==True: imageL=np.zeros((image_data.num_frames//2,image_data.frame_size[1],image_data.frame_size[0]//2)) imageR=np.zeros((image_data.num_frames//2,image_data.frame_size[1],image_data.frame_size[0]//2)) if params.start_channel=='L': for i in range(0,image_data.num_frames-1,2): imageL[i//2,:,:] = image_data.pixel_data[i,:,:image_data.frame_size[0]//2] imageR[i//2,:,:] = image_data.pixel_data[i+1,:,image_data.frame_size[0]//2:] else: for i in range(0,image_data.num_frames-1,2): imageR[i//2,:,:] = image_data.pixel_data[i,:,:image_data.frame_size[0]//2] imageL[i//2,:,:] = image_data.pixel_data[i+1,:,image_data.frame_size[0]//2:] image_data.num_frames = image_data.num_frames//2 #LHS image_data.pixel_data = imageL image_data.frame_size = [image_data.frame_size[0]//2,image_data.frame_size[1]] all_spots = [] for frame in range(image_data.num_frames): all_spots.append(track_frame(image_data[frame], frame, params)) trajs = trajectories.build_trajectories(all_spots, params) trajectories.write_trajectories(trajs, params.name + "_Lchannel_trajectories.tsv") #RHS image_data.pixel_data = imageR all_spots = [] for frame in range(image_data.num_frames): all_spots.append(track_frame(image_data[frame], frame, params)) trajs = trajectories.build_trajectories(all_spots, params) trajectories.write_trajectories(trajs, params.name + "_Rchannel_trajectories.tsv") # For each frame, detect spots else: all_spots = [] if params.num_procs == 0: for frame in range(image_data.num_frames): all_spots.append(track_frame(image_data[frame], frame, params)) else: res = [None] * image_data.num_frames with mp.Pool(params.num_procs) as pool: for frame in range(image_data.num_frames): res[frame] = pool.apply_async(track_frame, (image_data[frame], frame, params)) for frame in range(image_data.num_frames): all_spots.append(res[frame].get()) # Link the spot trajectories across the frames trajs = trajectories.build_trajectories(all_spots, params) trajectories.write_trajectories(trajs, params.name + "_trajectories.tsv")
def get_copy_number(params, calculated_isingle, channel=None): image_data = images.ImageData() image_data.read(params) frame = image_data.pixel_data[spots.laser_on_frame, :, :] copy_nums = [] f = open(params.name + "_copy_numbers.tsv") f.write("Cell\tCopy number\n") bg = np.mean(frame[image_data.mask == 0]) for i in range(1, np.amax(image_data.mask_data) + 1): copy_nums.append( np.sum(frame[image_data.mask_data == i]) / calculated_isingle) f.write(str(i) + "\t" + str(copy_nums[i - 1]) + "\n") f.close()
def update_slider(render_selection, vis_frame, active_file): if active_file: params = Parameters() params.name = active_file.replace('.tif','') image_data = images.ImageData() image_data.read(active_file, params) fig = px.imshow( image_data.pixel_data[vis_frame-1,:,:], color_continuous_scale='gray', zmin = 0, zmax = np.max(image_data.pixel_data[:,:,:]), ) if (render_selection == "render-all-trajectories" or render_selection == "render-current-trajectories"): trajs = read_trajectories(params.name + "_trajectories.tsv") colors = px.colors.qualitative.Plotly if trajs: for traj in trajs: if (render_selection == "render-current-trajectories" and (vis_frame < traj.start_frame or vis_frame > traj.end_frame)): continue Xs = [] Ys = [] color = colors[traj.id % len(colors)] for frame in range(traj.start_frame, traj.end_frame): Xs.append(traj.path[frame - traj.start_frame][0]) Ys.append(traj.path[frame - traj.start_frame][1]) fig.add_trace(go.Scatter(x=Xs, y=Ys, marker=dict(color=color, size=5))) fig.update_layout(showlegend=False) label = f"Frame {vis_frame} of {image_data.num_frames}" num_frames = image_data.num_frames else: logo_path = f"{Path(__file__).parent}/assets/pystachio_logo.tif" logo_tif = tifffile.imread(logo_path) fig = px.imshow( logo_tif[:,:,0], color_continuous_scale='gray', zmin = 0, zmax = np.max(logo_tif[:,:,:]), ) label = f"Upload a file to get started..." num_frames = 1 return fig, label, num_frames
def colocalize(params, Ltrajs, Rtrajs): image_data = images.ImageData() image_data.read(params.name + '.tif', params) imageL = np.zeros((image_data.num_frames // 2, image_data.frame_size[1], image_data.frame_size[0] // 2)) imageR = np.zeros((image_data.num_frames // 2, image_data.frame_size[1], image_data.frame_size[0] // 2)) if params.start_channel == 'L': for i in range(0, image_data.num_frames - 1, 2): imageL[i // 2, :, :] = image_data.pixel_data[ i, :, :image_data.frame_size[0] // 2] imageR[i // 2, :, :] = image_data.pixel_data[i + 1, :, image_data.frame_size[0] // 2:] else: for i in range(0, image_data.num_frames - 1, 2): imageR[i // 2, :, :] = image_data.pixel_data[ i, :, :image_data.frame_size[0] // 2] imageL[i // 2, :, :] = image_data.pixel_data[i + 1, :, image_data.frame_size[0] // 2:] Llinks = [] Rlinks = [] nlinks = [] for i in range(params.num_frames): s1 = [] s2 = [] for traj in Ltrajs: if traj.start_frame <= i and traj.end_frame >= i: s1.append( [traj.path[i - traj.start_frame], traj.width, traj.id]) for traj in Rtrajs: if traj.start_frame <= i and traj.end_frame >= i: s2.append( [traj.path[i - traj.start_frame], traj.width, traj.id]) id1, id2 = linker(params, s1, s2) for j in range(len(id1)): found = False if id1[i] in Llinks: indices = np.where(Llinks == id1) for index in indices: if Rlinks[index] == id2[j]: nlinks[index] += 1 found = True if found == False: Llinks.append(id1[j]) Rlinks.append(id2[j]) nlinks.append(1) outfile = params.name + "_colocalized_trajectories.tsv" f = open(outfile, 'w') f.write("Left_traj\tRight_traj\n_frames") for i in range(len(Llinks)): if nlinks[i] >= params.colocalize_n_frames: f.write("$i\t%i\t%i" % (Llinks[i], Rlinks[i], nlinks[i])) colors = ['r', 'b', 'g', 'm', 'y', 'c'] c = 0 z, y, x = imageL.shape disp_frame = np.zeros((y, 2 * x)) disp_frame[:, :x] = imageL[1, :, :] disp_frame[:, x:] = imageR[1, :, :] plt.imshow(disp_frame, cmap="Greys_r") plt.plot([x, x], [0, x], 'w--') plt.xticks([]) plt.yticks([]) plt.ylim([0, x]) if display_figures: plt.show() plt.imshow(disp_frame, cmap="Greys_r") for traj in Ltrajs: if traj.id in id1: plt.scatter(traj.path[0][0], traj.path[0][1], 10, marker='x', color=colors[c]) for t2 in Rtrajs: index = np.where(id1 == traj.id)[0][0] if t2.id == id2[index]: plt.scatter(t2.path[0][0] + x, t2.path[0][1], 10, marker='x', color=colors[c]) c += 1 plt.yticks([]) plt.xticks([]) plt.plot([x, x], [0, x], 'w--') plt.ylim([0, x]) plt.savefig("colocalized_spots.png", dpi=300) if params.display_figures: plt.show() plt.close()
def render(params): image_data = images.ImageData() image_data.read(params.name+".tif", params) trajs = trajectories.read_trajectories(params.name + "_trajectories.tsv") true_trajs = trajectories.read_trajectories(params.name + "_simulated.tsv") maxval = image_data.max_intensity() figure = plt.figure() plt_frames = [] px_size = params.pixel_size fr_size = image_data.frame_size colors = list(mcolors.TABLEAU_COLORS.keys()) if trajs: if params.verbose: print(f"Displaying {len(trajs)} trajectories") for traj in trajs: x = [] y = [] for frame in range(traj.start_frame, traj.end_frame): x.append(traj.path[frame - traj.start_frame][0]) y.append(traj.path[frame - traj.start_frame][1]) x = np.array(x) y = np.array(y) x_scaled = (x+0.5) * px_size y_scaled = (fr_size[1]-y-0.5) * px_size plt.plot(x_scaled,y_scaled,"o-",c=colors[traj.id % len(colors)]) if true_trajs: if params.verbose: print(f"Displaying {len(true_trajs)} true trajectories") for traj in true_trajs: x = [] y = [] for frame in range(traj.start_frame, traj.end_frame): x.append(traj.path[frame - traj.start_frame][0]) y.append(traj.path[frame - traj.start_frame][1]) x = np.array(x) y = np.array(y) x_scaled = (x+0.5) * px_size y_scaled = (fr_size[1]-y-0.5) * px_size plt.plot(x_scaled,y_scaled,"+--",c="tab:orange") plts = [] for frame in range(image_data.num_frames): plt_frame = plt.imshow( image_data.pixel_data[frame,:,:], vmin=0, animated=True, vmax=maxval, cmap=plt.get_cmap("gray"), extent=[0, fr_size[0]*px_size, 0, fr_size[1]*px_size] ) plts.append([plt_frame]) ani = animation.ArtistAnimation(figure, plts, interval=50,) plt.title(f"Simulated spot data, frame {image_data.num_frames}") plt.xlabel("μm") plt.ylabel("μm") plt.colorbar() plt.show()