-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprotein_fasta_download_test.py
319 lines (283 loc) · 10.7 KB
/
protein_fasta_download_test.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# -*- coding: utf-8 -*-
import os
import time
import unittest
from configparser import ConfigParser
from KBaseSynCom.KBaseSynComImpl import KBaseSynCom
from KBaseSynCom.KBaseSynComServer import MethodContext
from KBaseSynCom.authclient import KBaseAuth as _KBaseAuth
from installed_clients.WorkspaceClient import Workspace
from installed_clients.DataFileUtilClient import DataFileUtil
import pandas as pd
from collections import Counter
class KBaseSynComTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
token = os.environ.get('KB_AUTH_TOKEN', None)
config_file = os.environ.get('KB_DEPLOYMENT_CONFIG', None)
cls.cfg = {}
config = ConfigParser()
config.read(config_file)
for nameval in config.items('KBaseSynCom'):
cls.cfg[nameval[0]] = nameval[1]
# Getting username from Auth profile for token
authServiceUrl = cls.cfg['auth-service-url']
auth_client = _KBaseAuth(authServiceUrl)
user_id = auth_client.get_user(token)
# WARNING: don't call any logging methods on the context object,
# it'll result in a NoneType error
cls.ctx = MethodContext(None)
cls.ctx.update({'token': token,
'user_id': user_id,
'provenance': [
{'service': 'KBaseSynCom',
'method': 'please_never_use_it_in_production',
'method_params': []
}],
'authenticated': 1})
cls.wsURL = cls.cfg['workspace-url']
cls.wsClient = Workspace(cls.wsURL)
cls.serviceImpl = KBaseSynCom(cls.cfg)
cls.scratch = cls.cfg['scratch']
cls.callback_url = os.environ['SDK_CALLBACK_URL']
suffix = int(time.time() * 1000)
cls.wsName = "test_ContigFilter_" + str(suffix)
ret = cls.wsClient.create_workspace({'workspace': cls.wsName}) # noqa
@classmethod
def tearDownClass(cls):
if hasattr(cls, 'wsName'):
cls.wsClient.delete_workspace({'workspace': cls.wsName})
print('Test workspace was deleted')
@classmethod
def get_protein_handle_from_metagenome(self, ama_ref):
protein_handle = self.wsClient.get_object_subset([{
'included': ['/protein_handle_ref'],
'ref': ama_ref
}])[0]['data']['protein_handle_ref']
return (protein_handle)
@classmethod
def features_from_genome(self, genome_ref):
genome_features = self.wsClient.get_object_subset([{
'included': ['/features'],
'ref': genome_ref
}])[0]['data']['features']
return (genome_features)
@classmethod
def splitSequence(self, seq):
colsz = 50
start = 0
lenseq = len(seq)
line = ""
while True:
end = start + colsz
if end > lenseq:
end = lenseq
# print seq[start:end]
line += seq[start:end] + "\n"
start += colsz
if start > lenseq:
# False
break
return line
@classmethod
def create_Fasta_from_features(self, pyStr):
myFeat = pyStr
line = ""
for feat in myFeat:
if 'function' not in feat:
feat['function'] = 'unknown'
if 'type' in feat and feat['type'] not in ['CDS', 'gene']:
continue
if ('protein_translation' in feat):
line += ">" + feat['id'] + " " + feat['function']
line += " (len=" + str(feat['protein_translation_length']) + ")" + "\n"
# print line
line += self.splitSequence(feat['protein_translation']) + "\n"
return line
# NOTE: According to Python unittest naming rules test method names should start from 'test'. # noqa
@unittest.skip('x')
def test_your_method(self):
# Prepare test objects in workspace if needed using
# self.getWsClient().save_objects({'workspace': self.getWsName(),
# 'objects': []})
#
# Run your method by
# ret = self.getImpl().your_method(self.getContext(), parameters...)
#
# Check returned data with
# self.assertEqual(ret[...], ...) or other unittest methods
# ret = self.serviceImpl.run_KBaseSynCom(self.ctx, {'workspace_name': self.wsName,
# 'parameter_1': 'Hello World!'})
ama_ref = "63670/4/1"
protein_handle = self.get_protein_handle_from_metagenome(ama_ref)
print (protein_handle)
protein_file = "/kb/module/work/tmp/test.fa.gz"
dfu = DataFileUtil(os.environ['SDK_CALLBACK_URL'], token=self.ctx['token'])
shock_to_file = dfu.shock_to_file({'handle_id': protein_handle, 'file_path': protein_file})
print (shock_to_file)
#self.assertTrue(os.path.exists(protein_file))
@unittest.skip('x')
def test_download_genome_protein(self):
# Prepare test objects in workspace if needed using
# self.getWsClient().save_objects({'workspace': self.getWsName(),
# 'objects': []})
#
# Run your method by
# ret = self.getImpl().your_method(self.getContext(), parameters...)
#
# Check returned data with
# self.assertEqual(ret[...], ...) or other unittest methods
# ret = self.serviceImpl.run_KBaseSynCom(self.ctx, {'workspace_name': self.wsName,
# 'parameter_1': 'Hello World!'})
genome_ref = "63151/519/2"
genome_features = self.features_from_genome(genome_ref)
fasta_data = self.create_Fasta_from_features(genome_features)
protein_file = "/kb/module/work/genome_protein.fa"
with open(protein_file, "w") as file1:
file1.write(fasta_data)
@unittest.skip('x')
def test_download_genome_protein(self):
# Prepare test objects in workspace if needed using
# self.getWsClient().save_objects({'workspace': self.getWsName(),
# 'objects': []})
#
# Run your method by
# ret = self.getImpl().your_method(self.getContext(), parameters...)
#
# Check returned data with
# self.assertEqual(ret[...], ...) or other unittest methods
# ret = self.serviceImpl.run_KBaseSynCom(self.ctx, {'workspace_name': self.wsName,
# 'parameter_1': 'Hello World!'})
merged_pfam_file = "/kb/module/work/merged_pfam_genomes.txt"
domain_annotation_list = ['63727/187/1', '63727/185/1', '63727/183/1']
all_pfam_counters = dict()
for obj in domain_annotation_list:
all_pfam_counters[obj] = self.get_pfam_counts(obj)
merged_all_obj_count = self.merge_counters(all_pfam_counters)
df=pd.DataFrame(merged_all_obj_count)
df[df > 0] = 1
df.to_csv(merged_pfam_file, sep="\t", index_label="PfamID")
def test_runSyncom(self):
# Prepare test objects in workspace if needed using
# self.getWsClient().save_objects({'workspace': self.getWsName(),
# 'objects': []})
#
# Run your method by
# ret = self.getImpl().your_method(self.getContext(), parameters...)
#
# Check returned data with
# self.assertEqual(ret[...], ...) or other unittest methodsa
metagenome_pfam_output_files = ["/kb/module/test/metagenome_pfams/metagenome1.faa.pfam",
"/kb/module/test/metagenome_pfams/metagenome1_copy.faa.pfam"]
#domain_annotation_list = ['63727/187/1', '63727/185/1', '63727/183/1']
domain_annotation_list = ['63727/3/1',
'63727/5/1',
'63727/7/1',
'63727/9/1',
'63727/11/1',
'63727/13/1',
'63727/15/1',
'63727/17/1',
'63727/19/1',
'63727/21/1',
'63727/23/1',
'63727/25/1',
'63727/27/1',
'63727/29/1',
'63727/31/1',
'63727/33/1',
'63727/35/1',
'63727/37/1',
'63727/39/1',
'63727/41/1',
'63727/43/1',
'63727/45/1',
'63727/47/1',
'63727/49/1',
'63727/51/1',
'63727/53/1',
'63727/55/1',
'63727/57/1',
'63727/59/1',
'63727/61/1',
'63727/63/1',
'63727/65/1',
'63727/67/1',
'63727/69/1',
'63727/71/1',
'63727/73/1',
'63727/75/1',
'63727/77/1',
'63727/86/1',
'63727/88/1',
'63727/90/1',
'63727/92/1',
'63727/94/1',
'63727/96/1',
'63727/98/1',
'63727/100/1',
'63727/102/1',
'63727/104/1',
'63727/106/1',
'63727/108/1',
'63727/110/1',
'63727/112/1',
'63727/114/1',
'63727/116/1',
'63727/118/1',
'63727/120/1',
'63727/122/1',
'63727/124/1',
'63727/126/1',
'63727/128/1',
'63727/130/1',
'63727/132/1',
'63727/134/1',
'63727/136/1',
'63727/138/1',
'63727/140/1',
'63727/142/1',
'63727/144/1',
'63727/146/1',
'63727/148/1',
'63727/150/1',
'63727/152/1',
'63727/154/1',
'63727/156/1',
'63727/158/1',
'63727/160/1',
'63727/162/1',
'63727/164/1',
'63727/166/1',
'63727/168/1',
'63727/170/1',
'63727/172/1',
'63727/175/1',
'63727/177/1',
'63727/179/1',
'63727/181/1',
'63727/183/1',
'63727/185/1',
'63727/187/1',
'63727/189/1',
'63727/191/1',
'63727/193/1',
'63727/195/1',
'63727/198/1',
'63727/200/1',
'63727/202/1',
'63727/204/1',
'63727/206/1',
'63727/208/1',
'63727/210/1',
'63727/212/1',
'63727/214/1',
'63727/217/1',
'63727/219/1',
'63727/221/1',
'63727/223/1']
ret = self.serviceImpl.run_KBaseSynCom(self.ctx, {'workspace_name': self.wsName,
'metagenome_pfam_annotation_files': metagenome_pfam_output_files,
'genome_domain_annotation_objects': domain_annotation_list,
'iteration':10})
print (ret)