forked from LiukangWu/CytoCommunity2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStep0_GeneratePseudoSpatialMaps.py
More file actions
90 lines (64 loc) · 2.92 KB
/
Step0_GeneratePseudoSpatialMaps.py
File metadata and controls
90 lines (64 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import shutil
import pandas as pd
import datetime
import random
# Hyperparameters
InputFolderName = "./CODEX_SpleenDataset/"
## Below is for generation of pseudo-spatial maps by shuffling cell types in original REAL spatial maps.
ThisStep_OutputFolderName = "./Step0_Output/"
if os.path.exists(ThisStep_OutputFolderName):
shutil.rmtree(ThisStep_OutputFolderName)
os.makedirs(ThisStep_OutputFolderName)
def shuffle_lines(input_file_path, output_file_path):
# Read all lines in the input file.
with open(input_file_path, 'r') as file0:
lines = file0.readlines()
# Shuffle lines
random.shuffle(lines)
# Write all shuffled lines to the output file.
with open(output_file_path, 'w') as file1:
file1.writelines(lines)
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
print("Generate a pseudo-spatial map for each sample...")
# Copy files of real samples/images from the input folder.
for filename in os.listdir(InputFolderName):
if filename.endswith(".txt"):
file_path = os.path.join(InputFolderName, filename)
shutil.copy(file_path, ThisStep_OutputFolderName)
# Import image name list.
Region_filename = InputFolderName + "ImageNameList.txt"
region_name_list = pd.read_csv(
Region_filename,
sep="\t", # tab-separated
header=None, # no heading row
names=["Image"], # set our own names for the columns
)
for graph_index in range(0, len(region_name_list)):
print(f"This is image-{graph_index}")
region_name = region_name_list.Image[graph_index]
# Construct CellTypeLabel for pseudo-samples/images.
input_file_path = InputFolderName + region_name + '_CellTypeLabel.txt'
output_file_path = ThisStep_OutputFolderName + region_name + '_pseudo_CellTypeLabel.txt'
shuffle_lines(input_file_path, output_file_path)
# Copy Coordinates for pseudo-samples/images.
GraphCoord_filename = InputFolderName + region_name + "_Coordinates.txt"
GraphCoord_path = ThisStep_OutputFolderName + region_name + "_pseudo_Coordinates.txt"
shutil.copy(GraphCoord_filename, GraphCoord_path)
# Construct GraphLabel for real and pseudo-samples/images.
GraphLabel_file = ThisStep_OutputFolderName + '/' + region_name + "_GraphLabel.txt"
GraphLabel_pseudo_file = ThisStep_OutputFolderName + '/' + region_name + "_pseudo_GraphLabel.txt"
with open(GraphLabel_file, 'w') as file2: #REAL as "1"
file2.write('1')
with open(GraphLabel_pseudo_file, 'w') as file3: #PSEUDO as "0"
file3.write('0')
# Refine ImageNameList.
Region_filename_refined = ThisStep_OutputFolderName + "ImageNameList.txt"
with open(Region_filename_refined, 'r') as file4:
lines = file4.readlines()
new_lines = [line.strip() + '_pseudo\n' for line in lines]
all_lines = lines + new_lines
with open(Region_filename_refined, 'w') as file5:
file5.writelines(all_lines)
print("Step0 done!")
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))