Exemplo n.º 1
0
def convert_folder2imcfolder(fol, out_folder, dozip=True):
    """
    Convert a folder containing IMC acquisitions
    (mcd and tiff)
    to a zipfolder containing standardized names files.
    """
    if fol.endswith(ZIP_FILENDING):
        tmpdir = temporarydirectory.TemporaryDirectory()
        with zipfile.ZipFile(fol) as zipf:
            zipf.extractall(tmpdir.name)
        in_fol = tmpdir.name
        istmp = True
    else:
        in_fol = fol
        istmp = False

    try:
        files = [
            os.path.join(root, fn) for root, dirs, files in os.walk(in_fol)
            for fn in files
        ]
        mcd_files = [f for f in files if f.endswith(MCD_FILENDING)]
        assert (len(mcd_files) == 1)
        schema_files = [f for f in files if f.endswith(SCHEMA_FILENDING)]
        if len(schema_files) > 0:
            schema_file = schema_files[0]
        else:
            schema_file = None
        mcd = McdParser(mcd_files[0], metafilename=schema_file)
        txt_acids = {
            _txtfn_to_ac(f): f
            for f in files if f.endswith(TXT_FILENDING)
        }
        mcd_acs = mcd.get_all_imcacquistions()
        mcd_acids = set(m.image_ID for m in mcd_acs)
        txtonly_acs = set(txt_acids.keys()).difference(mcd_acids)
        for txta in txtonly_acs:
            print('Using TXT file for acquisition: ' + txta)
            try:
                mcd_acs.append(
                    TxtParser(txt_acids[txta]).get_imc_acquisition())
            except:
                print('TXT file was also corrupted.')
        imc_fol = ImcFolderWriter(out_folder,
                                  mcddata=mcd,
                                  imcacquisitions=mcd_acs)
        imc_fol.write_imc_folder(zipfolder=dozip)
        if istmp:
            tmpdir.cleanup()
    finally:
        if istmp:
            tmpdir.cleanup()
Exemplo n.º 2
0
def _txtfn_to_ac(fn):
    return TxtParser._txtfn_to_ac(fn)