def write_json(self, directory, color='index', rndname=True, model_num=None, title=None, filename=None, **kwargs): """ Save a model in the json format, read by TADkit. **Note:** If none of model_num, models or cluster parameter are set, ALL the models will be written. :param directory: location where the file will be written (note: the name of the file will be model_1.cmm if model number is 1) :param None model_num: the number of the model to save :param True rndname: If True, file names will be formatted as: model.RND.cmm, where RND is the random number feed used by IMP to generate the corresponding model. If False, the format will be: model_NUM_RND.cmm where NUM is the rank of the model in terms of objective function value :param None filename: overide the default file name writing :param 'index' color: can be: * a string as: * '**index**' to color particles according to their position in the model (:func:`pytadbit.utils.extraviews.color_residues`) * '**tad**' to color particles according to the TAD they belong to (:func:`pytadbit.utils.extraviews.tad_coloring`) * '**border**' to color particles marking borders. Color according to their score (:func:`pytadbit.utils.extraviews.tad_border_coloring`) coloring function like. * a function, that takes as argument a model and any other parameter passed through the kwargs. * a list of (r, g, b) tuples (as long as the number of particles). Each r, g, b between 0 and 1. :param kwargs: any extra argument will be passed to the coloring function """ if isinstance(color, basestring): if color == 'index': color = color_residues(self, **kwargs) elif color == 'tad': if not 'tads' in kwargs: raise Exception('ERROR: missing TADs\n ' + 'pass an Experiment.tads disctionary\n') color = tad_coloring(self, **kwargs) elif color == 'border': if not 'tads' in kwargs: raise Exception('ERROR: missing TADs\n ' + 'pass an Experiment.tads disctionary\n') color = tad_border_coloring(self, **kwargs) else: raise NotImplementedError( ('%s type of coloring is not yet ' + 'implemeted\n') % color) elif hasattr(color, '__call__'): # it's a function color = color(self, **kwargs) elif not isinstance(color, list): raise TypeError('one of function, list or string is required\n') form = ''' { "chromatin" : { "id" : %(sha)s, "title" : "%(title)s", "source" : "TADbit %(version)s", "metadata": {%(descr)s}, "type" : "tadbit", "data": { "models": [ { %(xyz)s }, ], "clusters":[%(cluster)s], "centroid":[%(centroid)s], "restraints": [[][]], "chromatinColor" : [ ] } } } ''' fil = {} fil['sha'] = hashlib.new(fil['xyz']).hexdigest() fil['title'] = title or "Sample TADbit data" fil['version'] = version fil['descr'] = ''.join( '\n', ',\n'.join([ '"%s" : %s' % (k, ('"%s"' % (v)) if isinstance(v, basestring) else v) for k, v in list(self.get('description', {}).items()) ]), '\n') fil['xyz'] = ','.join([ '[%.4f,%.4f,%.4f]' % (self['x'][i], self['y'][i], self['z'][i]) for i in range(len(self['x'])) ]) if filename: out_f = open('%s/%s' % (directory, filename), 'w') else: if rndname: out_f = open( '%s/model.%s.cmm' % (directory, self['rand_init']), 'w') else: out_f = open( '%s/model_%s_rnd%s.cmm' % (directory, model_num, self['rand_init']), 'w') out_f.write(out) out_f.close()
def write_json(self, directory, color='index', rndname=True, model_num=None, title=None, filename=None, **kwargs): """ Save a model in the json format, read by TADkit. **Note:** If none of model_num, models or cluster parameter are set, ALL the models will be written. :param directory: location where the file will be written (note: the name of the file will be model_1.cmm if model number is 1) :param None model_num: the number of the model to save :param True rndname: If True, file names will be formatted as: model.RND.cmm, where RND is the random number feed used by IMP to generate the corresponding model. If False, the format will be: model_NUM_RND.cmm where NUM is the rank of the model in terms of objective function value :param None filename: overide the default file name writing :param 'index' color: can be: * a string as: * '**index**' to color particles according to their position in the model (:func:`pytadbit.utils.extraviews.color_residues`) * '**tad**' to color particles according to the TAD they belong to (:func:`pytadbit.utils.extraviews.tad_coloring`) * '**border**' to color particles marking borders. Color according to their score (:func:`pytadbit.utils.extraviews.tad_border_coloring`) coloring function like. * a function, that takes as argument a model and any other parameter passed through the kwargs. * a list of (r, g, b) tuples (as long as the number of particles). Each r, g, b between 0 and 1. :param kwargs: any extra argument will be passed to the coloring function """ if isinstance(color, str): if color == 'index': color = color_residues(self, **kwargs) elif color == 'tad': if not 'tads' in kwargs: raise Exception('ERROR: missing TADs\n ' + 'pass an Experiment.tads disctionary\n') color = tad_coloring(self, **kwargs) elif color == 'border': if not 'tads' in kwargs: raise Exception('ERROR: missing TADs\n ' + 'pass an Experiment.tads disctionary\n') color = tad_border_coloring(self, **kwargs) else: raise NotImplementedError(('%s type of coloring is not yet ' + 'implemeted\n') % color) elif hasattr(color, '__call__'): # it's a function color = color(self, **kwargs) elif not isinstance(color, list): raise TypeError('one of function, list or string is required\n') form = ''' { "chromatin" : { "id" : %(sha)s, "title" : "%(title)s", "source" : "TADbit %(version)s", "metadata": {%(descr)s}, "type" : "tadbit", "data": { "models": [ { %(xyz)s }, ], "clusters":[%(cluster)s], "centroid":[%(centroid)s], "restraints": [[][]], "chromatinColor" : [ ] } } } ''' fil = {} fil['sha'] = sha.new(fil['xyz']).hexdigest() fil['title'] = title or "Sample TADbit data" fil['version'] = version fil['descr'] = ''.join('\n', ',\n'.join([ '"%s" : %s' % (k, ('"%s"' % (v)) if isinstance(v, str) else v) for k, v in self['description'].items()]), '\n') fil['xyz'] = ','.join(['[%.4f,%.4f,%.4f]' % (self['x'][i], self['y'][i], self['z'][i]) for i in xrange(len(self['x']))]) if filename: out_f = open('%s/%s' % (directory, filename), 'w') else: if rndname: out_f = open('%s/model.%s.cmm' % (directory, self['rand_init']), 'w') else: out_f = open('%s/model_%s_rnd%s.cmm' % ( directory, model_num, self['rand_init']), 'w') out_f.write(out) out_f.close()
def write_cmm(self, directory, color='index', rndname=True, model_num=None, filename=None, **kwargs): """ Save a model in the cmm format, read by Chimera (http://www.cgl.ucsf.edu/chimera). **Note:** If none of model_num, models or cluster parameter are set, ALL the models will be written. :param directory: location where the file will be written (note: the name of the file will be model_1.cmm if model number is 1) :param None model_num: the number of the model to save :param True rndname: If True, file names will be formatted as: model.RND.cmm, where RND is the random number feed used by IMP to generate the corresponding model. If False, the format will be: model_NUM_RND.cmm where NUM is the rank of the model in terms of objective function value :param None filename: overide the default file name writing :param 'index' color: can be: * a string as: * '**index**' to color particles according to their position in the model (:func:`pytadbit.utils.extraviews.color_residues`) * '**tad**' to color particles according to the TAD they belong to (:func:`pytadbit.utils.extraviews.tad_coloring`) * '**border**' to color particles marking borders. Color according to their score (:func:`pytadbit.utils.extraviews.tad_border_coloring`) coloring function like. * a function, that takes as argument a model and any other parameter passed through the kwargs. * a list of (r, g, b) tuples (as long as the number of particles). Each r, g, b between 0 and 1. :param kwargs: any extra argument will be passed to the coloring function """ if isinstance(color, basestring): if color == 'index': color = color_residues(self, **kwargs) elif color == 'tad': if not 'tads' in kwargs: raise Exception('ERROR: missing TADs\n ' + 'pass an Experiment.tads disctionary\n') color = tad_coloring(self, **kwargs) elif color == 'border': if not 'tads' in kwargs: raise Exception('ERROR: missing TADs\n ' + 'pass an Experiment.tads disctionary\n') color = tad_border_coloring(self, **kwargs) else: raise NotImplementedError( ('%s type of coloring is not yet ' + 'implemeted\n') % color) elif hasattr(color, '__call__'): # it's a function color = color(self, **kwargs) elif not isinstance(color, list): raise TypeError('one of function, list or string is required\n') out = '<marker_set name=\"%s\">\n' % (self['rand_init']) form = ( '<marker id=\"%s\" x=\"%s\" y=\"%s\" z=\"%s\"' + ' r=\"%s\" g=\"%s\" b=\"%s\" ' + 'radius=\"' + #str(30) + str(self['radius']) + '\" note=\"%s\"/>\n') for i in range(len(self['x'])): out += form % (i + 1, self['x'][i], self['y'][i], self['z'][i], color[i][0], color[i][1], color[i][2], i + 1) form = ( '<link id1=\"%s\" id2=\"%s\" r=\"1\" ' + 'g=\"1\" b=\"1\" radius=\"' + str(self['radius'] / 10.) + # str(self['radius']/2) + '\"/>\n') break_chroms = [1] try: for beg, end in zip(self['description']['start'], self['description']['end']): break_chroms.append((end - beg) / self['description']['resolution'] + break_chroms[-1]) except: pass for i in range(1, len(self['x'])): if i in break_chroms[1:]: continue out += form % (i, i + 1) out += '</marker_set>\n' if filename: out_f = open('%s/%s' % (directory, filename), 'w') else: if rndname: out_f = open( '%s/model.%s.cmm' % (directory, self['rand_init']), 'w') else: out_f = open( '%s/model_%s_rnd%s.cmm' % (directory, model_num, self['rand_init']), 'w') out_f.write(out) out_f.close()
def write_cmm(self, directory, color='index', rndname=True, model_num=None, filename=None, **kwargs): """ Save a model in the cmm format, read by Chimera (http://www.cgl.ucsf.edu/chimera). **Note:** If none of model_num, models or cluster parameter are set, ALL the models will be written. :param directory: location where the file will be written (note: the name of the file will be model_1.cmm if model number is 1) :param None model_num: the number of the model to save :param True rndname: If True, file names will be formatted as: model.RND.cmm, where RND is the random number feed used by IMP to generate the corresponding model. If False, the format will be: model_NUM_RND.cmm where NUM is the rank of the model in terms of objective function value :param None filename: overide the default file name writing :param 'index' color: can be: * a string as: * '**index**' to color particles according to their position in the model (:func:`pytadbit.utils.extraviews.color_residues`) * '**tad**' to color particles according to the TAD they belong to (:func:`pytadbit.utils.extraviews.tad_coloring`) * '**border**' to color particles marking borders. Color according to their score (:func:`pytadbit.utils.extraviews.tad_border_coloring`) coloring function like. * '**copy**' to color particles according to the copy they belong to (:func:`pytadbit.utils.extraviews.copy_coloring`) * a function, that takes as argument a model and any other parameter passed through the kwargs. * a list of (r, g, b) tuples (as long as the number of particles). Each r, g, b between 0 and 1. :param kwargs: any extra argument will be passed to the coloring function """ if isinstance(color, str): if color == 'index': color = color_residues(self, **kwargs) elif color == 'tad': if not 'tads' in kwargs: raise Exception('ERROR: missing TADs\n ' + 'pass an Experiment.tads dictionary\n') color = tad_coloring(self, **kwargs) elif color == 'border': if not 'tads' in kwargs: raise Exception('ERROR: missing TADs\n ' + 'pass an Experiment.tads dictionary\n') color = tad_border_coloring(self, **kwargs) elif color == 'copy': color = copy_coloring(self, **kwargs) else: raise NotImplementedError(('%s type of coloring is not yet ' + 'implemeted\n') % color) elif hasattr(color, '__call__'): # it's a function color = color(self, **kwargs) elif not isinstance(color, list): raise TypeError('one of function, list or string is required\n') out = '<marker_set name=\"%s\">\n' % (self['rand_init']) form = ('<marker id=\"%s\" x=\"%s\" y=\"%s\" z=\"%s\"' + ' r=\"%s\" g=\"%s\" b=\"%s\" ' + 'radius=\"' + str(self['radius']) + '\" note=\"%s\"/>\n') ncopy=0 for molecule in self['x']: for i in xrange(len(self['x'][molecule])): out += form % (i + 1, self['x'][molecule][i], self['y'][molecule][i], self['z'][molecule][i], color[ncopy][0], color[ncopy][1], color[ncopy][2], i + 1) ncopy = ncopy + 1 form = ('<link id1=\"%s\" id2=\"%s\" r=\"1\" ' + 'g=\"1\" b=\"1\" radius=\"' + str(10) + # str(self['radius']/2) + '\"/>\n') for i in xrange(1, len(self['x'])): out += form % (i, i + 1) out += '</marker_set>\n' if filename: out_f = open('%s/%s' % (directory, filename), 'w') else: if rndname: out_f = open('%s/model.%s.cmm' % (directory, self['rand_init']), 'w') else: out_f = open('%s/model_%s_rnd%s.cmm' % ( directory, model_num, self['rand_init']), 'w') out_f.write(out) out_f.close()