示例#1
0
def do_uploadfsa(args, dbh):

    cout('Uploading FSA files from input file: %s' % args.infile)

    b = dbh.get_batch(args.batch)

    inrows = csv.DictReader(
        open(args.infile),
        delimiter=',' if args.infile.endswith('.csv') else '\t')
    #next(inrows)

    total_fsa = 0
    line_counter = 1
    for r in inrows:

        line_counter += 1

        #if not row[0] or row[0].startswith('#'):
        #    continue
        if not (r['FILENAME'] and r['SAMPLE']) or '#' in [
                r['FILENAME'][0], r['SAMPLE'][0]
        ]:
            continue

        #if len(row) < 3:
        #    cerr('ERR - line %d only has %d item(s)' % (line_counter, len(row)))

        sample_code, fsa_filename, fsa_panel = r['SAMPLE'], r['FILENAME'], r[
            'PANEL']
        if r['OPTIONS']:
            options = tokenize(r['OPTIONS'])
        else:
            options = None

        try:

            s = b.search_sample(sample_code)
            if not s:
                cerr('ERR - sample %s does not exist' % sample_code)
                sys.exit(1)

            with open(args.indir + '/' + fsa_filename, 'rb') as f:
                trace = f.read()

            a = s.add_fsa_assay(trace,
                                filename=fsa_filename,
                                panel_code=fsa_panel,
                                options=options,
                                species=args.species,
                                dbhandler=dbh)
            cerr('INFO - sample: %s assay: %s panel: %s has been uploaded' %
                 (s.code, a.filename, fsa_panel))

        except Exception as exc:

            if not args.test:
                raise
            cerr('ERR - line %d' % line_counter)
            cerr(' => %s' % str(exc))
示例#2
0
文件: dbmgr.py 项目: edawine/fatools
def do_uploadfsa(args, dbh):

    cout('Uploading FSA files from input file: %s' % args.infile)

    b = dbh.get_batch(args.batch)

    inrows = csv.DictReader( open(args.infile),
                delimiter = ',' if args.infile.endswith('.csv') else '\t' )
    #next(inrows)

    total_fsa = 0
    line_counter = 1
    for r in inrows:

        line_counter += 1

        #if not row[0] or row[0].startswith('#'):
        #    continue
        if not (r['FILENAME'] and r['SAMPLE']) or '#' in [ r['FILENAME'][0], r['SAMPLE'][0] ]:
            continue

        #if len(row) < 3:
        #    cerr('ERR - line %d only has %d item(s)' % (line_counter, len(row)))

        sample_code, fsa_filename, fsa_panel = r['SAMPLE'], r['FILENAME'], r['PANEL']
        if r['OPTIONS']:
            options = tokenize( r['OPTIONS'] )
        else:
            options = None

        try:

            s = b.search_sample(sample_code)
            if not s:
                cerr('ERR - sample %s does not exist' % sample_code)
                sys.exit(1)

            with open( args.indir + '/' + fsa_filename, 'rb') as f:
                trace = f.read()

            a = s.add_fsa_assay( trace, filename=fsa_filename, panel_code = fsa_panel,
                        options = options, species = args.species, dbhandler = dbh)
            cerr('INFO - sample: %s assay: %s panel: %s has been uploaded' %
                        (s.code, a.filename, fsa_panel))

        except Exception as exc:

            if not args.test:
                raise
            cerr('ERR - line %d' % line_counter)
            cerr(' => %s' % str(exc))
示例#3
0
文件: cmds.py 项目: edawine/fatools
def open_fsa( args ):
    """ open FSA file(s) and prepare fsa instances
        requires: args.file, args.panel, args.panelfile
    """

    from fatools.lib.fileio.models import Marker, Panel, FSA

    if not args.panel:
        cexit('ERR: using FSA file(s) requires --panel argument!')

    if not args.panelfile:
        cerr('WARN: using default built-in panels')
        Panel.upload(params.default_panels)
    else:
        with open(args.panelfile) as f:
            # open a YAML file that describe panel sets
            Panel.upload(yaml.load(f))

    if not args.markerfile:
        Marker.upload(params.default_markers)
    else:
        raise NotImplementedError()

    panel = Panel.get_panel(args.panel)
    fsa_list = []
    index = 1

    # prepare caching
    cache_path = None
    if not args.no_cache:
        cache_path = os.path.join(os.path.expanduser('~'), '.fatools_caches', 'channels')
        if args.cache_path is not None:
            cache_path = os.path.join(args.cache_path, '.fatools_caches', 'channels')
        if not os.path.exists(cache_path):
            os.makedirs(cache_path)

    if args.file:
        for fsa_filename in args.file.split(','):
            fsa_filename = fsa_filename.strip()
            fsa = FSA.from_file(fsa_filename, panel, cache=not args.no_cache,
                                cache_path=cache_path)
            # yield (fsa, str(i))
            fsa_list.append( (fsa, str(index)) )
            index += 1

    elif args.infile:

        with open(args.infile) as f:
            buf, delim = detect_buffer( f.read() )
        inrows = csv.DictReader( StringIO(buf), delimiter=delim )
        line = 1
        index = 1

        for r in inrows:

            line += 1

            fsa_filename = r['FILENAME'].strip()
            if fsa_filename.startswith('#'):
                continue

            if r.get('OPTIONS', None):
                options = tokenize( r['OPTIONS'] )
            else:
                options = None

            panel_code = r.get('PANEL', None) or args.panel
            panel = Panel.get_panel(panel_code)

            fsa = FSA.from_file(fsa_filename, panel, options, cache=not args.no_cache,
                                cache_path=cache_path)
            if 'SAMPLE' in inrows.fieldnames:

                # yield (fsa, r['SAMPLE'])
                fsa_list.append( (fsa, r['SAMPLE']) )
            else:

                # yield (fsa, str(index))
                fsa_list.append( (fsa, str(index)) )
                index += 1

    return fsa_list
示例#4
0
def open_fsa(args, _params):
    """ open FSA file(s) and prepare fsa instances
        requires: args.file, args.panel, args.panelfile
    """

    from fatools.lib.fileio.models import Marker, Panel, FSA

    if not args.panel:
        cexit('ERR: using FSA file(s) requires --panel argument!')

    if not args.panelfile:
        cerr('WARN: using default built-in panels')
        Panel.upload(params.default_panels)
    else:
        with open(args.panelfile) as f:
            # open a YAML file that describe panel sets
            import yaml
            Panel.upload(yaml.load(f))

    if not args.markerfile:
        Marker.upload(params.default_markers)
    else:
        raise NotImplementedError()

    panel = Panel.get_panel(args.panel)
    fsa_list = []
    index = 1

    # prepare caching
    if args.use_cache:
        if not os.path.exists('.fatools_caches/channels'):
            os.makedirs('.fatools_caches/channels')

    if args.file:
        for fsa_filename in args.file.split(','):
            fsa_filename = fsa_filename.strip()

            if args.indir != "":
                filename = args.indir + "/" + fsa_filename
            else:
                filename = fsa_filename

            fsa = FSA.from_file(filename,
                                panel,
                                _params,
                                cache=not args.no_cache)
            # yield (fsa, str(i))
            fsa_list.append((fsa, str(index)))
            index += 1

    elif args.infile:

        with open(args.infile) as f:
            buf, delim = detect_buffer(f.read())
        inrows = csv.DictReader(StringIO(buf), delimiter=delim)
        line = 1
        index = 1

        for r in inrows:

            line += 1

            fsa_filename = r['FILENAME'].strip()
            if fsa_filename.startswith('#'):
                continue

            if r.get('OPTIONS', None):
                options = tokenize(r['OPTIONS'])
            else:
                options = None

            panel_code = r.get('PANEL', None) or args.panel
            panel = Panel.get_panel(panel_code)

            fsa = FSA.from_file(fsa_filename,
                                panel,
                                _params,
                                options,
                                cache=not args.no_cache)
            if 'SAMPLE' in inrows.fieldnames:

                # yield (fsa, r['SAMPLE'])
                fsa_list.append((fsa, r['SAMPLE']))
            else:

                # yield (fsa, str(index))
                fsa_list.append((fsa, str(index)))
                index += 1

    elif args.indir:
        import glob
        for fsa_filename in sorted(glob.glob(args.indir + "/*.fsa")):

            fsa_filename = fsa_filename.strip()
            fsa = FSA.from_file(fsa_filename,
                                panel,
                                _params,
                                cache=not args.no_cache)
            # yield (fsa, str(i))
            fsa_list.append((fsa, str(index)))
            index += 1

    return fsa_list