Mercurial > repos > agpetit > calculate_diameter
comparison estimate_nb_sub_trajectories.py @ 5:e504457035e5 draft
"planemo upload for repository https://github.com/mesocentre-clermont-auvergne/aubi_piaf commit b6488400d4478d46697019485e912c38ea2202a5-dirty"
author | agpetit |
---|---|
date | Mon, 30 May 2022 14:21:51 +0000 |
parents | |
children | e5cf7698a2af |
comparison
equal
deleted
inserted
replaced
4:433dba16af7d | 5:e504457035e5 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 # coding: utf-8 | |
4 """ | |
5 The script allows to estimate the number of sub-trajectories to obtain. | |
6 It also allows to split the trajectory into a number of sub-trajectories. | |
7 # USAGE : estimate_nb_sub_trajectories.py -c : file obtaining with gmx check | |
8 -log : name of log file (optional) -d : output directory (optional) | |
9 -o : name of output file (optional) | |
10 -f : desired number of frames per sub-trajectory (optional) | |
11 -start : start time of the trajectory (optional) | |
12 -end : end time of the trajectory (optional) | |
13 """ | |
14 | |
15 __all__ = [] | |
16 __author__ = "Agnès-Elisabeth Petit" | |
17 __date__ = "30/05/2022" | |
18 __version__ = "0.8" | |
19 __copyright__ = "(c) 2022 CC-BY-NC-SA" | |
20 | |
21 # Library import | |
22 import argparse | |
23 import logging | |
24 import os | |
25 import sys | |
26 | |
27 import numpy as np | |
28 | |
29 | |
30 def test_setup(): | |
31 global args | |
32 args = parse_arguments() | |
33 args.verbose = True | |
34 | |
35 | |
36 def parse_arguments(): | |
37 parser = argparse.ArgumentParser( | |
38 description="The script allows to estimate the number of " | |
39 "sub-trajectories to obtain. It also allows to split" | |
40 " the trajectory into a number of sub-trajectories.", | |
41 formatter_class=argparse.ArgumentDefaultsHelpFormatter, | |
42 prefix_chars="-", | |
43 add_help=True, | |
44 ) | |
45 parser.add_argument( | |
46 "-v", | |
47 "--verbose", | |
48 action="store_true", | |
49 help="""Information messages to stderr""", | |
50 ) | |
51 parser.add_argument( | |
52 "-c", | |
53 "--input_check", | |
54 type=str, | |
55 nargs=1, | |
56 help=""".txt file obtained with gmx check -f. | |
57 It contains information about the trajectory""", | |
58 ) | |
59 parser.add_argument( | |
60 "-log", "--log_output", type=str, | |
61 default="log/estimate_nb_sub_trajectories.log", | |
62 help="""Output for log file. Default : | |
63 log/estimate_nb_sub_trajectories.log""" | |
64 ) | |
65 parser.add_argument( | |
66 "-d", "--output_directory", type=str, nargs=1, | |
67 default="./", | |
68 help="""It's output Directory. Default : ./""" | |
69 ) | |
70 parser.add_argument( | |
71 "-o", | |
72 "--output_file", | |
73 type=str, | |
74 default="estimated_number_of_sub_trajectories.tsv", | |
75 help="""Output file. Default : | |
76 estimated_number_of_sub_trajectories.tsv""", | |
77 ) | |
78 parser.add_argument( | |
79 "-f", | |
80 "--nb_frames", | |
81 type=int, | |
82 default=30, | |
83 help="""Number of frames per sub-trajectory. Default : 30""", | |
84 ) | |
85 parser.add_argument( | |
86 "-start", "--start_traj", type=str, | |
87 help="""Start of the trajectory to be cut""" | |
88 ) | |
89 parser.add_argument( | |
90 "-end", "--end_traj", type=str, | |
91 help="""End of the trajectory to be cut""" | |
92 ) | |
93 return parser.parse_args() | |
94 | |
95 | |
96 def search_nbr_steps_time_step(txt_file): | |
97 """ | |
98 Description : Keeping the number of frames of the complete trajectory and | |
99 the time between each frame. | |
100 param txt_file: file obtained with gmx check. | |
101 return: list that contains the number of frames of the complete trajectory | |
102 and time between each frame. | |
103 """ | |
104 if args.verbose: | |
105 logging.info("\nFunction search_nbr_steps_time_step") | |
106 logging.info("The input file is " + txt_file) | |
107 with open(txt_file, "r") as f: | |
108 len_traj = "" | |
109 time_step = "" | |
110 for li in f: | |
111 li = li.rstrip() | |
112 if li.startswith("Step"): | |
113 li2 = li.split() | |
114 len_traj = int(li2[1]) | |
115 time_step = int(li2[2]) | |
116 if args.verbose: | |
117 logging.info("The length of the trajectory is " + str(len_traj)) | |
118 logging.info( | |
119 "The elapsed time between two steps is : " + str(time_step) + " ps" | |
120 ) | |
121 logging.info("End search_nbr_steps_time_step functions") | |
122 return len_traj, time_step | |
123 | |
124 | |
125 def estimate_nbr_sub_trajectories(nbr_step_time_step, nbr_frames_traj, | |
126 out_file): | |
127 """ | |
128 Description: Creation of a tsv file that contains the number of frames | |
129 of the complete trajectory, the number of sub-trajectories to create, | |
130 the duration between each frame and the number of frames | |
131 per sub-trajectory. | |
132 param nbr_step_time_step: list which contains the number of frames of | |
133 the complete trajectory and the time between each frame | |
134 param nbr_frames_traj: number of frames per sub-trajectory | |
135 param out_file: output file name | |
136 return: list that contains the number of frames of the complete trajectory, | |
137 the number of sub-trajectories to create, the duration between | |
138 each frame and the number of frames per sub-trajectory. | |
139 output: tsv file | |
140 """ | |
141 if args.verbose: | |
142 logging.info("\nFunction estimate_nbr_means") | |
143 logging.info("The length of the trajectory is " + | |
144 str(nbr_step_time_step[0])) | |
145 logging.info( | |
146 "The elapsed time between two steps is : " | |
147 + str(nbr_step_time_step[1]) | |
148 + " ps" | |
149 ) | |
150 logging.info("The output file is " + str(out_file)) | |
151 name_columns = [ | |
152 "Length_trajectory (frames)", | |
153 "Number_sub_trajectories", | |
154 "Time_steps (ps)", | |
155 "Number_frames_per_sub_trajectory", | |
156 "Start_trajectory (frames)", | |
157 "End_trajectory (frames)", | |
158 ] | |
159 time_step = nbr_step_time_step[1] | |
160 if nbr_step_time_step[2] is not None: | |
161 start_traj = nbr_step_time_step[2] | |
162 else: | |
163 start_traj = 0 | |
164 if nbr_step_time_step[3] is not None: | |
165 end_traj = nbr_step_time_step[3] | |
166 else: | |
167 end_traj = nbr_step_time_step[0] - 1 | |
168 if args.verbose: | |
169 logging.info( | |
170 "The first frame of the trajectory is the number " | |
171 + str(start_traj) | |
172 ) | |
173 logging.info("The first frame of the trajectory is the number " | |
174 + str(end_traj)) | |
175 len_traj = end_traj - start_traj + 1 | |
176 n_sub_traj = len_traj // nbr_frames_traj | |
177 if args.verbose: | |
178 logging.info("The estimated number of sub-trajectories is : " | |
179 + str(n_sub_traj)) | |
180 list_values = [ | |
181 str(len_traj), | |
182 str(n_sub_traj), | |
183 str(time_step), | |
184 str(nbr_frames_traj), | |
185 str(start_traj), | |
186 str(end_traj), | |
187 ] | |
188 tab_values = np.asarray([name_columns, list_values]) | |
189 np.savetxt(out_file, tab_values, delimiter="\t", fmt="%s") | |
190 if args.verbose: | |
191 logging.info("Save table in the file : " + str(out_file)) | |
192 logging.info("End estimate_nbr_sub_trajectories function") | |
193 | |
194 | |
195 if __name__ == "__main__": | |
196 args = parse_arguments() | |
197 if args.output_directory[0].endswith("/"): | |
198 out_directory = args.output_directory[0] | |
199 else: | |
200 out_directory = args.output_directory[0] + "/" | |
201 if args.verbose: | |
202 if "/" in args.log_output: | |
203 log_dir = args.log_output.rsplit("/", 1)[0] | |
204 else: | |
205 log_dir = "log/" | |
206 log_file = args.log_output | |
207 if not os.path.exists(log_dir): | |
208 os.makedirs(log_dir) | |
209 if os.path.isfile(log_file): | |
210 os.remove(log_file) | |
211 if args.log_output: | |
212 logging.basicConfig( | |
213 filename=log_file, | |
214 format="%(levelname)s - %(message)s", | |
215 level=logging.INFO, | |
216 ) | |
217 else: | |
218 logging.basicConfig( | |
219 filename=log_file, | |
220 format="%(levelname)s - %(message)s", | |
221 level=logging.INFO, | |
222 ) | |
223 logging.info("verbose mode on") | |
224 nb_frames_traj = args.nb_frames | |
225 if args.start_traj and args.start_traj != "": | |
226 start_trajectory = int(args.start_traj) | |
227 else: | |
228 start_trajectory = None | |
229 if args.end_traj and args.end_traj != "": | |
230 end_trajectory = int(args.end_traj) | |
231 else: | |
232 end_trajectory = None | |
233 if args.verbose: | |
234 logging.info("Start estimate number sub-trajectories") | |
235 output_file = args.output_file | |
236 if not args.input_check: | |
237 print("Please enter the file created with 'gmx_check -f'") | |
238 if args.verbose: | |
239 logging.error("Please enter the file created with 'gmx_check -f'") | |
240 sys.exit(1) | |
241 nb_steps_time_step = list(search_nbr_steps_time_step( | |
242 args.input_check[0])) | |
243 nb_steps_time_step.append(start_trajectory) | |
244 nb_steps_time_step.append(end_trajectory) | |
245 output_file = out_directory + output_file | |
246 estimate_nbr_sub_trajectories(nb_steps_time_step, nb_frames_traj, | |
247 output_file) |