atomes 1.1.14
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
48#include <omp.h>
49#include <ctype.h>
50
58double get_z_from_pdb_name (char * name)
59{
60 if (strlen(name) == 2) name[1] = tolower ((unsigned char)name[1]);
61 return get_z_from_periodic_table (name);
62}
63
71int pdb_get_atoms_data (int linec)
72{
73 typedef struct pdb_atom pdb_atom;
74 struct pdb_atom
75 {
76 int id;
77 int sp;
78 double nz;
79 double x, y, z;
80 pdb_atom * prev;
81 pdb_atom * next;
82 };
83#ifdef OPENMP
84 int h, i, j, k, l;
85 int res;
86 int numth = omp_get_max_threads ();
87 pdb_atom ** first_at = g_malloc0(numth*sizeof*first_at);
88 pdb_atom * other_at = NULL;
89 gchar * saved_line;
90 gboolean add_spec;
91 h = 0;
92 res = 1;
93 #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)
94 for (i=0; i<linec; i++)
95 {
96 if (! res) goto ends;
97 this_line = g_strdup_printf ("%s", coord_line[i]);
98 saved_line = g_strdup_printf ("%s", this_line);
99 this_word = strtok_r (this_line, " ", & saved_line);
100 if (this_word)
101 {
102 if (g_strcmp0(this_word, "HETATM") == 0 || g_strcmp0(this_word, "ATOM") == 0)
103 {
104 h ++;
105 j = omp_get_thread_num();
106 if (! first_at[j])
107 {
108 first_at[j] = g_malloc0(sizeof*first_at[j]);
109 other_at = g_malloc0(sizeof*other_at);
110 other_at = first_at[j];
111 }
112 else
113 {
114 other_at -> next = g_malloc0(sizeof*other_at -> next);
115 other_at -> prev = g_malloc0(sizeof*other_at -> prev);
116 other_at -> next -> prev = other_at;
117 other_at = other_at -> next;
118 }
119 for (k=0; k<13; k++)
120 {
121 this_word = strtok_r (NULL, " ", & saved_line);
122 if (! this_word)
123 {
124 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);
125 res = 0;
126 goto ends;
127 }
128 if (k == 8) other_at -> x = atof(this_word);
129 if (k == 9) other_at -> y = atof(this_word);
130 if (k == 10) other_at -> z = atof(this_word);
131 if (k == 12)
132 {
133 other_at -> nz = get_z_from_pdb_name (this_word);
134 add_spec = TRUE;
135 #pragma omp critical
136 if (other_at -> nz)
137 {
138 if (this_reader -> z)
139 {
140 for (l=0; l<this_reader -> nspec; l++)
141 {
142 if (active_chem -> chem_prop[CHEM_Z][l] == other_at -> nz)
143 {
144 other_at -> sp = l;
145 if (! i) this_reader -> nsps[l] ++;
146 add_spec = FALSE;
147 break;
148 }
149 }
150 }
151 else
152 {
153 this_reader -> z = allocdouble (1);
154 this_reader -> nsps = allocint (1);
155 }
156 if (add_spec)
157 {
158 if (this_reader -> nspec)
159 {
160 this_reader -> z = g_realloc (this_reader -> z, (this_reader -> nspec+1)*sizeof*this_reader -> z);
161 this_reader -> nsps = g_realloc (this_reader -> nsps, (this_reader -> nspec+1)*sizeof*this_reader -> nsps);
162 }
163 other_at -> sp = this_reader -> nspec;
164 this_reader -> nsps[this_reader -> nspec] ++;
165 this_reader -> nspec ++;
166 }
167 }
168 }
169 }
170 }
171 }
172 ends:;
173 }
174 if (! res)
175 {
176 g_free (first_at);
177 g_free (other_at);
178 return 0;
179 }
180 active_project -> natomes = h;
181 active_project -> steps = 1;
182 active_project -> nspec = this_reader -> nspec;
183 if (active_project -> natomes) return 0;
186 j = 0;
187 for (i=0; i<numth; i++)
188 {
189 other_at = first_at[i];
190 while (other_at)
191 {
192 active_project -> atoms[0][j].id = j;
193 active_project -> atoms[0][j].sp = other_at -> sp;
194 active_project -> atoms[0][j].x = other_at -> x;
195 active_project -> atoms[0][j].y = other_at -> y;
196 active_project -> atoms[0][j].z = other_at -> z;
197 j ++;
198 other_at = other_at -> next;
199 g_free (other_at -> prev);
200 }
201 // Get back results
202 }
203#else
204
205#endif
206 return active_project -> natomes;
207}
208
216int open_pdb_file (int linec)
217{
218 if (! pdb_get_atoms_data (linec)) return 2;
219 return 0;
220}
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:471
int * allocint(int val)
allocate an int * pointer
Definition global.c:326
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:269
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:70
coord_file * this_reader
Definition read_coord.c:69
void add_reader_info(gchar *info, int mid)
append information message to the reader information
Definition read_coord.c:84
char * this_word
Definition read_coord.c:72
gchar * this_line
Definition read_coord.c:71
double get_z_from_pdb_name(char *name)
get Z from the PDB atom string
Definition read_pdb.c:58
int pdb_get_atoms_data(int linec)
get the atomic data from the PDB file
Definition read_pdb.c:71
int open_pdb_file(int linec)
open PDB file
Definition read_pdb.c:216
Functions declaration to read atomic coordinates.
int id
Definition global.h:893
GtkWidget * res[2]
Definition w_encode.c:212