def line2tertiarystackDT(line): it = iter(line.split()) num = len(line.split()) if num != 13: raise MyError("func_line2ninfo", "line2tertiarystackDT", "") if next(it) != 'tbs-dist': raise MyError("func_line2ninfo", "line2tertiarystackDT", "This line is not tbs-dist") info = TertiaryStackDT() info.id = int(next(it)) info.iunit1 = int(next(it)) info.iunit2 = int(next(it)) info.imp1 = int(next(it)) info.imp2 = int(next(it)) info.imp1un = int(next(it)) info.imp2un = int(next(it)) info.factor = float(next(it)) info.native = float(next(it)) info.coef = float(next(it)) info.excess1 = int(next(it)) info.excess2 = int(next(it)) info.ang1_id = None info.ang2_id = None info.dih0_id = None info.dih1_id = None info.dih2_id = None return info
def line2dihedral(line): it = iter(line.split()) num = len(line.split()) if num != 17 and num != 18: raise MyError("func_line2ninfo", "line2dihedral", "") if next(it) != 'dihd': raise MyError("func_line2ninfo", "line2dihedral", "This line is not dihedral.") info = Dihedral() info.id = int(next(it)) info.iunit1 = int(next(it)) info.iunit2 = int(next(it)) info.imp1 = int(next(it)) info.imp2 = int(next(it)) info.imp3 = int(next(it)) info.imp4 = int(next(it)) info.imp1un = int(next(it)) info.imp2un = int(next(it)) info.imp3un = int(next(it)) info.imp4un = int(next(it)) info.native = float(next(it)) info.factor = float(next(it)) info.correct_mgo = float(next(it)) info.coef = float(next(it)) info.coef_3 = float(next(it)) if num == 18: info.type = next(it) return info
def read_header(self): if not self._file: raise MyError('DcdFile', 'read_header', 'Logical: _file is None') self._file.seek(0) # Read kind num1, num2 = struct.unpack('ii', self._file.read(8)) if num1 == 2: self._int_kind = 2 int_kind_str = 'h' elif num1 == 4: self._int_kind = 4 int_kind_str = 'i' elif num1 == 8: self._int_kind = 8 int_kind_str = 'q' else: raise MyError('SisbpFile', 'read_header', 'unknown int kind type: ' + '%i' % num1) if num2 == 4: self._real_kind = 4 real_kind_str = 'f' elif num2 == 8: self._real_kind = 8 real_kind_str = 'd' else: raise MyError('SisbpFile', 'read_header', 'unknown real kind type: ' + '%i' % num2) self._step_byte = 2 * self._int_kind + self._real_kind self._kind_str = int_kind_str + int_kind_str + real_kind_str self._seek_data = self._file.tell()
def line2bondangle(line): it = iter(line.split()) num = len(line.split()) if num != 14 and num != 15: raise MyError("func_line2ninfo", "line2bondangle", "") if next(it) != 'angl': raise MyError("func_line2ninfo", "line2bondangle", "This line is not bondangle.") info = BondAngle() info.id = int(next(it)) info.iunit1 = int(next(it)) info.iunit2 = int(next(it)) info.imp1 = int(next(it)) info.imp2 = int(next(it)) info.imp3 = int(next(it)) info.imp1un = int(next(it)) info.imp2un = int(next(it)) info.imp3un = int(next(it)) info.native = float(next(it)) info.factor = float(next(it)) info.correct_mgo = float(next(it)) info.coef = float(next(it)) if num == 15: info.type = next(it) return info
def line2basestackDT(line, fmt=2): if fmt == 1: it = iter(line.split()) num = len(line.split()) if num != 12: raise MyError("func_line2ninfo", "line2basestackDT", "") if next(it) != 'bs-dist': raise MyError("func_line2ninfo", "line2basestackDT", "This line is not bs-dist") info = BaseStackDT() info.id = int(next(it)) info.iunit1 = int(next(it)) info.iunit2 = int(next(it)) info.imp1 = int(next(it)) info.imp2 = int(next(it)) info.imp1un = int(next(it)) info.imp2un = int(next(it)) info.factor = float(next(it)) info.native = float(next(it)) info.coef = float(next(it)) info.type = next(it) info.dih1_id = None info.dih2_id = None return info elif fmt == 2: it = iter(line.split()) num = len(line.split()) if num != 14: raise MyError("func_line2ninfo", "line2basestackDT", "num!=14") if next(it) != 'bs-dist': raise MyError("func_line2ninfo", "line2basestackDT", "This line is not bs-dist") info = BaseStackDT() info.id = int(next(it)) info.iunit1 = int(next(it)) info.iunit2 = int(next(it)) info.imp1 = int(next(it)) info.imp2 = int(next(it)) info.imp1un = int(next(it)) info.imp2un = int(next(it)) info.h = float(next(it)) info.s = float(next(it)) info.Tm = float(next(it)) info.native = float(next(it)) info.coef = float(next(it)) info.type = next(it) info.dih1_id = None info.dih2_id = None return info else: raise MyError("func_line2ninfo", "line2basestackDT", "unknown fmt")
def read_header(self): ''' store lines to self.header_lines store information to self.head_str and self.head_col ''' if not self._file : raise MyError('TsFile', 'read_header', 'Logical: _file is None') self._file.seek(0) # skip 5 lines for i in range(5): self.header_lines.append( self._file.readline() ) # read header self.header_lines.append( self._file.readline() ) line = self._file.readline() self.header_lines.append( line ) self.head_str = line.split()[1:] self.head_col = self._head_str2col(self.head_str) # skip 2 lines self.header_lines.append( self._file.readline() ) self.header_lines.append( self._file.readline() ) #### Count the number of units #### # skip the first line of step 0 self._file.readline() if self._file.readline().split()[0] == '#all': self.flg_all = True #raise MyError('TsFile','read_header','Bad format: #all') self.num_unit = 0 for l in self._file: if l[0] == ' ': break if l.split()[1][0:1] == '-': ''' unit-unit lines exist''' self.flg_u_u = True break self.num_unit = int(l.split()[0][1:]) if self.num_unit < 0: raise MyError('TsFile','read_header','Bad format: num_unit <= 0') ########################################### # skip 9 lines self._file.seek(0) for i in range(9): self._file.readline()
def _line2atom(self, line): line = line.rstrip() if line[0:6] != 'ATOM ' and line[0:6] != 'HETATM' : raise MyError('PdbFile', 'decompose_atom', 'line does not begin with "ATOM"') atom = Atom() atom.serial = int(line[6:11]) atom.name = line[12:16] atom.alt_loc = line[16:17] atom.res_name = line[17:20] atom.chain_id = line[21:22] atom.res_seq = int(line[22:26]) #atom.ins_code = line[26:27] atom.ins_code = line[26:30] atom.xyz = Coord(float(line[30:38]), float(line[38:46]), float(line[46:54])) try : atom.occupancy = float(line[54:60]) except : atom.occupancy = 0.0 try : atom.temp_factor = float(line[60:66]) except : atom.temp_factor = 0.0 atom.element = line[76:78] try : atom.charge = int(line[78:80]) except : atom.charge = 0 return atom
def line2hbondDT(line): it = iter(line.split()) num = len(line.split()) if num < 11: raise MyError("func_line2ninfo", "line2hbondDT", "") if next(it) != 'hb-dist': raise MyError("func_line2ninfo", "line2hbondDT", "This line is not hb-dist") info = HBondDT() info.id = int(next(it)) info.iunit1 = int(next(it)) info.iunit2 = int(next(it)) info.imp1 = int(next(it)) info.imp2 = int(next(it)) info.imp1un = int(next(it)) info.imp2un = int(next(it)) info.factor = float(next(it)) info.native = float(next(it)) info.coef = float(next(it)) if num > 11: info.sectert = next(it) info.nHB = int(next(it)) info.atoms1 = [] info.atoms2 = [] try: while (True): a = next(it) info.atoms1.append(a) a = next(it) info.atoms2.append(a) except: pass else: info.sectert = None info.nHB = None info.ang1_id = None info.ang2_id = None info.dih0_id = None info.dih1_id = None info.dih2_id = None return info
def write_header(self): if not self._header: raise MyError('DcdFile', 'write_header', 'Logical: _header is None') self._file.seek(0) if self._header.nset is None: self._header.nset = 0 if self._header.istart is None: self._header.istart = 0 if self._header.nstep_save is None: self._header.nstep_save = 0 if self._header.nstep is None: self._header.nstep = 0 if self._header.nunit_real is None: self._header.nunit_real = 0 if self._header.delta is None: self._header.delta = 0.0 if self._header.tempk is None: self._header.tempk = 0.0 if self._header.lunit2mp is None: self._header.lunit2mp = [] #first block form = '4siii5iifi9i' binary = struct.pack(form, b'CORD', self._header.nset, self._header.istart, self._header.nstep_save, self._header.nstep, self._header.nunit_real, 0, 0, 0, 0, self._header.delta, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) self._put_data(binary, struct.calcsize(form)) #title block binary = struct.pack('i' + '80s' * 2, (3 + len(self._header.lunit2mp)), self._header.title[0], self._header.title[1]) import re re_null = re.compile(b'\0') p = struct.pack('80s', bytes('%f' % self._header.tempk, 'utf-8')) binary += re_null.sub(b' ', p) for i in range(self._header.nunit_real): p = struct.pack('80s', bytes('%i' % self._header.lunit2mp[i], 'utf-8')) binary += re_null.sub(b' ', p) form = 'i' + '80s' * (3 + self._header.nunit_real) self._put_data(binary, struct.calcsize(form)) # nmp_real binary = struct.pack('i', self._header.nmp_real) self._put_data(binary, 4)
def line2basestack(line): it = iter(line.split()) num = len(line.split()) if num != 13: raise MyError("func_line2ninfo", "line2basestack", "") if next(it) != 'basestack': raise MyError("func_line2ninfo", "line2basestack", "This line is not basestack") info = BaseStack() info.id = int(next(it)) info.iunit1 = int(next(it)) info.iunit2 = int(next(it)) info.imp1 = int(next(it)) info.imp2 = int(next(it)) info.imp1un = int(next(it)) info.imp2un = int(next(it)) info.native = float(next(it)) info.factor = float(next(it)) info.dummy = int(next(it)) info.coef = float(next(it)) info.type = next(it) return info
def line2contact(line): it = iter(line.split()) num = len(line.split()) if num != 12 and num != 13: raise MyError("func_line2ninfo", "line2contact", "") if next(it) != 'contact': raise MyError("func_line2ninfo", "line2contact", "This line is not contact.") info = Contact() info.id = int(next(it)) info.iunit1 = int(next(it)) info.iunit2 = int(next(it)) info.imp1 = int(next(it)) info.imp2 = int(next(it)) info.imp1un = int(next(it)) info.imp2un = int(next(it)) info.native = float(next(it)) info.factor = float(next(it)) info.dummy = int(next(it)) info.coef = float(next(it)) if num == 13: info.type = next(it) return info
def line2bondlength(line): it = iter(line.split()) num = len(line.split()) if num != 12 and num != 13: raise MyError("func_line2ninfo", "line2bondlength", "") if next(it) != 'bond': raise MyError("func_line2ninfo", "line2bondlength", "This line is not bondlength.") info = BondLength() info.id = int(next(it)) info.iunit1 = int(next(it)) info.iunit2 = int(next(it)) info.imp1 = int(next(it)) info.imp2 = int(next(it)) info.imp1un = int(next(it)) info.imp2un = int(next(it)) info.native = float(next(it)) info.factor = float(next(it)) info.correct_mgo = float(next(it)) info.coef = float(next(it)) if num == 13: info.type = next(it) return info
def __init__(self, filename, openmode=None, flg_HETATM=False): self._filename = filename self._status = 'Closed' self.flg_HETATM = flg_HETATM self.modelID = None self.remark = None if openmode is not None: if openmode == 'r': self.open_to_read() elif openmode == 'w': self.open_to_write() else: print("openmode has to be either 'r' or 'w'") raise MyError('PdbFile', '__init__', "openmode has to be either 'r' or 'w'")
def read_header(self): if not self._file: raise MyError('DridFile', 'read_header', 'Logical: _file is None') self._file.seek(0) # title block (number of line can be changed) b = self._pick_data() len_title = struct.unpack('i', b)[0] b = self._pick_data() self._header.title = struct.unpack('%ds' % (len_title, ), b)[0] self._header.version = int(self._header.title[3:]) if self._header.version != 2: print('Caution: This is not DRID ver.2 format!!') print(self._header.title) if self._header.version == 1: # centroids b = self._pick_data() n_centroids = struct.unpack('i', b)[0] b = self._pick_data() self._header.centroids = struct.unpack('i' * n_centroids, b) # atoms b = self._pick_data() n_atoms = struct.unpack('i', b)[0] b = self._pick_data() self._header.atoms = struct.unpack('i' * n_atoms, b) # Mask (ver >= 2) if self._header.version >= 2: b = self._pick_data() n_centroids, n_atoms = struct.unpack('ii', b) self._header.mask = np.empty((n_centroids, n_atoms)) for ic in range(n_centroids): b = self._pick_data() self._header.mask[ic, :] = struct.unpack('i' * n_atoms, b) self._seek_data = self._file.tell()
def write_header(self): if not self._header: raise MyError('DcdFile', 'write_header', 'Logical: _header is None') self._file.seek(0) # title block binary = struct.pack('i', len(self._header.title)) self._put_data(binary, 4) form = '%is' % (len(self._header.title), ) binary = struct.pack(form, self._header.title) self._put_data(binary, struct.calcsize(form)) if self._header.version == 1: # centroids binary = struct.pack('i', len(self._header.centroids)) self._put_data(binary, 4) binary = struct.pack('i' * len(self._header.centroids), *(self._header.centroids)) self._put_data(binary, 4 * len(self._header.centroids)) # atoms binary = struct.pack('i', len(self._header.atoms)) self._put_data(binary, 4) binary = struct.pack('i' * len(self._header.atoms), *(self._header.atoms)) self._put_data(binary, 4 * len(self._header.atoms)) # Mask (ver >= 2) if self._header.version >= 2: # Dimensions (# of centroids, # of atoms) form = 'ii' binary = struct.pack(form, self._header.mask.shape[0], self._header.mask.shape[1]) self._put_data(binary, struct.calcsize(form)) for ic in range(self._header.n_centroids()): form = '%ii' % self._header.mask.shape[1] binary = struct.pack(form, *(self._header.mask[ic, :])) self._put_data(binary, struct.calcsize(form))
def open_to_write(self): if self._status != 'Closed': raise MyError('PsfFile', 'open_for_read', 'file is not closed') self._file = open(self._filename, 'w')
def read_header(self): if not self._file: raise MyError('DcdFile', 'read_header', 'Logical: _file is None') ########################################### # Check the format reading title lines ########################################### self._file.seek(0) # Skip the first block b = self._pick_data() # title block b = self._pick_data() size = len(b) nline = (size - 4) // 80 bdata = struct.unpack(('i' + '80s' * nline), b) try: if bdata[1].decode("utf-8").find('Git commit') != -1 or bdata[ 2].decode("utf-8").find('CafeMol') != -1: self._header.format = 'cafemol' else: self._header.format = 'charmm' except: self._header.format = 'charmm' ########################################### # Load header infomation ########################################### self._file.seek(0) if self._header.format == 'cafemol': # first block b = self._pick_data() bdata = struct.unpack('4siii5iifi9i', b) #bdata = struct.unpack('4siii5iid9i',b) if bdata[0].decode("utf-8") != 'CORD': raise MyError('DcdFile', 'read_header', '%s is not coordinate file' % (self._filename, )) self._header.nset = bdata[1] self._header.istart = bdata[2] self._header.nstep_save = bdata[3] self._header.nstep = bdata[4] self._header.nunit_real = bdata[5] self._header.delta = bdata[10] # title block (number of line can be changed) b = self._pick_data() bdata = struct.unpack( ('i' + '80s' * (3 + self._header.nunit_real)), b) self._header.title = (bdata[1], bdata[2]) #self._header.tempk = float(bdata[3].strip('\0 ')) self._header.tempk = float(bdata[3]) self._header.lunit2mp = [] for i in range(self._header.nunit_real): # self._header.lunit2mp.append(int(bdata[i + 4].strip('\0 '))) self._header.lunit2mp.append(int(bdata[i + 4])) # nmp_real b = self._pick_data() self._header.nmp_real = struct.unpack('i', b)[0] elif self._header.format == 'charmm': # first block b = self._pick_data() bdata = struct.unpack('4s20i', b) if (bdata[11] == 1): self._header.with_unit_cell = True #bdata = struct.unpack('4siii5iid9i',b) #if bdata[0] != 'CORD' : # raise MyError('DcdFile', 'read_header', '%s is not coordinate file' % (self._filename,)) #self._header.nset = bdata[1] #self._header.istart = bdata[2] #self._header.nstep_save = bdata[3] #self._header.nstep = bdata[4] #self._header.nunit_real = bdata[5] #self._header.delta = bdata[10] # title block (number of line can be changed) b = self._pick_data() size = len(b) nline = (size - 4) // 80 bdata = struct.unpack(('i' + '80s' * nline), b) self._header.title = [] for i in range(nline): self._header.title.append(bdata[i + 1]) # nmp_real b = self._pick_data() self._header.nmp_real = struct.unpack('i', b)[0] self._seek_data = self._file.tell()
def open_to_read(self): if self._status != 'Closed': raise MyError('NinfoFile', 'open_for_read', 'file is not closed') self._file = open(self._filename, 'r')
def _head_str2col(self, strlist): th = TsHeader() for i, str in enumerate(strlist): if str == 'step': th.step = i elif str == 'tempk': th.temp = i elif str == 'label': th.label = i elif str == 'radg': th.rg = i elif str == 'rmsd': th.rmsd = i elif str == 'qscore': th.q = i # 現状、mgo未対応 #elif str == '': # th.mgo_kai = i #elif str == '': # th.mgo_coef = i #elif str == '': # th.mgo_q = i #elif str == '': # th.mgo_state = i elif str == 'etot': th.e_tot = i elif str == 'velet': th.e_velo = i elif str == 'local': th.e_local = i elif str == 'go': th.e_go = i elif str == 'morse': th.e_morse = i elif str == 'repul': th.e_repul = i elif str == 'solv_dna': th.e_dna_solv = i elif str == 'base': th.e_dna_base = i elif str == 'elect': th.e_ele = i elif str == 'hyd_ion': th.e_ion_hyd = i elif str == 'hp': th.e_hp = i elif str == 'tail': th.e_lip_tail = i elif str == 'int': th.e_lip_int = i elif str == 'solv_lip': th.e_lip_solv = i elif str == 'box': th.e_box = i elif str == 'cap': th.e_cap = i elif str == 'bridge': th.e_bridge = i elif str == 'pulling': th.e_pull = i elif str == 'anchor': th.e_anch = i elif str == 'rest1d': th.e_rest1d = i elif str == 'imp_lig': th.e_implig = i elif str == 'window': th.e_window = i elif str == 'stack': th.e_stack = i elif str == 'hbond': th.e_hbond = i elif str == 'tstack': th.e_tstack = i elif str == 'thbond': th.e_thbond = i else: raise MyError('ts.py','_head_str2col','str=%s' % (str,)) return th