1 /* LoadPDBTemplate.c */
2 /**********************************************************************************************************
3 Copyright (c) 2002-2013 Abdul-Rahman Allouche. All rights reserved
4
5 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
6 documentation files (the Gabedit), to deal in the Software without restriction, including without limitation
7 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
8 and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all copies or substantial portions
11 of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
14 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
16 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17 DEALINGS IN THE SOFTWARE.
18 ************************************************************************************************************/
19 #include "../../Config.h"
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <math.h>
23
24 #include "../Common/Global.h"
25 #include "../MolecularMechanics/PDBTemplate.h"
26 #include "../Utils/Utils.h"
27 #include "../Utils/Constants.h"
28 /**********************************************************************/
getResiduesList(FILE * file,gint * nResidue)29 gchar** getResiduesList(FILE* file,gint* nResidue)
30 {
31 gchar** t = (gchar**)g_malloc(sizeof(gchar*));
32
33 gchar dump[BSIZE];
34 gint len = BSIZE;
35 gint n;
36 gboolean Ok = FALSE;
37
38 *nResidue = 0;
39 fseek(file, 0L, SEEK_SET);
40 { char* e = fgets(dump,len,file);}
41 while(!feof(file))
42 {
43 if(fgets(dump,len,file))
44 {
45 if(strstr(dump,"Begin Residue List"))
46 {
47 Ok = TRUE;
48 break;
49 }
50 }
51 }
52 if(!Ok)
53 return NULL;
54 n = 0;
55 while(!feof(file))
56 {
57 if(fgets(dump,len,file))
58 {
59 if(strstr(dump,"End"))
60 break;
61 }
62 t = g_realloc(t,(n+1)*sizeof(gchar*));
63 t[n] = (gchar*)g_malloc(BSIZE*sizeof(gchar));
64 sscanf(dump,"%s",t[n]);
65 n++;
66 }
67 if(n==0)
68 {
69 free(t);
70 return NULL;
71 }
72 *nResidue = n;
73
74 return t;
75 }
76 /**********************************************************************/
setResiduesList(PDBTemplate * pdbTemplate,FILE * file)77 void setResiduesList(PDBTemplate* pdbTemplate, FILE* file)
78 {
79 gint nResidue;
80 gchar** t = getResiduesList(file,&nResidue);
81
82 pdbTemplate->numberOfResidues = nResidue;
83 pdbTemplate->residueTemplates = NULL;
84 if(nResidue>0 && t)
85 {
86 gint i;
87 pdbTemplate->residueTemplates = g_malloc(nResidue*sizeof(PDBResidueTemplate));
88 for(i=0;i<nResidue;i++)
89 {
90 pdbTemplate->residueTemplates[i].residueName = g_strdup(t[i]);
91 pdbTemplate->residueTemplates[i].numberOfTypes = 0;
92 pdbTemplate->residueTemplates[i].typeTemplates = NULL;
93 g_free(t[i]);
94 }
95 g_free(t);
96 }
97 }
98 /**********************************************************************/
setOneResidue(PDBTemplate * pdbTemplate,FILE * file,gint residueNumber)99 void setOneResidue(PDBTemplate* pdbTemplate, FILE* file,gint residueNumber)
100 {
101 gchar title[BSIZE];
102 gchar pdbType[BSIZE];
103 gchar mmType[BSIZE];
104 gchar charge[BSIZE];
105 gchar dump[BSIZE];
106 gint len = BSIZE;
107 gint n = 0;
108 PDBTypeTemplate *typeTemplates = NULL;
109 gboolean Ok = FALSE;
110
111 sprintf(title,"Begin %s Residue",pdbTemplate->residueTemplates[residueNumber].residueName);
112 fseek(file, 0L, SEEK_SET);
113 while(!feof(file))
114 {
115 if(fgets(dump,len,file))
116 {
117 if(strstr(dump,title))
118 {
119 Ok = TRUE;
120 break;
121 }
122 }
123 }
124 if(!Ok)
125 return;
126 n = 0;
127 typeTemplates = g_malloc(sizeof(PDBTypeTemplate));
128 while(!feof(file))
129 {
130 if(fgets(dump,len,file))
131 {
132 if(strstr(dump,"End"))
133 break;
134 }
135 sscanf(dump,"%s %s %s",pdbType, mmType, charge);
136 /*printf("pdbType = %s mmType = %s charge = %s\n",pdbType, mmType, charge);*/
137 typeTemplates[n].pdbType = g_strdup(pdbType);
138 typeTemplates[n].mmType = g_strdup(mmType);
139 typeTemplates[n].charge = atof(charge);
140
141 n++;
142 typeTemplates = g_realloc(typeTemplates,(n+1)*sizeof(PDBTypeTemplate));
143 }
144 if(n==0)
145 {
146 g_free(typeTemplates);
147 pdbTemplate->residueTemplates[residueNumber].numberOfTypes = 0;
148 pdbTemplate->residueTemplates[residueNumber].typeTemplates = NULL;
149 return;
150 }
151 typeTemplates = g_realloc(typeTemplates,n*sizeof(PDBTypeTemplate));
152
153 pdbTemplate->residueTemplates[residueNumber].numberOfTypes = n;
154 pdbTemplate->residueTemplates[residueNumber].typeTemplates = typeTemplates;
155 }
156 /**********************************************************************/
setAllResidues(PDBTemplate * pdbTemplate,FILE * file)157 void setAllResidues(PDBTemplate* pdbTemplate, FILE* file)
158 {
159 gint i;
160 gint n = pdbTemplate->numberOfResidues;
161 /* printf("numberOfResidues = %d\n",pdbTemplate->numberOfResidues);*/
162 for(i=0;i<n;i++)
163 {
164 /* printf("i = %d\n",i);*/
165 setOneResidue(pdbTemplate,file,i);
166 }
167 }
168 /**********************************************************************/
readPDBTemplate(PDBTemplate * pdbTemplate,gchar * filename)169 gboolean readPDBTemplate(PDBTemplate* pdbTemplate,gchar* filename)
170 {
171 FILE* file;
172 file = FOpen(filename,"rb");
173
174 /* printf("Read Default TPL file %s \n",filename);*/
175 if(file == NULL)
176 return FALSE;
177 else
178 {
179 /* printf("Read List Of residue\n");*/
180 setResiduesList(pdbTemplate,file);
181 /* printf("End Read List Of residue\n");*/
182 setAllResidues(pdbTemplate,file);
183 /* printf("End Set All Residue\n");*/
184 fclose(file);
185 }
186 return TRUE;
187 }
188 /**********************************************************************/
189