triplets_plotly.py 2.36 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#! /usr/bin/env python3

### ASDP PIPELINE ###
## triplets_plotly.py
## Version : 0.0.1
## Licence : FIXME
## Description : script to get automatically graphics from expansion pipeline results from getResults.py with Plotly
## Usage :
## Output : FIXME
## Requirements : FIXME

## Author : anne-sophie.denomme-pichon@u-bourgogne.fr
## Creation Date : 20200202
## last revision date : 20200202
## Known bugs : None

import collections
import csv
import os
import sys

22
path = '/work/gad/shared/analyse/STR/results'
23

24
def display_console_graph(title, tools, data):
25
    print(title)
26 27 28 29
    for tool, tool_data in zip(tools, data):
        print(tool)
        for x, y in tool_data:
            print(f'{x}\t{"*" * y}')
30

31
def display_html_graph(title, tools, data):
32
    import plotly.graph_objects as go
33
    import plotly.subplots as sp
34

35 36 37 38
    figure = sp.make_subplots(
        rows=len(tools),
        cols=1,
        subplot_titles=tools
39 40
    )

41 42 43 44 45 46 47 48 49 50 51 52 53
    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())
54 55 56

def graph_locus(locus):
    title = f'Effectif pour chaque nombre de répétitions au locus {locus}'
57
    data = []
58 59 60
    with open(f'{path}{os.sep}{locus}.tsv') as result_file:
        tsvreader = csv.reader(result_file, delimiter='\t')
        try:
61 62
            tools = next(tsvreader)[1:]
            counters = [collections.Counter({0: 0}) for tool in tools]
63
            for row in tsvreader:
64 65 66 67
                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
68 69 70
        except StopIteration:
            print('Input file is empty', file=sys.stderr)
            sys.exit(1)
71 72 73 74
        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)
75 76 77 78 79 80

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])