UnityMol  0.9.6-875
UnityMol viewer / In developement
PDBtoDEN.cs
Go to the documentation of this file.
1 
66 using UnityEngine;
67 using System.Collections;
68 using System.Collections.Generic;
69 using System.IO;
70 using System;
71 using Molecule.Model;
72 using UI;
73 
74 public class PDBtoDEN : MonoBehaviour {
75  private static float[,,] gridS;
76 
77  public static float[,,] GridS {
78  get {return gridS;}
79  }
80 
81  static Color[,,] VertColor;
82  public static Color[] colors;
83 
84  public static int X;
85  public static int Y;
86  public static int Z;
87 
88  // I have no idea where this 18f vector comes from, but it's used everywhere in this class and by others,
89  // so I might as well make it a public static member. --- Alexandre
90  public static Vector3 fudgeFactor = new Vector3(18f,18f,18f);
91 
92  public static Vector3 delta;
93  public static Vector3 origin = new Vector3(MoleculeModel.MinValue.x,
96  private const float DEFAULT_RESOLUTION = 2.75f;
97 
98 
99 
100 
113  private float CapResolution(float resolution) {
114  int nbAtoms = MoleculeModel.atomsLocationlist.Count;
115  //float resolution = DEFAULT_RESOLUTION;
116 
117  if(nbAtoms > 500)
118  resolution = 2.5f;
119  if(nbAtoms > 1000)
120  resolution = 2.2f;
121  if(nbAtoms > 2000)
122  resolution = 2.0f;
123  if(nbAtoms > 4000)
124  resolution = 1.8f;
125  if(nbAtoms > 5000)
126  resolution = 1.7f;
127  if(nbAtoms > 6000)
128  resolution = 1.6f;
129  if(nbAtoms > 8000)
130  resolution = 1.5f;
131  if(nbAtoms > 10000)
132  resolution = 1.4f;
133  if(nbAtoms > 14000)
134  resolution = 1.2f;
135  if(nbAtoms > 20000)
136  resolution = 1.0f;
137 
138  return resolution;
139  }
140 
141  public void TranPDBtoDEN(float resolution = DEFAULT_RESOLUTION, bool cap = true){
142  if(cap)
143  resolution = CapResolution(resolution);
144 
145  delta = new Vector3(resolution, resolution, resolution);
146 
147  // We need to refresh the molecule's origin when it's not
148  // the first molecule for which we generate a surface.
149  origin = MoleculeModel.MinValue;
150  Debug.Log("Entering :: Generation of density from PDB");
151  X = (int) (((MoleculeModel.MaxValue.x-MoleculeModel.MinValue.x) * resolution) + 40);
152  Y = (int) (((MoleculeModel.MaxValue.y-MoleculeModel.MinValue.y) * resolution) + 40);
153  Z = (int) (((MoleculeModel.MaxValue.z-MoleculeModel.MinValue.z) * resolution) + 40);
154 
155  Debug.Log("Density point X,Y,Z :: "+ X+","+Y+","+Z);
156  Debug.Log("Density minValue :: " + MoleculeModel.MinValue);
157  gridS = new float[X,Y,Z];
158  VertColor = new Color[X,Y,Z];
159 
160 
161  int i;
162  int j;
163  int k;
164  float Dist;
165  float bfactor;
166  int atomnumber=0;
167  Color atomColor;
168  string type;
169  float density;
170 
171  float maxValue =(float)MoleculeModel.BFactorList[0];
172  foreach (float f in MoleculeModel.BFactorList)
173  if (f>maxValue)
174  maxValue =f;
175 
176  for (i=0;i<X;i++)
177  for(j=0;j<Y;j++)
178  for(k=0;k<Z;k++)
179  VertColor[i,j,k].b=1f;
180 
181 
182  int index = -1;
183  foreach (float[] coord in MoleculeModel.atomsLocationlist) {
184  index++;
185 
186  bool useAtomForCalc = true;
187 
189  //Debug.Log((MoleculeModel.atomHetTypeList[index]=="HETATM"));
190  if ((MoleculeModel.atomHetTypeList[index]=="HETATM") && (!MoleculeModel.sugarResname.Contains(MoleculeModel.atomsResnamelist[index]))){
191  useAtomForCalc = false;
192  }
193  }
194 
197  useAtomForCalc = false;
198  }
199 
200  if (useAtomForCalc){
201  i = Mathf.RoundToInt( (coord[0]-MoleculeModel.MinValue.x) * delta.x + fudgeFactor.x);
202  j = Mathf.RoundToInt( (coord[1]-MoleculeModel.MinValue.y) * delta.y + fudgeFactor.y);
203  k = Mathf.RoundToInt( (coord[2]-MoleculeModel.MinValue.z) * delta.z + fudgeFactor.z);
204 
205  /*
206  float scaleFactor = 10f;
207  i = Mathf.RoundToInt(coord[0] * scaleFactor);
208  j = Mathf.RoundToInt(coord[1] * scaleFactor);
209  k = Mathf.RoundToInt(coord[2] * scaleFactor);
210  */
211 
212 // Debug.Log("i,j,k : " + i +","+j+","+k);
213 // gridS[i,j,k]=2;
214 // }
215  type = (MoleculeModel.atomsTypelist[atomnumber]).type;
216 // type = "C";
217 // Debug.Log("i j k : "+i+","+j+","+k);
218  // Vector3 v1 = new Vector3((coord[0]-MoleculeModel.MinValue.x-MoleculeModel.Offset.x)*2+18,
219  // (coord[1]-MoleculeModel.MinValue.y-MoleculeModel.Offset.y)*2+18,
220  // (coord[2]-MoleculeModel.MinValue.z-MoleculeModel.Offset.z)*2+18);
221 
222 
223  Vector3 v1 = new Vector3( (coord[0]-MoleculeModel.MinValue.x) * delta.x + fudgeFactor.x,
224  (coord[1]-MoleculeModel.MinValue.y) * delta.y + fudgeFactor.y,
225  (coord[2]-MoleculeModel.MinValue.z) * delta.z + fudgeFactor.z );
226 
227 
228  /*
229  Vector3 v1 = new Vector3( coord[0] * scaleFactor,
230  coord[1] * scaleFactor,
231  coord[2] * scaleFactor);
232  */
233 
234  float AtomRadius = 1f;
235 
236  // Possibilité de créer une liste a la lecture du pdb et de la reprendre ici.
237  // Comme cela on peut lire d'autre propriétés biologiques
238  switch(type) {
239  case "C":
240  AtomRadius =3.4f;
241  atomColor = MoleculeModel.carbonColor.color;
242  break;
243  case "N":
244  AtomRadius =3.1f;
245  atomColor =MoleculeModel.nitrogenColor.color;
246  break;
247  case "O":
248  AtomRadius =3.04f;
249  atomColor = MoleculeModel.oxygenColor.color;
250  break;
251  case "S":
252  AtomRadius =4.54f;
253  atomColor = MoleculeModel.sulphurColor.color;
254  break;
255  case "P":
256  AtomRadius =3.6f;
257  atomColor = MoleculeModel.phosphorusColor.color;
258  break;
259  case "H":
260  AtomRadius =2.4f;
261  atomColor = MoleculeModel.hydrogenColor.color;
262  break;
263  default:
264  AtomRadius =2f;
265  atomColor = MoleculeModel.unknownColor.color;
266  break;
267  }
268 
270  for (int l = i-8 ;l < i+9 ; l++)
271  for ( int m = j-8 ; m < j+9 ; m++)
272  for ( int n = k-8 ; n < k+9 ; n++){
273  Vector3 v2 = new Vector3(l,m,n);
274  Dist = Vector3.Distance(v1,v2);
275  density = (float)Math.Exp(-((Dist/AtomRadius)*(Dist/AtomRadius)));
276  if (density > gridS[l,m,n])
277 // if (VertColor[l,m,n].b!=0){
278 // if (density > 0.5)
279  VertColor[l,m,n] = atomColor;
280  gridS[l,m,n] += density;
281  }
282 
283  } else if (UIData.toggleBfac) { // these index bounds might need to be express as functions of fudgeFactor
284  for (int l = i-8 ;l < i+9 ; l++) {
285  for ( int m = j-8 ; m < j+9 ; m++) {
286  for ( int n = k-8 ; n < k+9 ; n++) {
287  Vector3 v2 = new Vector3(l,m,n);
288  Dist = Vector3.Distance(v1,v2);
289 
290  bfactor = ((float)MoleculeModel.BFactorList[atomnumber]/maxValue*5);
291  if (bfactor>0)
292  gridS[l,m,n] += (float)Math.Exp(-((Dist/bfactor)*(Dist/bfactor)));
293  else
294  gridS[l,m,n] -= (float)Math.Exp(-((Dist/bfactor)*(Dist/bfactor)));
295 
296  if (VertColor[l,m,n].b == 1f && VertColor[l,m,n].r==0f) {
297  VertColor[l,m,n].r +=((float)MoleculeModel.BFactorList[atomnumber]/(maxValue));
298  VertColor[l,m,n].b -=((float)MoleculeModel.BFactorList[atomnumber]/(2*maxValue));
299  }
300  // if ( (VertColor[l,m,n].r + ((float)MoleculeModel.BFactorList[atomnumber]/(maxValue))) > 1){
301  // VertColor[l,m,n].r=1f;
302  // VertColor[l,m,n].b=0f;
303  // }
304  else {
305  VertColor[l,m,n].r +=((float)MoleculeModel.BFactorList[atomnumber]/(20*maxValue));
306  VertColor[l,m,n].b -=((float)MoleculeModel.BFactorList[atomnumber]/(20*maxValue));
307  }
308 
309  }
310  }
311  }
312  }
313  atomnumber++;
314  }
315  }
316 
317 // }
318 // }
319 // }
320 
321  // export the density in a .dx file readable by pymol or vmd
322 // StreamWriter test;
323 // test = new StreamWriter("grille.dx");
324 // test.Write("# Data from APBS 1.3\n#\n# POTENTIAL (kT/e)\n#\nobject 1 class gridpositions counts "+X+" "+Y+" "+Z+"\n
325 // origin -2.330000e+01 -2.34000e+01 -2.550000e+01\ndelta 5.000000e-01 0.000000e+00 0.000000e+00\n
326 // delta 0.000000e+00 5.000000e-01 0.000000e+00\ndelta 0.000000e+00 0.000000e+00 5.000000e-01\n
327 // object 2 class gridconnections counts "+X+" "+Y+" "+Z+"\nobject 3 class array type double rank 0 items "+X*Y*Z+" data follows\n");
328 // for (i=0 ; i< X ; i++){
329 // for (j=0 ; j<Y ; j++){
330 // for (k=0 ; k<Z ; k++){
331 // test.WriteLine(gridS[i,j,k]);
332 // }
333 // }
334 // }
335 // test.Write("attribute \"dep\" string \"positions\"\nobject \"regular positions regular connections\" class field\n
336 // component \"positions\" value 1\ncomponent \"connections\" value 2\ncomponent \"data\" value 3");
337 // test.Close();
338  }
339 
340  public static void ProSurface(float seuil){
341  // to create the structure from the pdb
342 
343  Vector4[] points;
344  points = new Vector4[ (X) * (Y) * (Z)];
345  colors = new Color[(X) * (Y) * (Z)];
346  // convert grid
347  for (int j = 0; j < Y; j++) {
348 
349  for (int i = 0; i < Z; i++) {
350  for (int k = 0; k < X; k++) {
351  points[j*(Z)*(X) + i*(X) + k] = new Vector4 (k, j, i , gridS[k,j,i]);
352  colors[j*(Z)*(X) + i*(X) + k] = VertColor[k,j,i];
353  }
354  }
355  }
356 
357  /*
358  Debug.Log("Entering :: Marching Cubes");
359  MarchingCubesRec MCInstance;
360  MCInstance = new MarchingCubesRec();
361  DestroySurface();
362  MCInstance.MCRecMain(X, Y, Z, seuil, points, 0f,false, delta, origin, colors);
363  */
364 
365  GenerateMesh.CreateSurfaceObjects(gridS, seuil, delta, origin, colors,0);
366 
367  points = null;
368  colors = null;
369 // long bytebefore = GC.GetTotalMemory(false);
370 // long byteafter = GC.GetTotalMemory(true);
371  GC.GetTotalMemory(true);
372  GC.Collect();
373 // long byteafter2 = GC.GetTotalMemory(false);
374 // Debug.Log ("before: "+(bytebefore/1000000)+"+ " afterCollet: " +(byteafter2/1000000));
375  }
376 
377  public static void initColors(int X,int Y,int Z, Color col) {
378  colors = new Color[(X) * (Y) * (Z)];
379 
380  for (int j = 0; j < Y; j++) {
381  for (int i = 0; i < Z; i++) {
382  for (int k = 0; k < X; k++) {
383  colors[j*(Z)*(X) + i*(X) + k] = col;
384  }
385  }
386  }
387  }
388 
389 }
390 
static void initColors(int X, int Y, int Z, Color col)
Definition: PDBtoDEN.cs:377
static void CreateSurfaceObjects(List< Mesh > meshes, int isPos)
static bool useHetatmForSurface
static ColorObject phosphorusColor
static bool toggleSurf
Definition: UIData.cs:194
static ColorObject carbonColor
static List< string > atomHetTypeList
static Vector3 delta
Definition: PDBtoDEN.cs:92
void TranPDBtoDEN(float resolution=DEFAULT_RESOLUTION, bool cap=true)
Definition: PDBtoDEN.cs:141
static int Y
Definition: PDBtoDEN.cs:85
const float DEFAULT_RESOLUTION
Definition: PDBtoDEN.cs:96
float CapResolution(float resolution)
First we lower the resolution for bigger molecules.
Definition: PDBtoDEN.cs:113
static void ProSurface(float seuil)
Definition: PDBtoDEN.cs:340
static int Z
Definition: PDBtoDEN.cs:86
static Color[] colors
Definition: PDBtoDEN.cs:82
static float[,,] gridS
Definition: PDBtoDEN.cs:75
static int X
Definition: PDBtoDEN.cs:84
static List< AtomModel > atomsTypelist
The type of each atom.
static float[,,] GridS
Definition: PDBtoDEN.cs:77
static Color[,,] VertColor
Definition: PDBtoDEN.cs:81
Color color
Definition: ColorObject.cs:72
static ColorObject nitrogenColor
static List< float > BFactorList
Bfactor of each atom.
static bool toggleBfac
Definition: UIData.cs:195
static ColorObject hydrogenColor
static ColorObject oxygenColor
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
static Vector3 MaxValue
The "biggest" corner of the bounding box that encloses the molecule.
static Vector3 fudgeFactor
Definition: PDBtoDEN.cs:90
static bool useSugarForSurface
static ColorObject unknownColor
static Vector3 MinValue
The "smallest" corner of the bounding box that encloses the molecule.
static List< string > atomsResnamelist
The name of the residue to which each atom belongs.
static List< string > sugarResname
static Vector3 origin
Definition: PDBtoDEN.cs:93
static ColorObject sulphurColor
Definition: GUIDisplay.cs:66
static List< float[]> atomsLocationlist
The coordinates of each atom.