atomes 1.1.15
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
read_pdb.c
Go to the documentation of this file.
1/* This file is part of the 'atomes' software
2
3'atomes' is free software: you can redistribute it and/or modify it under the terms
4of the GNU Affero General Public License as published by the Free Software Foundation,
5either version 3 of the License, or (at your option) any later version.
6
7'atomes' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9See the GNU General Public License for more details.
10
11You should have received a copy of the GNU Affero General Public License along with 'atomes'.
12If not, see <https://www.gnu.org/licenses/>
13
14Copyright (C) 2022-2024 by CNRS and University of Strasbourg */
15
22/*
23* This file: 'read_pdb.c'
24*
25* Contains:
26*
27
28 - The functions to read PDB files
29
30*
31* List of functions:
32
33 int pdb_get_atoms_data (int linec);
34 int open_pdb_file (int linec);
35
36 double get_z_from_pdb_name (char * name);
37
38*/
39
40#include "global.h"
41#include "glview.h"
42#include "callbacks.h"
43#include "interface.h"
44#include "project.h"
45#include "bind.h"
46#include "readers.h"
47#include <ctype.h>
48#ifdef OPENMP
49# include <omp.h>
50#endif
51
59double get_z_from_pdb_name (char * name)
60{
61 if (strlen(name) == 2) name[1] = tolower ((unsigned char)name[1]);
62 return get_z_from_periodic_table (name);
63}
64
72int pdb_get_atoms_data (int linec)
73{
74 typedef struct pdb_atom pdb_atom;
75 struct pdb_atom
76 {
77 int id;
78 int sp;
79 double nz;
80 double x, y, z;
81 pdb_atom * prev;
82 pdb_atom * next;
83 };
84#ifdef OPENMP
85 int h, i, j, k, l;
86 int res;
87 int numth = omp_get_max_threads ();
88 pdb_atom ** first_at = g_malloc0(numth*sizeof*first_at);
89 pdb_atom * other_at = NULL;
90 gchar * saved_line;
91 gboolean add_spec;
92 h = 0;
93 res = 1;
94 #pragma omp parallel for num_threads(numth) private(i,j,k,l,this_line,saved_line,this_word,other_at,add_spec) shared(h,this_reader,res,coord_line,first_at)
95 for (i=0; i<linec; i++)
96 {
97 if (! res) goto ends;
98 this_line = g_strdup_printf ("%s", coord_line[i]);
99 saved_line = g_strdup_printf ("%s", this_line);
100 this_word = strtok_r (this_line, " ", & saved_line);
101 if (this_word)
102 {
103 if (g_strcmp0(this_word, "HETATM") == 0 || g_strcmp0(this_word, "ATOM") == 0)
104 {
105 h ++;
106 j = omp_get_thread_num();
107 if (! first_at[j])
108 {
109 first_at[j] = g_malloc0(sizeof*first_at[j]);
110 other_at = g_malloc0(sizeof*other_at);
111 other_at = first_at[j];
112 }
113 else
114 {
115 other_at -> next = g_malloc0(sizeof*other_at -> next);
116 other_at -> prev = g_malloc0(sizeof*other_at -> prev);
117 other_at -> next -> prev = other_at;
118 other_at = other_at -> next;
119 }
120 for (k=0; k<13; k++)
121 {
122 this_word = strtok_r (NULL, " ", & saved_line);
123 if (! this_word)
124 {
125 add_reader_info (g_strdup_printf ("Wrong file format - record <b>%d</b> on line <b>%d</b> is corrupted !", k+2, i+1), 0);
126 res = 0;
127 goto ends;
128 }
129 if (k == 8) other_at -> x = string_to_double ((gpointer)this_word);
130 if (k == 9) other_at -> y = string_to_double ((gpointer)this_word);
131 if (k == 10) other_at -> z = string_to_double ((gpointer)this_word);
132 if (k == 12)
133 {
134 other_at -> nz = get_z_from_pdb_name (this_word);
135 add_spec = TRUE;
136 #pragma omp critical
137 if (other_at -> nz)
138 {
139 if (this_reader -> z)
140 {
141 for (l=0; l<this_reader -> nspec; l++)
142 {
143 if (active_chem -> chem_prop[CHEM_Z][l] == other_at -> nz)
144 {
145 other_at -> sp = l;
146 if (! i) this_reader -> nsps[l] ++;
147 add_spec = FALSE;
148 break;
149 }
150 }
151 }
152 else
153 {
154 this_reader -> z = allocdouble (1);
155 this_reader -> nsps = allocint (1);
156 }
157 if (add_spec)
158 {
159 if (this_reader -> nspec)
160 {
161 this_reader -> z = g_realloc (this_reader -> z, (this_reader -> nspec+1)*sizeof*this_reader -> z);
162 this_reader -> nsps = g_realloc (this_reader -> nsps, (this_reader -> nspec+1)*sizeof*this_reader -> nsps);
163 }
164 other_at -> sp = this_reader -> nspec;
165 this_reader -> nsps[this_reader -> nspec] ++;
166 this_reader -> nspec ++;
167 }
168 }
169 }
170 }
171 }
172 }
173 ends:;
174 }
175 if (! res)
176 {
177 g_free (first_at);
178 g_free (other_at);
179 return 0;
180 }
181 active_project -> natomes = h;
182 active_project -> steps = 1;
183 active_project -> nspec = this_reader -> nspec;
184 if (active_project -> natomes) return 0;
187 j = 0;
188 for (i=0; i<numth; i++)
189 {
190 other_at = first_at[i];
191 while (other_at)
192 {
193 active_project -> atoms[0][j].id = j;
194 active_project -> atoms[0][j].sp = other_at -> sp;
195 active_project -> atoms[0][j].x = other_at -> x;
196 active_project -> atoms[0][j].y = other_at -> y;
197 active_project -> atoms[0][j].z = other_at -> z;
198 j ++;
199 other_at = other_at -> next;
200 g_free (other_at -> prev);
201 }
202 // Get back results
203 }
204#else
205
206#endif
207 return active_project -> natomes;
208}
209
217int open_pdb_file (int linec)
218{
219 if (! pdb_get_atoms_data (linec)) return 2;
220 return 0;
221}
Binding to the Fortran90 subroutines.
double get_z_from_periodic_table(gchar *lab)
get Z from atom label
Definition w_library.c:304
Callback declarations for main window.
int atoms[NUM_STYLES][2]
int activep
Definition global.c:159
double * allocdouble(int val)
allocate a double * pointer
Definition global.c:459
int * allocint(int val)
allocate an int * pointer
Definition global.c:314
double string_to_double(gpointer string)
convert string to double
Definition global.c:624
Global variable declarations Global convenience function declarations Global data structure defin...
chemical_data * active_chem
Definition project.c:48
#define CHEM_Z
Definition global.h:297
project * active_project
Definition project.c:47
Variable declarations related to the OpenGL window Function declarations related to the OpenGL wind...
Messaging function declarations.
double z
Definition ogl_draw.c:57
double y
Definition ogl_draw.c:57
double x
Definition ogl_draw.c:57
void alloc_proj_data(project *this_proj, int cid)
allocate data
Definition open_p.c:206
Function declarations for reading atomes project file Function declarations for saving atomes proje...
void active_project_changed(int id)
change the active project
Definition update_p.c:175
gchar ** coord_line
Definition read_coord.c:72
coord_file * this_reader
Definition read_coord.c:71
void add_reader_info(gchar *info, int mid)
append information message to the reader information
Definition read_coord.c:86
char * this_word
Definition read_coord.c:74
gchar * this_line
Definition read_coord.c:73
double get_z_from_pdb_name(char *name)
get Z from the PDB atom string
Definition read_pdb.c:59
int pdb_get_atoms_data(int linec)
get the atomic data from the PDB file
Definition read_pdb.c:72
int open_pdb_file(int linec)
open PDB file
Definition read_pdb.c:217
Functions declaration to read atomic coordinates.
int id
Definition global.h:941
GtkWidget * res[2]
Definition w_encode.c:212