This function converts our QTL matrix file into a MapChart input
    file.

    :arg qtl_matrix: the path to the QTL matrix file generated by
        the plugin.
    :arg lod_threshold: threshold used to determine if a given LOD value
        is reflective the presence of a QTL.
    :kwarg map_chart_file: name of the output file containing the
        MapChart information.

def generate_map_chart_file(qtl_matrix, lod_threshold,
                            map_chart_file='MapChart.map'):
    """ This function converts our QTL matrix file into a MapChart input
    file.

    :arg qtl_matrix: the path to the QTL matrix file generated by
        the plugin.
    :arg lod_threshold: threshold used to determine if a given LOD value
        is reflective the presence of a QTL.
    :kwarg map_chart_file: name of the output file containing the
        MapChart information.

    """

    qtl_matrix = read_input_file(qtl_matrix, sep=',')
    tmp_dic = {}
    cnt = 1
    tmp = {}
    block = {}
    for row in qtl_matrix[1:]:
        linkgrp = qtl_matrix[cnt - 1][1]
        if cnt == 1:
            linkgrp = qtl_matrix[cnt][1]

        if not linkgrp in tmp_dic:
            tmp_dic[linkgrp] = [[], []]

        infos = row[0:3]
        if qtl_matrix[cnt][1] != linkgrp:
            if tmp:
                qtls = _extrac_qtl(tmp, block, qtl_matrix[0])
                tmp_dic[linkgrp][1] = qtls
            linkgrp = qtl_matrix[cnt][1]
            tmp_dic[linkgrp] = [[], []]
            tmp = {}
            block = {}

        tmp_dic[linkgrp][0].append([row[0], row[2]])

        colcnt = 3
        for cel in row[3:-1]:
            blockrow = infos[:]
            blockrow.extend([qtl_matrix[0][colcnt], cel])
            if colcnt in block:
                block[colcnt].append(blockrow)
            else:
                block[colcnt] = [blockrow]
            if cel.strip() != '' and float(cel) >= float(lod_threshold):
                temp = infos[:]
                if not tmp\
                        or (qtl_matrix[0][colcnt] in tmp
                            and float(cel) >= float(
                                tmp[qtl_matrix[0][colcnt]][-1])
                            ) \
                        or qtl_matrix[0][colcnt] not in tmp:
                    temp.extend([qtl_matrix[0][colcnt], cel])
                    tmp[qtl_matrix[0][colcnt]] = temp
            colcnt = colcnt + 1
        cnt = cnt + 1

    qtl_info = {}

    try:
        stream = open(map_chart_file, 'w')
        keys = list(tmp_dic.keys())
        ## Remove unknown group, reason:
        # The unlinked markers, if present, are always put in group U by
        # MapQTL. If you don't omit them and there are many (often), then
        # their names take so much space that it is difficult to fit them
        # on the page.
        if 'U' in keys:
            keys.remove('U')
        # Try to convert all the groups to int, which would result in
        # a better sorting. If that fails, fail silently.
        try:
            keys = [int(key) for key in keys]
        except ValueError:
            pass
        keys.sort()
        for key in keys:
            key = str(key)  # Needed since we might have converted them to int
            if tmp_dic[key]:
                if key == 'U':  # pragma: no cover
                    # We removed the key before, we should not be here
                    continue
                stream.write('group %s\n' % key)
                for entry in _order_linkage_group(tmp_dic[key][0]):
                    stream.write('  '.join(entry) + '\n')
                if tmp_dic[key][1]:
                    stream.write('\n')
                    stream.write('qtls\n')
                    for qtl in tmp_dic[key][1]:
                        qtl_info[qtl.peak_mk] = qtl.get_flanking_markers()
                        stream.write('%s \n' % qtl.to_string())
                stream.write('\n')
                stream.write('\n')
    except IOError as err:  # pragma: no cover
        LOG.info('An error occured while writing the map chart map '
                 'to the file %s' % map_chart_file)
        LOG.debug("Error: %s" % err)
    finally:
        stream.close()
    LOG.info('Wrote MapChart map in file %s' % map_chart_file)

    return qtl_info