UnityMol  0.9.6-875
UnityMol viewer / In developement
ParsePDB.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5 using Molecule.Model;
6 using Molecule.Control;
7 using System.Net;
8 using UI;
9 using System.Linq;
10 
11 public class ParsePDB {
12 
13  public static void FetchPDB2(string url,string id, string proxyserver = "", int proxyport = 0) {
14  StreamReader sr ;
15  Stream dataStream=null;
16  HttpWebResponse response=null;
17 
18  if (UIData.loadHireRNA == false) {
19  UIData.ffType = UIData.FFType.atomic;
21  }
22  else
23  {
24  UIData.ffType = UIData.FFType.HiRERNA;
26  }
27 
28  HttpWebRequest request =(HttpWebRequest) WebRequest.Create (url+id+".pdb");
29 
30  // If required by the server, set the credentials.
31  request.Credentials = CredentialCache.DefaultCredentials;
32 
33  //Set proxy information if needed
34  // Debug.Log("LoadPDB: " + proxyserver + " " + proxyport);
35  if(proxyserver != "")
36  request.Proxy = new WebProxy(proxyserver,proxyport);
37 
38  // Get the response.
39  response = (HttpWebResponse)request.GetResponse ();
40  // Display the status.
41  Debug.Log("LoadPDB Status :: " + response.StatusDescription);
42 
43  // Get the stream containing content returned by the server.
44  dataStream = response.GetResponseStream ();
45  // Open the stream using a StreamReader for easy access.
46  sr = new StreamReader (dataStream);
47 
48  ReadPDB2(sr);
49  //ReadPDB(sr);
50 
51  if(dataStream!=null&& response!=null) {
52  dataStream.Close ();
53  response.Close ();
54  }
55  }
56  public static void LoadPDBRequest2(string file_base_name,bool withData = true) {
57  StreamReader sr ;
58 
59 // FileInfo file=new FileInfo(file_base_name+".pdb");
60  sr=new StreamReader(file_base_name+".pdb");
61 
62  if (UIData.loadHireRNA == false) {
63  UIData.ffType = UIData.FFType.atomic;
65  }
66  else
67  {
68  UIData.ffType = UIData.FFType.HiRERNA;
70  }
71 
72  ReadPDB2(sr);
73 
74  if(withData) {
75  FileInfo fieldlinefile=new FileInfo(file_base_name+".json");
76  FileInfo apffile=new FileInfo(file_base_name+".apf");
77  if(fieldlinefile.Exists) {
78  LoadJsonRequest(file_base_name+".json",MoleculeModel.Offset);
80  } else if(apffile.Exists) {
81  LoadJsonRequest(file_base_name+".apf",MoleculeModel.Offset);
83  } else {
86  }
87 
88  FileInfo Surfacefile=new FileInfo(file_base_name+".obj");
89  FileInfo Surfacefile0=new FileInfo(file_base_name+"0.obj");
90 
91  if(Surfacefile.Exists || Surfacefile0.Exists) {
92  LoadOBJRequest(file_base_name);
95  } else {
97  }
98 
99  FileInfo dxfile=new FileInfo(file_base_name+".dx");
100  MoleculeModel.dxFileExists = false ; // otherwise a molecule might load dx data from a previous molecule
101  if(dxfile.Exists) {
102  MoleculeModel.dxFileExists = true ;
103  LoadDxRequest(file_base_name+".dx",MoleculeModel.Offset);
104  }
105  }
106  }
107 
108 
109  public static void ReadPDB2(TextReader sr){
110 
111 
112  List<float[]> alist = new List<float[]>();
113  List<float[]> calist = new List<float[]>();
114 
115  List<float> BFactorList = new List<float>();
116 
117  List<string> resnamelist = new List<string>();
118  List<string> atomsNameList = new List<string>();
119  List<string> caChainlist = new List<string>();
120 
121  List<AtomModel> typelist = new List<AtomModel>();
122  List<string> chainList = new List<string>();
123 
124  List<Color> colorList = new List<Color>();
125 
126  List<float[]> sshelixlist = new List<float[]> ();
127  List<float[]> sssheetlist = new List<float[]> ();
128 
129 
130  string[] dnaBackboneAtoms = new string[] {"C5'"};
131  List<int> residueIds = new List<int>();
132  List<int> splits = new List<int>();
133  List<int> atomsNumberList = new List<int>();
134 
135  int resNb = 0;
136  int prevRes = int.MinValue;
137  int currentRes = int.MinValue+1;
138 
139  int nowline=0;
140  int nbAtoms = 0;
141  int nbTers = 0;
142 
143  string s;
144  string[] lines = sr.ReadToEnd().Split(new [] { '\r', '\n' });
145  int idl = 0;
146  int prevresnb = 0;
147 
148  // while((s=sr.ReadLine())!=null) {
149  while(idl < lines.Length){
150  s = lines[idl];
151  idl++;
152  if(s.StartsWith("ENDMDL"))
153  break;
154 
155  if(s.Length>4) {
156  bool isAtomLine = s.StartsWith("ATOM");
157  // bool isHetAtm = s.StartsWith("HETATM");
158  bool isHelixLine = s.StartsWith ("HELIX");
159  bool isSheetLine = s.StartsWith("SHEET");
160  bool isConnectLine = s.StartsWith("CONECT");
161  if(s.StartsWith("TER")) {
162  splits.Add(resNb);
163  nbTers++;
164  }
165 
166  if(isHelixLine){
167  string chainh = s.Substring (19,2).Trim ();
168  string initr = s.Substring(22,4);
169  string termr = s.Substring (34,4);
170  string classH = s.Substring(39,2);
171 
172  string len = s.Substring(72,5);
173  float[] vect = new float[4];
174  float initres = float.Parse (initr);
175  float termres = float.Parse (termr);
176  float length = float.Parse (len);
177  float classhelix = 1f;
178  try{
179  classhelix = float.Parse (classH);
180  }catch{
181  classhelix = float.Parse (s.Substring(38,2));
182  }
183  vect[0] = initres;
184  vect[1] = termres;
185  vect[2] = length;
186  vect[3] = classhelix;
187  sshelixlist.Add (vect);
188  MoleculeModel.helixChainList.Add (chainh);
189  }
190 
191  if(isSheetLine){
192  string chainS = s.Substring (21, 2).Trim ();
193  string initr = s.Substring (23, 4);
194  string termr = s.Substring (34, 4);
195  float[] vect = new float[2];
196  vect[0] = float.Parse (initr);
197  vect[1] = float.Parse (termr);
198  sssheetlist.Add (vect);
199  MoleculeModel.strandChainList.Add (chainS);
200  }
201 
202  if(UIData.readHetAtom)
203  isAtomLine = isAtomLine || s.StartsWith("HETATM");
204 
205  if(!UIData.readWater){
206  try{
207  if ((string) s.Substring(17,3).Trim() == "HOH")
208  isAtomLine = false;
209  }catch{
210  isAtomLine = false;
211  }
212  }
213 
214  if(isAtomLine) {
215  float[] vect=new float[4];
216  //Aurélien : reading the atom's index
217  string sind = s.Substring(7,5);
218  MoleculeModel.atomHetTypeList.Add(s.Split(' ')[0]);
219  string sx=s.Substring(30,8);
220  string sy=s.Substring(38,8);
221  string sz=s.Substring(46,8);
222  string sbfactor = null;
223  bool parseBFactor = false;
224  if (s.Length > 60) {
225  sbfactor = s.Substring(60,6);
226  parseBFactor = true;
227  }
228  string atomsNumber = s.Substring(6,5);
229  string typestring=s.Substring(12,4).Trim();
230  atomsNameList.Add(typestring);
231  int bout;
232  bool b = int.TryParse(typestring[0].ToString(), out bout);
233  string type;
234  if(b)
235  type=typestring[1].ToString();
236  else
237  type=typestring[0].ToString();
238 
239  string resname=s.Substring(17,3).Trim();
240  int resid = int.Parse(s.Substring(22,4));
241  residueIds.Add(resid);
242  currentRes = resid;
243  atomsNumberList.Add (int.Parse(atomsNumber));
244 
245 
246  //Unity has a left-handed coordinates system while PDBs are right-handed
247  //So we have to reverse the X coordinates
248  float x=-float.Parse(sx);
249  float y=float.Parse(sy);
250  float z=float.Parse(sz);
251  //Aurélien : parsing the atom's index
252  float ind = float.Parse(sind);
253  float bfactor = 0.0f;
254  if (parseBFactor == true)
255  {
256  bfactor= float.Parse(sbfactor);
257  }
258  vect[0]=x;
259  vect[1]=y;
260  vect[2]=z;
261  //Aurélien : stocking the atom's index
262  vect[3]=ind;
263 
264  //CA case
265  if(typestring[0].ToString()=="C" && typestring.Length>1) {
266  if(typestring[1].ToString()=="A") {
267  string chaintype = s.Substring(21,1);
268  calist.Add(vect);
269  caChainlist.Add(chaintype);
270  }
271  }
272 
273 
274  //??
275  if(dnaBackboneAtoms.Contains(typestring)) {
276  string chaintype = s.Substring(21,1);
277  calist.Add(vect);
278  caChainlist.Add(chaintype);
279  }
280 
281  if(s.Substring(21,1) != " "){
282  string chain = s.Substring(21,1);
283  chainList.Add(chain);
284 
285  }
286 
287  if (parseBFactor == true)
288  {
289  BFactorList.Add(bfactor);
290  }
291  alist.Add(vect);
292  AtomModel aModel;
293  if (UIData.ffType == UIData.FFType.atomic)
294  {
295  aModel = AtomModel.GetModel(type);
296  }
297  else
298  {
299  aModel = AtomModel.GetModel (typestring);
300  }
301 
302  if(aModel == null) aModel = AtomModel.GetModel("X");
303  typelist.Add(aModel);
304  if (MoleculeModel.sugarResname.Contains(resname))
305  MoleculeModel.atomsSugarTypelist.Add(aModel);
306 
307 
308  if (UIData.ffType == UIData.FFType.atomic)
309  {
310  // Debug.Log ("%%%%%% FFType atomic");
311  colorList.Add(MoleculeModel.GetAtomColor(type));
312  }
313  else
314  {
315  // Debug.Log ("%%%%%% FFType not atomic");
316  colorList.Add(aModel.baseColor);
317  }
318 
319  resnamelist.Add(resname);
320 
321  if(MoleculeModel.residues.ContainsKey(resid) == false){
322  MoleculeModel.residues.Add(resid, new ArrayList());
323  //add the chain name for each residu
324  MoleculeModel.resChainList.Add (s.Substring(21,1));
325  //If we have a sugar, we add theses informations in some special list
326  if (MoleculeModel.sugarResname.Contains(resname))
327  MoleculeModel.resSugarChainList.Add (s.Substring(21,1));
328  }
329 
330  int curresnb = int.Parse (s.Substring(22,4));
331  if(prevresnb == 0 && curresnb != 0)
332  MoleculeModel.firstresnb = curresnb;
333  if(curresnb != prevresnb){
334  MoleculeModel.resChainList2.Add (s.Substring (21,1));
335  prevresnb = curresnb;
336  }
337 
338  MoleculeModel.residues[resid].Add(nbAtoms);
339 
340  if(prevRes != currentRes)
341  resNb++;
342 
343  prevRes = currentRes;
344 
345  nbAtoms ++;
346 
347  //If we have a sugar, we add theses informations in some special list
348  if (MoleculeModel.sugarResname.Contains(resname)){
350  MoleculeModel.atomsSugarResnamelist.Add (resname);
351  MoleculeModel.atomsSugarNamelist.Add(typestring);
352  }
353 
354  }
355 
356  if (isConnectLine){
358  string[] splitedStringTemp = s.Split(' '); //0 is Connect, 1 is the atom, 2,3..... is the bounded atoms
359  List<string> splitedString = new List<string>();
360  for (int j=0; j<splitedStringTemp.Length; j++){
361  if (splitedStringTemp[j]!="")
362  splitedString.Add(splitedStringTemp[j]);
363  }
364  for (int j=2; j<splitedString.Count; j++){
365  MoleculeModel.BondListFromPDB.Add(new int[2]{int.Parse(splitedString[1])-1, int.Parse(splitedString[j])-1});
366  }
367  }
368  }
369 
370  nowline++;
371  }
372  }
373 
374  Ribbons.mustSplitDictList = (nbTers > 1);
375 
376  for(int i = 0; i < typelist.Count; i++)
378 
379  foreach(string name in atomsNameList)
380  if(!MoleculeModel.existingName.Contains(name))
381  MoleculeModel.existingName.Add(name);
382 
383  // existingRes is created in DisplayMolecule.CheckResidues after looking for protonated HIS
384 
385  foreach(string chain in chainList)
386  if(!MoleculeModel.existingChain.Contains(chain))
387  MoleculeModel.existingChain.Add(chain);
388 
391 
392  sr.Close ();
393  // Debug.Log("typelist:"+typelist.Count);
394  // Debug.Log("resnameList:"+resnamelist.Count);
395  // Debug.Log("caChainlist:"+caChainlist.Count);
396  // Debug.Log("chainlist:"+chainList.Count);
397  // Debug.Log("colorList:"+colorList.Count);
398  // Debug.Log ("atomsLocalScaleList:" + MoleculeModel.atomsLocalScaleList.Count);
399  // Debug.Log ("BfactorList: " + BFactorList.Count);
400  if(resnamelist.Count == typelist.Count)
401  UIData.hasResidues = true;
402  if(chainList.Count == typelist.Count)
403  UIData.hasChains = true;
404 
407  MoleculeModel.CaSplineChainList = caChainlist;
408  MoleculeModel.atomsTypelist = typelist;
409  MoleculeModel.atomsNamelist = atomsNameList;
410  MoleculeModel.atomsNumberList = atomsNumberList;
411  MoleculeModel.BFactorList = BFactorList;
412  MoleculeModel.atomsResnamelist = resnamelist;
413  MoleculeModel.residueIds = residueIds;
414  MoleculeModel.splits = splits;
415  MoleculeModel.atomsChainList = chainList;
416  MoleculeModel.atomsColorList = colorList;
417  MoleculeModel.ssHelixList = sshelixlist;
418  MoleculeModel.ssStrandList = sssheetlist;
419  //MoleculeModel.residueDictionaries = residueDictList;
420 
421  //Here we will fill the sortedIndex residue by the Chain ID
422  // MoleculeModel.sortedResIndexByList = sortResIndex (MoleculeModel.resChainList);
423  // MoleculeModel.sortedResIndexByListSugar = sortResIndex (MoleculeModel.resSugarChainList);
424 
425  UIData.isFileLoaded = true;
426 
427 
428  }
429  public static void LoadJsonRequest(string file_name, Vector3 Offset) {
430  StreamReader sr = new StreamReader(file_name);
431  string text;
432  using (sr)
433  {
434  text = sr.ReadToEnd();
435  }
436 
438  MoleculeModel.FieldLineList = readjson.ReadFile(text,Offset);
439 
440  }
441 
442  //Move this method to the related class
443  public static void LoadDxRequest(string file_name, Vector3 Offset) { // function Loading DX file
444 
445 // ReadDX readdx = new ReadDX();
446 // Debug.Log("time avant: "+ DateTime.Now);
447 // DateTime temp = DateTime.Now;
448 // readdx.getDX(file_name);
449 // Debug.Log("time : apres"+ (DateTime.Now-temp));
450 // temp = DateTime.Now;
451 //
454 //
455 // // Calcul et affichage des ligne de champs7
457 //
458 //
460 // readdx.isoSurface(10f);
461 // Debug.Log("time : surface "+ (DateTime.Now-temp));
462  // lancement de la transformation du pdb en density
463 // temp = DateTime.Now;
464 
465  }
466  //TODO put molecule offset here
467  public static void LoadOBJRequest(string file_base_name){
468 
469  Debug.Log("name:"+file_base_name+".obj");
470 
471 
472  string path = file_base_name+".obj";
473 // path = "file:/"+path;
474  // Debug.Log("file complet :"+ path);
475 // string path = "http://www.everyday3d/unity3d/obj/monkey.obj";
476 
477 
478  FileInfo file=new FileInfo(file_base_name+".obj");
479 
480  if ( file.Exists){
481 // ReadOBJ readObj =new ReadOBJ();
482 // readObj.GetOBJ(url,id);
483 // OBJ obj = Camera.main.GetComponent<OBJ>(path);
484 // var go = new GameObject("Your Script Container");
485 // var obj = go.AddComponent<OBJ>();
486  // THIS PRODUCES A WARNING:: You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
487  OBJ obj = new OBJ(path);
488  Debug.Log("Generate new OBJ from "+path);
489  obj.Load();
490 // Debug.Log("Coroutine Started");$
491  }
492  else {
493  for (int i =0;i< 6;i++){
494  file=new FileInfo(file_base_name+i+".obj");
495  if (file.Exists){
496  path = file_base_name+i+".obj";
497  OBJ obj = new OBJ(path);
498  Debug.Log("new OBJ");
499  obj.Load();
500  }
501  }
502  }
503  }
504 
505 
506 }
static void LoadDxRequest(string file_name, Vector3 Offset)
Definition: ParsePDB.cs:443
static void InitHiRERNA()
Definition: AtomModel.cs:227
static List< string > resChainList2
The chain of each residue.
static List< int[]> BondListFromPDB
static List< string > existingName
List of the names existing in the molecule.
static List< string > atomHetTypeList
static void ReadPDB2(TextReader sr)
Definition: ParsePDB.cs:109
static AtomModel GetModel(string type)
Definition: AtomModel.cs:257
static bool isFileLoaded
Definition: UIData.cs:115
static List< string > atomsSugarNamelist
static Vector3 Offset
The offset for the molecule.
static FFType ffType
Definition: UIData.cs:241
static void LoadPDBRequest2(string file_base_name, bool withData=true)
Definition: ParsePDB.cs:56
static List< string > atomsNamelist
The name of each atom.
static List< string > atomsChainList
The chain of each atom.
static List< AtomModel > atomsTypelist
The type of each atom.
static List< string > helixChainList
The helix chain list (extract from the pdb).
static List< float[]> atomsSugarLocationlist
static List< int > residueIds
The residue identifiers.
static List< float[]> CatomsLocationlist
The coordinates of each Carbon alpha.
static List< float > BFactorList
Bfactor of each atom.
static List< int > splits
Terminal residue number of each subunits.
static Color GetAtomColor(string atomType)
void Load()
Definition: OBJ.cs:149
static bool loadHireRNA
Definition: UIData.cs:240
static bool readWater
Definition: UIData.cs:111
static bool mustSplitDictList
Definition: Ribbons.cs:7
static bool hasChains
Definition: UIData.cs:98
static List< float[]> ssStrandList
First and last residue of each strand (extract from the pdb) float[0] is the first residue of each st...
static void LoadJsonRequest(string file_name, Vector3 Offset)
Definition: ParsePDB.cs:429
static bool hasResidues
Definition: UIData.cs:97
static List< AtomModel > atomsSugarTypelist
static List< float[]> ssHelixList
List of informations about each helix (extract from the pdb) float[0] is the first residue of each he...
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
static List< string > CaSplineChainList
The chain of each carbon alpha in the CA-Spline.
static List< Color > atomsColorList
The color of each atom.
static Dictionary< int, ArrayList > residues
The residues.
Definition: OBJ.cs:84
static List< List< Vector3 > > FieldLineList
static List< string > existingChain
List of the chains existing in the molecule.
static void InitAtomic()
Inits the atomic and chains scales.
Definition: AtomModel.cs:180
static List< int > atomsNumberList
The number of each atoms (in the PDB file)
static List< string > resSugarChainList
static List< string > atomsResnamelist
The name of the residue to which each atom belongs.
static List< string > resChainList
The chain of each residue (only work if residues are numbered by chain).
static int firstresnb
First residue number in pdb.
static bool readHetAtom
Definition: UIData.cs:110
static List< string > strandChainList
The strand chain list (extract from the pdb).
static List< string > sugarResname
static void LoadOBJRequest(string file_base_name)
Definition: ParsePDB.cs:467
static void FetchPDB2(string url, string id, string proxyserver="", int proxyport=0)
Definition: ParsePDB.cs:13
static bool connectivity_PDB
Definition: UIData.cs:113
Definition: GUIDisplay.cs:66
static List< float > atomsLocalScaleList
static List< string > atomsSugarResnamelist
static List< float[]> atomsLocationlist
The coordinates of each atom.