-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrajectories.c
304 lines (212 loc) · 7.84 KB
/
trajectories.c
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* The clust2 program.
* Copyright (C) 2024 Jakob Niessner.
* Contact: [email protected]
*
*
* This program was written at the Heidelberg Institute for theoretical studies,
* Schloß-Wolfsbrunnenweg 35, 69118 Heidelberg, Germany
* Under the supervision of Prof. Dr. Rebecca C. Wade
* Contact: [email protected]
*
* Permission to use, copy, modify, and distribute this software and its
* documentation with or without modifications and for any purpose and
* without fee is hereby granted, provided that any copyright notices
* appear in all copies and that both those copyright notices and this
* permission notice appear in supporting documentation, and that the
* names of the contributors or copyright holders not be used in
* advertising or publicity pertaining to distribution of the software
* without specific prior permission.
*
* THE CONTRIBUTORS AND COPYRIGHT HOLDERS OF THIS SOFTWARE DISCLAIM ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
* CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT
* OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
* OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/queue.h>
#include "trajectories.h"
#include "math_func.h"
int get_energy_signature(char* line, char***Energy_fields)
{
int line_length = strlen(line);
char* linecp = malloc(sizeof(char)*(line_length+1));
strcpy(linecp, line);
char* token = strtok(linecp, " ");
int j=0;
Energy_fields[0] = malloc(sizeof(char*)*100);
while(memcmp(token, "TotEn", 5)){
token = strtok(NULL, " ");
}
while( token != NULL)
{
if(!memcmp(token, "Occur", 5))
{
break;
}
Energy_fields[0][j]=malloc(sizeof(char)*(strlen(token)+1));
strcpy(Energy_fields[0][j], token);
token = strtok(NULL, " ");
j++;
}
free(linecp);
return j;
}
int read_record_from_line(char line[], Record* record, int num_energy_fields)
{
record->line = malloc(sizeof(char)*strlen(line));
strcpy(record->line, line);
const char* delimiter=" ";
char* token = strtok(line, delimiter);
sscanf(token, "%li", &(record->RunNb));
token = strtok(NULL, delimiter);
sscanf(token, "%li", &(record->StepNb));
record->Trans = (double*)malloc(sizeof(double)*3);
for(int i = 0; i<3; i++)
{
token=strtok(NULL, delimiter);
sscanf(token, "%lf", &(record->Trans[i]));
}
for(int i = 0; i<3; i++)
{
token=strtok(NULL, delimiter);
sscanf(token, "%lf", &(record->Rot_mat[0][i]));
}
for(int i = 0; i<3; i++)
{
token=strtok(NULL, delimiter);
sscanf(token, "%lf", &(record->Rot_mat[1][i]));
}
//Creating the third row of the matrix
cross(record->Rot_mat[0], record->Rot_mat[1], record->Rot_mat[2]);
//transposing the rotation matrix in order
transpose(record->Rot_mat);
record->energy_fields = (double*) malloc(num_energy_fields*sizeof(double));
for(int i=0; i<num_energy_fields; i++)
{
token=strtok(NULL, delimiter);
sscanf(token, "%lf", &(record->energy_fields[i]));
}
token=strtok(NULL, delimiter);
sscanf(token, "%lf", &(record->occurences));
//token=strtok(NULL, delimiter);
//sscanf(token, "%lf", &(record->AvEnergy));
//token=strtok(NULL, delimiter);
//sscanf(token, "%lf", &(record->StdAvEnergy));
}
int read_trajectory_file(char* filename, char*** head_lines, Record** records, int* num_energy_fields, char*** Energy_fields ,double pdb1_com[], double pdb2_com[])
{
FILE* traj_file = fopen(filename, "r");
const char* delimiter = " ";
size_t buffer_size = 0;
char * buffer = NULL;
ssize_t read;
head_lines[0] = malloc(sizeof(char*)*4);
if((read = getline(&buffer, &buffer_size, traj_file)) == -1 )
{
printf("There is something wrong with your trajectories file.\nThis is not an SDA complexes file.\n");
}
printf("Made it until right before energy_signature function call\n");
*num_energy_fields=get_energy_signature(buffer, Energy_fields);
printf("The number of energy fields is %i\n", *num_energy_fields);
//saving the first head_line
head_lines[0][0] = (char*)malloc(sizeof(char)*strlen(buffer));
memcpy(head_lines[0][0], buffer, strlen(buffer));
if((read = getline(&buffer, &buffer_size, traj_file)) == -1 )
{
printf("There is something wrong with your trajectories file.\nThis is not an SDA complexes file.\n");
exit(EXIT_FAILURE);
}
//saving the second head_line
head_lines[0][1] = (char*)malloc(sizeof(char)*strlen(buffer));
memcpy(head_lines[0][1], buffer, strlen(buffer));
char buffer2[strlen(buffer)-1];
if((read = getline(&buffer, &buffer_size, traj_file)) == -1 || buffer[0]!='#')
{
printf("The first center of mass is missing!");
exit(EXIT_FAILURE);
}
//saving the third head_line
head_lines[0][2] = (char*)malloc(sizeof(char)*strlen(buffer));
memcpy(head_lines[0][2], buffer, strlen(buffer));
memcpy(buffer2, &buffer[1], sizeof(char)*strlen(buffer)-1);
char* token = strtok(buffer2, delimiter);
int tok_number=0;
while(token != NULL)
{
sscanf(token, "%lf", &(pdb1_com[tok_number]));
token = strtok(NULL, delimiter);
tok_number++;
}
if((read = getline(&buffer, &buffer_size, traj_file)) == -1 || buffer[0]!='#')
{
printf("The second center of mass is missing!");
}
//saving the fourth head_line
head_lines[0][3] = (char*)malloc(sizeof(char)*strlen(buffer));
memcpy(head_lines[0][3], buffer, strlen(buffer));
memcpy(buffer2, &buffer[1], sizeof(char)*24);
token = strtok(buffer2, delimiter);
tok_number=0;
while(token != NULL)
{
sscanf(token, "%lf", &pdb2_com[tok_number++]);
token = strtok(NULL, delimiter);
}
struct Record_list rec_list;
SLIST_INIT(&rec_list);
//Now reading the trajectories file
int record_num=0;
while((read=getline(&buffer, &buffer_size, traj_file))!=-1)
{
Record_list_entry* list_entr = malloc(sizeof(Record_list_entry));
read_record_from_line(buffer, &(list_entr->record), *num_energy_fields);
SLIST_INSERT_HEAD(&rec_list, list_entr, entry);
record_num++;
}
Record* records2 = *records = (Record*)malloc(sizeof(Record)*record_num);
Record_list_entry* list_entr;
int j = 1;
SLIST_FOREACH(list_entr, &rec_list, entry)
{
records2[record_num-j]=list_entr->record;
j++;
}
while(!SLIST_EMPTY(&rec_list)) {
Record_list_entry *list_entry = SLIST_FIRST(&rec_list);
SLIST_REMOVE_HEAD(&rec_list, entry);
free(list_entry);
}
#ifdef DEBUG
for(int i= 0; i<3; i++)
print_record(records2[i], num_energy_fields);
#endif
return record_num;
};
void print_record(Record record, int energy_field_num)
{
printf("The values are: \n");
printf("RunNb: %li\n", record.RunNb);
printf("StepNb: %li\n", record.StepNb);
printf("Trans X1: %lf\n", record.Trans[0]);
printf("Trans X2: %lf\n", record.Trans[1]);
printf("Trans X3: %lf\n", record.Trans[2]);
printf("Rot1 x: %lf\t%lf\t%lf\n", record.Rot_mat[0][0], record.Rot_mat[1][0], record.Rot_mat[2][0]);
printf("Rot1 y: %lf\t%lf\t%lf\n", record.Rot_mat[0][1], record.Rot_mat[1][1], record.Rot_mat[2][1]);
printf("Rot1 z: %lf\t%lf\t%lf\n", record.Rot_mat[0][2], record.Rot_mat[1][2], record.Rot_mat[2][2]);
printf("Energyvalues: ");
for(int i=0; i<energy_field_num; i++)
printf("\t%lf", record.energy_fields[i]);
printf("\n");
printf("Occurences: %lf\n", record.occurences);
//printf("AvEnergy: %lf\n", record.AvEnergy);
//printf("StdAvEnergy: %lf\n", record.StdAvEnergy);
printf("Line:\n");
printf("%s", record.line);
};