annotate python/cluster.py @ 4:56508a6033a9

minutor chunk hacking
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Tue, 25 May 2021 14:01:26 -0400
parents
children 672621ab4db4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
1 #!/usr/bin/python3
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 '''Read a minutor block lockation tsv and resort it to show clusters'''
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3 import sys
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 from util import *
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
7 if len(sys.argv)==1 or sys.argv[1]=='-h':
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
8 print("""Usage: cluster.py [-h] [-c n] infile.tsv [outfile.tsv]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
9 n is cluster diameter, default is 5
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
10 default outfile is [infile]_c[n].tsv""")
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 exit(1)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12 if sys.argv[1]=='-c':
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13 sys.argv.pop(1)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 n=float(sys.argv.pop(1))
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 else:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16 n=5.0
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18 infile_name=sys.argv.pop(1)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19 if len(sys.argv)>1:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
20 outfile_name=sys.argv.pop(1)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
21 else:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
22 outfile_name="%s_c%s.tsv"%(infile_name.split('.')[0],n)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
23
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
24 cc=[]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
25
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
26 with open(infile_name,'r') as infile:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
27 with open(outfile_name,'w') as outfile:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
28 l=infile.readline().rstrip()
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
29 print(l,file=outfile)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
30 ff=PPAT.split(l)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
31 (nr,ox,oy,oz)=intsMaybe(ff)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
32 et=ff[9]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
33 l=infile.readline().rstrip()
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
34 print(l,file=outfile)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
35 (orad,ymin,ymax)=intsMaybe(PPAT.split(l))
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
36 print(nr,ox,oy,oz,et,orad,ymin,ymax)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
37 _=infile.readline()
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
38 for l in infile:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
39 found=False
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
40 q=[float(i) for i in l.split('\t')[2].split(',')]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
41 for c in cc:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
42 for p in c:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
43 if d(p,q)<=n:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
44 c.append(q)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
45 found=True
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
46 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
47 if found:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
48 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
49 if not found:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
50 cc.append([q])
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
51 oc=cc
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
52 cc=[] # lose
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
53 w=0
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
54 ow=-1
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
55 nc=[] # win
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
56 while True:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
57 for i,c in enumerate(oc):
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
58 win=False
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
59 for p in c:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
60 for g in oc[i+1:]:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
61 for q in g:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
62 if d(p,q)<=n:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
63 win=True
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
64 w+=1
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
65 nc.append(c+g)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
66 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
67 if win:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
68 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
69 if win:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
70 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
71 if not win:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
72 cc.append(c)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
73 print(len(cc),len(nc),ow,w,file=sys.stderr)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
74 if ow==w:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
75 break
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
76 ow=w
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
77 oc=nc
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
78 nc=[]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
79 for c in sorted(cc,reverse=True,key=lambda x:len(x)):
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
80 print(c,file=outfile)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
81
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
82
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
83
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
84