Esempio n. 1
0
def histone_track(name, color="200,0,0"):
    composite = trackhub.CompositeTrack(name=name + "_composite",
                                        short_label=name,
                                        tracktype='bigWig',
                                        visibility='full',
                                        color=color)
    signal_view = trackhub.ViewTrack(name=name + "_signal",
                                     view='signal',
                                     visibility='full',
                                     tracktype='bigWig',
                                     short_label=name + '_signal',
                                     autoScale="on")
    regions_view = trackhub.ViewTrack(name=name + '_region',
                                      view='regions',
                                      visibility='dense',
                                      tracktype='bigWig',
                                      short_label=name + '_regions')

    composite.add_view(signal_view)
    composite.add_view(regions_view)

    for signal_type in ["qvalues", "treat_pileup", "control_lambda"]:
        track = trackhub.Track(tracktype='bigWig',
                               name=name + "_" + signal_type,
                               url="%s_%s.bw" % (name, signal_type),
                               short_label=signal_type,
                               autoScale="on")
        signal_view.add_tracks(track)
    for region_type in ["peaks", "domains"]:
        track = trackhub.Track(name=name + "_" + region_type,
                               url="%s_%s.bb" % (name, region_type),
                               short_label=region_type,
                               tracktype='bigBed')
        regions_view.add_tracks(track)
    return composite
def make_bam_trackhub(libraries, trackdb):
    cluster = trackhub.SubGroupDefinition(
            name='cluster',
            label='cluster',
            mapping=cluster_mapping)

    bam_composite = trackhub.CompositeTrack(
        name='reads',
        short_label='reads',
        dimensions='dimX=cluster',
        tracktype='bam',
        visibility='dense',
    )
    bam_composite.add_subgroups([cluster])
    trackdb.add_tracks(bam_composite)

    bam_view = trackhub.ViewTrack(
        name='readview',
        view='reads',
        visibility='dense',
        tracktype='bam',
        short_label='Reads')
    bam_composite.add_view(bam_view)

    curdir = os.getcwd()
    #os.chdir(public_dir)

    try:
        for i, (library_id, row) in enumerate(libraries.iterrows()):
            cluster_assignment = human_cluster_mapping[row.cluster_assignment]
            priority = "{:03d}".format(i)
            path, name = os.path.split(row.bam)
            bam_index = row.bam + '.bai'
            if not os.path.exists(name):
                os.symlink(row.bam, name)
            if not os.path.exists(name + '.bai'):
                os.symlink(bam_index, name + '.bai')

            track = trackhub.Track(
                url=make_home_url(row['bam']),
                name=priority + '_' + row.analysis_name + '_reads',
                visibility='full',
                tracktype='bam',
                subgroups={'cluster': cluster_assignment},
                color=hex_to_ucsc_color(colors[row.cluster_assignment]),
                priority=i,
            )
            bam_view.add_tracks(track)
    finally:
        os.chdir(curdir)
def make_bigwig_trackhub(libraries, trackdb):
    cluster = trackhub.SubGroupDefinition(
            name='cluster',
            label='cluster',
            mapping=cluster_mapping)

    subgroups = [
        cluster,
        trackhub.SubGroupDefinition(
            name='multi',
            label='multi',
            mapping={
                'uniq': 'uniq',
                'all': 'all',
            }),
    ]

    composite = trackhub.CompositeTrack(
        name='composite',
        short_label='signal',
        dimensions='dimX=cluster dimY=multi',
        tracktype='bigWig',
        visibility='dense',
    )
    composite.add_subgroups(subgroups)
    trackdb.add_tracks(composite)

    signal_view = trackhub.ViewTrack(
        name='signalviewtrack',
        view='signal',
        visibility='dense',
        tracktype='bigWig',
        short_label='Signal')
    composite.add_view(signal_view)

    for i, (library_id, row) in enumerate(libraries.iterrows()):
        for track_index, track_type in enumerate(['uniq', 'all']):
            cluster_assignment = human_cluster_mapping[row.cluster_assignment]
            priority = i * 10 + track_index
            track = trackhub.Track(
                url=make_home_url(row[track_type]),
                name="{:04d}".format(priority) + '_' + row.analysis_name + '_' + track_type,
                visibility='full',
                tracktype='bigWig',
                subgroups={'cluster': cluster_assignment, 'multi': track_type},
                color=hex_to_ucsc_color(colors[row.cluster_assignment]),
                priority=priority,
            )
            signal_view.add_tracks(track)
Esempio n. 4
0
composite = trackhub.CompositeTrack(
    name='composite',
    short_label='Signal and regions',
    tracktype='bigWig',
    visibility='full',
    html_string=dedent("""
    Composite track
    ---------------

    This is a composite track that groups together signal (bigWig format)
    and regions (bigBed format) into a single configurable container.

    See `here <https://genome.ucsc.edu/goldenPath/help/trackDb/trackDbHub.html#compositeTrack>`_
    for details.

    The following is a test of various ReStructured Text contstructs.

    Here's a literal block::

        #!/usr/bin/env python
        import trackhub

    Here's an example definition block:

    :example definition:
        Definition would go here.

    A list:

    - list item 1
    - list item 2

    A numbered list:

    #. one
    #. two

        **bold**, *italic*, ``monospace``, all within a ReST block quote

    A table:

    ======== ========
      col 1   col 2
    ======== ========
      item1   item2
      item3   item4
    ======== ========

    See [1]_ for more.

    .. [1] http://docutils.sourceforge.net/docs/ref/rst/restructuredtext.html
    """),
)
Esempio n. 5
0
def create_trackhub(OutFolder,ListFile,Genome,EMAIL,DesignFile,Postfix,PathPrefix=''):
    ERROR_STR = 'ERROR: Please check design file'
    HEADER = ['group', 'replicate', 'fastq_1', 'fastq_2']
    
    makedir(OutFolder)
    
    dIn = open(DesignFile, 'r')
    header = dIn.readline().strip().split(',')
    if header[:4] != HEADER:
        print("{} header: {} != {}".format(ERROR_STR,','.join(header),','.join(HEADER)))
        sys.exit(1)

    paramColn = {}
    for i in range(len(header)):
      if header[i][:6]=="track_":
        paramColn[header[i][6:]]=i

    sampleDesignDict = {}
    designDict = {}
    if paramColn:
      while True:
        line = dIn.readline()
        if line:
          lspl = [x.strip() for x in line.strip().split(',')]
          lspl[0] = [lspl[0]+Postfix[1], lspl[0]+'_R'+lspl[1]+Postfix[0]]
          lspl[0] = [trackhub.helpers.sanitize(lspl[0][0].replace(".", "_"), strict=False),trackhub.helpers.sanitize(lspl[0][1].replace(".", "_"), strict=False)]
          sampleDesignDict[lspl[0][0]] = {}
          sampleDesignDict[lspl[0][1]] = {}
          for k in paramColn.keys():
            sampleDesignDict[lspl[0][0]][k]=lspl[paramColn[k]]
            sampleDesignDict[lspl[0][1]][k]=lspl[paramColn[k]]
            if k in designDict:
              designDict[k][lspl[paramColn[k]]] = lspl[paramColn[k]]
            else:
              designDict[k] = {lspl[paramColn[k]]:lspl[paramColn[k]]}
        else:
          break

    
    dIn.close()
      
    fileList = []
    fin = open(ListFile,'r')
    while True:
        line = fin.readline()
        if line:
            ifile,colour = line.strip().split('\t')
            if sampleDesignDict:
                kfile = trackhub.helpers.sanitize(os.path.splitext(os.path.basename(ifile))[0].replace(".", "_"), strict=False)
                if kfile in sampleDesignDict:
                  if "color" in sampleDesignDict[kfile]:
                    h = sampleDesignDict[kfile]["color"].lstrip('#')
                    colour = ','.join(str(x) for x in tuple(int(h[i:i+2], 16) for i in (0, 2, 4)))
            if len(colour.strip()) == 0:
              colour = '0,0,178'
            fileList.append((PathPrefix.strip()+ifile,colour))
        else:
            break
            fin.close()
    

    # Initialize the components of a track hub, already connected together
    hub, genomes_file, genome, trackdb = trackhub.default_hub(
        hub_name="RNISRS_hub",
        short_label='Regeneromics Shared Resource hub',
        long_label='Regeneration Next Initiative Regeneromics Shared Resource hub',
        genome=Genome,
        email=EMAIL)
    
    # create compositeTracks
    if sampleDesignDict:
      composite = trackhub.CompositeTrack(
        name = 'composite',
        short_label='singlal'
      )
      # Add those subgroups to the composite track
      subgroups = []
      for k in designDict.keys():
        if k!='color':
          subg = trackhub.SubGroupDefinition(
            name=k,
            label=k,
            mapping=designDict[k]
          )
          subgroups.append(subg)
      
      composite.add_subgroups(subgroups)
      
      # Add the composite track to the trackDb
      trackdb.add_tracks(composite)
      signal_view = trackhub.ViewTrack(
          name='signalviewtrack',
          view='signal',
          short_label='Signal')
      composite.add_view(signal_view)
      regions_view = trackhub.ViewTrack(
          name='regionsviewtrack',
          view='regions',
          short_label='Regions')
      composite.add_view(regions_view)
    
    for ifile,color in fileList:
        extension = os.path.splitext(ifile)[1].replace(".", "").lower()
        filename = trackhub.helpers.sanitize(os.path.splitext(os.path.basename(ifile))[0].replace(".", "_"), strict=False)
        if extension in ['bed','broadpeak','narrowpeak']:
          pass
        elif extension in TrackType.keys():
          if filename in sampleDesignDict:
            track = trackhub.Track(
              name=filename,
              source=ifile,
              color=color,
              visibility=Visibility[extension],
              tracktype=TrackType[extension],
              subgroups=sampleDesignDict[filename],
              autoScale='on')
            signal_view.add_tracks(track)
          else:
            track = trackhub.Track(
              name=filename,
              source=ifile,
              color=color,
              visibility=Visibility[extension],
              tracktype=TrackType[extension],
              autoScale='on')
            trackdb.add_tracks(track)
          linkname=os.path.join(OutFolder, Genome, filename+"."+TrackType[extension])
          makedir(os.path.join(OutFolder, Genome))
          os.symlink(ifile, linkname)
        else:
          pass
    
    hub.render(staging=OutFolder)