Skip to content

Commit fd6ffc1

Browse files
committed
First commit of Flexutils-Scripts
0 parents  commit fd6ffc1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+7030
-0
lines changed

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Python compilation files
2+
*.pyc
3+
4+
# Ignore temporary packaging folder
5+
*.egg-info
6+
toolkit/*.egg-info
7+
8+
# IDE files
9+
.idea

CHANGES.txt

Whitespace-only changes.

LICENSE

+674
Large diffs are not rendered by default.

MANIFEST.in

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include LICENSE
2+
include requirement/*.yml
3+
include icon_square.png
4+
include scripts/*.py
5+
include socket/*.py
6+
include viewers/*.py
7+
include *.sh
8+
include *.rst
9+
include *.txt
10+
include *.py

README.rst

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=======================
2+
Flexutils Scripts
3+
=======================
4+
5+
This package provides a the environment and scripts needed by `scipion-em-flexutils <https://github.com/scipion-em/scipion-em-flexutils>`_ plugin

flexutils_scripts/__init__.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# **************************************************************************
2+
# *
3+
# * Authors: David Herreros Calero ([email protected])
4+
# *
5+
# * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6+
# *
7+
# * This program is free software; you can redistribute it and/or modify
8+
# * it under the terms of the GNU General Public License as published by
9+
# * the Free Software Foundation; either version 2 of the License, or
10+
# * (at your option) any later version.
11+
# *
12+
# * This program is distributed in the hope that it will be useful,
13+
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# * GNU General Public License for more details.
16+
# *
17+
# * You should have received a copy of the GNU General Public License
18+
# * along with this program; if not, write to the Free Software
19+
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20+
# * 02111-1307 USA
21+
# *
22+
# * All comments concerning this program package may be sent to the
23+
# * e-mail address '[email protected]'
24+
# *
25+
# **************************************************************************
26+
27+
28+
import os
29+
import sys
30+
import subprocess
31+
32+
33+
__version__ = "1.0.0"
34+
35+
36+
def getImagePath(image_name):
37+
"""Returns the absolute path to an image within the package."""
38+
module_dir = os.path.dirname(__file__) # Get the directory of this module
39+
image_path = os.path.join(module_dir, 'media', image_name)
40+
return image_path
41+
42+
def getCondaBase():
43+
try:
44+
conda_base = subprocess.check_output("conda info --base", shell=True, text=True).strip()
45+
return conda_base
46+
except subprocess.CalledProcessError as e:
47+
print(f"Error finding Conda base: {e}")
48+
return None
49+
50+
def getCondaSourceFile():
51+
conda_source_file = f"{getCondaBase()}/etc/profile.d/conda.sh"
52+
return conda_source_file
53+
54+
def getProgram(program, env_name=None):
55+
env_name = env_name if env_name is not None else f"flexutils-{__version__}"
56+
return f"source {getCondaSourceFile()} && conda activate {env_name} && python {program}"
57+
58+
def runProgram(program, args, env=None, cwd=None):
59+
command = program + " " + args
60+
subprocess.check_call(command, shell=True, stdout=sys.stdout, stderr=sys.stderr,
61+
env=env, cwd=cwd)
34.6 KB
Loading
2.23 KB
Loading

flexutils_scripts/scripts/__init__.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# **************************************************************************
2+
# *
3+
# * Authors: David Herreros Calero ([email protected])
4+
# *
5+
# * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6+
# *
7+
# * This program is free software; you can redistribute it and/or modify
8+
# * it under the terms of the GNU General Public License as published by
9+
# * the Free Software Foundation; either version 2 of the License, or
10+
# * (at your option) any later version.
11+
# *
12+
# * This program is distributed in the hope that it will be useful,
13+
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# * GNU General Public License for more details.
16+
# *
17+
# * You should have received a copy of the GNU General Public License
18+
# * along with this program; if not, write to the Free Software
19+
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20+
# * 02111-1307 USA
21+
# *
22+
# * All comments concerning this program package may be sent to the
23+
# * e-mail address '[email protected]'
24+
# *
25+
# **************************************************************************
26+
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# **************************************************************************
2+
# *
3+
# * Authors: David Herreros Calero ([email protected])
4+
# *
5+
# * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6+
# *
7+
# * This program is free software; you can redistribute it and/or modify
8+
# * it under the terms of the GNU General Public License as published by
9+
# * the Free Software Foundation; either version 2 of the License, or
10+
# * (at your option) any later version.
11+
# *
12+
# * This program is distributed in the hope that it will be useful,
13+
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# * GNU General Public License for more details.
16+
# *
17+
# * You should have received a copy of the GNU General Public License
18+
# * along with this program; if not, write to the Free Software
19+
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20+
# * 02111-1307 USA
21+
# *
22+
# * All comments concerning this program package may be sent to the
23+
# * e-mail address '[email protected]'
24+
# *
25+
# **************************************************************************
26+
27+
28+
from flexutils_scripts import utils as utl
29+
30+
31+
def alignMaps(file_input, file_target, file_output, global_search=None):
32+
# Align maps in ChimeraX using fitmap
33+
_ = utl.alignMapsChimeraX(file_input, file_target, global_search=global_search,
34+
output_map=file_output)
35+
36+
37+
if __name__ == '__main__':
38+
import argparse
39+
40+
# Input parameters
41+
parser = argparse.ArgumentParser()
42+
parser.add_argument('--i', type=str, required=True)
43+
parser.add_argument('--r', type=str, required=True)
44+
parser.add_argument('--o', type=str, required=True)
45+
parser.add_argument('--gs', type=int)
46+
47+
args = parser.parse_args()
48+
49+
if args.gs is None:
50+
gs = None
51+
else:
52+
gs = args.gs
53+
54+
# Initialize volume slicer
55+
alignMaps(args.i, args.r, args.o, gs)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# **************************************************************************
2+
# *
3+
# * Authors: David Herreros Calero ([email protected])
4+
# *
5+
# * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6+
# *
7+
# * This program is free software; you can redistribute it and/or modify
8+
# * it under the terms of the GNU General Public License as published by
9+
# * the Free Software Foundation; either version 2 of the License, or
10+
# * (at your option) any later version.
11+
# *
12+
# * This program is distributed in the hope that it will be useful,
13+
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# * GNU General Public License for more details.
16+
# *
17+
# * You should have received a copy of the GNU General Public License
18+
# * along with this program; if not, write to the Free Software
19+
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20+
# * 02111-1307 USA
21+
# *
22+
# * All comments concerning this program package may be sent to the
23+
# * e-mail address '[email protected]'
24+
# *
25+
# **************************************************************************
26+
27+
28+
import numpy as np
29+
from pathlib import Path
30+
import prody as pd
31+
from scipy.ndimage import gaussian_filter
32+
from xmipp_metadata.image_handler import ImageHandler
33+
34+
from flexutils_scripts import utils as utl
35+
36+
37+
def apply_deformation_field_zernike3d(ref_file, vol_file, z_file, out_file, boxsize, sr):
38+
ref_file = Path(ref_file)
39+
40+
# Get coords
41+
if ref_file.suffix in [".mrc", ".vol"]:
42+
mode = "volume"
43+
mask = ImageHandler(ref_file).getData()
44+
volume = ImageHandler(vol_file).getData()
45+
indices = np.asarray(np.where(mask > 0))
46+
indices = np.transpose(np.asarray([indices[2, :], indices[1, :], indices[0, :]]))
47+
coords = indices - 0.5 * boxsize
48+
groups = mask[indices[:, 2], indices[:, 1], indices[:, 0]]
49+
values = volume[indices[:, 2], indices[:, 1], indices[:, 0]]
50+
51+
if np.unique(groups).size > 1:
52+
centers = []
53+
for group in np.unique(groups):
54+
centers.append(np.mean(coords[groups == group], axis=0))
55+
centers = np.asarray(centers)
56+
else:
57+
groups, centers = None, None
58+
59+
elif ref_file.suffix in [".pdb", ".cif"]:
60+
mode = "structure"
61+
pd_struct = pd.parsePDB(str(ref_file), subset=None, compressed=False)
62+
coords = pd_struct.getCoords()
63+
centers = None
64+
groups = None
65+
66+
# Read Zernike params
67+
basis_params, A = utl.readZernikeFile(z_file)
68+
L1, L2, r = basis_params
69+
r = sr * r if mode == "structure" else r
70+
71+
# Zernike coefficients
72+
A = utl.resizeZernikeCoefficients(A.reshape(-1))
73+
A = sr * A if mode == "structure" else A
74+
75+
# Compute basis
76+
Z = utl.computeBasis(L1=int(L1), L2=int(L2), pos=coords, r=r, groups=groups, centers=centers)
77+
78+
# Compute deformation field
79+
d_f = Z @ A.T
80+
81+
# Apply deformation field
82+
if mode == "volume":
83+
indices_moved = np.round(indices + d_f).astype(int)
84+
85+
# Scatter in volume
86+
def_vol = np.zeros((boxsize, boxsize, boxsize))
87+
for idx in range(indices_moved.shape[0]):
88+
if np.all(np.abs(indices_moved[idx]) < boxsize):
89+
def_vol[indices_moved[idx, 2], indices_moved[idx, 1], indices_moved[idx, 0]] += values[idx]
90+
# np.add.at(def_vol, [indices_moved[:, 2], indices_moved[:, 1], indices_moved[:, 0]], 1)
91+
92+
# Gaussian filter map
93+
def_vol = gaussian_filter(def_vol, sigma=1.0)
94+
95+
# Save results
96+
ImageHandler().write(def_vol, filename=out_file, overwrite=True, sr=sr)
97+
98+
elif mode == "structure":
99+
# Move coords
100+
coords_moved = coords + d_f
101+
102+
# Save results
103+
pd_struct_out = pd_struct.copy()
104+
pd_struct_out.setCoords(coords_moved)
105+
pd.writePDB(out_file, pd_struct_out, hybrid36=True)
106+
107+
108+
if __name__ == '__main__':
109+
import argparse
110+
111+
# Input parameters
112+
parser = argparse.ArgumentParser()
113+
parser.add_argument('--ref_file', type=str, required=True)
114+
parser.add_argument('--vol_file', type=str, required=True)
115+
parser.add_argument('--z_file', type=str, required=True)
116+
parser.add_argument('--out_file', type=str, required=True)
117+
parser.add_argument('--boxsize', type=int, required=True)
118+
parser.add_argument('--sr', type=float, required=True)
119+
120+
args = parser.parse_args()
121+
122+
# Initialize volume slicer
123+
apply_deformation_field_zernike3d(args.ref_file, args.vol_file, args.z_file, args.out_file,
124+
args.boxsize, args.sr)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# **************************************************************************
2+
# *
3+
# * Authors: David Herreros Calero ([email protected])
4+
# *
5+
# * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6+
# *
7+
# * This program is free software; you can redistribute it and/or modify
8+
# * it under the terms of the GNU General Public License as published by
9+
# * the Free Software Foundation; either version 2 of the License, or
10+
# * (at your option) any later version.
11+
# *
12+
# * This program is distributed in the hope that it will be useful,
13+
# * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# * GNU General Public License for more details.
16+
# *
17+
# * You should have received a copy of the GNU General Public License
18+
# * along with this program; if not, write to the Free Software
19+
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20+
# * 02111-1307 USA
21+
# *
22+
# * All comments concerning this program package may be sent to the
23+
# * e-mail address '[email protected]'
24+
# *
25+
# **************************************************************************
26+
27+
28+
import numpy as np
29+
import pickle
30+
from shapely.geometry import Point
31+
32+
33+
def checkInROI(coords, polygons_file, outFile):
34+
# Define polygons based on selected borders
35+
with open(polygons_file, "rb") as f:
36+
polygons = pickle.load(f)
37+
38+
in_area_vec = []
39+
for coord in coords:
40+
in_area = 0
41+
point = Point(coord[0], coord[1])
42+
for polygon in polygons:
43+
if polygon.contains(point):
44+
in_area = 1
45+
break
46+
in_area_vec.append(in_area)
47+
48+
np.savetxt(outFile, np.asarray(in_area_vec))
49+
50+
51+
if __name__ == '__main__':
52+
import argparse
53+
54+
# Input parameters
55+
parser = argparse.ArgumentParser()
56+
parser.add_argument('--input', type=str, required=True)
57+
parser.add_argument('--polygons', type=str, required=True)
58+
parser.add_argument('--output', type=str, required=True)
59+
60+
args = parser.parse_args()
61+
62+
coords = np.loadtxt(args.input, delimiter=' ')
63+
64+
# Initialize volume slicer
65+
checkInROI(coords, args.polygons, args.output)

0 commit comments

Comments
 (0)