def make_igv(url, files_dir):

    # Get all file types in files_dir.
    file_list = list(
        set(os.path.splitext(file)[1] for file in os.listdir(files_dir)))
    ext_list = [x for x in file_list if x in FILE_EXTENSIONS]

    # Get track types
    track_types = list(map(lambda item: item.replace(".", ""), ext_list))

    # Get the genome file.
    fasta_idx = os.path.join(files_dir, f'*.fa.fai')
    genome = glob.glob(fasta_idx)[0].replace(".fai", "")
    genome = os.path.basename(genome)

    # Collect all bam files.
    bam_files = get_files(files_dir, f'*.bam')

    # Collect all samples.
    samples = [os.path.splitext(os.path.basename(f))[0] for f in bam_files]

    path = os.path.join(url, os.path.basename(files_dir))

    # Render template.
    igv_template = "igv.xml"
    data = dict(genome=genome,
                path=path,
                samples=samples,
                track_types=track_types)
    html = render_template(data, igv_template)
    print(html)
Beispiel #2
0
def demo():
    from random import randint

    def make(x):
        value = randint(1, 5)
        return f'A{value}', value

    p1 = ChartParams()
    p1.data = map(make, range(50))

    p1.xlabel = "Size 1"
    p1.ylabel = "Value 1"

    p1.options = '''    
        title: 'Histogram Plot 1',
        legend: {position: 'none'},
    '''

    p2 = ChartParams()
    p2.type = 'BarChart'
    p2.data = map(make, range(15))
    p2.xlabel = "Size 2"
    p2.ylabel = "Value 2"

    p2.options = '''    
           title: 'Histogram Plot 2' ,
           legend: {position: 'none'},
       '''

    # Plot table.

    t1 = TableParams()
    rows = [["'Mike'", 10000, 'true'], ["'Jim'", 8000, 'false'],
            ["'Alice'", 12500, 'true'], ["'Bob'", 7000, 'false']]
    columns = [('string', 'Name'), ('number', 'Salary'),
               ('boolean', 'Full Time Employee')]
    t1.columns = columns
    t1.rows = rows
    t1.options = '''    
            title: 'Table 1',
            width: '50%',
            height: '50%'
        '''

    # This is the context.
    data = dict(p1=p1, p2=p2, t1=t1)

    name = "chart_demo.html"

    html = render_template(data, name)

    with open('index.html', 'wt') as fp:
        fp.write(html)
Beispiel #3
0
def plot(data1, data2, data3):

    # Plot 1
    # Sort by counts.
    data1.sort(key=lambda x: int(x[1]), reverse=True)

    # Get  only single-end specific details.
    details = [
        'total', 'mapped', 'supplementary', 'secondary', 'duplicates',
        'singletons'
    ]
    data1 = list(filter(lambda x: x[0].lower() in details, data1))

    p1 = ChartParams()
    p1.type = 'BarChart'
    p1.data = data1
    p1.xlabel = "Flag category"
    p1.ylabel = "Read counts"
    p1.options = '''    
            title: '',
            legend: {position: 'none'},
            colors : ['green']
        '''

    # Plot 2
    p2 = ChartParams()
    p2.type = 'BarChart'
    p2.data = data2
    p2.xlabel = "Chromosomes"
    p2.ylabel = "Read counts"
    p2.options = '''    
            title: '',
            legend: {position: 'none'},
            colors: ['green']
        '''

    # Plot table.
    t1 = TableParams()
    t1.rows = data3.rows
    t1.columns = data3.columns

    t1.options = '''    
               width : '100%',
               height : '50%'
           '''

    # This is the context.
    data = dict(p1=p1, p2=p2, t1=t1)

    name = "bwa_classify.html"

    html = render_template(data, name)
    print(html)
Beispiel #4
0
def plot(data):
    # Plot 1

    p1 = ChartParams()
    p1.type = 'BarChart'
    p1.data = data
    p1.xlabel = "Species"
    p1.ylabel = "Read counts"
    p1.options = '''    
            title: 'Unique reads in species',
            legend: {position: 'none'},
        '''

    # Plot 2

    p2 = ChartParams()
    p2.type = 'PieChart'
    p2.data = data
    p2.xlabel = "Species"
    p2.ylabel = "Read counts"
    p2.options = '''
            title: 'Unique reads in species',
            chartArea:{left:20,top:0,width:'80%',height:'75%'},
            legend:{position:'left'}

        '''

    # Plot3

    locations = [(41.015908, -77.53246, 'Lamar PA')]
    p3 = ChartParams()
    p3.type = 'Map'
    p3.data = locations
    p3.options = '''
            zoomLevel: 6,
            showTooltip: true,
            showInfoWindow: true

        '''

    # This is the context.
    data = dict(p1=p1, p2=p2, p3=p3)

    name = "classify.html"

    html = render_template(data, name)

    print(html)
Beispiel #5
0
def plot(data1, data2):

    # Plot 1
    # Sort by counts.
    data1.sort(key=lambda x: int(x[1]), reverse=True)

    # Get  only single-end specific details.
    details = [
        'total', 'mapped', 'supplementary', 'secondary', 'duplicates',
        'singletons'
    ]
    data1 = list(filter(lambda x: x[0].lower() in details, data1))

    p1 = ChartParams()
    p1.type = 'BarChart'
    p1.data = data1
    p1.xlabel = "Flag category"
    p1.ylabel = "Read counts"
    p1.options = '''    
            title: 'Alignment details.',
            legend: {position: 'none'},
        '''

    # Plot 2
    p2 = ChartParams()
    p2.type = 'BarChart'
    p2.data = data2
    p2.xlabel = "Chromosomes"
    p2.ylabel = "Read counts"
    p2.options = '''    
            title: 'Mapped counts per chromosome.',
            legend: {position: 'none'},
        '''

    # This is the context.
    data = dict(p1=p1, p2=p2)

    name = "align/align_index.html"

    html = render_template(data, name)
    print(html)
    return plot1


if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(
        description='Create barcharts from samtools idxstats output.')

    parser.add_argument('--mapped', help='File with idxstats output ')
    parser.add_argument('--total', help='File with total read counts ')
    parser.add_argument('--selected',
                        help='No. of reads subselected for mapping')

    args = parser.parse_args()

    # Read the arguments
    mapped = args.mapped
    total = args.total
    selected = args.selected

    plot1 = create_plot(mapped, total, selected)

    # This is the context.
    data = dict(p1=plot1)

    name = "align/scaffold_index.html"
    html = render_template(data, name)
    print(html)