def install_jbrowse(install_dir, root_url, rename_to=None, conf_file=None): if not conf_file: conf_file = config.get_default_conf_file() install_dir = util.abspath(install_dir) pkg_data_dir = config.get_pkg_data_dir() jbrowse_zip = glob(pjoin(pkg_data_dir,"JBrowse*")) assert len(jbrowse_zip) == 1, "Expected a single JBrowse archive "+\ "stored within the package data dir: %s" % (pkg_data_dir,) jbrowse_zip = jbrowse_zip[0] util.makedir(install_dir) #context manager only works for zipfile in Python 2.7 f = zipfile.ZipFile(jbrowse_zip, 'r') try: install_name = os.path.dirname(f.namelist()[0]) assert (install_name not in (".","..")) and \ os.path.dirname(install_name) == "",\ "Unsafe path detected in JBrowse archive: {}".format(f.namelist()[0]) install_home = pjoin(install_dir,install_name) #JBrowse setup script will not install Pelr modules #if it is executed in a directory where it was ran before, #even unsuccessfuly. #Wack the existing directory: if os.path.exists(install_home): shutil.rmtree(install_home) #somehow zipfile module wacks executable bits #f.extractall(path=install_dir) #unsafe: check_call(["unzip","-q","-o",jbrowse_zip],cwd=install_dir) finally: f.close() if rename_to: install_home_new = pjoin(install_dir,rename_to) if os.path.exists(install_home_new): shutil.rmtree(install_home_new) os.rename(install_home,install_home_new) install_home = install_home_new check_call(["./setup.sh"],cwd=install_home) for line in fileinput.input(pjoin(install_home,"index.html"),inplace=True): #Galaxy Web server intercepts 'data' in URL params, we need to use another name print line.replace('queryParams.data','queryParams.jbrowse_data'), conf = util.load_config_json(conf_file) conf["jbrowse_bin_dir"] = util.abspath(pjoin(install_home,"bin")) conf["jbrowse_url"] = util.urljoin_path(root_url, os.path.basename(install_home)) util.save_config_json(conf,conf_file) return conf_file
def install_jbrowse(install_dir, root_url, rename_to=None, conf_file=None): if not conf_file: conf_file = config.get_default_conf_file() install_dir = util.abspath(install_dir) pkg_data_dir = config.get_pkg_data_dir() jbrowse_zip = glob(pjoin(pkg_data_dir, "JBrowse*")) assert len(jbrowse_zip) == 1, "Expected a single JBrowse archive "+\ "stored within the package data dir: %s" % (pkg_data_dir,) jbrowse_zip = jbrowse_zip[0] util.makedir(install_dir) #context manager only works for zipfile in Python 2.7 f = zipfile.ZipFile(jbrowse_zip, 'r') try: install_name = os.path.dirname(f.namelist()[0]) assert (install_name not in (".","..")) and \ os.path.dirname(install_name) == "",\ "Unsafe path detected in JBrowse archive: {}".format(f.namelist()[0]) install_home = pjoin(install_dir, install_name) #JBrowse setup script will not install Pelr modules #if it is executed in a directory where it was ran before, #even unsuccessfuly. #Wack the existing directory: if os.path.exists(install_home): shutil.rmtree(install_home) #somehow zipfile module wacks executable bits #f.extractall(path=install_dir) #unsafe: check_call(["unzip", "-q", "-o", jbrowse_zip], cwd=install_dir) finally: f.close() if rename_to: install_home_new = pjoin(install_dir, rename_to) if os.path.exists(install_home_new): shutil.rmtree(install_home_new) os.rename(install_home, install_home_new) install_home = install_home_new check_call(["./setup.sh"], cwd=install_home) for line in fileinput.input(pjoin(install_home, "index.html"), inplace=True): #Galaxy Web server intercepts 'data' in URL params, we need to use another name print line.replace('queryParams.data', 'queryParams.jbrowse_data'), conf = util.load_config_json(conf_file) conf["jbrowse_bin_dir"] = util.abspath(pjoin(install_home, "bin")) conf["jbrowse_url"] = util.urljoin_path(root_url, os.path.basename(install_home)) util.save_config_json(conf, conf_file) return conf_file
def gff_to_jbrowse(self, gff_file, index_html, data_dir_out, jbrowse_url=None ): opt = self.opt if jbrowse_url is None: jbrowse_url = opt["jbrowse_url"] env = None if self.opt.get("jbrowse_bin_dir",None): env = os.environ.copy() util.add_to_path(opt["jbrowse_bin_dir"], prepend=True, env=env) if not os.path.exists(data_dir_out): os.makedirs(data_dir_out) gff_file = util.abspath(gff_file) #fasta_file = util.abspath(fasta_file) jbrowse_out_dir = os.path.join(data_dir_out,opt["jbrowse_data_subdir"]) #can use os.devnull to discard all output jbrowse_conv_log_base = os.path.join(os.getcwd(),"jbrowse_conv_log") with nested( open(jbrowse_conv_log_base+".out","w"),\ open(jbrowse_conv_log_base+".err","w") ) as ( stdout, stderr ): check_call(["prepare-refseqs.pl","--gff",gff_file,"--out",jbrowse_out_dir], env=env, stdout=stdout, stderr=stderr) #@todo use biodb-to-json instead with flat file input, and accept config #file as a parameter (provide a default one too). See volvox.json config #in the distribution. Also add dropped_features param to load everything #unique in field 3 of GFF and check that only dropped_features are missing #from the config check_call(["flatfile-to-json.pl","--gff",gff_file,"--out",jbrowse_out_dir, "--trackLabel","Genes", "--cssClass","feature5", "--type","gene", "--autocomplete","all" "--getLabel", "--getType" ], env=env, stdout=stdout, stderr=stderr) check_call(["flatfile-to-json.pl","--gff",gff_file,"--out",jbrowse_out_dir, "--trackLabel","CDS", "--cssClass","generic_parent", "--subfeatureClasses",'{ "exon" : "exon" }', "--type","CDS", "--type","exon", "--autocomplete","all" "--getLabel", "--getType", "--getSubs", "--getPhase" ], env=env, stdout=stdout, stderr=stderr) check_call(["flatfile-to-json.pl","--gff",gff_file,"--out",jbrowse_out_dir, "--trackLabel","Peptides", "--cssClass","est", "--subfeatureClasses",'{ "mat_peptide" : "transcript-CDS" }', "--type","mat_peptide", "--autocomplete","all" "--getLabel", "--getType", "--getSubs", "--getPhase" ], env=env, stdout=stdout, stderr=stderr) check_call(["flatfile-to-json.pl","--gff",gff_file,"--out",jbrowse_out_dir, "--trackLabel","Misc", "--cssClass","feature3", "--type","misc_feature", "--autocomplete","all" "--getLabel", "--getType", "--getSubs", "--getPhase" ], env=env, stdout=stdout, stderr=stderr) check_call(["generate-names.pl","--out",jbrowse_out_dir], env=env, stdout=stdout, stderr=stderr) tracks_conf_file = os.path.join(jbrowse_out_dir,"trackList.json") tracks_conf = util.load_config_json(tracks_conf_file) tracks_conf["refSeqDropdown"] = True #show pull-down even for very many sequences util.save_config_json(tracks_conf,tracks_conf_file) #create index.html that redirects to JBrowse index.html with correct data param etc _jbrowse_dataset_index_html = \ config.get_data_string(self.opt["jbrowse_galaxy_index_html_tpl"], "galaxy.index.html") jbrowse_url_params = util.to_url_params(dict( tracks=",".join(("DNA","Genes","CDS","Peptides","Misc")), tracklist=0 )) with open(index_html,"w") as f: f.write(_jbrowse_dataset_index_html.\ format(jbrowse_url=jbrowse_url.rstrip("/"), jbrowse_data_subdir=opt["jbrowse_data_subdir"], jbrowse_url_params=jbrowse_url_params))