def readTable(lines,
              assign,
              separator="\t",
              numeric_type=numpy.float,
              take_columns="all",
              headers=True,
              row_names=None,
              colours=None,
              ):
    """read a matrix. There probably is a routine for this in Numpy, which
    I haven't found yet.

    The advantage of using rpy's mechanism is to allow for 
    missing values.

    row_names: column with row names
    """

    # now import R as stdin has been read.
    take = take_columns

    handle, name = tempfile.mkstemp()
    os.close(handle)

    if take_columns == "all":
        num_cols = len(string.split(lines[0][:-1], "\t"))
        take = range(0, num_cols)
    elif take_columns == "all-but-first":
        num_cols = len(string.split(lines[0][:-1], "\t"))
        take = range(1, num_cols)

    outfile = open(name, "w")
    c = []

    # delete columns with colour information/legend
    to_delete = []
    if row_names is not None:
        legend = []
        if take_columns == "all":
            to_delete.append(row_names)
    else:
        legend = None

    if colours is not None:
        if take_columns == "all":
            to_delete.append(colours)

    to_delete.sort()
    to_delete.reverse()
    for x in to_delete:
        del take[x]

    # get column headers
    if headers:
        headers = lines[0][:-1].split("\t")
        headers = map(lambda x: headers[x], take)
        del lines[0]

    for l in lines:
        data = [x.strip() for x in l[:-1].split("\t")]
        if not data or not [x for x in data if x != ""]:
            continue
        outfile.write(string.join(map(lambda x: data[x], take), "\t") + "\n")
        if row_names is not None:
            legend.append(data[row_names])

        if colours is not None:
            c.append(data[colours])

    outfile.close()

    # rpy.set_default_mode(rpy.NO_CONVERSION)
    # note that the conversion
    # is not perfect. Some missing values are assigned to "nan", while
    # some are -2147483648. They seem to treated correctly, though,
    # within R, but note that when computing something like sum(), the
    # result in python after conversion might be -2147483648.
    if headers:
        matrix = R("""%s <- read.table( '%s', na.string = c("NA", "na", 'nan', 'NaN'), col.names=c('%s'), sep="\t" )""" %
                   (assign, name, "','".join(headers)))
    else:
        matrix = R(
            """%s <- read.table( '%s', na.string = c("NA", "na", 'nan', 'NaN'), col.names=headers, sep="\t" )""" % (assign, name))

    # rpy.set_default_mode(rpy.BASIC_CONVERSION)
    os.remove(name)

    return matrix, headers, c, legend
dataset/ETHPy150Open CGATOxford/cgat/scripts/r_table2scatter.py/readTable/original