-
Notifications
You must be signed in to change notification settings - Fork 4
/
relion_remove_overlapping_particles.py
executable file
·147 lines (110 loc) · 4.84 KB
/
relion_remove_overlapping_particles.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
#!/usr/bin/env python
# **************************************************************************
# *
# * Author: Juha T. Huiskonen ([email protected])
# *
# * This program is free software; you can redistribute it and/or modify
# * it under the terms of the GNU General Public License as published by
# * the Free Software Foundation; either version 2 of the License, or
# * (at your option) any later version.
# *
# * This program is distributed in the hope that it will be useful,
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# * GNU General Public License for more details.
# *
# * You should have received a copy of the GNU General Public License
# * along with this program; if not, write to the Free Software
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# * 02111-1307 USA
# *
# **************************************************************************
import os
import sys
from pyrelion import MetaData
import argparse
from collections import OrderedDict
class RemoveOverlappingParticles():
def define_parser(self):
self.parser = argparse.ArgumentParser(
description="Removes overlapping particles within a group of particles read from the input STAR file."
"One particle is left and the rest are removed.")
add = self.parser.add_argument
add('input_star', help="Input STAR filename with particles.")
add('--mindist', type=float, default=-1,
help="Minimum distance allowed between particles (pixels).")
add('--originalParticles', action='store_true',
help="Use original particle names to define particle groups (default: micrograph names).")
add('--output', help="Output STAR filename.")
def usage(self):
self.parser.print_help()
def error(self, *msgs):
self.usage()
print "Error: " + '\n'.join(msgs)
print " "
sys.exit(2)
def validate(self, args):
if len(sys.argv) == 1:
self.error("Error: No input file given.")
if not os.path.exists(args.input_star):
self.error("Error: Input file '%s' not found."
% args.input_star)
def get_micrographs(self, md):
micrographs = []
for particle in md:
micrographs.append(particle.rlnMicrographName)
return list(OrderedDict.fromkeys(micrographs))
def get_originalParticleNames(self, md):
originalParticleNames = []
for particle in md:
originalParticleNames.append(particle.rlnOriginalParticleName)
return list(OrderedDict.fromkeys(originalParticleNames))
def get_particles(self, md, micrograph):
particles = []
for particle in md:
if (particle.rlnMicrographName == micrograph) or (particle.rlnOriginalParticleName == micrograph):
particles.append(particle)
return particles
def overlaps(self, p1, p2, mindist):
''' returns True if overlap is found '''
x1 = p1.rlnCoordinateX + p1.rlnOriginX
y1 = p1.rlnCoordinateY + p1.rlnOriginY
x2 = p2.rlnCoordinateX + p2.rlnOriginX
y2 = p2.rlnCoordinateY + p2.rlnOriginY
distance_sqr = (x1 - x2) ** 2 + (y1 - y2) ** 2
mindist_sqr = mindist ** 2
return distance_sqr < mindist_sqr
def remove_overlapping_particles(self, particles, mindist):
for i, particle in enumerate(particles):
overlap_found = False
for j, particle2 in enumerate(particles[i+1:]):
if mindist > 0:
if self.overlaps(particle, particle2, mindist):
particles.remove(particle2)
return particles
def main(self):
self.define_parser()
args = self.parser.parse_args()
self.validate(args)
print "Remove overlapping particles..."
md = MetaData(args.input_star)
mdOut = MetaData()
mdOut.addLabels(md.getLabels())
new_particles = []
# get list of tags 'micrographs' but can be anything that defines
# a set of particles to compare
if args.originalParticles:
micrographs = self.get_originalParticleNames(md)
else:
micrographs = self.get_micrographs(md)
for micrograph in micrographs:
print "Processing micrograph %s..." % micrograph,
particles = self.get_particles(md, micrograph)
particles_unique = self.remove_overlapping_particles(particles, args.mindist)
new_particles.extend(particles_unique)
print "%s particle(s) kept." % len(particles_unique)
mdOut.addData(new_particles)
mdOut.write(args.output)
print "All done!"
if __name__ == "__main__":
RemoveOverlappingParticles().main()