-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSelectRepresentative.py
More file actions
executable file
·74 lines (64 loc) · 2.71 KB
/
Copy pathSelectRepresentative.py
File metadata and controls
executable file
·74 lines (64 loc) · 2.71 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
#!/usr/bin/env python
import argparse
import UPhO
import Consensus
import Al2Phylo
import re
from sys import argv
###Function definitions
def representative(dic, criterion):
if criterion=='longest':
long = 0
maxk = ''
for k in dic.iterkeys():
c = len([ x for x in dic[k] if x not in ['-', '?'] ])
if c > long:
long = c
maxk = k
return '>%s\n%s\n' %(maxk, dic[maxk])
elif criterion =='consensus':
r=Consensus.make_Consensus(dic, 1.0)
nid=dic.keys()[0] + " consensus"
return '>%s\n%s\n' % (nid, r)
def main(fastaFile, treeFile, criterion):
Seq=Consensus.Fasta_to_Dict(fastaFile)
T= open(treeFile, 'r')
newick_str=T.readline()
T.close()
P=UPhO.myPhylo(newick_str)
OutFile=open('%s_rep.fasta' % fastaFile.split('.')[0], 'w+')
collapsible=[]
collapsed=[]
for S in P.splits:
for isplit in S.vecs:
Otus = UPhO.spp_in_list(isplit)
if len(set(Otus)) == 1 and len(Otus) > 1: # find splits representing in-paralogs and update costs
collapsible.append(isplit)
collapsible=UPhO.LargestBox(collapsible)
# print collapsible
for i in collapsible:
staged={}
for k in i:
staged[k]=Seq[k]
# print staged
selected=representative(staged, criterion)
# print selected
OutFile.write(selected)
collapsed.extend(i)
for Rec in Seq.iterkeys():
if Rec not in collapsed:
OutFile.write('>%s\n' % Rec)
OutFile.write(Seq[Rec] + '\n')
OutFile.close()
######MAIN######
parser = argparse.ArgumentParser(description="Script for selecting a representative sequence in a FASTA file for groups of sequences of the same species forming monophyletic groups in a reference tree. Two criterions are enabled: longest and strict consensus sequences. NOTE: the script assumes leave and sequence names are identical in sequence and tree files. Sequences must be aligned for using the consensus option.")
parser.add_argument('-s', dest = 'fasta', type = str, help = 'Sequence file in FASTA format')
parser.add_argument('-t', dest = 'tree', type = str, help = 'Tree File in NEWICK format.')
parser.add_argument('-c', dest = 'criterion', type = str, default = 'longest', help= 'Criterion for selecting one representaive sequence. Options: longest (default), consensus. Note: for the consensus option, the sequence input files must be aligned')
args, unknown = parser.parse_known_args()
#print args
if __name__ == "__main__":
fastaFile=args.fasta
treeFile=args.tree
criterion=args.criterion
main(fastaFile, treeFile, criterion)