-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenes2targets.py
75 lines (42 loc) · 1.11 KB
/
genes2targets.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
#!/usr/bin/env python3
from Bio import SeqIO
import sys
import os
import re
import glob
import concurrent.futures
#r10.11
digits = re.compile(r'(\d+)')
def tokenize(filename):
return tuple(int(token) if match else token
for token, match in
((fragment, digits.search(fragment))
for fragment in digits.split(filename)))
def maketargets(i):
print(i)
f=open(i,'r')
gene=i.split("/")[-1].split(".")[0]
c2=0
for x in SeqIO.parse(f,'fasta'):
species="_".join(str(p) for p in x.id.split("_")[:-1])
species=species.replace(".","")
name=f"{species}-{gene}"
if name in names: #rename if duplicated
c2=c2+1
print(name,"duplicated, renaming",f"{species}_{c2}-{gene}")
name=f"{species}_{c2}-{gene}"
names.append(name)
else:
names.append(name)
c2=0
g.write(f">{name}\n{str(x.seq)}\n")
#global
filelist=glob.glob(sys.argv[1])
filelist.sort(key=tokenize)
outfile=sys.argv[2]
g=open(outfile,'w')
names=[]
print("files to process:",filelist)
if __name__ == '__main__':
for i in filelist:
maketargets(i)