Ejemplo n.º 1
0
def main(argv):
    root = argv[1]
    an_mod = __import__(argv[2])
    an = an_mod.Analysis()

    fieldnos = set('1234')
    def wanted(bn, path, isdir):
        return isdir and bn in fieldnos

    count = 0
    skipset = set()
    done = set()

    def error_in(path, tbck, skip=False):
        if (path, tbck) in done:
            return
        done.add((path, tbck))
        if skip:
            assert not path in skipset, path
            skipset.add(path)
        import sys
        print >> sys.stderr, 'ERROR in %s:\n%s' % (path, tbck)

    for pass_ in an.passes:
        for hdf in gimme(root, wanted):
            cell_line, plate, rc, sdcdir = (hdf.split('/'))[-5:-1]
            fieldno = sdcdir[0]
            index = '%s_%s_%s_%s' % (cell_line, plate, rc, fieldno)
#             plate, rc = (hdf.split('/'))[-4:-2]
            r, c = rc[0], rc[1:]
            try:
                with h5py.File(hdf, 'r') as h5:
                    wells = list(sdc_extract.iterwells(h5))
                    w = None

                    try:
                        w = wells[0][1]
                        assert len(wells) == 1
                    except:
                        wisnone = w is None
                        error_in(hdf, tb.format_exc(), skip=wisnone)
                        if wisnone:
                            continue

                    try:
                        assert (plate, r, c) == sdc_extract.well_coords(w)
                    except:
                        error_in(hdf, tb.format_exc())

                    try:
                        means, stddevs = sdc_extract.well_stats(w)
                        assert (len(means), len(stddevs)) == (26, 26), \
                               repr((len(means), len(stddevs)))
                    except:
                        error_in(hdf, tb.format_exc())

                    flds = list(sdc_extract.iterfields(w))
                    f = None
                    try:
                        f = flds[0]
                        assert len(flds) == 1, repr((hdf, len(flds)))
                    except:
                        fisnone = f is None
                        error_in(hdf, tb.format_exc(), skip=fisnone)
                        if fisnone:
                            continue

                    an.process_rows(sdc_extract.field_feature_values(f), index, pass_)
            except SystemExit:
                raise
            except:
                error_in(hdf, tb.format_exc())
            finally:
                try:
                    h5.close()
                except:
                    pass
                    #print >> sys.stderr, 'oops: %s' % tb.format_exc()

#             count += 1
#             if count >= 5:
#                 break

        an.finish(pass_)

    an.finish()
    an.report()
Ejemplo n.º 2
0
def main(argv=sys.argv):
    global READOUTS
    READOUTS = dict()
    global HEADERS
    HEADERS = dict()
    global COUNT
    COUNT = 0
    global OUTH
    OUTH = sys.stdout

    root = argv[1]
    platemap = _get_exp_design(argv[2])
    try:
        outpath = argv[3]
    except IndexError:
        outpath = None

    try:
        terminator = '\r\n' if argv[4].lower() == 'windows' else '\n'
    except IndexError:
        terminator = None

    payload = namedtuple('_payload',
                         'cell_line treatment info data')
    
    wl2wl={'530': '488', '685': '647'}
    wanted_wavelengths = sorted(wl2wl.keys())
    wanted_features = tuple(['Whole_w%s (Mean)' % s
                             for s in wanted_wavelengths])
    
    last_cell_line = None
    print_headers = True
    cell_line_data = headers = None
    done = False

    def wanted(bn, path, isdir):
        return isdir and is_valid_rc(bn)

    well_paths = gimme(root, wanted)

    while True:
        try:
            well_path = well_paths.next()
        except StopIteration:
            well_path = None

        if well_path:
            cell_line, plate, rc = (well_path.split('/'))[-3:]
            treatment, info = platemap[plate][rc]
            coords = info.coords
            r, c = rc[0], rc[1:]
        else:
            done = True

        if done or cell_line != last_cell_line:
            _print_as_datapflex(cell_line_data,
                                print_headers,
                                wl2wl, wanted_wavelengths,
                                wanted_features,
                                outpath=outpath,
                                terminator=terminator)
            if done:
                break
            last_cell_line = cell_line
            print_headers = False
            cell_line_data = defaultdict(lambda:
                               defaultdict(lambda:
                                 defaultdict(lambda:
                                   defaultdict(list))))

        fields_used = []
        data = []
        for sdc in sorted(os.listdir(well_path)):
            if not sdc.endswith('.sdc'):
                continue

            hdf = op.join(well_path, sdc, 'Data.h5')
            try:
                with h5py.File(hdf, 'r') as h5:
                    wells = list(sdc_extract.iterwells(h5))
                    if not len(wells):
                        # TODO: log warning
                        continue
                    flds = list(sdc_extract.iterfields(wells[0][1]))
                    assert len(flds) == 1, str((hdf, len(flds)))
                    data.append(sdc_extract.field_feature_values(flds[0],
                                                                 wanted_features))
                    fieldno = sdc[-5]
                    fields_used.append(unicode(fieldno))
            except IOError:
                tb.print_exc()
            finally:
                try:
                    h5.close()
                except:
                    pass

        info = info._replace(coords=tuple([coords + (fn,)
                                           for fn in fields_used]))

        zone = info.zone
        ligand = treatment.ligand_name
        conc = treatment.ligand_concentration
        time_ = treatment.time
        cell_line_data[zone][ligand][conc][time_].append(payload(unicode(cell_line),
                                                                 treatment, info, data))

    return 0