annotate python/util.py @ 11:e411408d64ec

from python repo
author Henry S. Thompson <ht@inf.ed.ac.uk>
date Sat, 11 Dec 2021 16:13:04 -0500
parents 56508a6033a9
children
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 '''Utility fns for dealing with block .tsv files'''
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
2 import math,re
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
3
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
4 IPAT=re.compile('-?[0-9][0-9]*$')
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
5 PPAT=re.compile('[ ,;\[\]]')
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
6
11
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
7 Red=''
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
8 eRed=''
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
9 RedFmt=Red+'%s'+eRed
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
10
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
11 def intsMaybe(ii):
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
12 return (int(i) for i in ii if IPAT.match(i))
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
13
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
14 def readHeaders(host,infile,skipColHdrs=True):
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
15 l=infile.readline().rstrip()
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
16 ff=PPAT.split(l)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
17 (host.nr,host.ox,host.oy,host.oz)=intsMaybe(ff)
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
18 host.et=ff[9]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
19 l=infile.readline().rstrip()
11
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
20 l2=PPAT.split(l)
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
21 if l2[-1]=='bounded':
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
22 host.orad=int(l2[1])
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
23 host.ymin=host.ymax=-1
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
24 else:
e411408d64ec from python repo
Henry S. Thompson <ht@inf.ed.ac.uk>
parents: 4
diff changeset
25 (host.orad,host.ymin,host.ymax)=intsMaybe(PPAT.split(l))
4
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
26 if skipColHdrs:
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
27 _=infile.readline()
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
28
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
29 def d(p1,p2):
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
30 dx=p1[0]-p2[0]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
31 dz=p1[1]-p2[1]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
32 dy=p1[2]-p2[2]
56508a6033a9 minutor chunk hacking
Henry S. Thompson <ht@inf.ed.ac.uk>
parents:
diff changeset
33 return math.sqrt((dx*dx)+(dy*dy)+(dz*dz))