#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import os
import zipfile

from django.utils.encoding import force_str

from disease.models import SNPMarker

log = logging.getLogger(__name__)

def parse_raw_genome_file_gen(file):
    RSID = 0
    GENOTYPE = 3
    POSITION = 2
    for line in file:
        line = force_str(line)
        if line.startswith('#'):
            continue
        l = line.strip().split('\t')
        if not l[RSID].startswith('rs'):
            continue
        rsid = l[RSID].replace('rs', '', 1)
        yield rsid, {'genotype': l[GENOTYPE], 'position': l[POSITION]}


def parse_raw_genome_file(file):
    data = {}
    log.debug('PID: %s, PARSING GENOME FILE STARTED', os.getpid())
    for rsid, line in parse_raw_genome_file_gen(file):
        data[rsid] = line
    log.debug('PID: %s, PARSING GENOME FILE FINISHED', os.getpid())
    return data


def process_genoome_data_gen(data):
    log.debug('PID: %s, PROCESING MARKERS', os.getpid())
    markers = SNPMarker.objects.prefetch_related('allele_colors').filter(rsid__in=data.keys())
    for marker in markers:
        mrsid = str(marker.rsid)
        if mrsid not in data:
            continue

        row = {'rsid': mrsid,
               'risk_allele': marker.risk_allele,
               'chromosome_position': data[mrsid]['position'],
               'disease_trait': marker.disease_trait,
               'p_value': marker.p_value,
               'or_or_beta': marker.or_or_beta,
               'genotype': data[mrsid]['genotype'],
               'risk': data[mrsid]['genotype'].count(marker.risk_allele),
               'link': marker.link
               }

        allele_colors = marker.allele_colors.all()
        for allele_color in allele_colors:
            if row['genotype'] == allele_color.allele:
                row.update({'color': allele_color.color, 'priority': allele_color.priority})
                break

        yield row
    log.debug('PID: %s, MARKERS PROCESSED', os.getpid())


def process_genoome_data(data):
    table = []
    for row in process_genoome_data_gen(data):
        table.append(row)
    return table


def process_filename(filename, filename_suffix=None):
    if filename_suffix is not None:
        filename, ext = filename.rsplit('.', 1)
        filename = '{}{}.{}'.format(filename, filename_suffix, ext)
    return filename


def get_genome_dirpath(user):
    app_dir = 'disease'
    user_subdir = '{}:{}'.format(user.pk, user.email)
    return os.path.join(app_dir, user_subdir)


def get_genome_filepath(user, filename):
    return os.path.join(get_genome_dirpath(user), filename)


def handle_zipped_genome_file(genome_file):
    parsed_file = None

    with zipfile.ZipFile(genome_file) as zipped_file:
        #Get filename without extension
        _, zipped_filename = os.path.split(zipped_file.filename)
        zipped_filename, _ = zipped_filename.rsplit('.', 1)

        # Check if archive contains .txt file with the same filename as archive
        namelist = zipped_file.namelist()
        for unzipped_full_filename in namelist:
            unzipped_filename, unzipped_ext = unzipped_full_filename.rsplit('.', 1)
            if unzipped_ext == 'txt' and unzipped_filename == zipped_filename:
                with zipped_file.open(unzipped_full_filename) as unzipped_file:
                    parsed_file = parse_raw_genome_file(unzipped_file)

    if parsed_file is None:
        raise KeyError('There is no valid genome file in the archive')
    return parsed_file
@@ -3417,24 +3417,87 @@
 le is None:%0A
+        log.error('No valid genome files found: %25s', namelist)%0A
         rais

Add error log when no valid file found