-
Notifications
You must be signed in to change notification settings - Fork 1
/
FileProcessing.py
265 lines (202 loc) · 10.8 KB
/
FileProcessing.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
import pandas as pd
from Bio import SeqIO
class ReadFiles(object):
"""docstring for ReadFiles"""
def __init__(self, param):
super(ReadFiles, self).__init__()
self.dataset_file = param.getDatasetFilePath()
self.essential_gene_seq_file = param.getEssentialGeneSeqFilePath()
self.essential_gene_anno_file = param.getEssentialGeneAnnotFilePath()
self.essential_aa_file = param.getEssentialAAFilePath()
self.essential_hit_file = param.getEsentialHitFilePath()
self.non_essential_gene_seq_file = param.getNonEssentialGeneSeqFilePath()
self.non_essential_gene_anno_file = param.getNonEssentialGeneAnnotFilePath()
self.non_essential_aa_file = param.getNonEssentialAAFilePath()
self.non_essential_hit_file = param.getNonEssentialHitFilePath()
self.combined_mcl_file = param.getCombinedMCLFilePath()
# dataset related attributes
self.datasetInfoDict = dict()
self.completeOrganismNameList = list()
self.completeOrganismAccessionList = list()
self.gramPositiveOrganismNameList = list()
self.gramPositiveOrganismAccessionList = list()
self.gramNegativeOrganismNameList = list()
self.gramNegativeOrganismAccessionList = list()
# gene annotation related attributes
self.essentialAnnotationInfoDict = dict()
self.nonEssentialAnnotationInfoDict = dict()
self.accessionWiseEssentialGeneCount = dict()
self.accessionWiseNonEssentialGeneCount = dict()
# gene seq related attributes
self.essentialGeneSeqDict = dict()
self.nonEssentialGeneSeqDict = dict()
# protein seq related attributes
self.essentialProteinSeqDict = dict()
self.nonEssentialProteinSeqDict = dict()
# mcl related attributes
self.essentialMCLDict = dict()
self.nonEssentialMCLDict = dict()
# combined MCL data info
self.combinedMCLDict = processCombinedMCLInfo(self.combined_mcl_file)
# hit related attributes
self.essentialHitDict = dict()
self.nonEssentialHitDict = dict()
# combine essential non essential cluster info
self.clusterStatInfoDict = getClusterStatistics(self.combinedMCLDict)
# returns the complete dataset information in a dictionary
def getDatasetFileInfo(self):
DataInfo = pd.read_table(self.dataset_file, sep='\t', header='infer')
for index, row in DataInfo.iterrows():
self.datasetInfoDict[row['NCBI_Accession_ID']] = [row['Organism'], row['Abbr.'],
row['GP/GN']] # need to change the dataset file
return self.datasetInfoDict
# returns the complete list of names of the organisms
def getCompleteListOrganismsName(self):
for acc_id in self.datasetInfoDict:
self.completeOrganismNameList.append(self.datasetInfoDict[acc_id][0])
return self.completeOrganismNameList
# returns the complete list of accession IDs of the organisms
def getCompleteListOrganismAccession(self):
for acc_id in self.datasetInfoDict:
self.completeOrganismAccessionList.append(acc_id)
return self.completeOrganismAccessionList
# returns the list of gram positive organism name
def getGramPositiveOrganismName(self):
for acc_id in self.datasetInfoDict:
if self.datasetInfoDict[acc_id][2] == '+':
self.gramPositiveOrganismNameList.append(self.datasetInfoDict[acc_id][0])
return self.gramPositiveOrganismNameList
# returns the list of gram positive organism accession IDs
def getGramPositiveOrganismAccession(self):
for acc_id in self.datasetInfoDict:
if self.datasetInfoDict[acc_id][2] == '+':
self.gramPositiveOrganismAccessionList.append(acc_id)
return self.gramPositiveOrganismAccessionList
# returns the list of gram negative organism names
def getGramNegativeOrganismName(self):
for acc_id in self.datasetInfoDict:
if self.datasetInfoDict[acc_id][2] == '-':
self.gramNegativeOrganismNameList.append(self.datasetInfoDict[acc_id][0])
return self.gramNegativeOrganismNameList
# returns the list of gram negative organisms accession IDs
def getGramNegativeOrganismAccession(self):
for acc_id in self.datasetInfoDict:
if self.datasetInfoDict[acc_id][2] == '-':
self.gramNegativeOrganismAccessionList.append(acc_id)
return self.gramNegativeOrganismAccessionList
# returns the complete information of essential gene name and the gene sequence
def getEssentialGeneSeqInfo(self):
fastaSequences = SeqIO.parse(open(self.essential_gene_seq_file), 'fasta')
for fasta in fastaSequences:
name, sequence = fasta.id, str(fasta.seq)
if "Not" in sequence:
continue
self.essentialGeneSeqDict[name] = sequence
return self.essentialGeneSeqDict
# returns the essential gene annotation information
# need to rewrite the annotation file, change header
def getEssentialGeneAnnoInfo(self):
AnnotationInfo = pd.read_table(self.essential_gene_anno_file, sep='\t', header='infer')
for index, row in AnnotationInfo.iterrows():
self.essentialAnnotationInfoDict[row['#Gene_Name']] = [row['#COG'], row['#Organism'], row['#Conditions']]
if row['#Conditions'] not in self.accessionWiseEssentialGeneCount:
self.accessionWiseEssentialGeneCount[row['#Conditions']] = 1
else:
self.accessionWiseEssentialGeneCount[row['#Conditions']] += 1
# for plot
fopen_ess_gene_count = open('Accession_wise_ess_gene_count.txt', 'w')
for accession in self.accessionWiseEssentialGeneCount:
fopen_ess_gene_count.write(str(accession) + '\t' + str(self.accessionWiseEssentialGeneCount[accession]) + '\n')
fopen_ess_gene_count.close()
return self.essentialAnnotationInfoDict
# returns the essential gene name and protein sequence
def getEssentialProteinInfo(self):
fastaSequences = SeqIO.parse(open(self.essential_aa_file), 'fasta')
for fasta in fastaSequences:
name, sequence = fasta.id, str(fasta.seq)
self.essentialProteinSeqDict[name] = sequence
return self.essentialProteinSeqDict
# returns the MCL information of the essential genes
def getEssentialHitInfo(self):
HitInfo = pd.read_table(self.essential_hit_file, sep='\t', header='infer')
for index, row in HitInfo.iterrows():
gene = row['Query'].split('>')[1]
self.essentialHitDict[gene] = [row['E-Value'], row['Bitscore'], row['Short_name']]
return self.essentialHitDict
# returns the complete information of non essential gene name and the gene sequence
def getNonEssentialGeneSeqInfo(self):
fastaSequences = SeqIO.parse(open(self.non_essential_gene_seq_file), 'fasta')
for fasta in fastaSequences:
name, sequence = fasta.id, str(fasta.seq)
if "Not" in sequence:
continue
self.nonEssentialGeneSeqDict[name] = sequence
return self.nonEssentialGeneSeqDict
# returns the non essential gene annotation information
# need to rewrite the annotation file, change header
def getNonEssentialGeneAnnoInfo(self):
AnnotationInfo = pd.read_table(self.non_essential_gene_anno_file, sep='\t', header='infer')
for index, row in AnnotationInfo.iterrows():
self.nonEssentialAnnotationInfoDict[row['#Gene_Name']] = [row['#COG'], row['#Organism'], row['#Conditions']]
if row['#Conditions'] not in self.accessionWiseNonEssentialGeneCount:
self.accessionWiseNonEssentialGeneCount[row['#Conditions']] = 1
else:
self.accessionWiseNonEssentialGeneCount[row['#Conditions']] += 1
# for plot
# print len(self.accessionWiseNonEssentialGeneCount)
fopen_ness_gene_count = open('Accession_wise_ness_gene_count.txt', 'w')
for accession in self.accessionWiseNonEssentialGeneCount:
fopen_ness_gene_count.write(str(accession) + '\t' + str(self.accessionWiseNonEssentialGeneCount[accession]) + '\n')
fopen_ness_gene_count.close()
return self.nonEssentialAnnotationInfoDict
# returns the essential gene name and protein sequence
def getNonEssentialProteinInfo(self):
fastaSequences = SeqIO.parse(open(self.non_essential_aa_file), 'fasta')
for fasta in fastaSequences:
name, sequence = fasta.id, str(fasta.seq)
self.nonEssentialProteinSeqDict[name] = sequence
return self.nonEssentialProteinSeqDict
# returns the MCL information of the non essential genes
def getNonEssentialHitInfo(self):
HitInfo = pd.read_table(self.non_essential_mcl_file, sep='\t', header='infer')
for index, row in HitInfo.iterrows():
gene = row['Query'].split('>')[1]
self.nonEssentialHitDict[gene] = [row['E-Value'], row['Bitscore'], row['Short_name']]
return self.nonEssentialHitDict
# return combined MCL dict
def getCombinedMCLInfo(self):
return self.combinedMCLDict
# returns cluster (MCL) statistics
def getMCLStatistics(self):
return self.clusterStatInfoDict
# return non_essential gene MCL information
def processCombinedMCLInfo(mcl_file):
combinedMCLDict = dict()
clusterID = 1
with open(mcl_file) as f:
for line in f:
line = line.strip()
cols = line.split()
combinedMCLDict[clusterID] = cols[1:]
clusterID += 1
return combinedMCLDict
# returns essential non essential MCL combined statistics
def getClusterStatistics(combinedMCLDict):
clusterStatInfoDict = dict()
for cluster in combinedMCLDict:
essentialGeneCount, nonEssentialGeneCount = 0, 0
geneList = combinedMCLDict[cluster]
for i in range(0, len(geneList)):
if 'DEG' in geneList[i]:
essentialGeneCount += 1
elif 'DNEG' in geneList[i]:
nonEssentialGeneCount += 1
essentialGenePercentage = float(essentialGeneCount) / (essentialGeneCount + nonEssentialGeneCount)
nonEssentialGenePercentage = float(nonEssentialGeneCount) / (essentialGeneCount + nonEssentialGeneCount)
clusterStatInfoDict[cluster] = [essentialGeneCount,
nonEssentialGeneCount,
essentialGenePercentage,
nonEssentialGenePercentage,
abs(essentialGenePercentage - nonEssentialGenePercentage)
]
return clusterStatInfoDict