Interpolate between-spots

Main steps

Step 1:Filter spots that are not in tissue, only reserve the spots with 1 in in_tissue column
Step 2:Horizontal direction, interpolate new spot between the given spot (x_1, y_1) and its right spot (x_2, y_2)
Step 3:Vertical direction, for one row and its up-adjacent row,
- select two closest spots (x_l, y_l) and (x_r, y_r) in the above row to the given spot (x_o, y_o) in this row,
- interpolate one new spot in two adjacent rows.
[2]:
import os
import numpy as np
import pandas as pd
[3]:
path = '/mnt/lingyu/nfs_share2/Python/'
[4]:
os.chdir(str(path) + 'FineST/FineST/')
import FineST as fst
from FineST.datasets import dataset
import FineST.plottings as fstplt
[5]:
import importlib
import sys
sys.path.append(str(path)+'FineST/FineST/')
import FineST as fst
import FineST.processData
importlib.reload(FineST.processData )
from FineST.processData  import *

1. Load and filter original position list

Input: tissue_positions_list.csv file in spatial folder of ST data.

[6]:
patientxy = 'patient1'
os.chdir(str(path)+'FineST/FineST_local/Dataset/NPC/'+str(patientxy)+'/')

position = fst.filter_pos_list('tissue_positions_list.csv')
position
[6]:
barcode in_tissue array_row array_col pxl_row_in_fullres pxl_col_in_fullres
33 GTGTGAGCCGAGGTGC-1 1 1 33 1612 5423
35 GGGAACCACCTGTTTC-1 1 1 35 1613 5638
36 GTTCGTTGCGGACCAG-1 1 0 36 1426 5745
37 TGAGGTTGATCCCAAG-1 1 1 37 1613 5852
38 GATGCCACACTACAGC-1 1 0 38 1427 5960
... ... ... ... ... ... ...
3396 TTCGACAGAGCCCGTG-1 1 52 68 11140 9153
3397 AAGCATACTCTCCTGA-1 1 53 69 11327 9260
3398 GACGACGATCCGCGTT-1 1 52 70 11141 9368
3399 GGTAGAAGACCGCCTG-1 1 53 71 11328 9474
3400 GAGATGGGAGTCGACA-1 1 52 72 11141 9582

1331 rows × 6 columns

2. Interpolate ‘between spots’ in horizontal and vertical directions

[7]:
position_x = fst.inter_spot(position, direction='x')
position_x
[7]:
array_row array_col pxl_row_in_fullres pxl_col_in_fullres
0 21 18 5340.5 3804.5
1 20 19 5154.5 3912.5
2 22 19 5527.5 3911.5
3 19 20 4968.0 4020.0
4 21 20 5341.5 4019.5
... ... ... ... ...
1125 38 103 8537.0 12914.0
1126 40 103 8910.5 12913.5
1127 37 104 8350.5 13021.5
1128 39 104 8724.0 13020.5
1129 38 105 8537.5 13128.5

1130 rows × 4 columns

[8]:
position_y = fst.inter_spot(position, direction='y')
position_y
[8]:
array_row array_col pxl_row_in_fullres pxl_col_in_fullres
0 20.5 17.5 5247.5 3751.0
1 21.5 17.5 5434.0 3750.5
2 28.5 17.5 6740.5 3747.0
3 25.5 17.5 6180.5 3748.5
4 19.5 18.5 5061.0 3859.0
... ... ... ... ...
2651 38.5 104.5 8630.5 13074.5
2652 39.5 104.5 8817.5 13074.0
2653 38.5 104.5 8631.0 13074.5
2654 37.5 105.5 8444.5 13182.5
2655 38.5 105.5 8631.0 13182.0

2578 rows × 4 columns

3. Integrate ‘between spot’ and ‘within spot’

position_add: the position list of all between spots (m ~= 3n)

[9]:
position_add = fst.final_pos_list(position_x, position_y, position=None)
position_add
[9]:
array_row array_col pxl_row_in_fullres pxl_col_in_fullres
0 20.5 17.5 5247.5 3751.0
1 21.5 17.5 5434.0 3750.5
2 25.5 17.5 6180.5 3748.5
3 28.5 17.5 6740.5 3747.0
4 21.0 18.0 5340.5 3804.5
... ... ... ... ...
3703 38.5 104.5 8631.0 13074.5
3704 39.5 104.5 8817.5 13074.0
3705 38.0 105.0 8537.5 13128.5
3706 37.5 105.5 8444.5 13182.5
3707 38.5 105.5 8631.0 13182.0

3708 rows × 4 columns

position_all: the position list of all between spots and all within spot (m+n ~= 4n)

[10]:
position_all = fst.final_pos_list(position_x, position_y, position)
position_all
[10]:
array_row array_col pxl_row_in_fullres pxl_col_in_fullres
0 50.0 0.0 10749.0 1860.0
1 21.0 17.0 5341.0 3697.0
2 29.0 17.0 6834.0 3693.0
3 20.5 17.5 5247.5 3751.0
4 21.5 17.5 5434.0 3750.5
... ... ... ... ...
5034 38.0 105.0 8537.5 13128.5
5035 39.0 105.0 8724.0 13128.0
5036 37.5 105.5 8444.5 13182.5
5037 38.5 105.5 8631.0 13182.0
5038 38.0 106.0 8538.0 13236.0

5039 rows × 4 columns

[11]:
!pwd
/mnt/lingyu/nfs_share2/Python/FineST/FineST_local/Dataset/NPC/patient1
[ ]:
## save position list to .csv file
position_add.to_csv(str(patientxy)+"_position_add_tissue.csv")
position_all.to_csv(str(patientxy)+"_position_all_tissue.csv")
[13]:
print(position_add.shape[0]/position.shape[0])
print(position_all.shape[0]/position.shape[0])
2.7858752817430505
3.7858752817430505