-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkbase_bulk_upload.py
More file actions
171 lines (134 loc) · 6.05 KB
/
Copy pathkbase_bulk_upload.py
File metadata and controls
171 lines (134 loc) · 6.05 KB
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
#!/usr/bin/python3
# Using Workspace Metadata
# from https://kbase.github.io/kb_sdk_docs/howtos/workspace.html
#Import KBase app manager
from biokbase.narrative.jobs.appmanager import AppManager
#List all files in Staging Area
#Set up the link to the staging area helper class
from biokbase.narrative.staging.helper import Helper as StagingHelper
staging_helper = StagingHelper()
#list root directory files and all subdirectory files (default)
file_list = staging_helper.list()
print 'All files:'
print '\n'.join(file_list)
#Import FASTA File as Assembly from Staging Area
from biokbase.narrative.jobs.appmanager import AppManager
AppManager().run_app(
"kb_uploadmethods/import_fasta_as_assembly_from_staging",
{
"staging_file_subdir_path": "_Test/TARA_ANE_MAG_00002.fna",
"type": "mag",
"min_contig_length": 500,
"assembly_name": "TARA_ANE_MAG_00002.fna_assembly"
},
tag="release",
version="1.0.25",
cell_id="ff2abe14-0a9c-4d12-8f81-30379f8ec311",
run_id="3f295585-1b0d-44bc-a59e-3cad29ec88ed"
# Bulk extraction of bins based on Binned Contigs object followed by genome annotation # Janaka Edirisinghe. v.o1 September 5th, 2017
import time
import pprint
import json
import sys
from biokbase.narrative.jobs.appmanager import AppManager
version = "release"
current_ws = os.environ['KB_WORKSPACE_ID']
ws = biokbase.narrative.clients.get("workspace")
my_list = ws.list_objects({'workspaces':[current_ws],'type':'KBaseMetagenomes.BinnedContigs'})
count =0
for binnedContig in my_list:
print ("\nNow running Bin extraction on binned contig " + binnedContig[1] + "\n")
contigOb = ws.get_objects([{'ref': str (binnedContig[6]) + '/' + str (binnedContig[0]) + '/' + str(binnedContig[4])}])[0]['data']['bins']
data = contigOb
assembly_name = binnedContig[1].split('.Bins')[0]
bin_list = []
for eachbin in range(len (contigOb)):
bin_hash = {}
bin_hash['bin_id'] = [contigOb[eachbin]['bid']]
bin_hash['assembly_suffix'] = assembly_name + '_' + contigOb[eachbin]['bid'].split('.fasta')[0]
bin_list.append(bin_hash)
print ("\n\n individual bins in for binnedContig[1]")
pprint.pprint (bin_list)
job = AppManager().run_app(
"MetagenomeUtils/extract_bins_as_assemblies",
{
"binned_contig_obj_ref": str (binnedContig[6]) + '/' + str (binnedContig[0]) + '/' + str(binnedContig[4]),
"extracted_assemblies": bin_list
},
tag="release",
)
job_result = []
while True:
time.sleep(10)
try:
job_result = job.state()
if job_result['job_state'] in ['completed', 'suspend']:
break
except Error:
print "Job state command failed... try again."
if job_result['job_state'] != 'completed':
print "Bin extracting Failed : " + ",".join(job_result['status'][0:3])
else:
print "Succeeded - extract binning of :"+ binnedContig[1] +" file "+ ",".join(job_result['status'][0:3])
#pprint.pprint (job_result)
print ("\n Moving to annotation step on each binned contig\n\n")
for eachGenome in range(len (bin_list)):
print ("Now annotating " + bin_list[eachGenome]['assembly_suffix'] + "\n")
assemblyprefix = bin_list[eachGenome]['assembly_suffix'].split('_')[-1]
inputContigname = assemblyprefix + '.fasta' + bin_list[eachGenome]['assembly_suffix']
job = AppManager().run_app(
"RAST_SDK/annotate_contigset",
{
"input_contigset": inputContigname,
"scientific_name": "unknown",
"domain": "B",
"genetic_code": "11",
"output_genome": bin_list[eachGenome]['assembly_suffix'],
"call_features_rRNA_SEED": 1,
"call_features_tRNA_trnascan": 1,
"call_selenoproteins": 1,
"call_pyrrolysoproteins": 1,
"call_features_repeat_region_SEED": 1,
"call_features_insertion_sequences": 0,
"call_features_strep_suis_repeat": 1,
"call_features_strep_pneumo_repeat": 1,
"call_features_crispr": 1,
"call_features_CDS_glimmer3": 1,
"call_features_CDS_prodigal": 1,
"annotate_proteins_kmer_v2": 1,
"kmer_v1_parameters": 1,
"annotate_proteins_similarity": 1,
"resolve_overlapping_features": 1,
"find_close_neighbors": 1,
"call_features_prophage_phispy": 0
},
tag=version
)
job_result = []
while True:
time.sleep(10)
try:
job_result = job.state()
if job_result['job_state'] in ['completed', 'suspend']:
break
except Error:
print "Job state command failed... try again."
if job_result['job_state'] != 'completed':
print "Annotation Failed : " + ",".join(job_result['status'][0:3])
else:
print "\nSucceeded - Annotation of :"+ bin_list[eachGenome]['assembly_suffix'] +" file "+ ",".join(job_result['status'][0:3])
####User-defined function
# WAIT for Job to finish
import time
def wait_for_results(job,obj_name,app_name):
if job is None:
print "Failed - " + app_name + " for " + obj_name + " failed to return job handle"
else:
while job.state()['job_state'] not in ['completed', 'suspend']:
time.sleep(5)
job_result = job.state()
if job_result['job_state'] != 'completed':
print "Failed - " + app_name + " for " + obj_name + " job did not complete: " + ",".join(job_result['status'][0:3])
else:
print "Succeeded - " + app_name + " for " + obj_name + ",".join(job_result['status'][0:3])
return