-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_haplotypePandas.py
272 lines (203 loc) · 8.81 KB
/
merge_haplotypePandas.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/home/bin/python
print("\nChecking required modules \n")
import argparse
from functools import reduce
import time
import resource
import re
import sys
import os
import shutil
import pandas as pd
""" Purpose of the program: Used to merge the haplotype file generated by phase-Extender
and phase-Stitcher. """
start01 = time.time()
""" Call input and output argument variable names"""
# inFile = sys.argv[1]
# outFile = sys.argv[2]
# hapList = hapList.txt
# f1List= f1_list.txt
# outFile = mergedHaplotype
# hap_paths="/media/everestial007/SeagateBackup4.0TB2/RNAseq_Data_Analyses/" \
# "04_B-GenomeWidePhasing/Step02_A-PhaseExtension/hapList.txt"
# f1_paths="/media/everestial007/SeagateBackup4.0TB2/RNAseq_Data_Analyses/" \
# "04_B-GenomeWidePhasing/Step02_A-PhaseExtension/f1List.txt"
def main():
"""Define required argument for interactive mode program."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--hapList",
help="name of the file that contains list of path to haplotype files obtained from phase-Extender.",
required=False,
)
parser.add_argument(
"--f1List",
help="name of the file that contains list of the path to F1 hybrid haplotype files obtained "
"from phase-Stitcher.",
required=False,
)
parser.add_argument(
"--f1ParentIDs",
help="comma separated names of the column header that indicates paternal haplotype "
"vs. maternal haplotype in F1 hybrids haplotypes. "
"Only required if 'f1List' is reported. ",
required=False,
)
parser.add_argument(
"--output",
help="Directory name to store the merged haplotype file.",
required=True,
)
global args
args = parser.parse_args()
global pat_hap
global mat_hap
## Assign the argument variables
hap_paths = args.hapList
f1_paths = args.f1List
output = args.output
if args.f1ParentIDs != None:
pat_hap = args.f1ParentIDs.split(",")[0] + "_hap"
mat_hap = args.f1ParentIDs.split(",")[1] + "_hap"
else:
pat_hap = "pat_hap"
mat_hap = "mat_hap"
# Create an output directory
if os.path.exists(output):
shutil.rmtree(output, ignore_errors=False, onerror=None)
os.makedirs(output, exist_ok=True)
with open(output + "/merged_haplotype.txt", "w+") as fileout:
if hap_paths == None and f1_paths == None:
print(
"Haplotype files from both phase-Stitcher and phase-Extender are missing."
)
print("Provide HAPLOTYPE files from at least one category.")
print()
sys.exit()
else:
if hap_paths != None:
hap_list = open(hap_paths, "r")
print("Reading HAPLOTYPE file names obtained from phase-Extender")
if f1_paths != None:
f1_list = open(f1_paths, "r")
print("Reading HAPLOTYPE file names obtained from phase-Stitcher")
pd_df_list = [] # to store dataframes
"""Now, pipe the list to function, that will extract required information. """
print()
print("## Loading the haplotype files")
print()
"""Step 01: Now, read each haplotype file using pandas dataframe.
** - possible future upgrade using sqlite. """
## from regular haplotype obtained from phase-Extender
if hap_paths != None:
pd_df_list = extract_haplotype(hap_list, pd_df_list, list_type="phaseExt")
## from regular haplotype obtained from phase-Stitcher
if f1_paths != None:
pd_df_list = extract_haplotype(f1_list, pd_df_list, list_type="phaseStc")
print()
""" Step 02: Merging the dataframe """
print("Merging all the haplotype files together")
pd_df_merged = reduce(
lambda left, right: pd.merge(
left,
right,
on=["CHROM", "POS", "REF", "all-alleles"],
how="outer",
sort=["CHROM", "POS"],
),
pd_df_list,
).fillna(".")
## ** if "concat" is required
# df_by_dask_merged = dd.concat(dask_df)
# add column with empty/default value
# - this is done to maintain required data stucture downstream
if "all-freq" not in pd_df_merged.columns:
pd_df_merged.insert(4, "all-freq", ".")
""" Step 03: Write data to file (single or multiple) """
## 03 - A: find the unique CHROM values, column names and group the df by index (CHROM)
## ** If a single file is to be written then the header should be written early.
## find all the column names in dask dataframe
dask_cols = list(pd_df_merged.columns)
# write the header for the file that is output as a single file
fileout.write("\t".join(dask_cols) + "\n")
pd_df_merged.to_csv(fileout, mode="a", sep="\t", index=False, header=False)
print(" - Worker maximum memory usage : %.2f (mb)" % (current_mem_usage()))
print("Global maximum memory usage: %.2f (mb)" % current_mem_usage())
print("elapsed time: ", time.time() - start01)
def extract_haplotype(list_names, pd_df_list, list_type):
for names in list_names:
names = names.rstrip("\n")
print("names :", names)
print()
## Reading dataframe (use dask if the file is too large)
# make sure the "CHROM" and "POS" fields are read as integer
# df_by_pd = pd.read_csv(names, sep='\t', dtype={"CHROM": object, "POS": int})
df_by_pd = pd.read_csv(names, sep="\t")
# df_by_pd['CHROM'] = df_by_pd['CHROM'].apply(lambda row: int(row))
df_by_pd["CHROM"] = df_by_pd["CHROM"].apply(pd.to_numeric, errors="coerce")
df_by_pd["POS"] = df_by_pd["POS"].apply(pd.to_numeric, errors="coerce")
for cols in list(df_by_pd.columns):
if ":" in cols:
df_by_pd[[cols]] = df_by_pd[[cols]].astype(object)
### find the sample name
column_names = list(df_by_pd.columns)
sample_name = [x for x in column_names if x.endswith(":PI")]
sample_name = [x.split(":")[0] for x in sample_name]
sample_name = ",".join(sample_name)
print("Sample name: %s " % sample_name)
print("Dropping columns and appending the pandas to a list")
if "all-freq" in df_by_pd.columns:
df_by_pd.drop(["all-freq"], axis=1, inplace=True)
if "log2odds" in df_by_pd.columns:
df_by_pd.drop(["log2odds"], axis=1, inplace=True)
if "lods" in df_by_pd.columns:
df_by_pd.drop(["lods"], axis=1, inplace=True)
""" for the haplotypes that are from "F1-Hybrids" we need to change the
format a little bit. It should have GT,former PI-PG, new PI-PG. """
if list_type == "phaseStc":
print("Using F1 haplotype")
### first find the sample name
column_names = list(df_by_pd.columns)
sample_name = [x for x in column_names if x.endswith(":PI")]
sample_name = [x.split(":")[0] for x in sample_name]
sample_name = ",".join(sample_name)
# print('Sample name: %s ' % sample_name)
""" Update the column values """
# ** Note: the column names assignment i.e "mat_hap" might change in the future.
df_by_pd[sample_name + ":PG_al"] = (
df_by_pd[pat_hap] + "|" + df_by_pd[mat_hap]
)
df_by_pd[sample_name + ":PI"] = df_by_pd.apply(
lambda row: update_cols(row, sample_name), axis=1
)
## further update the ":PG_al" values
df_by_pd[sample_name + ":PG_al"] = df_by_pd[sample_name + ":PG_al"].apply(
lambda x: "." if "N" in x else x
)
# Drop the non-required columns
df_by_pd.drop([pat_hap], axis=1, inplace=True)
df_by_pd.drop([mat_hap], axis=1, inplace=True)
## append the dataframe to a list
pd_df_list.append(df_by_pd)
# measuring memory
print(" - Worker maximum memory usage : %.2f (mb)" % (current_mem_usage()))
print()
return pd_df_list
def update_cols(df_row, sample_name):
if "N|N" in df_row[sample_name + ":PG_al"]:
return "."
else:
return df_row["CHROM"]
""" to monitor memory """
def current_mem_usage():
return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024.0
## ** deprecated - use for alpha-numeric sorting in the future.
"""function for name sorting while reading file.
- This function helps to read the file in alpha-numerical order when multiprocessing. """
numbers = re.compile(r"(\d+)")
def numericalSort(value):
parts = numbers.split(value)
parts[1::2] = map(int, parts[1::2])
return parts
if __name__ == "__main__":
main()