Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
rcsb.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # :noTabs=true:
3 
4 
5 # (c) Copyright Rosetta Commons Member Institutions.
6 # (c) This file is part of the Rosetta software suite and is made available under license.
7 # (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
8 # (c) For more information, see http://www.rosettacommons.org. Questions about this can be
9 # (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
10 
11 ## @file rcsb.py
12 ## @brief
13 ## @author Evan H. Baugh, Johns Hopkins University
14 
15 import os
16 import urllib
17 
18 from rosetta import Pose
19 from rosetta import pose_from_pdb
20 
21 # other tools
22 from cleaning import cleanATOM
23 from cleaning import cleanCRYS
24 
25 # retreives pdbData from rcsb for <pdb_code>
26 # ADD NAMING OPTION
27 def load_from_rcsb( pdb_code , pdb_outfile = '' ):
28  """
29  Writes PDB data for RCSB data for <pdb_code> into the file <pdb_code>.pdb
30 
31  example:
32  load_from_rcsb('1YY8')
33  See also:
34  Pose
35  pose_from_pdb
36  pose_from_rcsb
37  pose_from_sequence
38  cleanATOM
39  cleanCRYS
40  """
41  if pdb_code: # if something input...
42  pdb_code = pdb_code.upper()
43  try:
44  filename = urllib.urlretrieve('http://www.rcsb.org/pdb/files/' + pdb_code + '.pdb')[0]
45  except:
46  raise IOError('Cannot access the PDB database, please check your Internet access')
47  else:
48  if (os.path.getsize(filename) > 1500): # arbitrary 1500...then pdb_code was invalid
49  # load in the data
50  pdb_file = open(filename)
51  pdb_data = pdb_file.readlines()
52  pdb_file.close()
53 
54  # setup proper naming
55  pdb_code = pdb_code + '.pdb'
56  if not pdb_outfile:
57  # default the name to the <pdb_code>.pdb
58  pdb_outfile = pdb_code
59  if os.path.exists( os.getcwd() + '/' + pdb_outfile ):
60  print 'the file',pdb_outfile,'already exists, this file will be overwritten'
61  #if input('Do you want to overwrite ' + pdbCode + '.pdb')
62  pdb_file = open(pdb_outfile,'w')
63  pdb_file.writelines(pdb_data)
64  pdb_file.close()
65 
66  print 'PDB',pdb_code[:-4],'successfully loaded from rcsb into',pdb_outfile
67 # if auto_clean:
68 # cleanATOM(pdb_code)
69  else:
70  raise IOError('Invalid PDB code')
71  os.remove(filename) # remove temp file
72 
73 # packaged my method to fit naming
74 def pose_from_rcsb( pdb_code , ATOM = True , CRYS = False , pdb_outfile = '' ):
75  """
76  Returns a pose for RCSB PDB <pdb_code> , also writes this data to
77  <pdb_code>.pdb, optionally calls cleanATOM and cleanCYRS
78 
79  example:
80  pose=pose_from_rcsb('1YY8')
81  See also:
82  Pose
83  pose_from_pdb
84  pose_from_sequence
85  load_from_rcsb
86  cleanATOM
87  cleanCRYS
88  """
89  load_from_rcsb(pdb_code,pdb_outfile)
90  # ensure the names are proper
91  edit = -4
92  if not pdb_outfile:
93  pdb_outfile = pdb_code + '.pdb'
94  else:
95  edit = 0
96 # if not pdb_outfile[:-4]=='.pdb':
97 # pdb_outfile = pdb_outfile
98  # cleaning calls
99  if ATOM:
100  cleanATOM(pdb_outfile,edit=edit)
101  pdb_outfile = pdb_outfile[:edit]+'.clean.pdb'
102  if CRYS:
103  cleanCRYS(pdb_outfile)
104  pdb_outfile = pdb_outfile[:edit]+'.mono.pdb'
105  pose = pose_from_pdb(pdb_outfile)
106  return pose
107 
108 # retreives pdbData from rcsb for <pdb_code>
109 # ADD NAMING OPTION
110 def load_fasta_from_rcsb( pdb_code , fasta_outfile ):
111  if pdb_code: # if something input...
112  pdb_code = pdb_code.upper()
113  try:
114  filename = urllib.urlretrieve('http://www.rcsb.org/pdb/files/fasta.txt?structureIdList=' + pdb_code)[0]
115  except:
116  raise IOError('Cannot access the PDB database, please check your Internet access')
117  else:
118  if (os.path.getsize(filename)): # arbitrary 1500...then pdb_code was invalid
119  pdb_file = open(filename)
120  pdb_data = pdb_file.readlines()
121  pdb_file.close()
122 
123 # pdb_code = pdb_code + '.fa'
124  if not fasta_outfile:
125  fasta_outfile = pdb_code + '.fa'
126  if os.path.exists( os.getcwd() + '/' + fasta_outfile ):
127  print 'the file',fasta_outfile,'already exists, this file will be overwritten'
128  #if input('Do you want to overwrite ' + pdbCode + '.pdb')
129  pdb_file = open(fasta_outfile,'w')
130  pdb_file.writelines(pdb_data)
131  pdb_file.close()
132 
133  print 'PDB',pdb_code,'sequence successfully loaded from rcsb into',fasta_outfile
134  else:
135  raise IOError('Invalid PDB code')
136  os.remove(filename) # remove temp file
137 
138 
def pose_from_rcsb
Definition: rcsb.py:74
def cleanATOM
Definition: __init__.py:160
def cleanCRYS
Definition: __init__.py:193
def load_fasta_from_rcsb
Definition: rcsb.py:110
bool open(utility::io::izstream &db_stream, std::string const &db_file, bool warn)
Open a database file on a provided stream.
Definition: open.cc:55
def load_from_rcsb
Definition: rcsb.py:27