-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphenoptrreports_mergefile_splitter.py
159 lines (131 loc) · 6.03 KB
/
phenoptrreports_mergefile_splitter.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python3
"""
Copyright 2021 The Regents of the University of Colorado
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Author: Christian Rickert <[email protected]>
Group: Human Immune Monitoring Shared Resource (HIMSR)
University of Colorado, Anschutz Medical Campus
Title: phenoptrreports_mergefile_splitter
Summary: Reverse the merging process and split merge data
by sample name and/or MSI coordinates
DOI: https://doi.org/10.5281/zenodo.4741394
URL: https://github.com/christianrickert/CU-HIMSR/
Description:
We split merge files into individual files by using the sample name
and, upon request, also the sample's MSI coordinates.
Unmerged files can either be used to convert sets of samples/MSIs
into flow cytometry standard files or to re-merge and consolidate
smaller data subsets.
The header lines are preserved for each of the unmerged files.
"""
# imports
import os
import sys
# functions
def export_data(out_path='/home/user', out_data=None):
""" Writes data from an array to a file. """
with open(out_path, 'w') as out_file:
for out_line in out_data:
out_file.write(out_line)
def flatten(deep_list=None):
""" Returns a flattened list with elements from (deep) lists or tuples """
flat_list = []
for item in deep_list:
if isinstance(item, (list, tuple)):
flat_list.extend(flatten(item))
else:
flat_list.append(item)
return flat_list
def get_files(path='/home/user/', pattern='', recursive=False):
""" Returns all files in path matching the pattern. """
files = []
realpath = os.path.realpath(path)
with os.scandir(realpath) as fileobject_iterator:
for fileobject in fileobject_iterator:
if not os.path.islink(fileobject.path):
if fileobject.is_file() and pattern in fileobject.name: # match pattern
files.append(fileobject.path)
elif recursive and fileobject.is_dir():
files.append( \
get_files(path=fileobject.path, pattern=pattern, recursive=recursive))
return flatten(files)
def get_name_index(path='', delimiter='', name=None):
""" Returns the column index with the sample/MSI name. """
with open(path, 'r') as textfile:
for line_index, line in enumerate(textfile):
if line_index == 0: # read header for pattern matching
headers = line.split(delimiter)
else: # stop iteration afterwards
break
for index, column in enumerate(headers):
if column == name:
return index # index found
return float("nan") # no index found
def println(string=""):
""" Prints a string and forces immediate output. """
print(string)
sys.stdout.flush()
def unmerge_data(in_path='', index=0, by_msi=False, out_path=''):
""" Imports data from a text file and writes out all columns on a per-file basis
using the first column's data for labeling of individual export files. """
with open(in_path, 'r') as in_file:
name = os.path.splitext(os.path.basename(in_path))[0]
println("\tFOLDER: \"" + out_path + "\"")
if not os.path.exists(out_path):
os.mkdir(out_path)
println("\t\tSAMPLES:")
for in_index, in_line in enumerate(in_file):
if in_index == 0: # header
file_data = []
header = in_line
file_data.append(header)
current_sample = ""
previous_sample = ""
else: # data
current_sample = in_line.split("\t")[index].rsplit(sep=".", maxsplit=1)[0]
if not by_msi: # ignore MSI coordinates
current_sample = current_sample.rsplit(sep="_", maxsplit=1)[0]
if current_sample != previous_sample: # sample name or MSI coordinates changed
println("\t\t\t\t\"" + current_sample + "\"")
if previous_sample: # save collected data
export_data(out_path=out_path + os.path.sep + name + \
" - " + previous_sample + ".txt", out_data=file_data)
file_data = [] # prepare next sample
file_data.append(header)
previous_sample = current_sample
file_data.append(in_line)
# write last sample before opening a new input file
export_data(out_path=out_path + os.path.sep + name + \
" - " + previous_sample + ".txt", out_data=file_data)
# constants & variables
EXPORT_FOLDER = r".\export"
FILE_TARGET = "Merge_cell_seg_data.txt"
IMPORT_FOLDER = r".\import"
SPLIT_BY_MSI = False # split by name *and* MSI coordinates
VERSION = "phenoptrreports_mergefile_splitter 1.0 (2021-10-12)"
# main program
println(VERSION)
println(os.linesep)
println("UNMERGING files in folder:")
println("-----------------------")
println("FILE: \"*" + FILE_TARGET + "\"")
FILE_COUNT = 0
if not os.path.exists(EXPORT_FOLDER):
os.mkdir(EXPORT_FOLDER)
for file in get_files(IMPORT_FOLDER, FILE_TARGET):
println("\tNAME: \"" + file + "\"")
name_index = get_name_index(path=file, delimiter='\t', name="Sample Name")
unmerge_data(in_path=file, index=name_index, by_msi=SPLIT_BY_MSI, out_path=EXPORT_FOLDER)
FILE_COUNT += 1
print("UNMERGED FILES: " + str(FILE_COUNT) + ".")
println(os.linesep)
WAIT = input("Press ENTER to exit this program.")