def myLoad(filename, dummy=None): #ugly workaround for simulating HU: if filename[:4] == "_HU_": HU = "HU" filename = filename[4:] elif filename[:4] == "_GY_": HU = "GY" filename = filename[4:] else: HU = False bonds = makeBondsForBrush() bonds.load(os.path.join(os.path.split(filename)[0], "chains")) data = polymerutils.load(filename) converted = bonds.convertChain(data) if HU == "HU": converted += gaussian_filter1d(10 * np.random.randn(*converted.shape), sigma=2.8, axis=0) elif HU == "GY": converted += gaussian_filter1d(7 * np.random.randn(*converted.shape), sigma=2.8, axis=0) #using only one arm for scalings if np.random.random() < 0.5: return converted[:int(0.4 * len(converted))] else: return converted[int(0.6 * len(converted)):]
def __init__( self, filenames, mapStarts, mapN, cutoff=1.7, loadFunction=None, exceptionsToIgnore=[], contactFunction=None, ): """ Init accepts arguments to initialize the iterator. filenames will be one of the items in the inValues list of the "averageContacts" function cutoff and loadFunction should be provided either in classInitArgs or classInitKwargs of averageContacts When initialized, the iterator should store these args properly and create all necessary constructs """ from openmmlib import contactmaps self.contactmaps = contactmaps self.filenames = filenames self.cutoff = cutoff self.exceptionsToIgnore = exceptionsToIgnore self.mapStarts = mapStarts self.mapN = mapN if loadFunction is None: import polymerutils loadFunction = polymerutils.load() if contactFunction is None: contactFunction = self.contactmaps.giveContacts self.contactFunction = contactFunction self.loadFunction = loadFunction self.i = 0 self.curStarts = []
def saveToPdb(input): i, dataPath = input d = polymerutils.load(dataPath) pdbFilename = '{0:0{width}}.pdb'.format(i, width=numDigits) savePath = pdbFolder + '/' + pdbFilename polymerutils.save(d, savePath, mode='pdb', pdbGroups=colorArray) return os.path.abspath(savePath)
def showOneChain(): data = load("L_201_num_1.0_final_protocol/block500.dat") #data = load("../globules_expanded/crumpled1.dat_expanded") balls = cPickle.load(open("L_201_num_1.0_final_protocol/coreParticles")) balls = np.array(balls) balls = balls[(balls > 47000) * (balls < 53000)] - 47000 data = data[47000:53000] pymol_show.show_chain(data, dataMult=1.1, spherePositions=balls, sphereRadius=.02)
def highlightThreeRegions(): folder = "L_200_num_8.0_final_protocol" filenum = 500 offset = 3 data = load(folder + "/block%d.dat" % filenum) #data = data[30000:80000] newData = interpolateData(data, targetN=90000) pymol_show.do_coloring(newData[0], [(40000, 40400), (44000, 44400), (48000, 48400), (52000, 52400), (56000, 56400)], ["green", "red", "blue", "brown", "orange"], [0, 0, 0, 0, 0], chain_radius=.07, subchain_radius=.2, chain_transparency=0.5, multiplier=.8) exit()
def myLoad(filename, dummy=None): # ugly workaround: if filename[:4] == "_HU_": HU = "HU" filename = filename[4:] elif filename[:4] == "_GY_": HU = "GY" filename = filename[4:] else: HU = False bonds = makeBondsForBrush() bonds.load(os.path.join(os.path.split(filename)[0], "chains")) data = polymerutils.load(filename) converted = bonds.convertChain(data) if HU == "HU": converted += gaussian_filter1d(10 * np.random.randn(*converted.shape), sigma=2.8, axis=0) elif HU == "GY": converted += gaussian_filter1d(7 * np.random.randn(*converted.shape), sigma=2.8, axis=0) return converted
def do_polymer_simulation(steps, dens, stiff, folder, N, SEPARATION, LIFETIME, forw, rev, save_blocks=2000, smc_steps=3, no_SMC=False, randomize_SMC=False, gpu_number='default', cpu_simulation=False, skip_start=100, save_mode='joblib'): """Do the polymer simulation given all the parameters.""" i = 0 SMCTran = initModel(i, N, SEPARATION, LIFETIME, forw, rev) box = (N / dens) ** 0.33 # density = 0.1 if exists(join(folder, "block10.dat")): block = scanBlocks(folder)["keys"].max() - 1 data = polymerutils.load(join(folder, "block{0}.dat".format(block))) else: data = grow_rw(N, int(box) - 2) block = 0 assert len(data) == N skip = 0 time.sleep(0.1) while True: SMCTran.steps(smc_steps) if randomize_SMC: SMCTran.steps(500000) if (block % 2000 == 0) and (skip == 0): print("doing dummy steps") SMCTran.steps(500000) skip = skip_start print(skip, "blocks to skip") # initialize a polymer for the 3D simualton step. a = Simulation(timestep=80, thermostat=0.01) if cpu_simulation: a.setup(platform="cpu", PBC=True, PBCbox=[ box, box, box], precision="mixed") else: a.setup(platform="OpenCL", PBC=True, PBCbox=[ box, box, box], precision="mixed", GPU=gpu_number) # a.setup(platform="CUDA", PBC=True, PBCbox=[box, box, box], # GPU=sys.argv[4], precision="mixed") a.saveFolder(folder) a.load(data) a.addHarmonicPolymerBonds(wiggleDist=0.1) if stiff > 0: a.addGrosbergStiffness(stiff) a.addPolynomialRepulsiveForce(trunc=1.5, radiusMult=1.05) # get positions of SMCs, add bonds to polymer left, right = SMCTran.getSMCs() for l, r in zip(left, right): a.addBond(l, r, bondWiggleDistance=0.5, distance=0.3, bondType="harmonic") a.step = block if skip > 0: print("skipping block") a.doBlock(steps, increment=False) skip -= 1 if skip == 0: a.doBlock(steps) block += 1 a.save(mode=save_mode) if block == save_blocks: break data = a.getData() del a time.sleep(0.1)
numLoops = 5 * (N / avLoopSize) lens = np.random.randn(numLoops) * avLoopSize lens = lens[(lens > minLoopSize) * (lens < maxLoopSize)] lens = np.array(lens, dtype=int) lens = np.cumsum(lens) lens = lens[lens < N - avLoopSize / 2] lens = np.r_[0, lens , N - 1] return lens pancakeWidth = 30 pancakeSize = 15000 foldername = "L_{0}_num_{1}_len_{2}".format(L, num, intLen) if not os.path.exists(foldername): os.mkdir(foldername) shutil.copy(sys.argv[0], foldername) data = load("globule32")[:30000] polymerutils.save(data, os.path.join(foldername, "block0.dat")) N = len(data) pancakeNum = N / float(pancakeSize) coreParticles = numpy.nonzero(numpy.random.random(N) < (1. / L))[0] smcStep = 5 stepsPerSmc = 6000 bondBlocks = 70 minBondLen = 5 maxBondLen = 20 bondNum = 3 secondSmcGap = 2
def showCoreParticles(): folder = "L_100_num_1_len_30" #"L_100_num_11_final_protocol" filenum = 400 offset = 40 coreParticles = cPickle.load(open(os.path.join(folder, "coreParticles"))) data = load(folder + "/block%d.dat" % filenum) #data = load("../globules_expanded/crumpled1.dat_expanded") colorArray = np.zeros(len(data), int) loopArray = np.zeros(len(data), int) for j, i in enumerate(coreParticles): if (j < len(coreParticles) - 1) and (j % 25 == 2): colorArray[max(i + offset + 1, 0):min(coreParticles[j + 1] - offset - 1, len(data))] = 2 colorArray[max(i - offset, 0):min(i + offset + 1, len(data))] = 1 newData = interpolateData(data, targetN=90000, colorArrays=[colorArray]) coords = newData[0] colorArray = newData[1][0] colorArray = np.ceil(colorArray) """ greenColors = np.zeros_like(colors) greenColors[40000:65000] = 1 greenColors[colors] = 0 greenRegions = pymol_show.create_regions(greenColors) greenRegions = list(greenRegions) greenColors = ["green"] * len(greenRegions) greenTransparencies = [0] * len(greenColors) """ regions = pymol_show.createRegions(colorArray == 1) regions = list(regions) colors = ["blue"] * len(regions) transparencies = [0] * len(regions) regions2 = pymol_show.createRegions(colorArray == 2) M = len(regions2) print M allColors = ["br{0}".format(i) for i in range(10)] S = len(allColors) colors2 = [ allColors[int(float(i) * float(S) / float(M))] for i in range(M) ] transparencies2 = [0 for i in colors2] pymol_show.do_coloring(coords, list(regions) + list(regions2), colors + colors2, transparencies + transparencies2, showChain="worm", chainRadius=.4, subchainRadius=.4, chainTransparency=0.6, multiplier=.8, pdbGroups=colorArray, support=""" bg black set orthoscopic, 0 set field_of_view, 70 create back, chain 1 color brown, back show surface, back """) exit() pymol_show.do_coloring(coords, regions, colors, transparencies, chain_radius=.1, subchain_radius=.2, chain_transparency=0.2, multiplier=.8)
def showAllChain(): data = load("L_100_num_1_len_30/block500.dat") #data = data[25000:50000] pymol_show.show_chain(data, dataMult=1.1)
def makeCoverFigure(): folder = "L_100_num_16_len_30" filenum = 550 #folder = "../026_controls_loopSize/ConsScaff133_3" #filenum = 250 offset = 1 coreParticles = cPickle.load(open(os.path.join(folder, "coreParticles"))) print len(coreParticles) data = load(folder + "/block%d.dat" % filenum) coreArray = np.zeros(len(data), dtype=float) coreArray[coreParticles] = 2 #data = data[::-1] #coreParticles = len(data) - coreParticles #data = data[:30000] #coreParticles = coreParticles[coreParticles < 30000 ] if len(data) > 90000: data, (coreArray, ) = interpolateData(data, 90000, colorArrays=[coreArray]) coreArray[np.nonzero(coreArray > 0.4)[0] + 1] = 0 coreParticles = np.nonzero(coreArray > .4)[0] data = np.dot( data, np.array([[ 0.782993197, -0.090055801, 0.615476012, ], [ 0.042767350, 0.994917035, 0.091168165, ], [ -0.620556772, -0.045062274, 0.782866657, ]])) #data = np.dot(data, np.array([[0.829197228, -0.461252242, -0.315715969, ], # [0.031428415, 0.602414608, -0.797561765, ], # [0.558069587, 0.651413143, 0.514018297, ]] #)) #data = load("../globules_expanded/crumpled1.dat_expanded") colorArray = np.zeros(len(data), int) loopArray = np.zeros(len(data), int) for S, offset in enumerate(range(5, 0, -2)): for j, i in enumerate(coreParticles): colorArray[max(i - offset, 0):min(i + offset + 1, len(data))] = S + 1 inds = np.diff(colorArray) != 0 print inds indCumsum = np.r_[0, np.cumsum(inds)] newdata = np.zeros(shape=(len(data) + indCumsum[-1], 3), dtype=float) newcolorarray = np.zeros(shape=(len(data) + indCumsum[-1], ), dtype=float) newdata[np.arange(len(data), dtype=int) + indCumsum] = data newcolorarray[np.arange(len(data), dtype=int) + indCumsum] = colorArray mask = newdata[:, 0] == 0 newdata[mask] = data[np.nonzero(inds)[0] + 1] newcolorarray[mask] = colorArray[np.nonzero(inds)[0]] coords, colorArray = newdata, newcolorarray colorStrings = [] for colorInd in range(0, int(max(colorArray) + 1))[::-1]: colorFloat = colorInd / (float(max(colorArray) + 1)) colorString = "[{0}, {0}, {1}]".format(max(1. - colorFloat * 1.2, 0), 1.) colorName = "bubu{0}".format(colorInd) chainName = "subset{0}".format(colorInd) #transp = [0.55, 0.55, 0.55, 0.55, 0., 0., 0., 0.][colorInd] transp = [0.75, 0.75, 0.75, 0., 0., 0., 0.][colorInd] colorStrings.append(""" set_color {color}, {colorString} create {chain}, chain {chainNum} color {color}, {chain} set cartoon_trace_atoms,1, {chain} set cartoon_tube_radius, 0.3, {chain} set cartoon_transparency, {transp}, {chain} set cartoon_tube_quality, 6 cartoon tube, {chain} show cartoon, {chain} """.format(chain=chainName, color=colorName, colorString=colorString, chainNum=colorInd, transp=transp)) colorStrings = "".join(colorStrings) pymol_show.new_coloring(coords, [], [], [], showChain="none", chainRadius=.4, subchainRadius=.4, chainTransparency=1.0, pdbGroups=colorArray, transparentBackground=True, multiplier=.8, support=""" bg white """ + colorStrings + """ run axes.py axes set spec_direct, 0.1 set spec_reflect, 2. set spec_power, 100 set spec_direct_power, 100 set ray_max_passes, 2000 set ray_shadow, off zoom set antialias, 1 set light, [0,-1,-0.3] """, presupport=""" """) exit()
def loadFunction(x, y): return polymerutils.load(x, y) start = numpy.random.randint(0, 80000) end = start + 20000 return polymerutils.load(x, y)[start:end]
def exampleOpenmm(x): """ You need to have a OpenMM-compatible GPU and OpenMM installed to run this script. Otherwise you can switch to "reference" platform a.setup(platform = "reference") But this will be extremely slow... Installing OpenMM may be not easy too... but you can try """ a = SimulationWithCrosslinks(timestep=80, thermostat=0.005) a.setup(platform="OpenCL", verbose=True) a.saveFolder("sizes_12_200_2500") # folder where to save trajectory #a.load("trajectory2/start.dat") #filename to load g1 = polymerutils.load( "/net/evolution/home/magus/trajectories/globule_creation/128000_SAW/crumpled%d.dat" % (x + 1)) #Comment out two lines below for a full-length chain print "using shorter chain for protocol refinement" g1 = g1[:40000] # for faster developement of the protocol a.load(g1) a.setLayout(mode="chain") a.addHarmonicPolymerBonds(wiggleDist=0.1) a.addGrosbergRepulsiveForce(trunc=1.4) a.addGrosbergStiffness(k=5) pancakeWidth = 20 pancakeSize = 20000 pancakeNum = a.N / float(pancakeSize) print pancakeNum a.addCylindricalConfinement(r=34, bottom=0, top=pancakeWidth * pancakeNum, k=0.7) a.fixParticlesZCoordinate(particles=range(0, a.N, 10), zCoordinates=(0, pancakeWidth * pancakeNum), k=0.03, mode="quadratic", gap=pancakeWidth) #a.addRgLimitations(num = 13,volumeParameter = 0.05,relativeSmeer = 0.60,pancakeLength = 35000) #a.addConsecutiveRandomBonds(12,0.3,0.5,0.5,distanceBetweenBonds = 2) #a.addConsecutiveRandomBonds(200,0.3,0.5,0.5,distanceBetweenBonds = 5) a.addConsecutiveRandomBonds(12, 0.3, 0.5, 0.5, distanceBetweenBonds=2) a.addConsecutiveRandomBonds(200, 0.3, 0.5, 0.5, distanceBetweenBonds=6) a.addConsecutiveRandomBonds(2500, 1, 0.5, 0.5, distanceBetweenBonds=100) #coreParticles = numpy.nonzero(numpy.random.random(a.N) < 0.01)[0] #a.addAttractionToTheCore(.5,6, coreParticles) a.energyMinimization(stepsPerIteration=30, force=True) for _ in xrange(200): a.doBlock(10000) a.save() a.printStats()
def showCoreParticles(): folder = "L_100_num_12_len_30" filenum = 25 offset = 1 coreParticles = cPickle.load(open(os.path.join(folder, "coreParticles"))) data = load(folder + "/block%d.dat" % filenum) #data = load("../globules_expanded/crumpled1.dat_expanded") colorArray = np.zeros(len(data), int) for j, i in enumerate(coreParticles): if (j < len(coreParticles) - 1) and (j % 20 == 1): colorArray[max(i, 0):min(coreParticles[j + 1] - 1, len(data))] = 2 colorArray[max(i - offset, 0):min(i + offset + 1, len(data))] = 1 loopArray = np.zeros(len(data), int) coords = data regions1 = pymol_show.createRegions(colorArray == 1) M = len(regions1) print M colors1 = ["brown" for i in range(M)] transparencies1 = [0 for i in colors1] regions2 = pymol_show.createRegions(colorArray == 2) M = len(regions2) print M allColors = ["br{0}".format(i) for i in range(10)] S = len(allColors) colors2 = [ allColors[int(float(i) * float(S) / float(M))] for i in range(M) ] transparencies2 = [0 for i in colors2] return pymol_show.do_coloring( coords, list(regions2), list(colors2), list(transparencies2), chainRadius=.25, subchainRadius=.35, chainTransparency=0.85, returnScriptName="mov", showChain="worm", pdbGroups=colorArray, showGui=True, # saveTo="bla.png", multiplier=.8, support=""" create back, chain 1 set cartoon_transparency,0.000000,back set cartoon_trace_atoms,1, back set cartoon_tube_radius,0.280000, back cartoon tube, back color brown, back set depth_cue, 0 set_view (\ _ 0.572576821, -0.411692470, -0.708985031,\ _ -0.745704532, 0.097847328, -0.659046650,\ _ 0.340698749, 0.906049132, -0.250979245,\ _ 0.000131026, -0.000255849, -401.058715820,\ _ 49.410499573, 50.416854858, 58.617347717,\ _ 315.981750488, 458.882690430, -20.000000000 ) """), colorArray
def showAllChain(): data = load("L_200_num_5.0_final_protocol/block500.dat") #data = data[25000:50000] pymol_show.show_chain(data, dataMult=1.1)
def smallFunction(x): i, dataPath = x savePath = imgFolder + '/{0:0{width}}.png'.format(i, width=numDigits) coreParticles = cPickle.load( open(os.path.join(os.path.split(dataPath)[0], "coreParticles"))) data = load(dataPath) #data = load("../globules_expanded/crumpled1.dat_expanded") colorArray = np.zeros(len(data), int) for j, i in enumerate(coreParticles): if (j < len(coreParticles) - 1) and (j % 20 == 1): colorArray[max(i, 0):min(coreParticles[j + 1] - 1, len(data))] = 2 colorArray[max(i - offset, 0):min(i + offset + 1, len(data))] = 1 loopArray = np.zeros(len(data), int) coords = data regions1 = pymol_show.createRegions(colorArray == 1) M = len(regions1) print M colors1 = ["brown" for i in range(M)] transparencies1 = [0 for i in colors1] regions2 = pymol_show.createRegions(colorArray == 2) M = len(regions2) print M allColors = ["br{0}".format(i) for i in range(10)] S = len(allColors) colors2 = [ allColors[int(float(i) * float(S) / float(M))] for i in range(M) ] transparencies2 = [0 for i in colors2] pymol_show.do_coloring( coords, list(regions2), list(colors2), list(transparencies2), chainRadius=.25, subchainRadius=.35, chainTransparency=0.9, #returnScriptName="mov", showChain="worm", pdbGroups=colorArray, showGui=True, #saveTo=savePath, multiplier=.8, support=""" create back, chain 1 set cartoon_transparency,0.000000,back set cartoon_trace_atoms,1, back set cartoon_tube_radius,0.280000, back cartoon tube, back color brown, back set depth_cue, 0 set field_of_view, 10 set_view (\ 0.362947434, -0.752908945, 0.548998356,\ -0.843832374, -0.015661031, 0.536378920,\ -0.395252228, -0.657936990, -0.641013563,\ -0.000659317, 0.000253409, -788.783874512,\ 81.561027527, 81.701515198, 121.653610229,\ 615.844299316, 961.749450684, -10.000000000 ) png {savepath} quit """.format(savepath=savePath))