-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfish_circos.py
152 lines (137 loc) · 5.19 KB
/
fish_circos.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 29 16:42:26 2017
show-coords -Trlcd
sort -k1,1 -k2,2n
@author: scott
"""
import argparse
from collections import defaultdict
# from collections import OrderedDict
parser = argparse.ArgumentParser()
parser.add_argument("--infish", type=str, required=True, help="")
parser.add_argument("--outfile", type=str, required=True, help="to-from")
parser.add_argument("--infile", type=str, required=True, help="")
parser.add_argument("--region", action="store_true", help="")
parser.add_argument("--pacbio", action="store_true", help="")
parser.add_argument("--mapping", action="store_true", help="")
args = parser.parse_args()
def nonblank_lines(f):
"""
"""
for l in f:
line = l.rstrip()
if line:
yield line
def makeheader(h):
"""
"""
header = [i.lstrip('[').rstrip(']') for i in h]
return(header)
def makemap(infish, region, pacbio):
"""
"""
ddfish = defaultdict(list)
if not pacbio:
with open(infish, 'r') as fish:
for line in fish:
x = line.strip().split(",")
chrom = x[3].split(":")[0]
sector = x[3].split(":")[1].split("-")[0]
if region:
ddfish["{}_{}".format(chrom, sector)].append(x[1])
else:
ddfish[chrom].append(x[1])
else:
with open(infish, 'r') as fish:
for line in fish:
x = line.strip().split()
chrom = x[1].split("_")[0]
sector = x[1].split("_")[1]
if region:
ddfish["{}_{}".format(chrom, sector)].append(x[0])
else:
ddfish[chrom].append(x[0])
return(ddfish)
def addmaptoaln(infile, ddfish):
"""
"""
with open("{}.map".format(infile), 'w') as f:
with open(infile, 'r') as nuc:
for line in nonblank_lines(nuc):
if line.startswith('['):
f.write("{}\n".format(line))
elif line.strip().split()[0].isdigit():
x = line.strip().split()
if any([x[-1] in k for k in ddfish.values()]):
for key in ddfish.keys():
if x[-1] in ddfish[key]:
chrom = "{}_{}".format(key, x[-1])
x[-1] = chrom
f.write("{}\n".format("\t".join(x)))
break
else:
f.write("{}\n".format(line))
else:
f.write("{}\n".format(line))
return(None)
def makelinks(ddfish, outfile, infile, size=1000):
"""links are from - to, but files are named to - from
"""
qs = []
qn = []
ss = []
sn = []
with open("circos.{}.links.txt".format(outfile), 'w') as f:
with open(infile, 'r') as nuc:
for line in nonblank_lines(nuc):
if line.startswith('['):
h = line.strip().split("\t")
print(h)
header = makeheader(h)
elif line.strip().split()[0].isdigit():
x = line.strip().split()
alnlen = header.index("LEN 1")
if int(x[alnlen]) >= size:
if any([x[-1] in k for k in ddfish.values()]):
for key in ddfish.keys():
if x[-1] in ddfish[key]:
chrom = "{}_{}".format(key, x[-1])
x[-1] = chrom
break
qn.append(x[-1])
lenq = header.index("LEN Q")
qs.append(x[lenq])
lens = header.index("LEN R")
ss.append(x[lens])
sn.append(x[-2])
rstart = header.index("S1")
rend = header.index("E1")
qstart = header.index("S2")
qend = header.index("E2")
import ipdb;ipdb.set_trace()
f.write("{}\n".format(
" ".join([x[-1], x[qstart],
x[qend], x[-2],
x[rstart],
x[rend]])))
return(zip(sorted(set(sn), key=sn.index), sorted(set(ss), key=ss.index)),
zip(sorted(set(qn), key=qn.index), sorted(set(qs), key=qs.index)))
def makechrom(outfile, aln):
"""
"""
for i, j in enumerate(outfile.split("-")):
chromlist = aln[i]
with open("circos.{}.{}.karyotype.txt".format(j, outfile), 'w') as f:
for n, s in chromlist:
f.write("chr - {} {} 0 {} black\n".format(n, n, s))
if __name__ == "__main__":
fishin = args.infish
outfile = args.outfile
infile = args.infile
ddfish = makemap(fishin, args.region, args.pacbio)
addmaptoaln(infile, ddfish)
if not args.mapping:
aln = makelinks(ddfish, outfile, infile)
makechrom(outfile, aln)