str_plotly.py 2.65 KB
Newer Older
1 2 3
#! /usr/bin/env python3

### ASDP PIPELINE ###
4 5
## Version: 0.0.1
## Licence: AGPLv3
6
## Author : Anne-Sophie Denommé-Pichon
7
## Description: script to get automatically graphics from expansion pipeline results from getResults.py with Plotly
8 9 10

import collections
import csv
11
import logging
12
import os
13
import os.path
14 15
import sys

16 17 18 19 20 21 22 23 24 25 26 27
output_directory = None

with open(os.path.join(os.path.dirname(sys.argv[0]), 'config.sh'))) as config:
    for line in config:
        if '=' in line:
            variable, value = line.split('=', 1)
            elif variable == 'RESULTS_OUTPUTDIR':
                output_directory = value.split('#')[0].strip('"\' ') # strip double quotes, simple quotes and spaces

if output_directory is None
    logging.error('RESULTS_OUTPUTDIR is missing in config.sh')
    sys.exit(1)
28

29
def display_console_graph(title, tools, data):
30
    print(title)
31 32 33 34
    for tool, tool_data in zip(tools, data):
        print(tool)
        for x, y in tool_data:
            print(f'{x}\t{"*" * y}')
35

36
def display_html_graph(title, tools, data):
37
    import plotly.graph_objects as go
38
    import plotly.subplots as sp
39

40 41 42 43
    figure = sp.make_subplots(
        rows=len(tools),
        cols=1,
        subplot_titles=tools
44 45
    )

46 47 48 49 50 51 52 53 54 55 56 57 58
    for tool_id in range(len(tools)):
        figure.add_trace(
            go.Bar(
                x=[x for (x, y) in data[tool_id]],
                y=[y for (x, y) in data[tool_id]]
            ),
            row=tool_id + 1,
            col=1
        )

    figure.update_layout(title_text=title, showlegend=False)

    print(figure.to_html())
59 60 61

def graph_locus(locus):
    title = f'Effectif pour chaque nombre de répétitions au locus {locus}'
62
    data = []
63
    with open(f'{output_directory}{os.sep}{locus}.tsv') as result_file:
64 65
        tsvreader = csv.reader(result_file, delimiter='\t')
        try:
66 67
            tools = next(tsvreader)[1:]
            counters = [collections.Counter({0: 0}) for tool in tools]
68
            for row in tsvreader:
69 70 71 72
                for tool_id in range(len(tools)):
                    if row[tool_id + 1] not in ['.', 'nofile']:
                        for count in row[tool_id + 1].split(','):
                            counters[tool_id][int(count)] += 1
73 74 75
        except StopIteration:
            print('Input file is empty', file=sys.stderr)
            sys.exit(1)
76 77 78 79
        for tool_id in range(len(tools)):
            data.append(sorted(counters[tool_id].items()))
        #display_console_graph(title, tools, data)
        display_html_graph(title, tools, data)
80 81 82 83 84 85

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print(f'Usage: {sys.argv[0].split(os.sep)[-1]} <LOCUS>', file=sys.stderr)
        sys.exit(1)
    graph_locus(sys.argv[1])