コード例 #1
0
ファイル: lpsolve.py プロジェクト: zhaotao1987/jcvi
    def parse_output(self, outfile):

        fp = open(outfile)
        for row in fp:
            if row.startswith("objective value"):
                obj_row = row
                break

        results = []
        for row in fp:
            """
            objective value:               8
            x1                             1   (obj:5)
            x2                             1   (obj:3)
            """
            if row.strip() == "":  # blank line ends the section
                break
            x = row.split()[0]
            results.append(int(x[1:]) - 1)  # 0-based indexing

        if results:
            self.obj_val = flexible_cast(obj_row.split(":")[1].strip())

        fp.close()

        if self.clean:
            self.cleanup()

        return results
コード例 #2
0
ファイル: bed.py プロジェクト: yangjl/jcvi
def random(args):
    """
    %prog random bedfile number_of_features

    Extract a random subset of features. Number of features can be an integer
    number, or a fractional number in which case a random fraction (for example
    0.1 = 10% of all features) will be extracted.
    """
    from random import sample
    from jcvi.formats.base import flexible_cast

    p = OptionParser(random.__doc__)
    opts, args = p.parse_args(args)

    if len(args) != 2:
        sys.exit(not p.print_help())

    bedfile, N = args
    assert is_number(N)

    b = Bed(bedfile)
    NN = flexible_cast(N)
    if NN < 1:
        NN = int(round(NN * len(b)))

    beds = sample(b, NN)
    new_bed = Bed()
    new_bed.extend(beds)

    outfile = bedfile.rsplit(".", 1)[0] + ".{0}.bed".format(N)
    new_bed.print_to_file(outfile)
    logging.debug("Write {0} features to `{1}`".format(NN, outfile))
コード例 #3
0
def fromcsv(args):
    """
    %prog fromcsv csvfile

    Convert csv file to EXCEL.
    """
    from csv import reader
    from xlwt import Workbook, easyxf
    from jcvi.formats.base import flexible_cast

    p = OptionParser(fromcsv.__doc__)
    p.add_option(
        "--noheader",
        default=False,
        action="store_true",
        help="Do not treat the first row as header",
    )
    p.add_option("--rgb", default=-1, type="int", help="Show RGB color box")
    p.set_sep()
    opts, args = p.parse_args(args)

    if len(args) != 1:
        sys.exit(not p.print_help())

    (csvfile, ) = args
    header = not opts.noheader
    rgb = opts.rgb
    excelfile = csvfile.rsplit(".", 1)[0] + ".xls"

    data = []
    for row in reader(open(csvfile), delimiter=opts.sep):
        data.append(row)

    w = Workbook()
    s = w.add_sheet(op.basename(csvfile))

    header_style = easyxf("font: bold on")
    if header:
        s.panes_frozen = True
        s.horz_split_pos = 1

    cm = ColorMatcher()
    for i, row in enumerate(data):
        for j, cell in enumerate(row):
            cell = flexible_cast(cell)
            if header and i == 0:
                s.write(i, j, cell, header_style)
            else:
                if j == rgb:
                    cix = cm.match_color_index(cell)
                    color_style = easyxf("font: color_index {0}".format(cix))
                    s.write(i, j, cell, color_style)
                else:
                    s.write(i, j, cell)

    w.save(excelfile)
    logging.debug("File written to `%s`.", excelfile)
    return excelfile
コード例 #4
0
ファイル: reformat.py プロジェクト: zhaotao1987/jcvi
def chr_number(chr):
    chr_pat = re.compile(r"(?P<prefix>\D*)(?P<chr>[\d|C|M]+)$", re.VERBOSE | re.IGNORECASE)

    if chr is not None:
        m = re.match(chr_pat, chr)
        if m is not None:
            return flexible_cast(m.group('chr'))

    return None
コード例 #5
0
ファイル: excel.py プロジェクト: Hensonmw/jcvi
def fromcsv(args):
    """
    %prog fromcsv csvfile

    Convert csv file to EXCEL.
    """
    from csv import reader
    from xlwt import Workbook, easyxf
    from jcvi.formats.base import flexible_cast

    p = OptionParser(fromcsv.__doc__)
    p.add_option("--noheader", default=False, action="store_true",
                 help="Do not treat the first row as header")
    p.add_option("--rgb", default=-1, type="int",
                 help="Show RGB color box")
    p.set_sep()
    opts, args = p.parse_args(args)

    if len(args) != 1:
        sys.exit(not p.print_help())

    csvfile, = args
    header = not opts.noheader
    rgb = opts.rgb
    excelfile = csvfile.rsplit(".", 1)[0] + ".xls"

    data = []
    for row in reader(open(csvfile), delimiter=opts.sep):
        data.append(row)

    w = Workbook()
    s = w.add_sheet(op.basename(csvfile))

    header_style = easyxf('font: bold on')
    if header:
        s.panes_frozen = True
        s.horz_split_pos = 1

    cm = ColorMatcher()
    for i, row in enumerate(data):
        for j, cell in enumerate(row):
            cell = flexible_cast(cell)
            if header and i == 0:
                s.write(i, j, cell, header_style)
            else:
                if j == rgb:
                    cix = cm.match_color_index(cell)
                    color_style = easyxf('font: color_index {0}'.format(cix))
                    s.write(i, j, cell, color_style)
                else:
                    s.write(i, j, cell)

    w.save(excelfile)
    logging.debug("File written to `{0}`.".format(excelfile))
    return excelfile