import pandas as pd
import gzip
import os
from concurrent import futures
def divide_name(filename):
    #home-made os.path.splitext, for it can't handle "name.a.b.c" properly
    basename = os.path.basename(filename)
    parts = basename.split(".") #split return >= 1 length list
    if len(parts) == 1:
        return parts[0], ""
    else:
        return parts[0], "."+".".join(parts[1:]) 
def parse_pairs(filename:str)->"Cell":
    '''
    read from 4DN's standard .pairs format
    compatible with all hickit originated pairs-like format 
    '''
    #comment lines are stored in dataframe.attrs["comment"]
    name_array = "readID chr1 pos1 chr2 pos2 strand1 strand2 phase0 phase1 phase_prob00 phase_prob01 phase_prob10 phase_prob11".split()
    #read comment line
    with gzip.open(filename,"rt") as f:
        comments = []
        for line in f.readlines():
            if line[0] != "#":
                break
            comments.append(line)
    #read table format data
    pairs = pd.read_table(filename, header=None, comment="#",low_memory=False)
    pairs.attrs["comments"] = comments
    pairs.attrs["name"], _ = divide_name(filename) # get real sample name
    #assign column names
    pairs.columns = name_array[0:pairs.shape[1]]
    #sys.stderr.write("pairs_parser: %s parsed \n" % filename)
    return pairs
def collect(meta:pd.DataFrame, name_col:str, file_col:str, func:"callable", threads)->pd.DataFrame:
    # get cons for cells in dir
    file_names = meta[file_col]
    with futures.ProcessPoolExecutor(threads) as pool:
        res = pool.map(func, file_names)
    result = pd.concat(res, axis=1)
    result.columns = meta[name_col]
    return result
import pandas as pd
import gzip
import os
from concurrent import futures
def divide_name(filename):
    #home-made os.path.splitext, for it can't handle "name.a.b.c" properly
    basename = os.path.basename(filename)
    parts = basename.split(".") #split return >= 1 length list
    if len(parts) == 1:
        return parts[0], ""
    else:
        return parts[0], "."+".".join(parts[1:]) 
def parse_pairs(filename:str)->"Cell":
    '''
    read from 4DN's standard .pairs format
    compatible with all hickit originated pairs-like format 
    '''
    #comment lines are stored in dataframe.attrs["comment"]
    name_array = "readID chr1 pos1 chr2 pos2 strand1 strand2 phase0 phase1 phase_prob00 phase_prob01 phase_prob10 phase_prob11".split()
    #read comment line
    with gzip.open(filename,"rt") as f:
        comments = []
        for line in f.readlines():
            if line[0] != "#":
                break
            comments.append(line)
    #read table format data
    pairs = pd.read_table(filename, header=None, comment="#",low_memory=False)
    pairs.attrs["comments"] = comments
    pairs.attrs["name"], _ = divide_name(filename) # get real sample name
    #assign column names
    pairs.columns = name_array[0:pairs.shape[1]]
    #sys.stderr.write("pairs_parser: %s parsed \n" % filename)
    return pairs
def collect(meta:pd.DataFrame, name_col:str, file_col:str, func:"callable", threads)->pd.DataFrame:
    # get cons for cells in dir
    file_names = meta[file_col]
    with futures.ProcessPoolExecutor(threads) as pool:
        res = pool.map(func, file_names)
    result = pd.concat(res, axis=1)
    result.columns = meta[name_col]
    return result
#home-made os.path.splitext, for it can't handle "name.a.b.c" properly #split return >= 1 length list read from 4DN's standard .pairs format     compatible with all hickit originated pairs-like format #comment lines are stored in dataframe.attrs["comment"] #read comment line #read table format data # get real sample name #assign column names #sys.stderr.write("pairs_parser: %s parsed \n" % filename) # get cons for cells in dir