-
Notifications
You must be signed in to change notification settings - Fork 8
/
phanotate.py
executable file
·81 lines (62 loc) · 3.28 KB
/
phanotate.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
#!/usr/bin/env python3
import os
import sys
import getopt
#from subprocess import Popen, PIPE, STDOUT
sys.path.pop(0)
from phanotate_modules.file import File
import fastpathz as fz
from phanotate_modules import file_handling
from phanotate_modules import functions
from phanotate_modules.nodes import Node
from phanotate_modules.edges import Edge
from phanotate_modules.file_handling import pairwise
sign = lambda x: (1, -1)[x<0]
#--------------------------------------------------------------------------------------------------#
# ARGUMENTS #
#--------------------------------------------------------------------------------------------------#
args = file_handling.get_args(File)
if args.format == 'fasta':
args.format = 'fna'
#--------------------------------------------------------------------------------------------------#
# FILE INPUT #
#--------------------------------------------------------------------------------------------------#
base_trans = str.maketrans('sbvdefhijklmnopqruwxyz','gggaaaaaaaaaaaaaaaaaaa')
genbank = File(args.infile);
if not genbank.seq():
sys.stdout.write("Error: no sequences found in infile\n")
sys.exit()
#--------------------------------------------------------------------------------------------------#
# MAIN ROUTINE #
#--------------------------------------------------------------------------------------------------#
for locus in genbank:
#-------------------------------Find the ORFs----------------------------------------------#
locus.start_codons = args.start_codons
locus.stop_codons = args.stop_codons
locus.min_orf_len = args.min_orf_len
orfs = functions.get_orfs(locus)
#-------------------------------Create the Graph-------------------------------------------#
graph = functions.get_graph(orfs)
#-------------------------------Run Bellman-Ford-------------------------------------------#
source = "Node('source','source',0,0)"
target = "Node('target','target',0," + str(locus.length()+1) + ")"
# Write edges to the fastpath program, and multiply the weight to not lose decimal places
fz.empty_graph()
for e in graph.iteredges():
if args.dump: print(e)
ret = fz.add_edge(str(e))
if args.dump: sys.exit()
shortest_path = fz.get_path(source=source, target=target)
#-------------------------------Write Output ----------------------------------------------#
#file_handling.write_output(locus.name(), args, shortest_path, graph, orfs)
shortest_path = shortest_path[1:]
for source, target in pairwise(shortest_path):
left,right = eval(source) , eval(target)
weight = graph.weight(Edge(left,right,0))
pairs = [[left.position , right.position]]
feature = locus.add_feature(left.gene, sign(left.frame), pairs, {'note':['score:%E' % weight]})
feature.weight = '%E' % weight
locus.write(args)
#--------------------------------------------------------------------------------------------------#
# END #
#--------------------------------------------------------------------------------------------------#