-
Notifications
You must be signed in to change notification settings - Fork 0
/
HRRdb_filemaker_original.py
executable file
·152 lines (83 loc) · 3.55 KB
/
HRRdb_filemaker_original.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 15 17:36:32 2022
@author: [email protected]
"""
#### Package Import
import pandas as pd
import numpy as np
import os
#### Package Import
wd = os.getcwd()
files = os.listdir(wd)
files.remove('HRRdb_filemaker.py')
filegrp=[]
it = iter(files)
for x, y in zip(it, it):
print (x, y)
file_tuple = (x,y)
filegrp.append(file_tuple)
hrr_genes = pd.read_csv("/home/bioinfo/git/HRRdb/HRR_genes.txt", sep='/t')
#hrr_df = pd.merge(df_all, hrr_genes, how="inner", left_on='Ref.Gene', right_on='HRR_Genes')
#hrr_df.pop("HRR_Genes")
#del hrr_df["HRR_Genes"] #Deleting the HRR_Genes col
for (j, k) in filegrp:
#print(j)
#print(k)
df1 = pd.read_csv(j)
#f = j+".csv"
#df1.to_csv(f, index=False)
hrr_df1 = pd.merge(df1, hrr_genes, how="inner", left_on='Ref.Gene', right_on='HRR_Genes')
#hrr_df.pop("HRR_Genes")
del hrr_df1["HRR_Genes"] #Deleting the HRR_Genes col
hrr_df1["Sample_ID"] = np.nan
hrr_df1["Sample_ID"] = hrr_df1["Sample_ID"].fillna(j)
column_to_move = hrr_df1.pop("Sample_ID") # insert column with insert(location, column_name, column_value)
hrr_df1.insert(0, "Sample_ID", column_to_move)
df2 = pd.read_csv(k)
#g = k+".csv"
#df2.to_csv(g, index=False)
hrr_df2 = pd.merge(df2, hrr_genes, how="inner", left_on='Ref.Gene', right_on='HRR_Genes')
#hrr_df.pop("HRR_Genes")
del hrr_df2["HRR_Genes"] #Deleting the HRR_Genes col
hrr_df2["Sample_ID"] = np.nan
hrr_df2["Sample_ID"] = hrr_df2["Sample_ID"].fillna(k)
column_to_move = hrr_df2.pop("Sample_ID") # insert column with insert(location, column_name, column_value)
hrr_df2.insert(0, "Sample_ID", column_to_move)
#~ Export
filename = j.replace('_output.csv', '') + "_"+ k.replace('_output.csv', '') + ".csv"
frames = [hrr_df1, hrr_df2]
result = pd.concat(frames)
#result.to_csv(filename, index=False)
#%% Module 1 Merging Files
source_files = sorted(Path('/home/max/Github/HRRdb/HRRdb_ScriptWork/').glob('*.csv'))
#%%
dataframes =[]
for group in chunker(source_files, 2):
print(group)
for file in group:
print(file)
df = pd.read_csv(file, sep=',') # additional arguments up to your needs
df['Sample_ID'] = file.name
column_to_move = df.pop('Sample_ID') # insert column with insert(location, column_name, column_value)
df.insert(0, "Sample_ID", column_to_move)
hrr_genes = pd.read_csv("/home/max/Github/HRRdb/HRR_genes.txt", sep='/t')
hrr_df = pd.merge(df, hrr_genes, how="inner", left_on='Ref.Gene', right_on='HRR_Genes')
del hrr_df["HRR_Genes"] #Deleting the HRR_Genes col
hrr_df.to_csv(nameoffile)
#[(file1, file2), (file3,file4)]
#dataframes.append(df)
#%%
df_all = pd.concat(dataframes)
column_to_move = df_all.pop('Sample_ID') # insert column with insert(location, column_name, column_value)
df_all.insert(0, "Sample_ID", column_to_move)
#~ Export
#df_all.to_csv('/home/bioinfo/Nilesh/FE_merged/merged.csv', index = False)
#%%Module 2 Filtering Files based on HRR Genes
hrr_genes = pd.read_csv("/home/max/Github/HRRdb/HRR_genes.txt", sep='/t')
hrr_df = pd.merge(df_all, hrr_genes, how="inner", left_on='Ref.Gene', right_on='HRR_Genes')
#hrr_df.pop("HRR_Genes")
del hrr_df["HRR_Genes"] #Deleting the HRR_Genes col
#~ Export
#hrr_df.to_csv('/home/max/Github/HRRdb/HRRdb_ScriptWork/df_new.csv', index = False)