-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathway_preprocessing.py
109 lines (72 loc) · 2.13 KB
/
Pathway_preprocessing.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
#!/usr/bin/env python
# coding: utf-8
# Step 1: Importing necessary libraries:
# In[ ]:
import os
import pandas as pd
import re
import numpy as np
# Step 2: Loading all files from the Group:
# In[ ]:
#Use folder where the files to be processed are placed
folder = r'C:/Users/Harini01.R/Desktop/Git/Microbiome-project/import_data/nonIBD'
files = os.listdir(folder)
pwyfiles = []
ecsfiles = []
genfiles = []
taxfiles = []
for file in files:
if re.search(r"path", file):
pwyfiles.append(f"{folder}/{file}")
elif re.search(r"ecs", file):
ecsfiles.append(f"{folder}/{file}")
elif re.search(r"gene", file):
genfiles.append(f"{folder}/{file}")
elif re.search(r"taxon", file):
taxfiles.append(f"{folder}/{file}")
# Step 3: Pre-processing the individual files:
# In[ ]:
visitname = []
for file in pwyfiles:
visitname.append(file[70:78])
# In[ ]:
def pwypreprocess_1(df):
otu = df.OTU_ID
otup = otu.str.split(':')
df["PWY"] = ""
for i in range (0,len(otup)):
df.PWY[i] = otup[i][0]
otug = otu.str.split('|')
df["Genus"] = ""
for j in range(0,len(otug)):
if(len(otug[j])<=1):
df.Genus[j] = 0
else:
df.Genus[j] = otug[j][1]
return df
# In[ ]:
def pwypreprocess_2(df):
df = df.drop(df[df['Genus'] == 0].index)
df = df.drop(df[df['Genus'] == 'unclassified'].index)
df = df.drop(df[df['Abundance'] == 0].index)
df = df.drop('OTU_ID', axis = 1)
if 'taxonomy' in df.columns:
df = df.drop('taxonomy', axis = 1)
return df
# In[ ]:
for i in range(0, len(pwyfiles)):
df = pd.read_table(pwyfiles[i])
df_1 = pwypreprocess_1(df)
df_2 = pwypreprocess_2(df_1)
df_2["Visit_name"] = visitname[i]
pwyfiles[i] = df_2
print("Completed", i+1, "/", len(pwyfiles), "files.")
# Step 4: Merging the files & downloading the merged file(s):
# In[ ]:
for i in range(0, len(pwyfiles)):
Grouppwyfiles = pd.concat(pwyfiles)
# In[ ]:
helperdf = pd.read_excel('helper.xlsx')
Grouppwyfiles = Grouppwyfiles.merge(helperdf)
#Name file as per folder
Grouppwyfiles.to_csv('nonIBD_processed.csv')