-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrappers.py
78 lines (72 loc) · 2.71 KB
/
wrappers.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
import dna
import os
import subprocess
def run_blast(sequences, db, path='', blast_type='blastn', num_hits=1, evalue=10):
command = [path + blast_type,
'-query', 'tmp.fasta',
'-db', db,
'-culling_limit', str(num_hits),
#'-best_hit_overhang', '0.1',
#'-best_hit_score_edge', '0.1',
# fix this later to work with more than just best hit
#'-max_target_seqs', str(num_hits),
'-evalue', str(evalue),
'-outfmt', '6 qseqid qlen qstart qend sseqid length sstart send evalue nident']
dna.write_fasta(sequences, 'tmp.fasta')
blast = subprocess.Popen(command, stdout=subprocess.PIPE)
blast_out, blast_err = blast.communicate()
blast_results = [hit.split("\t") for hit in blast_out.split("\n")]
os.remove('tmp.fasta')
return blast_results
def run_prank(sequences, path='', tree='', codon=False, translate=False):
command = [path + 'prank',
'-d=tmp.fasta',
'-o=tmp',
'-F']
if tree != '':
command += ['t=' + tree]
if codon:
command += ['-codon']
if translate:
command += ['-translate']
dna.write_fasta(sequences, 'tmp.fasta')
prank = subprocess.Popen(command, stdout=subprocess.PIPE)
prank_out, prank_err = prank.communicate()
prank_results = dna.read_fasta('tmp.best.fas')
os.remove('tmp.fasta')
os.remove('tmp.best.fas')
return prank_results
def convert_prank(sequences, path='', dna_input='', input_format=''):
command = [path + 'prank',
'-convert',
'-d=tmp.fasta',
'-o=tmp',
'-keep']
formats = ['fasta','phylipi','phylips','paml','nexus','raxml']
if input_format != '':
if input_format in format:
command += ['-f=' + input_format]
else:
sys.exit('Specify one of the following: %s' % (', '.join(formats)))
if dna_input != '':
command += ['dna=' + dna_input]
else:
command += ['-translate']
dna.write_fasta(sequences, 'tmp.fasta')
prank = subprocess.Popen(command, stdout=subprocess.PIPE)
prank_out, prank_err = prank.communicate()
prank_results = dna.read_fasta('tmp.fas')
os.remove('tmp.fasta')
os.remove('tmp.fas')
return prank_results
def run_fsa(sequences, path=''):
command = ['fsa', 'tmp.fasta']
dna.write_fasta(sequences, 'tmp.fasta')
fsa = subprocess.Popen(command, stdout=subprocess.PIPE)
fsa_out, fsa_err = fsa.communicate()
fsa_file = open('tmp.aln', 'w')
fsa_file.write(fsa_out)
fsa_results = dna.read_fasta('tmp.aln')
os.remove('tmp.fasta')
os.remove('tmp.aln')
return fsa_results