if track.endswith(".bb") or track.endswith(".bigBed") ] for bigBed_file in bigBed_files: color = "0,100,0" if "pos" in bigBed_file else "100,0,0" base_track = remove_special_chars(os.path.basename(bigBed_file)) long_name = args.sep.join(base_track.split( args.sep)[:args.num_sep]) + ".bb" track = Track(name=long_name, url=os.path.join(URLBASE, GENOME, base_track), tracktype="bigBed", short_label=long_name, long_label=long_name, color=color, local_fn=bigBed_file, remote_fn=os.path.join(upload_dir, GENOME, base_track), visibility="dense") #trackdb.add_tracks(track) supertrack.add_track(track) trackdb.add_tracks(supertrack) result = hub.render() hub.remote_fn = os.path.join(upload_dir, "hub.txt") for track in trackdb.tracks: upload_track(track=track, host=args.server, user=args.user, run_s3=args.no_s3) upload_hub(hub=hub, host=args.server, user=args.user, run_s3=args.no_s3)
if track.endswith(".bw") or track.endswith('.bigWig'): tracktype = "bigWig" if track.endswith(".bb") or track.endswith('.bigBed'): tracktype = "bigBed" if track.endswith(".bam"): tracktype = "bam" print base_track track = Track( name= base_track, url = os.path.join(URLBASE, GENOME, base_track), tracktype = tracktype, short_label=base_track, long_label=base_track, color = color, local_fn = track, remote_fn = os.path.join(upload_dir, GENOME, base_track) ) aggregate.add_subtrack(track) trackdb.add_tracks(aggregate) result = hub.render() hub.remote_fn = os.path.join(upload_dir, "hub.txt") for track in trackdb.tracks: upload_track(track=track, host=args.server, user=args.user) upload_hub(hub=hub, host=args.server, user=args.user)
def main(): parser = argparse.ArgumentParser( description= 'Takes in files to turn into trackhub. This version automatically ') # tracks files ############## parser.add_argument('files', nargs='+', help='Files to turn into track hub') # namings ######### parser.add_argument('--hub', help="hub name (no spaces)", required=True) parser.add_argument('--genome', help="genome name", required=True) # upload (in fact run_local=True) ######## #parser.add_argument('--no_s3', default=False, action="store_true", help="upload to defined server instead of s3") #parser.add_argument('--serverscp', default="tscc-login2.sdsc.edu", help="server to SCP to") #parser.add_argument('--user', default='adomissy', help="that is uploading files") # parser.add_argument('--uploaddir', default='yeolab-trackhubs', help="directory to upload files to if not uploading to aws") # web access ############ # parser.add_argument('--urldomain', default="s3-us-west-2.amazonaws.com", help="url domain for public access to trackhubs") # parser.add_argument('--urldir', default="yeolab-trackhubs", help="url directory for public access to trackhubs") # hub labels ############ # parser.add_argument('--hub_short_label', default=None, help="short label for hub") # parser.add_argument('--hub_long_label', default=None, help="long label for hub") parser.add_argument('--hub_email', default='*****@*****.**', help="email for hub") # name parts grouping ##################### parser.add_argument('--sep', default=".", help="Seperator") parser.add_argument('--num_sep', default=2, type=int, help="Number of seperators deep to group on") ########################################################################### args = parser.parse_args() # TODO: unhack this, but let's keep all trackhubs here for now. urldomain = "s3-us-west-2.amazonaws.com" urldir = "yeolab-trackhubs" uploaddir = "yeolab-trackhubs" hub_name = args.hub hub_email = args.hub_email # default label settings ######################## hub_short_label = hub_name hub_long_label = hub_name # hard coding serverscp, in variable HOST HOST = "localhost" # hard coding user, in variable USER USER = "******" GENOME = args.genome # hack for tutorial dataset so it is easy to view in ucsd genome browser if GENOME == 'hg19chr19kbp255': GENOME == 'hg19' uploaddir = os.path.join(uploaddir, hub_name) URLBASE = os.path.join("http://" + urldomain + "/" + urldir + "/", hub_name) # create data structures ######################## hub = Hub( hub=hub_name, short_label=hub_short_label, long_label=hub_long_label, email=hub_email, ) hub.upload_fn = uploaddir genomes_file = GenomesFile() hub.add_genomes_file(genomes_file) genome = Genome(GENOME) genomes_file.add_genome(genome) trackdb = TrackDb() genome.add_trackdb(trackdb) supertrack = SuperTrack(name=hub_name, short_label=hub_short_label, long_label=hub_long_label) # separate bigwigs, bigbeds and others for different processing methods ####################################################################### bigwig_files = [ file for file in args.files if file.endswith(".posbw") or file.endswith(".negbw") or file.endswith( ".bw") or file.endswith(".bigWig") or file.endswith(".bigwig") ] bigbed_files = [ file for file in args.files if file.endswith(".bb") or file.endswith(".bigBed") or file.endswith(".bigbed") ] # not used #other_files = [file for file in args.files if (file not in bigwig_files and file not in bigbed_files )] # process bigwig files , re-grouped by third 2 dot-sepatarated name-parts, as multitracks ########################################################################################## key_func = lambda x: x.split(args.sep)[:args.num_sep] for group_key, group_bigwig_files in groupby( sorted(bigwig_files, key=key_func), key_func): group_bigwig_files_list = list(group_bigwig_files) print("args sep: {}".format(args.sep)) print("args num sep: {}".format(args.num_sep)) print("split filename: {}".format(bigwig_files[0].split( args.sep)[:args.num_sep])) print "-----------------------------------------" print "processing bigwig files group with key :", group_key print "comprised of following files:", group_bigwig_files_list print "-----------------------------------------" long_name = remove_plus_and_pct( os.path.basename(args.sep.join(group_key[:args.num_sep]))) aggregate = AggregateTrack(name=long_name, tracktype='bigWig', short_label=long_name, long_label=long_name, aggregate='transparentOverlay', showSubtrackColorOnUi='on', autoScale='on', priority='1.4', alwaysZero='on', visibility="full") for bigwigfile in group_bigwig_files_list: print "--------------------------" print "bigwigfile", bigwigfile print "--------------------------" base_track = remove_plus_and_pct(os.path.basename(bigwigfile)) split_track = base_track.split(args.sep) long_name = args.sep.join(split_track[:args.num_sep] + split_track[-3:]) color = "0,100,0" if "pos" in bigwigfile else "100,0,0" track = Track(name=long_name, url=os.path.join(URLBASE, GENOME, base_track), tracktype="bigWig", short_label=long_name, long_label=long_name, color=color, local_fn=bigwigfile, remote_fn=os.path.join(uploaddir, GENOME, base_track)) #print "aggregate.add_subtrack", track.name aggregate.add_subtrack(track) #print "supertrack.add_track", aggregate supertrack.add_track(aggregate) #print "trackdb.add_tracks", aggregate #trackdb.add_tracks(aggregate) # process bigbed files as single track ###################################### for bigbed_file in bigbed_files: # print "--------------------------" # print "bigbedfile", bigbedfile # print "--------------------------" color = "0,100,0" if "pos" in bigbed_file else "100,0,0" base_track = remove_plus_and_pct(os.path.basename(bigbed_file)) long_name = args.sep.join(base_track.split( args.sep)[:args.num_sep]) + ".bb" track = Track(name=long_name, url=os.path.join(URLBASE, GENOME, base_track), tracktype="bigBed", short_label=long_name, long_label=long_name, color=color, local_fn=bigbed_file, remote_fn=os.path.join(uploaddir, GENOME, base_track), visibility="full") #trackdb.add_tracks(track) supertrack.add_track(track) trackdb.add_tracks(supertrack) result = hub.render() hub.remote_fn = os.path.join(uploaddir, "hub.txt") # process bigbed files (bam?) ###################### ## UNUSED # if bigwigfile.endswith(".bw") or bigwigfile.endswith('.bigWig'): tracktype = "bigWig" # if bigwigfile.endswith(".bb") or bigwigfile.endswith('.bigBed'): tracktype = "bigBed" # if bigwigfile.endswith(".bam"): tracktype = "bam" # 'upolading' (locally) ######################## for track in trackdb.tracks: #print("upload_track(track=" + track.__repr__() + ", host=" + args.serverscp + ", user="******"run_local=True") #upload_track(track=track, host=args.serverscp, user=args.user) # upload_track(track=track, host=args.serverscp, user=args.user, run_s3=args.no_s3) upload_track(track=track, host=HOST, user=USER, run_local=True) #print("upload_hub(hub=" + hub.__repr__() + ", host=" + args.serverscp + ", user="******"run_local=True") #upload_hub(hub=hub, host=args.serverscp, user=args.user) # upload_hub(hub=hub, host=args.serverscp, user=args.user, run_s3=args.no_s3) pass upload_hub(hub=hub, host=HOST, user=USER, run_local=True) # print("UPLOADDIR: {}".format(uploaddir)) print("BUCKET: {}".format(uploaddir)) copy_dir_to_aws( src=uploaddir, dest=uploaddir, ) print("FINAL URL: {}/hub.txt".format(URLBASE))
def main(): parser = argparse.ArgumentParser( description='Make trackhubs for UCSC browser using bigBed files. \ Outputs to CURRENT DIRECTORY.') parser.add_argument('inputdir', metavar='INDIR', help='Directory containing bigBed files .bb ending') parser.add_argument('outdir', metavar='OUTDIR', help='Directory for staging files') parser.add_argument('--quiet', '-q', action='store_true', help='Suppress some print statements') parser.add_argument('--render', '-r', action='store_true', help='Render file to current dir') parser.add_argument('--upload', '-u', action='store_true', help='Upload file to webserver') parser.add_argument('--mm9', '-m', action='store_true', help='Switch from mm10 to mm9') parser.add_argument('--has_strand', '-s', action='store_true', help='Bed has strand (changes from 5 columns to 6)') parser.add_argument('--suffix', '-S', metavar="trackhub label suffix", default="", help='Suffix to label, for example H3K4me1') args = parser.parse_args() # store command line arguments for reproducibility CMD_INPUTS = ' '.join(['python'] + sys.argv) # easy printing later # store argparse inputs for reproducibility / debugging purposes args_dic = vars(args) ARG_INPUTS = ['%s=%s' % (key, val) for key, val in args_dic.iteritems()] # ARG_INPUTS = ['%s=%s' % (key, val) for key, val in args_dic.items()] ARG_INPUTS = ' '.join(ARG_INPUTS) # Print arguments supplied by user if not args.quiet: print('Command line inputs:') print(CMD_INPUTS) print('Argparse variables:') print(ARG_INPUTS) # define constants (hard coded) if args.mm9: genobuild = "mm9" else: genobuild = "mm10" jsuffix = "%s_%s" % (genobuild, args.suffix) print("Assigning prefix: %s" % jsuffix) # dirname: motevo_from_peaks/H3K4me1_peaks dirname = "motevo_from_peaks/%s_peaks/motevo_motifs_%s" % (args.suffix, jsuffix) hubname = "motevo_motifs_%s" % jsuffix shortlab = "motevo_%s" % jsuffix longlab = "Motevo motifs %s" % jsuffix email = "*****@*****.**" # url = "http://upnaepc2.epfl.ch" url = "http://upnaesrv1.epfl.ch" assay = "bigbed" jvis = "dense" # bigbed options loaded into ViewTrack jspectrum = "on" scoremax = 1000 scoremin = 500 # define URLs url_main = "%s/%s" % (url, dirname) url_base = "%s/%s/data" % (url, dirname) # upload_main = "~/Sites/%s" % dirname # upload_base = "~/Sites/%s/data" % dirname upload_main = "%s" % hubname upload_base = "%s/data" % hubname if not args.has_strand: ftype = "bigBed 5" else: ftype = "bigBed 6" # host = "circadian.epfl.ch" # user = "******" host = "upnaesrv1.epfl.ch" user = "******" # define constants genomebuild = genobuild files_dic = get_files_from_dir(args.inputdir, ext=".bb") samples_dic = {} for sample in files_dic.keys(): samples_dic[sample] = sample # init hub genomes file genome trackdb # Make my hub hub = Hub(hub=hubname, short_label=shortlab, long_label=longlab, email=email) # url = "%s/%s" % (url, dirname)) hub.url = os.path.join(url_main, "%s.hub.txt" % hub.hub) genomes_file = GenomesFile() genome = Genome(genomebuild) trackdb = TrackDb() # add remote fn # hub.remote_fn = os.path.join(upload_main, "hub.txt") # genomes_file.remote_fn = os.path.join(upload_main, "genomes.txt") hub.remote_fn = upload_main genomes_file.remote_fn = upload_main trackdb.remote_fn = os.path.join(upload_main, genomebuild, "trackDb.txt") hub.add_genomes_file(genomes_file) genome.add_trackdb(trackdb) genomes_file.add_genome(genome) # init composite composite = CompositeTrack(name=hubname, short_label=shortlab, long_label=longlab, tracktype=ftype) # make subgroups subgroups = [ SubGroupDefinition(name="sample", label="sample", mapping=samples_dic), ] composite.add_subgroups(subgroups) # make viewTrack, a hierarchy containing my files, for example view = ViewTrack( name="%sViewTrack" % assay, view="%s" % assay, visibility=jvis, tracktype=ftype, short_label="%s" % assay, long_label="%s assay" % assay, # big bed labels spectrum=jspectrum, scoreMin=scoremin, scoreMax=scoremax) composite.add_view(view) # make track for sample, wfs in files_dic.iteritems(): for wf in wfs: sampname = os.path.basename(wf) bname = sampname.split(".")[0] track = Track(name=bname, tracktype=ftype, url=os.path.join(url_base, "%s" % sampname), local_fn=os.path.abspath(wf), remote_fn=os.path.join(upload_base, "%s" % sampname), visibility=jvis, shortLabel=bname, longLabel=bname, spectrum=jspectrum, scoreMin=scoremin, scoreMax=scoremax, subgroups={"sample": sample}) view.add_tracks(track) trackdb.add_tracks(composite) print('Track looks like this:') print(trackdb) if args.render: # print('Rendering to %s' % hub.local_fn) # results = hub.render() # upload_hub(hub=hub, host='localhost', remote_dir='example_grouping_hub') stage_hub(hub, staging=args.outdir) if args.upload: print('Uploading to [email protected]') # for track in trackdb.tracks: # upload_track(track = track, host = host, user = user) upload_hub(hub=hub, host=host, user=user, remote_dir="/data/web/sites/motevo_from_peaks") print('Subgroups:') for sg in subgroups: print(sg) print("Staging to path: %s" % args.outdir)