Example #1
0
def add_marker_to_qtls(qtlfile, mapfile, outputfile='qtls_with_mk.csv'):
    """This function adds to a list of QTLs, the closest marker to the
    QTL peak.

    :arg qtlfile: a CSV list of all the QTLs found.
        The file should be structured as follow::
            Trait, Linkage group, position, other columns

        The other columns will not matter as long as the first three
        columns are as such.
    :arg mapfile: a CSV representation of the map used for the QTL
        mapping analysis.
        The file should be structured as follow::
            Marker, Linkage group, position
    :kwarg outputfile: the name of the output file in which the list of
        QTLs with their closest marker will be written.

    """
    qtl_list = read_input_file(qtlfile, ',')
    map_list = read_input_file(mapfile, ',')
    if not qtl_list or not map_list:  # pragma: no cover
        return
    qtl_list[0].append('Closest marker')
    qtls = []
    qtls.append(qtl_list[0])
    for qtl in qtl_list[1:]:
        qtl.append(add_marker_to_qtl(qtl, map_list))
        qtls.append(qtl)
    LOG.info('- %s QTLs processed in %s' % (len(qtls), qtlfile))
    write_matrix(outputfile, qtls)
Example #2
0
def add_marker_to_qtls(qtlfile, mapfile, outputfile='qtls_with_mk.csv'):
    """This function adds to a list of QTLs, the closest marker to the
    QTL peak.

    :arg qtlfile: a CSV list of all the QTLs found.
        The file should be structured as follow::
            Trait, Linkage group, position, other columns

        The other columns will not matter as long as the first three
        columns are as such.
    :arg mapfile: a CSV representation of the map used for the QTL
        mapping analysis.
        The file should be structured as follow::
            Marker, Linkage group, position
    :kwarg outputfile: the name of the output file in which the list of
        QTLs with their closest marker will be written.

    """
    qtl_list = read_input_file(qtlfile, ',')
    map_list = read_input_file(mapfile, ',')
    if not qtl_list or not map_list:  # pragma: no cover
        return
    qtl_list[0].append('Closest marker')
    qtls = []
    qtls.append(qtl_list[0])
    for qtl in qtl_list[1:]:
        qtl.append(add_marker_to_qtl(qtl, map_list))
        qtls.append(qtl)
    LOG.info('- %s QTLs processed in %s' % (len(qtls), qtlfile))
    write_matrix(outputfile, qtls)
Example #3
0
def _append_count_to_matrix(qtl_matrixfile, lod_threshold):
    """ Append an extra column at the end of the matrix file containing
    for each row (marker) the number of QTL found if the marker is known
    ie: Locus != ''

    :arg qtl_matrix, the matrix in which to save the output.
    :arg threshold, threshold used to determine if a given LOD value is
        reflective the presence of a QTL.

    """
    if not os.path.exists(qtl_matrixfile):  # pragma: no cover
        raise MQ2Exception('File not found: "%s"' % qtl_matrixfile)
    matrix = read_input_file(qtl_matrixfile, sep=',')
    tmp = list(matrix[0])
    tmp.append('# QTLs')
    matrix[0] = tmp
    cnt = 1
    while cnt < len(matrix):
        row = list(matrix[cnt])
        nr_qtl = 0
        for cel in row[3:]:
            if cel and float(cel) > float(lod_threshold):
                nr_qtl = nr_qtl + 1
        row.append(str(nr_qtl))
        matrix[cnt] = row
        cnt = cnt + 1
    write_matrix(qtl_matrixfile, matrix)
Example #4
0
File: mq2.py Project: PBR/MQ2
def _append_count_to_matrix(qtl_matrixfile, lod_threshold):
    """ Append an extra column at the end of the matrix file containing
    for each row (marker) the number of QTL found if the marker is known
    ie: Locus != ''

    :arg qtl_matrix, the matrix in which to save the output.
    :arg threshold, threshold used to determine if a given LOD value is
        reflective the presence of a QTL.

    """
    if not os.path.exists(qtl_matrixfile):  # pragma: no cover
        raise MQ2Exception('File not found: "%s"' % qtl_matrixfile)
    matrix = read_input_file(qtl_matrixfile, sep=',')
    tmp = list(matrix[0])
    tmp.append('# QTLs')
    matrix[0] = tmp
    cnt = 1
    while cnt < len(matrix):
        row = list(matrix[cnt])
        nr_qtl = 0
        for cel in row[3:]:
            if cel and float(cel) > float(lod_threshold):
                nr_qtl = nr_qtl + 1
        row.append(str(nr_qtl))
        matrix[cnt] = row
        cnt = cnt + 1
    write_matrix(qtl_matrixfile, matrix)
Example #5
0
def get_map_matrix(inputfile):
    """ Return the matrix representation of the genetic map.

    :arg inputfile: the path to the input file from which to retrieve the
        genetic map.

    """
    matrix = read_input_file(inputfile)
    output = []
    for row in matrix:
        if row[3]:
            output.append([row[3], row[1], row[2]])
    return output
Example #6
0
def get_map_matrix(inputfile):
    """ Return the matrix representation of the genetic map.

    :arg inputfile: the path to the input file from which to retrieve the
        genetic map.

    """
    matrix = read_input_file(inputfile)
    output = []
    for row in matrix:
        if row[3]:
            output.append([row[3], row[1], row[2]])
    return output
Example #7
0
def get_map_matrix(inputfile):
    """ Return the matrix representation of the genetic map.

    :arg inputfile: the path to the input file from which to retrieve the
        genetic map.

    """
    matrix = read_input_file(inputfile, sep=',', noquote=True)
    output = [['Locus', 'Group', 'Position']]
    for row in matrix:
        if row[0] and not re.match(r'c\d+\.loc[\d\.]+', row[0]):
            output.append([row[0], row[1], row[2]])
    return output
Example #8
0
def get_map_matrix(inputfile):
    """ Return the matrix representation of the genetic map.

    :arg inputfile: the path to the input file from which to retrieve the
        genetic map.

    """
    matrix = read_input_file(inputfile, sep=',', noquote=True)
    output = [['Locus', 'Group', 'Position']]
    for row in matrix:
        if row[0] and not re.match(r'c\d+\.loc[\d\.]+', row[0]):
            output.append([row[0], row[1], row[2]])
    return output
Example #9
0
def add_qtl_to_map(qtlfile, mapfile, outputfile='map_with_qtls.csv'):
    """ This function adds to a genetic map for each marker the number
    of significant QTLs found.

    :arg qtlfile, the output from MapQTL transformed to a csv file via
        'parse_mapqtl_file' which contains the closest markers.
    :arg mapfile, the genetic map with all the markers.
    :kwarg outputfile, the name of the output file in which the map will
        be written.

    """
    qtl_list = read_input_file(qtlfile, ',')
    map_list = read_input_file(mapfile, ',')
    map_list[0].append('# QTLs')
    markers = []
    markers.append(map_list[0])
    qtl_cnt = 0
    for marker in map_list[1:]:
        markers.append(add_qtl_to_marker(marker, qtl_list[1:]))
        qtl_cnt = qtl_cnt + int(markers[-1][-1])
    LOG.info('- %s markers processed in %s' % (len(markers), mapfile))
    LOG.info('- %s QTLs located in the map: %s' % (qtl_cnt, outputfile))
    write_matrix(outputfile, markers)
Example #10
0
def add_qtl_to_map(qtlfile, mapfile, outputfile='map_with_qtls.csv'):
    """ This function adds to a genetic map for each marker the number
    of significant QTLs found.

    :arg qtlfile, the output from MapQTL transformed to a csv file via
        'parse_mapqtl_file' which contains the closest markers.
    :arg mapfile, the genetic map with all the markers.
    :kwarg outputfile, the name of the output file in which the map will
        be written.

    """
    qtl_list = read_input_file(qtlfile, ',')
    map_list = read_input_file(mapfile, ',')
    map_list[0].append('# QTLs')
    markers = []
    markers.append(map_list[0])
    qtl_cnt = 0
    for marker in map_list[1:]:
        markers.append(add_qtl_to_marker(marker, qtl_list[1:]))
        qtl_cnt = qtl_cnt + int(markers[-1][-1])
    LOG.info('- %s markers processed in %s' % (len(markers), mapfile))
    LOG.info('- %s QTLs located in the map: %s' % (qtl_cnt, outputfile))
    write_matrix(outputfile, markers)
Example #11
0
def append_flanking_markers(qtls_mk_file, flanking_markers):
    """ Append the flanking markers extracted in the process of
    generating the MapChart to the QTL list file.
    """
    matrix = read_input_file(qtls_mk_file, sep=',')
    output = []
    cnt = 0
    for row in matrix:
        if cnt == 0:
            markers = ['LOD2 interval start', 'LOD2 interval end']
        elif row[3] in flanking_markers:
            markers = flanking_markers[row[3]]
        else:
            markers = ['NA', 'NA']
        cnt += 1
        row.extend(markers)
        output.append(row)
    write_matrix(qtls_mk_file, output)
Example #12
0
File: mapchart.py Project: PBR/MQ2
def append_flanking_markers(qtls_mk_file, flanking_markers):
    """ Append the flanking markers extracted in the process of
    generating the MapChart to the QTL list file.
    """
    matrix = read_input_file(qtls_mk_file, sep=',')
    output = []
    cnt = 0
    for row in matrix:
        if cnt == 0:
            markers = ['LOD2 interval start', 'LOD2 interval end']
        elif row[3] in flanking_markers:
            markers = flanking_markers[row[3]]
        else:
            markers = ['NA', 'NA']
        cnt += 1
        row.extend(markers)
        output.append(row)
    write_matrix(qtls_mk_file, output)
Example #13
0
    def convert_inputfiles(
        cls,
        folder=None,
        inputfile=None,
        session=None,
        lod_threshold=None,
        qtls_file="qtls.csv",
        matrix_file="qtls_matrix.csv",
        map_file="map.csv",
    ):
        """ Convert the input files present in the given folder or
        inputfile.
        This method creates the matrix representation of the QTLs
        results providing for each marker position the LOD value found
        for each trait as well as a representation of the genetic map
        used in the experiment.
        The genetic map should be cleared of any markers added by the
        QTL mapping software.

        :kwarg folder: the path to the folder containing the files to
            check. This folder may contain sub-folders.
        :kwarg inputfile: the path to the input file to use
        :kwarg session: the session identifier used to identify which
            session to process
        :kwarg lod_threshold: the LOD threshold to apply to determine if
            a QTL is significant or not
        :kwarg qtls_file: a csv file containing the list of all the
            significant QTLs found in the analysis.
            The matrix is of type:
               trait, linkage group, position, Marker, LOD other columns
        :kwarg matrix_file: a csv file containing a matrix representation
            of the QTL data. This matrix is of type:
               marker, linkage group, position, trait1 lod, trait2, lod
        :kwarg map_file: a csv file containing the genetic map used
            in this experiment. The map is of structure:
               marker, linkage group, position

        """
        if folder is None and inputfile is None:
            raise MQ2Exception("You must specify either a folder or an " "input file")

        sessions = cls.get_session_identifiers(folder)
        if session is None:
            raise MQ2NoSessionException(
                "The MapQTL plugin requires a session identifier to "
                "identify the session to process."
                "Sessions are: %s" % ",".join(sessions)
            )
        elif str(session) not in sessions:
            raise MQ2NoSuchSessionException(
                "The MapQTL session provided (%s) could not be found in the "
                "dataset. "
                "Sessions are: %s" % (session, ",".join(sessions))
            )

        if folder is not None:
            if not os.path.isdir(folder):  # pragma: no cover
                raise MQ2Exception("The specified folder is actually " "not a folder")
            else:
                inputfiles = cls.get_files(folder, session_id=session)

        if inputfile is not None:  # pragma: no cover
            if os.path.isdir(inputfile):
                raise MQ2Exception("The specified input file is actually " "a folder")
            else:
                inputfiles = [inputfile]

        try:
            lod_threshold = float(lod_threshold)
        except ValueError:
            raise MQ2Exception("LOD threshold should be a number")

        inputfiles.sort()

        # QTL matrix and QTL files
        qtl_matrix = []
        qtls = []
        filename = None
        for filename in inputfiles:
            matrix = read_input_file(filename)
            headers = matrix[0]
            qtl_matrix = get_qtls_matrix(qtl_matrix, matrix, filename)
            qtls.extend(get_qtls_from_mapqtl_data(matrix, lod_threshold, filename))
        # format QTLs and write down the selection
        headers[0] = "Trait name"
        qtls.insert(0, headers)
        write_matrix(qtls_file, qtls)

        # Write down the QTL matrix
        del (qtl_matrix[0])
        # Reorganize a couple of columns
        qtl_matrix.insert(0, qtl_matrix[2])
        del (qtl_matrix[3])
        # write output
        qtl_matrix = list(zip(*qtl_matrix))
        write_matrix(matrix_file, qtl_matrix)

        # Map matrix
        map_matrix = get_map_matrix(inputfiles[0])
        write_matrix(map_file, map_matrix)
Example #14
0
File: mapchart.py Project: PBR/MQ2
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
Example #15
0
    def convert_inputfiles(cls,
                           folder=None,
                           inputfile=None,
                           session=None,
                           lod_threshold=None,
                           qtls_file='qtls.csv',
                           matrix_file='qtls_matrix.csv',
                           map_file='map.csv'):
        """ Convert the input files present in the given folder or
        inputfile.
        This method creates the matrix representation of the QTLs
        results providing for each marker position the LOD value found
        for each trait as well as a representation of the genetic map
        used in the experiment.
        The genetic map should be cleared of any markers added by the
        QTL mapping software.

        :kwarg folder: the path to the folder containing the files to
            check. This folder may contain sub-folders.
        :kwarg inputfile: the path to the input file to use
        :kwarg session: the session identifier used to identify which
            session to process
        :kwarg lod_threshold: the LOD threshold to apply to determine if
            a QTL is significant or not
        :kwarg qtls_file: a csv file containing the list of all the
            significant QTLs found in the analysis.
            The matrix is of type:
               trait, linkage group, position, Marker, LOD other columns
        :kwarg matrix_file: a csv file containing a matrix representation
            of the QTL data. This matrix is of type:
               marker, linkage group, position, trait1 lod, trait2, lod
        :kwarg map_file: a csv file containing the genetic map used
            in this experiment. The map is of structure:
               marker, linkage group, position

        """
        if folder is None and inputfile is None:
            raise MQ2Exception('You must specify either a folder or an '
                               'input file')

        if folder is not None:  # pragma: no cover
            if not os.path.isdir(folder):
                raise MQ2Exception('The specified folder is actually '
                                   'not a folder')
            else:
                inputfiles = cls.get_files(folder)

        if inputfile is not None:  # pragma: no cover
            if os.path.isdir(inputfile):
                raise MQ2Exception('The specified input file is actually '
                                   'a folder')
            else:
                inputfiles = [inputfile]

        if len(inputfiles) == 0:  # pragma: no cover
            raise MQ2Exception('No files correspond to this plugin')

        if len(inputfiles) > 1:  # pragma: no cover
            raise MQ2Exception(
                'This plugin can only process one file at a time')

        try:
            lod_threshold = float(lod_threshold)
        except ValueError:
            raise MQ2Exception('LOD threshold should be a number')

        inputfile = inputfiles[0]

        # QTL matrix and QTL files
        qtls = []
        matrix = read_input_file(inputfile, sep=',', noquote=True)
        qtls.extend(get_qtls_from_rqtl_data(matrix, lod_threshold))
        # format QTLs and write down the selection
        write_matrix(qtls_file, qtls)

        # Write down the QTL matrix
        write_matrix(matrix_file, matrix)

        # Map matrix
        map_matrix = get_map_matrix(inputfile)
        write_matrix(map_file, map_matrix)
Example #16
0
    def convert_inputfiles(cls,
                           folder=None,
                           inputfile=None,
                           session=None,
                           lod_threshold=None,
                           qtls_file='qtls.csv',
                           matrix_file='qtls_matrix.csv',
                           map_file='map.csv'):
        """ Convert the input files present in the given folder or
        inputfile.
        This method creates the matrix representation of the QTLs
        results providing for each marker position the LOD value found
        for each trait as well as a representation of the genetic map
        used in the experiment.
        The genetic map should be cleared of any markers added by the
        QTL mapping software.

        :kwarg folder: the path to the folder containing the files to
            check. This folder may contain sub-folders.
        :kwarg inputfile: the path to the input file to use
        :kwarg session: the session identifier used to identify which
            session to process
        :kwarg lod_threshold: the LOD threshold to apply to determine if
            a QTL is significant or not
        :kwarg qtls_file: a csv file containing the list of all the
            significant QTLs found in the analysis.
            The matrix is of type:
               trait, linkage group, position, Marker, LOD other columns
        :kwarg matrix_file: a csv file containing a matrix representation
            of the QTL data. This matrix is of type:
               marker, linkage group, position, trait1 lod, trait2, lod
        :kwarg map_file: a csv file containing the genetic map used
            in this experiment. The map is of structure:
               marker, linkage group, position

        """
        if folder is None and inputfile is None:
            raise MQ2Exception('You must specify either a folder or an '
                               'input file')

        sessions = cls.get_session_identifiers(folder)
        if session is None:
            raise MQ2NoSessionException(
                'The MapQTL plugin requires a session identifier to '
                'identify the session to process.'
                'Sessions are: %s' % ','.join(sessions))
        elif str(session) not in sessions:
            raise MQ2NoSuchSessionException(
                'The MapQTL session provided (%s) could not be found in the '
                'dataset. '
                'Sessions are: %s' % (session, ','.join(sessions)))

        if folder is not None:
            if not os.path.isdir(folder):  # pragma: no cover
                raise MQ2Exception('The specified folder is actually '
                                   'not a folder')
            else:
                inputfiles = cls.get_files(folder, session_id=session)

        if inputfile is not None:  # pragma: no cover
            if os.path.isdir(inputfile):
                raise MQ2Exception('The specified input file is actually '
                                   'a folder')
            else:
                inputfiles = [inputfile]

        try:
            lod_threshold = float(lod_threshold)
        except ValueError:
            raise MQ2Exception('LOD threshold should be a number')

        inputfiles.sort()

        # QTL matrix and QTL files
        qtl_matrix = []
        qtls = []
        filename = None
        for filename in inputfiles:
            matrix = read_input_file(filename)
            headers = matrix[0]
            qtl_matrix = get_qtls_matrix(qtl_matrix, matrix, filename)
            qtls.extend(
                get_qtls_from_mapqtl_data(matrix, lod_threshold, filename))
        # format QTLs and write down the selection
        headers[0] = 'Trait name'
        qtls.insert(0, headers)
        write_matrix(qtls_file, qtls)

        # Write down the QTL matrix
        del (qtl_matrix[0])
        # Reorganize a couple of columns
        qtl_matrix.insert(0, qtl_matrix[2])
        del (qtl_matrix[3])
        # write output
        qtl_matrix = list(zip(*qtl_matrix))
        write_matrix(matrix_file, qtl_matrix)

        # Map matrix
        map_matrix = get_map_matrix(inputfiles[0])
        write_matrix(map_file, map_matrix)
Example #17
0
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
Example #18
0
    def convert_inputfiles(cls,
                           folder=None,
                           inputfile=None,
                           session=None,
                           lod_threshold=None,
                           qtls_file='qtls.csv',
                           matrix_file='qtls_matrix.csv',
                           map_file='map.csv'):
        """ Convert the input files present in the given folder or
        inputfile.
        This method creates the matrix representation of the QTLs
        results providing for each marker position the LOD value found
        for each trait as well as a representation of the genetic map
        used in the experiment.
        The genetic map should be cleared of any markers added by the
        QTL mapping software.

        :kwarg folder: the path to the folder containing the files to
            check. This folder may contain sub-folders.
        :kwarg inputfile: the path to the input file to use
        :kwarg session: the session identifier used to identify which
            session to process
        :kwarg lod_threshold: the LOD threshold to apply to determine if
            a QTL is significant or not
        :kwarg qtls_file: a csv file containing the list of all the
            significant QTLs found in the analysis.
            The matrix is of type:
               trait, linkage group, position, Marker, LOD other columns
        :kwarg matrix_file: a csv file containing a matrix representation
            of the QTL data. This matrix is of type:
               marker, linkage group, position, trait1 lod, trait2, lod
        :kwarg map_file: a csv file containing the genetic map used
            in this experiment. The map is of structure:
               marker, linkage group, position

        """
        if folder is None and inputfile is None:
            raise MQ2Exception('You must specify either a folder or an '
                               'input file')

        if folder is not None:  # pragma: no cover
            if not os.path.isdir(folder):
                raise MQ2Exception('The specified folder is actually '
                                   'not a folder')
            else:
                inputfiles = cls.get_files(folder)

        if inputfile is not None:  # pragma: no cover
            if os.path.isdir(inputfile):
                raise MQ2Exception('The specified input file is actually '
                                   'a folder')
            else:
                inputfiles = [inputfile]

        if len(inputfiles) == 0:  # pragma: no cover
            raise MQ2Exception('No files correspond to this plugin')

        if len(inputfiles) > 1:  # pragma: no cover
            raise MQ2Exception(
                'This plugin can only process one file at a time')

        try:
            lod_threshold = float(lod_threshold)
        except ValueError:
            raise MQ2Exception('LOD threshold should be a number')

        inputfile = inputfiles[0]

        # QTL matrix and QTL files
        qtls = []
        matrix = read_input_file(inputfile, sep=',', noquote=True)
        qtls.extend(get_qtls_from_rqtl_data(matrix, lod_threshold))
        # format QTLs and write down the selection
        write_matrix(qtls_file, qtls)

        # Write down the QTL matrix
        write_matrix(matrix_file, matrix)

        # Map matrix
        map_matrix = get_map_matrix(inputfile)
        write_matrix(map_file, map_matrix)