forked from cryoEM-CNIO/CNIO_Relion_Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ice.py
executable file
·91 lines (70 loc) · 2.45 KB
/
ice.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
#!/usr/bin/env python3
"""
Estimate Ice Thickness from CtfFind4
Rafael Fernandez-Leiro & Nayim Gonzalez-Rodriguez 2022
"""
"""
Activate conda environment before opening relion
Execute from relion gui as external job providing the input micrograph_ctf.star and callyng ice.py executable
"""
### Setup
import os
import pandas as pd
# from pandas.core.common import SettingWithCopyWarning
import glob as glob
import numpy as np
import starfile
import pathlib
import sys
import argparse
import warnings
from os.path import exists
# warnings.filterwarnings("ignore", category=FutureWarning)
# warnings.filterwarnings("ignore", category=SettingWithCopyWarning)
print('running ...')
parser = argparse.ArgumentParser()
parser.add_argument("-o", "--output" , "--o", help = "output folder")
parser.add_argument("-i", "--input", "--in_mics", help = "input micrographs")
args, unknown = parser.parse_known_args()
inargs=args.input
outargs=args.output
print(str('grabbed arguments: ')+ inargs + str(' ') + outargs)
print('calling script...')
### Import
filename = inargs
try:
dfoptics = starfile.read(filename)['optics']
df = starfile.read(filename)['micrographs']
except:
print("No input detected")
f=open(outargs+"RELION_JOB_EXIT_SUCCESS","w+")
f.close()
exit()
if exists(outargs+str('/micrographs_ctf_ice.star')):
olddf = starfile.read(outargs+str('/micrographs_ctf_ice.star'))['micrographs']
outdf = df.join(olddf['rlnMicrographIceThickness'])
else:
outdf = df
outdf['rlnMicrographIceThickness']=np.nan
### Estimate ice thickness
n = -1
for row in outdf['rlnMicrographIceThickness']:
n+= 1
if np.isnan(row):
txtfile = outdf['rlnCtfImage'][n][:-8]+'_avrot.txt'
print(txtfile)
dftxt = (pd.read_csv(txtfile, skiprows=[0,1,2,3,4,7,8,9,10,11,12], header=None, delim_whitespace=True)).transpose()
ice_ring = dftxt[0].between(0.25,0.28, inclusive = 'both')
outdf['rlnMicrographIceThickness'][n]=(round((sum(np.abs(dftxt[ice_ring][1]))),6))
else:
continue
### Output
dict_output = {'optics' : dfoptics , 'micrographs' : outdf}
starfile.write(dict_output, outargs+'/micrographs_ctf_ice.star', overwrite=True)
### Finish
f=open(outargs+"RELION_OUTPUT_NODES.star","w+")
f.write("data_output_nodes\nloop_\n_rlnPipeLineNodeName #1\n_rlnPipeLineNodeTypeLabel #2\n"+outargs+"/micrographs_ctf_ice.star MicrographsData.star.relion")
f.close()
print('done!')
f=open(outargs+"RELION_JOB_EXIT_SUCCESS","w+")
f.close()