import json
import re
import sys

data = []

#Filtruje vrcholy zarovnania z json suboru
def one_line_parse(one_line, out_file, is_assembly):
    if (is_assembly == '1'):
        out_file.write(one_line['name'] + '\n ')
    else:
        out_file.write(re.split("_", one_line['name'])[18] + '\n ')
    path = one_line['path']
    mapping = path['mapping']
    read_cur_count = 0
    path = []
    for mid in mapping:
        pos = mid['position']
        if 'node_id' not in pos:
            continue
        id = pos['node_id']
        read_cur_count += 1
        if 'is_reverse' in pos and pos['is_reverse'] == True:
            path.append('-' + id)
        else:
            path.append('+' + id)
    for x in path:
        out_file.write(x + ' ')
    out_file.write('\n')
    return read_cur_count

#Ako vstup berie json subor s cestou a vystupny subor(musi byt ukoncene _rwp pre dalsie spracovanie)
arguments = sys.argv
json_file = arguments[1]
is_assembly = '1'
file_name = arguments[2]

#Parsuje json subor zarovnani po jednotlivych zarovnaniach - json objektoch
with open(json_file) as json_file:
    out_file = open(file_name,"w+")
    counter = 0
    for line in json_file:
        counter += 1
        one_line = json.loads(line)
        res = one_line_parse(one_line, out_file, is_assembly)
    out_file.close
