if __name__ == '__main__': # read density map from mrc import mrcfile import iomap as IM rootdir = '../IOfile/map_single' v = IM.readMrcMapDir(rootdir) # get packing info import sys sys.path.append("..") import packing_single_sphere.simulate as SI import numpy as np import packing_single_sphere.simulate as SI target_name = '1bxn' packing_result = SI.packing_with_target(target_protein=target_name, random_protein_number=4) protein_name = packing_result['optimal_result']['pdb_id'] x = packing_result['optimal_result']['x'] / 10 y = packing_result['optimal_result']['y'] / 10 z = packing_result['optimal_result']['z'] / 10 print('initialization', packing_result['optimal_result']['initialization']) x0 = np.array(packing_result['optimal_result']['initialization'][0]) / 10 y0 = np.array(packing_result['optimal_result']['initialization'][1]) / 10 z0 = np.array(packing_result['optimal_result']['initialization'][2]) / 10 box_size = packing_result['general_info']['box_size'] / 10 # merge map to hugemap, save random angle in packing_result import map_tomo.merge_map as MM initmap, init_angle_list = MM.merge_map(v, protein_name, x0, y0, z0, box_size) packmap, pack_angle_list = MM.merge_map(v, protein_name, x, y, z, box_size)
def simu_subtomo(op, packing_op, output, save_tomo=0, save_target=1, save_tomo_slice=0): """ :param op: parameter of convert pdb/ density map to tomogram packing_op: parameter of packing process output: path of output file save_tomo: 1 save, 0 not save, if the large subtomogram.mrc of the whole packing scene(including 5-10 macromolecules) will be saved save_target: 1 save, 0 not save, if the subtomogram.mrc of the target will be saved save_tomo_slice: 1 save, 0 not save, if the sliced image will be saved. :return: target_simu_tomo = {'tomo': subtomogram of target macromolecule, .mrc file 'density_map': density map, .mrc file 'info': {'loc': coordinate of target macromolecule 'rotate': the rotation angle of this macromolecule (ZYZ, Euler angle) 'name': the name of a macromolecule}} the json file of packing result and target will be saved in '../IOfile/json' """ from map_tomo import pdb2map as PM from map_tomo import iomap as IM from map_tomo import mrc2singlepic as MS from map_tomo import merge_map as MM from map_tomo import map2tomogram as MT from packing_single_sphere import simulate as SI # convert pdb to map # ms = PM.pdb2map(op['map']) # for n in ms: # v = ms[n] # IM.map2mrc(v, op['map']['map_single_path']+ '/{}.mrc'.format(n)) # read density map from mrc # rootdir = op['map']['map_single_path'] # v = IM.readMrcMapDir(rootdir) # print('read density map done') v = op['v'] # get packing info target_name = packing_op['target'] print("Name", target_name) packing_result = SI.packing_with_target(packing_op) print("Print", packing_result.keys()) protein_name = packing_result['optimal_result']['pdb_id'] x = packing_result['optimal_result']['x'] / 10 y = packing_result['optimal_result']['y'] / 10 z = packing_result['optimal_result']['z'] / 10 # print('initialization',packing_result['optimal_result']['initialization']) x0 = np.array(packing_result['optimal_result']['initialization'][0]) / 10 y0 = np.array(packing_result['optimal_result']['initialization'][1]) / 10 z0 = np.array(packing_result['optimal_result']['initialization'][2]) / 10 box_size = packing_result['general_info']['box_size'] / 10 print('get packing info done') # merge map to hugemap, save random angle in packing_result initmap, init_angle_list = MM.merge_map(v, protein_name, np.int64(x0), np.int64(y0), np.int64(z0), np.int64(box_size)) packmap, pack_angle_list = MM.merge_map(v, protein_name, np.int64(x), np.int64(y), np.int64(z), np.int64(box_size)) packing_result['optimal_result']['initmap_rotate_angle'] = init_angle_list packing_result['optimal_result']['packmap_rotate_angle'] = pack_angle_list print('merge huge map done ') # save packing info with open(output['json']['pack'], 'w') as f: json.dump(packing_result, f, cls=MM.NumpyEncoder) print('save packing info') # convert packmap to tomogram tomo = MT.map2tomo(packmap, op['tomo']) print('convert to tomo') if save_tomo != 0: # save init & pack map to mrc IM.map2mrc(initmap, output['initmap']['mrc']) IM.map2mrc(packmap, output['packmap']['mrc']) # save init & pack map to png IM.map2png(initmap, output['initmap']['png']) IM.map2png(packmap, output['packmap']['png']) # save pack tomo to mrc and png IM.map2mrc(tomo, output['tomo']['mrc']) IM.map2png(tomo, output['tomo']['png']) # trim hugemap # trim_initmap = MM.trim_margin(initmap) # trim_packmap = MM.trim_margin(packmap) # trim_tomo = MM.trim_margin(tomo) # print('initmap shape',initmap.shape) # print('trimmed shape',trim_initmap.shape) # print('packmap shape',packmap.shape) # print('trimmed shape',trim_packmap.shape) # print('tomo shape',tomo.shape) # print('trimmed shape',trim_tomo.shape) # IM.map2mrc(trim_initmap, output['initmap']['trim']) # IM.map2mrc(trim_packmap, output['packmap']['trim']) # IM.map2mrc(trim_tomo, output['tomo']['trim']) # trim target i = protein_name.index(target_name) # print('i',i) target_packmap, loc_r = MM.trim_target(packmap, np.array([x[i], y[i], z[i]]), op['target_size']) target_tomo, loc_r = MM.trim_target(tomo, np.array([x[i], y[i], z[i]]), op['target_size'], loc_r) if save_target != 0: IM.map2mrc(target_packmap, output['packmap']['target']['mrc']) IM.map2mrc(target_tomo, output['tomo']['target']['mrc']) IM.map2png(target_packmap, output['packmap']['target']['png']) IM.map2png(target_tomo, output['tomo']['target']['png']) print('trim target done') # save target info target_info = dict() target_info['loc'] = loc_r target_info['rotate'] = pack_angle_list[i] target_info['name'] = packing_op['target'] with open(output['json']['target'], 'w') as f: json.dump(target_info, f, cls=MM.NumpyEncoder) print('get target info done') if save_tomo_slice != 0: # convert packmap & tomo to separate pictures MS.mrc2singlepic(output['packmap']['mrc'], output['packmap']['png'] + 'packmap{}/'.format(num), 'packmap{}'.format(num)) MS.mrc2singlepic(output['tomo']['mrc'], output['tomo']['png'] + 'tomo{}/'.format(num), 'tomo{}'.format(num)) target_simu_tomo = dict() target_simu_tomo['tomo'] = target_tomo target_simu_tomo['density_map'] = target_packmap target_simu_tomo['info'] = target_info print('all done') return target_simu_tomo
def simu_subtomo(op, packing_op, output, save_tomo=0, save_target=1, save_tomo_slice=0): ''' :param op: parameter of convert pdb/ density map to tomogram :param packing_op: parameter of packing process :param output: path of output file :param save_tomget: 1 save, 0 not save, if the subtomogram.mrc of the target will be saved :param save_tomo_slice: 1 save, 0 not save, if the sliced image will be saved. :return: target_simu_tomo = { 'tomo' : subtomogram of target macromolecule, .mrc file 'density_map': density map, .mrc file 'info': { 'loc' : coordinate of target macromolecule 'rotate' : the rotation angle of this macromolecule (ZYZ, Euler angle) 'name' : the name of a macromolecule } } the json file of packing result and target will be saved in '../IOfile/json' ''' import datetime starttime = datetime.datetime.now() # # convert pdb to map # # If you already have the map to each single macromolecule, # # then change the op['map']['map_single_path'] to the path of the single density map. # # If you need to generate map yourself, please uncomment line 89-99 and comment line 100. # import map_tomo.pdb2map as PM # ms = PM.pdb2map(op['map']) # for n in ms: # v = ms[n] # IM.map2mrc(v, op['map']['map_single_path']+ '/{}.mrc'.format(n)) # # # read density map from mrc # rootdir = op['map']['map_single_path'] # v = IM.readMrcMapDir(rootdir) # print('read density map done') v = op['v'] # get packing info import packing_single_sphere.simulate as SI target_name = packing_op['target'] packing_result = SI.packing_with_target(packing_op) protein_name = packing_result['optimal_result']['pdb_id'] x = packing_result['optimal_result']['x'] / 10 y = packing_result['optimal_result']['y'] / 10 z = packing_result['optimal_result']['z'] / 10 # print('initialization',packing_result['optimal_result']['initialization']) x0 = np.array(packing_result['optimal_result']['initialization'][0]) / 10 y0 = np.array(packing_result['optimal_result']['initialization'][1]) / 10 z0 = np.array(packing_result['optimal_result']['initialization'][2]) / 10 box_size = packing_result['general_info']['box_size'] / 10 print('get packing info done') # merge map to hugemap, save random angle in packing_result import map_tomo.merge_map as MM initmap, init_angle_list = MM.merge_map(v, protein_name, x0, y0, z0, box_size) packmap, pack_angle_list = MM.merge_map(v, protein_name, x, y, z, box_size) packing_result['optimal_result']['initmap_rotate_angle'] = init_angle_list packing_result['optimal_result']['packmap_rotate_angle'] = pack_angle_list print('merge huge map done ') # save packing info with open(output['json']['pack'], 'w') as f: json.dump(packing_result, f, cls=MM.NumpyEncoder) print('save packing info done') # convert packmap to tomogram if save_tomo != 0: # save init & pack map to mrc IM.map2mrc(initmap, output['initmap']['mrc']) IM.map2mrc(packmap, output['packmap']['mrc']) # save init & pack map to png IM.map2png(initmap, output['initmap']['png']) IM.map2png(packmap, output['packmap']['png']) #save pack tomo to mrc and png IM.map2mrc(tomo, output['tomo']['mrc']) IM.map2png(tomo, output['tomo']['png']) # # trim hugemap: cut the volume without macromolecule # trim_initmap = MM.trim_margin(initmap) # trim_packmap = MM.trim_margin(packmap) # trim_tomo = MM.trim_margin(tomo) # print('initmap shape',initmap.shape) # print('trimmed shape',trim_initmap.shape) # print('packmap shape',packmap.shape) # print('trimmed shape',trim_packmap.shape) # print('tomo shape',tomo.shape) # print('trimmed shape',trim_tomo.shape) # IM.map2mrc(trim_initmap, output['initmap']['trim']) # IM.map2mrc(trim_packmap, output['packmap']['trim']) # IM.map2mrc(trim_tomo, output['tomo']['trim']) # trim target i = protein_name.index(target_name) target_packmap, loc_r = MM.trim_target(packmap, np.array([x[i], y[i], z[i]]), op['target_size']) print('trim target done') # import map_tomo.map2tomogram as MT # target_tomo = MT.map2tomo(target_packmap, op['tomo']) # print('convert to tomo') if save_target != 0: IM.map2mrc(target_packmap, output['packmap']['target']['mrc']) # IM.map2mrc(target_tomo, output['tomo']['target']['mrc']) # IM.map2png(target_packmap, output['packmap']['target']['png']) # IM.map2png(target_tomo, output['tomo']['target']['png']) # save target info target_info = {} target_info['loc'] = loc_r target_info['rotate'] = pack_angle_list[i] target_info['name'] = packing_op['target'] target_info['SNR'] = op['tomo']['model']['SNR'] with open(output['json']['target'], 'w') as f: json.dump(target_info, f, cls=MM.NumpyEncoder) print('get target info done') if save_tomo_slice != 0: # convert packmap & tomo to separate pictures import map_tomo.mrc2singlepic as MS MS.mrc2singlepic(output['packmap']['mrc'], output['packmap']['png'] + 'packmap{}/'.format(num), 'packmap{}'.format(num)) MS.mrc2singlepic(output['tomo']['mrc'], output['tomo']['png'] + 'tomo{}/'.format(num), 'tomo{}'.format(num)) target_simu_tomo = {} # target_simu_tomo['tomo'] = target_tomo target_simu_tomo['density_map'] = target_packmap target_simu_tomo['info'] = target_info print('all done') endtime = datetime.datetime.now() print('simulation time:', (endtime - starttime).seconds, 'seconds') return target_simu_tomo
import map_tomo.pdb2map as PM import map_tomo.iomap as IM ms = PM.pdb2map(op['map']) for n in ms: v = ms[n] IM.map2mrc(v, 'IOfile/map_single/{}.mrc'.format(n)) # read density map from mrc rootdir = '/ldap_shared/home/v_sinuo_liu/Simple-Packing/IOfile/map_single' v = IM.readMrcMapDir(rootdir) # get packing info import packing_single_sphere.simulate as SI target_name = '1bxn' packing_result = SI.packing_with_target(target_protein=target_name, random_protein_number=4, PDB_ori_path=op['map']['pdb_dir']) protein_name = packing_result['optimal_result']['pdb_id'] x = packing_result['optimal_result']['x'] / 10 y = packing_result['optimal_result']['y'] / 10 z = packing_result['optimal_result']['z'] / 10 print('initialization', packing_result['optimal_result']['initialization']) x0 = np.array(packing_result['optimal_result']['initialization'][0]) / 10 y0 = np.array(packing_result['optimal_result']['initialization'][1]) / 10 z0 = np.array(packing_result['optimal_result']['initialization'][2]) / 10 box_size = packing_result['general_info']['box_size'] / 10 # merge map to hugemap, save random angle in packing_result import map_tomo.merge_map as MM initmap, init_angle_list = MM.merge_map(v, protein_name, x0, y0, z0, box_size) packmap, pack_angle_list = MM.merge_map(v, protein_name, x, y, z, box_size)
def simu_subtomo(op, packing_op, output, save_tomo=0, save_target=1, save_tomo_slice=0): ''' :param op: parameter of convert pdb/ density map to tomogram :param packing_op: parameter of packing process :param output: path of output file :param save_tomo: 1 save, 0 not save, if the large subtomogram.mrc of the whole packing scene(including 5-10 macromolecules) will be saved :param save_target: 1 save, 0 not save, if the subtomogram.mrc of the target will be saved :param save_tomo_slice: 1 save, 0 not save, if the sliced image will be saved. :return: target_simu_tomo = { 'tomo' : subtomogram of target macromolecule, .mrc file 'density_map': density map, .mrc file 'info': { 'loc' : coordinate of target macromolecule 'rotate' : the rotation angle of this macromolecule (ZYZ, Euler angle) 'name' : the name of a macromolecule } } the json file of packing result and target will be saved in '../IOfile/json' ''' # # convert pdb to map import map_tomo.pdb2map as PM import map_tomo.iomap as IM # ms = PM.pdb2map(op['map']) # for n in ms: # v = ms[n] # IM.map2mrc(v, op['map']['map_single_path']+ '/{}.mrc'.format(n)) # # read density map from mrc # rootdir = op['map']['map_single_path'] # v = IM.readMrcMapDir(rootdir) # print('read density map done') v = op['v'] # get packing info import packing_single_sphere.simulate as SI target_name = packing_op['target'] packing_result = SI.packing_with_target(packing_op) protein_name = packing_result['optimal_result']['pdb_id'] # x = packing_result['optimal_result']['x']/10 # y = packing_result['optimal_result']['y']/10 # z = packing_result['optimal_result']['z']/10 # print(x,y,z) # print('initialization',packing_result['optimal_result']['initialization']) # x0 = np.array(packing_result['optimal_result']['initialization'][0])/10 # y0 = np.array(packing_result['optimal_result']['initialization'][1])/10 # z0 = np.array(packing_result['optimal_result']['initialization'][2])/10 values = [-1, 1] x0 = np.array(packing_result['optimal_result']['initialization'][0]) / 50 y0 = np.array(packing_result['optimal_result']['initialization'][1]) / 50 z0 = np.array(packing_result['optimal_result']['initialization'][2]) / 50 x0 = 150 + random.choice(values) * x0 y0 = 150 + random.choice(values) * y0 z0 = 150 + random.choice(values) * z0 x = np.array(x0) y = np.array(y0) z = np.array(z0) box_size = packing_result['general_info']['box_size'] / 10 packing_result['general_info']['box_size'] = 3000 box_size = 300 # print('[x0,y0,z0]:',x0,y0,z0) print('box_size:', box_size) print('get packing info done') # merge map to hugemap, save random angle in packing_result import map_tomo.merge_map as MM initmap, init_angle_list = MM.merge_map(v, protein_name, x0, y0, z0, box_size) # packmap,pack_angle_list = MM.merge_map(v, protein_name, x, y, z, box_size) packmap, pack_angle_list = initmap, init_angle_list packing_result['optimal_result']['initmap_rotate_angle'] = init_angle_list packing_result['optimal_result']['packmap_rotate_angle'] = pack_angle_list print('merge huge map done ') # save packing info with open(output['json']['pack'], 'w') as f: json.dump(packing_result, f, cls=MM.NumpyEncoder) print('save packing info') # convert packmap to tomogram import map_tomo.map2tomogram as MT tomo = MT.map2tomo(packmap, op['tomo']) print('convert to tomo') if save_tomo != 0: # save init & pack map to mrc IM.map2mrc(initmap, output['initmap']['mrc']) IM.map2mrc(packmap, output['packmap']['mrc']) # save init & pack map to png IM.map2png(initmap, output['initmap']['png']) IM.map2png(packmap, output['packmap']['png']) #save pack tomo to mrc and png IM.map2mrc(tomo, output['tomo']['mrc']) IM.map2png(tomo, output['tomo']['png']) # # trim hugemap # trim_initmap = MM.trim_margin(initmap) # trim_packmap = MM.trim_margin(packmap) # trim_tomo = MM.trim_margin(tomo) # print('initmap shape',initmap.shape) # print('trimmed shape',trim_initmap.shape) # print('packmap shape',packmap.shape) # print('trimmed shape',trim_packmap.shape) # print('tomo shape',tomo.shape) # print('trimmed shape',trim_tomo.shape) # IM.map2mrc(trim_initmap, output['initmap']['trim']) # IM.map2mrc(trim_packmap, output['packmap']['trim']) # IM.map2mrc(trim_tomo, output['tomo']['trim']) # trim target i = protein_name.index(target_name) print('i:', i) print('x:', x) print('x[i]:', x[i]) target_packmap, loc_r = MM.trim_target(packmap, np.array([x[i], y[i], z[i]]), op['target_size']) target_tomo, loc_r = MM.trim_target(tomo, np.array([x[i], y[i], z[i]]), op['target_size'], loc_r) if save_target != 0: IM.map2mrc(target_packmap, output['packmap']['target']['mrc']) IM.map2mrc(target_tomo, output['tomo']['target']['mrc']) IM.map2png(target_packmap, output['packmap']['target']['png']) IM.map2png(target_tomo, output['tomo']['target']['png']) print('trim target done') # save target info target_info = {} target_info['loc'] = loc_r target_info['rotate'] = pack_angle_list[i] target_info['name'] = packing_op['target'] with open(output['json']['target'], 'w') as f: json.dump(target_info, f, cls=MM.NumpyEncoder) print('get target info done') if save_tomo_slice != 0: # convert packmap & tomo to separate pictures import map_tomo.mrc2singlepic as MS MS.mrc2singlepic(output['packmap']['mrc'], output['packmap']['png'] + 'packmap{}/'.format(num), 'packmap{}'.format(num)) MS.mrc2singlepic(output['tomo']['mrc'], output['tomo']['png'] + 'tomo{}/'.format(num), 'tomo{}'.format(num)) target_simu_tomo = {} target_simu_tomo['tomo'] = target_tomo target_simu_tomo['density_map'] = target_packmap target_simu_tomo['info'] = target_info print('all done') return target_simu_tomo