-
Notifications
You must be signed in to change notification settings - Fork 1
/
apollo_annotator_utilities.py
executable file
·268 lines (182 loc) · 7.87 KB
/
apollo_annotator_utilities.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
#!/usr/bin/env python3
name = 'apollo_annotator_utilities.py'
version = '0.3.0'
updated = '2023-06-24'
usage = f"""
NAME {name}
VERSION {version}
UPDATED {updated}
SYNOPSIS Interface between Apollo Arrow, Apollo, and the user that minimizes intermidiate steps.
REQUIRES Apollo (https://github.com/GMOD/Apollo)
python-apollo (https://github.com/galaxy-genome-annotation/python-apollo)
$APOLLO enviromental variable (/path/to/Apollo_distribution)
------------------------------------------------------------------------------------------------------------------------
Add an Organism
------------------------------------------------------------------------------------------------------------------------
COMMAND {name} --add_organism \\
-f 50507.fasta \\
-g Encephalitozoon \\
-s intestinalis \\
-i E_intestinalis_50507
-f (--fasta) Assembly fasta file
-g (--genus) Genus
-s (--species) Species
-i (--id) Organism ID
------------------------------------------------------------------------------------------------------------------------
Delete an Organism
------------------------------------------------------------------------------------------------------------------------
COMMAND {name} --delete_organism \\
-i E_intestinalis_50507
-i (--id) Organism ID
------------------------------------------------------------------------------------------------------------------------
Load Annotations
------------------------------------------------------------------------------------------------------------------------
COMMAND {name} --load_annotations \\
-i E_intestinalis_50507 \\
-a proteins.long.gff
-i (--id) Organism ID
-a (--annot) Annotation gff file
------------------------------------------------------------------------------------------------------------------------
Remove Annotations
------------------------------------------------------------------------------------------------------------------------
COMMAND {name} --remove_annotations \\
-i E_intestinalis_50507
-i (--id) Organism ID
------------------------------------------------------------------------------------------------------------------------
Add a Reference
------------------------------------------------------------------------------------------------------------------------
COMMAND {name} --add_reference \\
-r E_intestinalis_50506.blast.gff \\
-t match,match_part \\
-l E_intestinalis_50506 \\
-d -d /media/FatCat/apollo_data/E_intestinalis_50507
-r (--ref) Reference gff file
-t (--type) Reference type (CDS;match,match_part;tRNA)
-l (--label) Reference label
-c (--color) Track color
------------------------------------------------------------------------------------------------------------------------
Remove a Track
------------------------------------------------------------------------------------------------------------------------
COMMAND {name} --remove_track \\
-l E_intestinalis_50506 \\
-d /media/FatCat/apollo_data/E_intestinalis_50507
-l (--label) Track label
------------------------------------------------------------------------------------------------------------------------
Upload BAM file
------------------------------------------------------------------------------------------------------------------------
COMMAND {name} --add_bam \\
-b (--bam) BAM file
-l (--label) BAM label
-t (--type) Track type (alignment or coverage) [Default: alignment]
-n (--min_cov) Minimum coverage (Applicable if --type coverage) [Default: 0]
-x (--max_cov) Maximum coverage (Applicable if --type coverage) [Default: 50]
------------------------------------------------------------------------------------------------------------------------
"""
from sys import argv
if len(argv) < 2:
print(f"\n{usage}")
exit()
from argparse import ArgumentParser
from subprocess import run
from os import environ, makedirs
from os.path import isdir, dirname, isfile, basename
from shutil import copy
GetOptions = ArgumentParser()
group = GetOptions.add_mutually_exclusive_group(required=True)
group.add_argument("--add_organism",default=False,action='store_true')
group.add_argument("--delete_organism",default=False,action='store_true')
group.add_argument("--load_annotations",default=False,action='store_true')
group.add_argument("--remove_annotations",default=False,action='store_true')
group.add_argument("--add_reference",default=False,action='store_true')
group.add_argument("--remove_track",default=False,action='store_true')
group.add_argument("--add_bam",default=False,action='store_true')
GetOptions.add_argument("-i","--id",required=True)
args = GetOptions.parse_known_args()[0]
add_org = args.add_organism
del_org = args.delete_organism
load_annot = args.load_annotations
rem_annot = args.remove_annotations
add_ref = args.add_reference
rem_ref = args.remove_track
add_bam = args.add_bam
org_id = args.id
APOLLO = environ['APOLLO']
path = f"{APOLLO}/ORGANISMS/{org_id}"
if add_org:
GetOptions = ArgumentParser()
GetOptions.add_argument("-f","--fasta",required=True)
GetOptions.add_argument("-g","--genus",required=True)
GetOptions.add_argument("-s","--species",required=True)
args = GetOptions.parse_known_args()[0]
fasta = args.fasta
genus = args.genus
species = args.species
if not isdir(path):
makedirs(path)
run([f"{APOLLO}/web-app/jbrowse/bin/prepare-refseqs.pl","--fasta",fasta,"--out",path])
run(["arrow","organisms","add_organism","--genus",genus,"--species",species,org_id,path])
if del_org:
GetOptions = ArgumentParser()
run(["arrow","organisms","delete_features",org_id])
run(["arrow","organisms","delete_organism",org_id])
if load_annot:
GetOptions = ArgumentParser()
GetOptions.add_argument("-a","--annot",required=True)
args = GetOptions.parse_known_args()[0]
annot = args.annot
run(['arrow','annotations','load_gff3',org_id,annot])
if rem_annot:
run(["arrow","organisms","delete_features",org_id])
if add_ref:
GetOptions = ArgumentParser()
GetOptions.add_argument("-r","--ref",required=True)
GetOptions.add_argument("-t","--type",required=True)
GetOptions.add_argument("-l","--label",required=True)
GetOptions.add_argument("-c","--color",default='blue')
args = GetOptions.parse_known_args()[0]
ref = args.ref
type = args.type
label = args.label
color = args.color
run([f'{APOLLO}/web-app/jbrowse/bin/flatfile-to-json.pl','--gff',ref,'--type',type,'--trackLabel',label,'--out',path])#,'--subfeatureClasses',f"{{\"match_part\":\"{color}\"}}"])
if rem_ref:
GetOptions = ArgumentParser()
GetOptions.add_argument("-l","--label",required=True)
args = GetOptions.parse_known_args()[0]
label = args.label
run([f"{APOLLO}/web-app/jbrowse/bin/remove-track.pl",'--trackLabel',label,'--delete','--dir',path])
if add_bam:
GetOptions = ArgumentParser()
GetOptions.add_argument("-b","--bam",required=True)
GetOptions.add_argument("-l","--label",required=True)
GetOptions.add_argument("-c","--coverage",default=False,action='store_true')
args = GetOptions.parse_known_args()[0]
bam = args.bam
label = args.label
coverage = args.coverage
bam_name = basename(bam)
bam_path = dirname(bam)
if bam_path == "":
bam_path = "."
if not isfile(f"{bam_path}/{bam_name}.bai"):
print(f" [E] Could not find the index file ({bam_name}.bai) for the provided bam file ({bam})")
copy(bam,f"{path}/")
copy(f"{bam_path}/{bam_name}.bai",f"{path}/")
if coverage:
GetOptions.add_argument("-n","--minimum",type=int,default=0)
GetOptions.add_argument("-x","--maximum",type=int,default=500)
args = GetOptions.parse_known_args()[0]
minimum = args.minimum
maximum = args.maximum
run([f"{APOLLO}/web-app/jbrowse/bin/add-bam-track.pl",
"--in", f"{path}/trackList.json",
"--bam_url", f"{bam_name}",
"--label", f"{label}",
"--coverage",
"--min_score", f"{minimum}",
"--max_score", f"{maximum}"])
else:
run([f"{APOLLO}/web-app/jbrowse/bin/add-bam-track.pl",
"--in", f"{path}/trackList.json",
"--bam_url", f"{bam_name}",
"--label", f"{label}"])