Beispiel #1
0
def Fig2():
    fig = plt.figure()

    path_006 = mydir + '/data/Box1Fig/Sample_006/'
    path_012 = mydir + '/data/Box1Fig/Sample_012/'
    path_264 = mydir + '/data/Box1Fig/Sample_264/'
    plate_006 = FCPlate.from_dir(ID='Demo Plate', path = path_006, parser='name')
    plate_012 = FCPlate.from_dir(ID='Demo Plate', path = path_012, parser='name')
    plate_264 = FCPlate.from_dir(ID='Demo Plate', path = path_264, parser='name')
    plate_006 = plate_006.dropna()
    plate_012 = plate_012.dropna()
    plate_264 = plate_264.dropna()
    plate_006 = plate_006.transform('hlog', channels=['FSC-A', 'SSC-A', \
        'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
    plate_012 = plate_012.transform('hlog', channels=['FSC-A', 'SSC-A', \
        'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
    plate_264 = plate_264.transform('hlog', channels=['FSC-A', 'SSC-A', \
        'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
    threshold_006 = getDAPIgate(plate_006)
    threshold_012 = getDAPIgate(plate_012)
    threshold_264 = getDAPIgate(plate_264)
    gate_006 = ThresholdGate(threshold_006, 'Pacific Blue-A', region='above')
    gate_012 = ThresholdGate(threshold_012, 'Pacific Blue-A', region='above')
    gate_264 = ThresholdGate(threshold_264, 'Pacific Blue-A', region='above')
    gated_sample_006 = plate_006['A8'].gate(gate_006)
    gated_sample_012 = plate_012['A8'].gate(gate_012)
    gated_sample_264 = plate_264['A8'].gate(gate_264)
    RSG_006 = gated_sample_006.data[['FITC-A']].values
    RSG_012 = gated_sample_012.data[['FITC-A']].values
    RSG_264 = gated_sample_264.data[['FITC-A']].values
    #colors = ['#FF6347', '#FFA500', '#87CEEB']

    plt.hist(RSG_006, 40, fc='#87CEEB', histtype='bar', alpha=0.5, normed=True)
    plt.hist(RSG_012, 40, fc='#FFA500', histtype='bar', alpha=0.5, normed=True)
    plt.hist(RSG_264, 40, fc='#FF6347', histtype='bar', alpha=0.5, normed=True)

    plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
    #plt.title('The distribution of reductase \n acivity in a microbial population', \
    #    fontsize = 20, weight = 'heavy')
    plt.xlabel('Metabolic activity', fontsize = 18)
    plt.ylabel('Frequency', fontsize = 18)
    plt.arrow(2500, 0.00075, 3400, 0, width=0.00004, \
        head_width=0.00012, head_length=450,  length_includes_head=True, \
        shape='full', color = '#87CEEB')
    plt.arrow(5350, 0.00064, -3400, 0, width=0.00004, \
        head_width=0.00012, head_length=450,  length_includes_head=True, \
        shape='full', color = '#FF6347')

    plt.xlim(0, 8000)
    plt.ylim(0, 0.001)
    fig.tight_layout()
    plt.gca().invert_xaxis()
    plt.text(4800, 0.00055 , 'Initiation', color = '#FF6347', fontsize = 18, weight = 'heavy')
    plt.text(5050, 0.0008, 'Resucitation', color = '#87CEEB', fontsize = 18, weight = 'heavy')
    fig_name = mydir + '/figures/Fig2.png'
    fig.savefig(fig_name, bbox_inches = "tight", pad_inches = 0.4, dpi = 600)
    plt.close()
Beispiel #2
0
def load_data(path,plate_name = 'None',channels = ['SSC-A','FSC-A']):
    #Formatting File names for FCPlate Parser Import
    chdir(path)
    files = listdir(getcwd())
    try:
        files.remove('.DS_Store')
    except:
        pass
    for i,name in enumerate(files):
        if name[-6] == '0':
            newname = plate_name + '_Well_' + name[-7] + name[-5:-4] + '_' + name[-7:-4] + name[-4:]
        else:
            newname = plate_name + '_Well_' + name[-7:-4] + '_' + name[-7:-4] + name[-4:]
        rename(name,newname)
    #import Data as plate
    files = listdir(getcwd())
    try:
        files.remove('.DS_Store')
    except:
        pass
    plate = FCPlate.from_files(ID = plate_name, datafiles = files,parser = 'name')
    trans = plate.transform('hlog',channels = channels, b = 10)
    chdir(r'../')
    print '%s Data Loaded!'%(plate_name)
    return trans
Beispiel #3
0
def get_RSG_movie():
    # Set up formatting for the movie files
    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
    number_of_frames = len(samples)
    for sample in samples:
        print sample
        path = data_path + 'Sample_' + sample + '/'
        plate = FCPlate.from_dir(ID='Demo Plate', path = path, parser='name')
        plate = plate.dropna()
        plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', \
            'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
        threshold = getDAPIgate(plate)
        gate = ThresholdGate(threshold, 'Pacific Blue-A', region='above')
        gated_sample = plate['A8'].gate(gate)
        RSG = gated_sample.data[['FITC-A']].values


        l, = plt.plot([], [], 'r-')
        plt.xlim(0, 1)
        plt.ylim(0, 1)
        plt.xlabel('x')
        plt.title('test')
        line_ani = animation.FuncAnimation(fig1, update_line, 25, fargs=(data, l),
                                           interval=50, blit=True)
        line_ani.save('lines.mp4', writer=writer)


        def update_hist(num, data):
            plt.cla()
            plt.hist(data[num])

        fig = plt.figure()
        hist = plt.hist(RSG, 30, fc='gray', histtype='stepfilled', alpha=0.3, normed=True)
Beispiel #4
0
def get_dist_data():
    makeFigFolders(analysis = 'RSG')
    OUT1 = open(git_path +'/data/FlowSummary.txt', 'w+')
    print>> OUT1, 'Sample', 'Mean', 'Std', 'Median', 'Skew'
    for sample in samples:
        sample_name = 'Sample_' + sample
        path = data_path + 'Sample_' + sample + '/'
        files = os.listdir(path)
        remove = ['misc', '.DS_Store']
        files1 = [i for i in files if i not in remove]
        files_split = [re.split(r'[._]',x)[3] for x in files1]

        path = data_path + 'Sample_' + sample + '/'
        plate = FCPlate.from_dir(ID='Demo Plate', path = path, parser='name')
        plate = plate.dropna()
        plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', \
            'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
        DAPI_threshold = getDAPIgate(plate)
        DAPI_gate = ThresholdGate(DAPI_threshold, 'Pacific Blue-A', region='above')
        intersection_gate = DAPI_gate
        DAPI_gated_A9 = plate['A8'].gate(intersection_gate)
        # RSG
        RSG = DAPI_gated_A9.data[['FITC-A']].values

        print>> OUT1, sample_name, str(np.mean(RSG)), str(np.std(RSG)), \
            str(np.median(RSG)), str(stats.skew(RSG)[0])

    OUT1.close()
Beispiel #5
0
    def plate_gated_median_fluorescence(self,
                                        channel_name1='',
                                        channel_name2=''):
        if not channel_name1:
            channel_name1 = self.channel_names[0]
            channel_name2 = self.channel_names[1]

        # Check if file already exists
        if CHECK_IF_FILE_EXIST:
            if self.check_if_images_exist('plate_gated_median_fluorescence'):
                return True

        # Load plate
        plate = FCPlate.from_dir(ID='Demo Plate',
                                 path=SHARED_RAW_DIR,
                                 parser='name')
        plate = plate.transform('hlog',
                                channels=[channel_name1, channel_name2],
                                b=500.0)

        # Clear prev sub plot
        subplots(clear=True)

        # Drop empty cols / rows
        plate = plate.dropna()

        # Create a threshold gates
        y2_gate = ThresholdGate(1000.0, channel_name1, region='above')

        # Plot
        plate = plate.gate(y2_gate)

        output = plate.apply(self.__calculate_median_Y2)

        plot_heat_map(output,
                      include_values=True,
                      show_colorbar=True,
                      cmap=cm.Reds)
        title('Heat map of median RFP fluorescence on plate')

        png_file = os.path.join(
            SHARED_PLOTTING_DIR,
            self.get_file_name('plate_gated_median_fluorescence.png'))
        print(png_file)
        grid(False)
        savefig(png_file)
        self.response['plate_gated_median_fluorescence'] = self.get_file_name(
            'plate_gated_median_fluorescence.png')
Beispiel #6
0
    def plate_gated_counts(self, channel_name1='', channel_name2=''):
        if not channel_name1:
            channel_name1 = self.channel_names[0]
            channel_name2 = self.channel_names[1]

        # Check if file already exists
        if CHECK_IF_FILE_EXIST:
            if self.check_if_images_exist('plate_gated_counts'):
                return True

        # Plot - Counting fluorescent events¶
        self.sample.plot([channel_name1, channel_name2],
                         kind='scatter',
                         alpha=0.6,
                         color='gray')

        # Clear prev sub plot
        subplots(clear=True)

        # Load plate
        plate = FCPlate.from_dir(ID='Demo Plate',
                                 path=SHARED_RAW_DIR,
                                 parser='name')
        plate = plate.transform('hlog',
                                channels=[channel_name1, channel_name2],
                                b=500.0)

        # Drop empty cols / rows
        plate = plate.dropna()

        # Create a threshold gates
        y2_gate = ThresholdGate(1000.0, channel_name1, region='above')

        # Plot
        plate = plate.gate(y2_gate)
        plot_heat_map(plate.counts(),
                      include_values=True,
                      show_colorbar=True,
                      cmap=cm.Oranges)
        title('Heat map of fluorescent counts on plate')

        png_file = os.path.join(SHARED_PLOTTING_DIR,
                                self.get_file_name('plate_gated_counts.png'))
        print(png_file)
        grid(False)
        savefig(png_file)
        self.response['plate_gated_counts'] = self.get_file_name(
            'plate_gated_counts.png')
Beispiel #7
0
def animate(i):
    sample = samples[i]
    path = data_path + 'Sample_' + sample + '/'
    plate = FCPlate.from_dir(ID='Demo Plate', path=path, parser='name')
    plate = plate.dropna()
    plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', \
        'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
    threshold = getDAPIgate(plate)
    gate = ThresholdGate(threshold, 'Pacific Blue-A', region='above')
    gated_sample = plate['A8'].gate(gate)
    RSG = gated_sample.data[['FITC-A']].values

    # simulate new data coming in
    n, bins = np.histogram(RSG, 40, normed=True)
    top = bottom + n
    verts[1::5, 1] = top
    verts[2::5, 1] = top
    #t = ax.annotate(sample + ' hours',(7900,1200)) # add text
    time_text.set_text(sample + ' hours')
    return [patch, time_text]
Beispiel #8
0
def get_DAPI_RGF_Figs():
    As = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8']
    for sample in samples:
        print sample
        path = data_path + 'Sample_' + sample + '/'
        plate = FCPlate.from_dir(ID='Demo Plate', path = path, parser='name')
        plate = plate.dropna()
        plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', \
            'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
        threshold = getDAPIgate(plate)
        for A in As:
            fig = plt.figure()
            DAPI = plate[A].data[['Pacific Blue-A']].values
            RSG = plate[A].data[['FITC-A']].values
            plt.scatter(DAPI, RSG)
            plt.axvline(x = threshold)
            flow_fig_path = git_path + '/figures/flow_figs/RSG_DAPI/Sample_' + sample + '/'
            if not os.path.exists(flow_fig_path):
                os.makedirs(flow_fig_path)
            name =  'RSG_DAPI_Sample_' + sample + '_' + A
            plt.savefig(flow_fig_path + name + '.png')
Beispiel #9
0
def get_RSG_hist():
    folder_path = git_path + '/figures/flow_figs/RSG'
    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    fig1 = plt.figure()
    for sample in samples:
        print sample
        path = data_path + 'Sample_' + sample + '/'
        plate = FCPlate.from_dir(ID='Demo Plate', path = path, parser='name')
        plate = plate.dropna()
        plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', \
            'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
        threshold = getDAPIgate(plate)
        gate = ThresholdGate(threshold, 'Pacific Blue-A', region='above')
        gated_sample = plate['A8'].gate(gate)
        RSG = gated_sample.data[['FITC-A']].values

        fig, ax = plt.subplots()
        ax.hist(RSG, 30, fc='gray', histtype='stepfilled', alpha=0.3, normed=True)
        axes.set_xlim([0,10000])
        plt.savefig(git_path + '/figures/flow_figs/RSG/RSG_Sample_' + sample + '.png')
        plt.close()
Beispiel #10
0
def get_DAPI_RGF_Figs():
    strains = ['B', 'C', 'D', 'F', 'P', 'J']
    transfers = ['D1', 'D10', 'D100']
    reps = ['1', '2', '3', '4', '5']
    As = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8']
    for strain in strains:
        for transfer in transfers:
            for rep in reps:
                print strain, transfer, rep
                path = data_path + strain + '/' + transfer + '/' + rep + '/'
                plate = FCPlate.from_dir(ID='Demo Plate', path = path, parser='name')
                plate = plate.dropna()
                plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', \
                    'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
                threshold = getDAPIgate(plate)
                for A in As:
                    fig = plt.figure()
                    DAPI = plate[A].data[['Pacific Blue-A']].values
                    RSG = plate[A].data[['FITC-A']].values
                    plt.scatter(DAPI, RSG)
                    plt.axvline(x = threshold)
                    name =  'RSG_DAPI' + strain + '_' + transfer + '_' + rep + '_' + A
                    plt.savefig(git_path + 'figs/RSG_DAPI/' + strain + '/' + transfer + \
                        '/' + rep + '/' + name + '.png')
Beispiel #11
0
from FlowCytometryTools import FCPlate

plate = FCPlate.from_dir(ID='Demo Plate',
                         path='./20151203/CTC_FVD_Hoechst_test120315',
                         parser='name')
print plate
Beispiel #12
0
def extract_fcs_data(root_path: str, expt_folder: str, metadata_fn_core: str,
                     folder_patterns_to_search: list, json_output_fn: str):
    ''' Process .fcs files, gate, extract RFP2-H values and save as a dataframe
    
    All .fcs files of the same plate should be placed under a folder ending
    with '_FCS' in an experiment folder. An experiment folder can contain
    multiple fcs plate folders. This function processs all those folders and
    their fcs files inside by gating them, extracting their single-cell fluo
    values and then putting them into a table.
    
    It then adds metadata to the table by finding the metadata files within
    the same experiment folder. The metadata file is matched according to the
    plate folder name.
    
    This function automatically saves the final dataframe as a json object
    under the experiment folder.
    
    Parameters:
        root_path (str): the root directory where the experiment folder is,
            abs path (raw string) or relative path
            
        expt_folder (str): name of experiment folder
        
        json_output_fn (str): filename of json, must end in '.json'
        
        metadata_fn_core (str): core components of metadata filename, 
            see CustomFunctions.find_meta_xlsx()
            
        folder_patterns_to_search (list): a list of folder patterns to be 
            searched, each should be a str ending with '_FCS'
        
    Returns: dataframe containing all cytometry data of one experiment

    '''
    #Core Processing Codes

    cyto_data = pd.DataFrame()

    for folder_pattern_to_search in folder_patterns_to_search:

        directories_to_search = os.path.join(root_path, expt_folder,
                                             folder_pattern_to_search)
        matched_folders = glob.glob(directories_to_search)

        for matched_folder in matched_folders:

            plate_name_core = matched_folder.rsplit('\\')[-1].split('_FCS')[0]

            # Read Metadata Excelfile
            metadata_fn = cf.find_meta_xlsx(plate_name_core + '.',
                                            metadata_fn_core)
            # metadata_fn = "PRMD_IBM_EXP012.xlsx" # for debugging
            metadata_path = os.path.join(root_path, expt_folder, metadata_fn)
            metadata = cf.metadata_to_metadf(metadata_path)

            # plate_name_core = 'IBM_EXP012R1' #for debugging
            fcs_folder = plate_name_core + '_FCS'
            datadir = os.path.join(root_path, expt_folder, fcs_folder)
            plate = FCPlate.from_dir(ID='Plate',
                                     path=datadir,
                                     parser=cf.fcsNameParser,
                                     position_mapper='name')

            # Gating
            fsc_gate = IntervalGate((1e3, 1e5), ['FSC-H'], region='in')
            ssc_gate = IntervalGate((1e3, 1e5), ['SSC-H'], region='in')

            rfpA_gate = IntervalGate((1, 1e6), ['RFP2-A'], region='in')
            rfpH_gate = IntervalGate((1, 1e6), ['RFP2-H'], region='in')

            fsc_ssc_gate = CompositeGate(fsc_gate, 'and', ssc_gate)
            rfp_AH_gate = CompositeGate(rfpA_gate, 'and', rfpH_gate)
            plate = plate.gate(fsc_ssc_gate)
            plate = plate.gate(rfp_AH_gate)

            cyto_data_entries = pd.DataFrame()
            for well, fc_data in plate.items():
                entry = pd.DataFrame({
                    'well':
                    well,
                    'FC_fluorescence (a.u.)':
                    [np.array(fc_data.data['RFP2-H'].to_list())],
                    'FC_median fluorescence (a.u.)':
                    fc_data.data['RFP2-H'].median(),
                    'FC_Count':
                    fc_data.counts
                })
                cyto_data_entries = cyto_data_entries.append(entry,
                                                             ignore_index=True)

            del plate, well, fc_data, entry  # delete var "plate" because it is taking too much space

            # Infer metadata from code
            cyto_data_entries['run'] = int(
                plate_name_core.split('R')[1].split('PI')
                [0])  # add run number to df

            if "PI" in plate_name_core:
                post_induction_time = plate_name_core.split('PI')[1].split(
                    'P')[0]

                # In the event that post_induction_time is not an integer
                try:
                    cyto_data_entries['post-induction (hrs)'] = int(
                        post_induction_time)
                except ValueError:
                    cyto_data_entries[
                        'post-induction (hrs)'] = post_induction_time

            # Add FC_Plate number to dataset, if it is present
            try:
                fc_plate_no = plate_name_core.split('PI')[1].split('P')[1]
                cyto_data_entries['FC_plate'] = int(fc_plate_no)
            except IndexError or ValueError:
                pass

            cyto_data_entries = cyto_data_entries.merge(metadata,
                                                        left_on='well',
                                                        right_index=True,
                                                        how='inner')
            cyto_data = cyto_data.append(cyto_data_entries, ignore_index=True)

    json_output_path = os.path.join(root_path, expt_folder, json_output_fn)
    cyto_data.to_json(json_output_path)
    return cyto_data
from FlowCytometryTools import FCPlate
from GoreUtilities import plot_heat_map
import os, FlowCytometryTools
from pylab import *
import matplotlib.pyplot as plt

# Locate sample data included with this package
datadir = os.path.join(FlowCytometryTools.__path__[0], 'tests', 'data', 'Plate01')

# datadir = '[insert path to your own folder with fcs files.]'
# Make sure that the files follow the correct naming convention for the 'name' parser.
# Alternatively, change the parser (see tutorial online for further information).

# Load plate
plate = FCPlate.from_dir(ID='Demo Plate', path=datadir, parser='name')
plate = plate.transform('hlog', channels=['Y2-A', 'B1-A'], b=500.0)

# Drop empty cols / rows
plate = plate.dropna()

# Create a threshold gates
from FlowCytometryTools import ThresholdGate
y2_gate = ThresholdGate(1000.0, 'Y2-A', region='above')

# Plot
plate = plate.gate(y2_gate)
plot_heat_map(plate.counts(), include_values=True, show_colorbar=True,
        cmap=cm.Oranges)
title('Heat map of fluorescent counts on plate')

#show() # <-- Uncomment when running as a script.
Beispiel #14
0
from FlowCytometryTools import FCPlate

plate = FCPlate.from_dir(ID='Demo Plate', path= './20151203/CTC_FVD_Hoechst_test120315', parser='name')
print plate
Beispiel #15
0
def getDistRSG(KDEs = False):
    makeFigFolders(analysis = 'RSG')
    strains = ['B', 'C', 'D', 'F', 'P', 'J']
    transfers = ['D1', 'D10', 'D100']
    reps = ['1', '2', '3', '4', '5']
    #strains = ['C']
    #transfers = ['D100']
    #reps = ['4']
    OUT1 = open(git_path +'data/bandwidth.txt', 'w+')
    if KDEs == True:
        print>> OUT1, 'strain', 'transfer', 'rep', 'bandwidth', 'mean', 'std', 'median', 'skew'
        for strain in strains:
            for transfer in transfers:
                for rep in reps:
                    signal.signal(signal.SIGALRM, timeout_handler)
                    print strain, transfer, rep
                    path = data_path + strain + '/' + transfer + '/' + rep + '/'
                    plate = FCPlate.from_dir(ID='Demo Plate', path = path, parser='name')
                    plate = plate.dropna()
                    plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', \
                        'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
                    threshold = getDAPIgate(plate)
                    gate = ThresholdGate(threshold, 'Pacific Blue-A', region='above')
                    gated_sample = plate['A8'].gate(gate)
                    RSG = gated_sample.data[['FITC-A']].values
                    signal.alarm(500)
                    a = datetime.datetime.now()
                    try:
                        KDE = CV_KDE(RSG)

                        df = pd.DataFrame({'x_grid':KDE[0],'pdf':KDE[1]})
                        if not os.path.exists(git_path + 'data/RSG'):
                            os.makedirs(git_path + 'data/RSG')
                        bw_round = round(KDE[2], 3)
                        pandas_name = 'RSG_'+ strain + '_' + transfer + '_' + rep + '.txt'
                        df.to_csv(path_or_buf= git_path + 'data/RSG/' + pandas_name, sep = ' ', index=False)
                        fig, ax = plt.subplots()
                        ax.plot(KDE[0], KDE[1], linewidth=3, alpha=0.5, label='bw=%.2f' % KDE[2])
                        ax.hist(RSG, 30, fc='gray', histtype='stepfilled', alpha=0.3, normed=True)
                        ax.legend(loc='upper left')
                        name = 'RSG_' + strain + '_' + transfer + '_' + rep
                        plt.savefig(git_path + 'figs/RSG'  + '/' + strain + '/' + transfer + '/' + name + '.png')
                        plt.close()

                        print>> OUT1, strain, transfer[1:], rep, str(KDE[2]), str(np.mean(RSG)), str(np.std(RSG)), \
                            str(np.median(RSG)), str(stats.skew(RSG)[0])
                        b = datetime.datetime.now()
                        c = b - a
                        print str(c.seconds) + " seconds"

                    except TimeoutException:
                        continue # continue the for loop if function takes more than x seconds
                    else:
                        # Reset the alarm
                        signal.alarm(0)
    else:
        print>> OUT1, 'Strain', 'Transfer', 'Rep', 'Mean', 'Std', 'Median', 'Skew'
        for strain in strains:
            for transfer in transfers:
                for rep in reps:
                    print strain, transfer, rep
                    path = data_path + strain + '/' + transfer + '/' + rep + '/'
                    plate = FCPlate.from_dir(ID='Demo Plate', path = path, parser='name')
                    plate = plate.dropna()
                    plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', \
                        'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
                    threshold = getDAPIgate(plate)
                    gate = ThresholdGate(threshold, 'Pacific Blue-A', region='above')
                    gated_sample = plate['A8'].gate(gate)
                    RSG = gated_sample.data[['FITC-A']].values
                    print>> OUT1, strain, transfer[1:], rep, str(np.mean(RSG)), str(np.std(RSG)), \
                        str(np.median(RSG)), str(stats.skew(RSG)[0])



    OUT1.close()
Beispiel #16
0
        plateNameCore = matchedFolder.rsplit('\\')[-1].split('_FCS')[0]

        # Read Metadata Excelfile
        metafilename = cf.findMetaXlsx(plateNameCore + '.', metadatafnCore)
        metadataDir = os.path.join(dataRootDir, dataFolderDir, metafilename)
        metaxls = pd.ExcelFile(metadataDir)
        metadata = {
            sheet: metaxls.parse(sheet, index_col=0)
            for sheet in metaxls.sheet_names
        }  #import all sheets in metadata file into a dict, property name=keys, metadata df = values

        #plateNameCore = '' #for debugging
        fcsFolderDir = plateNameCore + '_FCS'
        datadir = os.path.join(dataRootDir, dataFolderDir, fcsFolderDir)
        plate = FCPlate.from_dir(ID='Plate',
                                 path=datadir,
                                 parser=cf.fcsNameParser,
                                 position_mapper='name')

        # Gating
        fsc_gate = ThresholdGate(1000.0, ['FSC-H'], region='above')
        ssc_gate = ThresholdGate(1000.0, ['SSC-H'], region='above')

        rfpA_gate = ThresholdGate(1.0, ['RFP2-A'], region='above')
        fscssc_gate = CompositeGate(fsc_gate, 'and', ssc_gate)
        plate = plate.gate(fscssc_gate)
        plate = plate.gate(rfpA_gate)

        # Produces 96 well layout for intuitive observation
        # Calculate Median from data
        def calculate_median_Y2(well):
            return well.data['RFP2-H'].median()
Beispiel #17
0
    index = search('_[A-Z][0-9]+_', file_path).group()

    col, row = index[2:-1], index[1]
    return (row_layout[row], col_layout[col])


plates = {'C6': [], 'C12': []}
data_dirs = [dir for dir in glob('*/*') if '.py' not in dir]

for data_dir in data_dirs:
    primed, date = data_dir.split('/')

    plate = FCPlate.from_dir(position_mapper=lambda x: x,
                             ID=date,
                             path=data_dir,
                             pattern='*.fcs',
                             parser=parser,
                             col_labels=list(col_layout.values()),
                             row_labels=list(row_layout.values()))

    for idx in plate:

        plate[idx].data = plate[idx].data.rename(columns=channel_map)
        channel_differences = set(plate[idx].data.columns) - set(markers)

        if len(channel_differences) > 0:
            raise ValueError(
                '''{}\nChannel names not in markers. Change marker list or use channel_map'''
                .format(channel_differences))

        # map channel names
Beispiel #18
0
from sklearn.cluster import KMeans, MeanShift, estimate_bandwidth, DBSCAN,MiniBatchKMeans,spectral_clustering
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KernelDensity
from sklearn.cross_validation import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.grid_search import GridSearchCV
from sklearn import metrics
#import FlowFunctions as ff
import scipy.signal as signal
import pandas as pd
from scipy.sparse import hstack
import numpy as np
from scipy.stats import gaussian_kde


plate = FCPlate.from_dir(ID='Demo Plate', path= '/Users/WRShoemaker/github/Task2/FlowCyto/20151203/CTC_FVD_Hoechst_test120315/', parser='name')
plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', 'PI (B)-A', 'Alexa Fluor 488-A', 'Pacific Blue-A'])
plate = plate.dropna()
gate = ThresholdGate(2000.0, 'Pacific Blue-A', region='below')
gated_sample_beads_A3= plate['A3'].gate(gate)

plateData = plate['A3'].data[['Pacific Blue-A', 'PI (B)-A']]

CTCnumpy = gated_sample_beads_A3[['PI (B)-A']].values
DNAnumpy = gated_sample_beads_A3[['Pacific Blue-A']].values

def get_kdens_choose_kernel(xlist,expand, kernel = 0.5):
    """ Finds the kernel density function across a vector of values """
    xlist = xlist[np.logical_not(np.isnan(xlist))]
    density = gaussian_kde(xlist)
    n = len(xlist)
Beispiel #19
0
    As = ['A3', 'A4']
    cutoffs = []
    for A in As:
        DAPI = plate[A].data[['Pacific Blue-A']].values
        DAPI_gate = np.mean(DAPI) + (2 * np.std(DAPI))
        cutoffs.append(DAPI_gate)
    cutoff = np.mean(cutoffs)
    return cutoff


fig, ax = plt.subplots()

# histogram our data with numpy

path1 = data_path + 'Sample_' + samples[0] + '/'
plate1 = FCPlate.from_dir(ID='Demo Plate', path=path1, parser='name')
plate1 = plate1.dropna()
plate1 = plate1.transform('hlog', channels=['FSC-A', 'SSC-A', \
    'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
threshold = getDAPIgate(plate1)
gate1 = ThresholdGate(threshold, 'Pacific Blue-A', region='above')
gated_sample1 = plate1['A8'].gate(gate1)
RSG1 = gated_sample1.data[['FITC-A']].values
n, bins = np.histogram(RSG1, 40, normed=True)

# get the corners of the rectangles for the histogram
left = np.array(bins[:-1])
right = np.array(bins[1:])
bottom = np.zeros(len(left))
top = bottom + n
nrects = len(left)
Beispiel #20
0
def testCounts():
    tubes = ['A8']
    for sample in samples:
        print sample
        path = data_path + 'Sample_' + sample + '/'
        files = os.listdir(path)
        remove = ['misc', '.DS_Store']
        files1 = [i for i in files if i not in remove]
        files_split = [re.split(r'[._]',x)[3] for x in files1]
        if 'H' in files_split:
            for tube in tubes:
                file_folder_path = git_path + '/figures/flow_figs/' + tube
                if not os.path.exists(file_folder_path):
                    os.makedirs(file_folder_path)

                file_folder_sample_path = git_path + '/figures/flow_figs/' + \
                    tube +'/Sample_' + sample + '/'
                if not os.path.exists(file_folder_sample_path):
                    os.makedirs(file_folder_sample_path)

                plate = FCPlate.from_dir(ID='Demo Plate', path = path, parser='name')
                plate = plate.dropna()
                plate = plate.transform('hlog', channels=['FSC-A', 'SSC-A', \
                    'FSC PMT-A','PI (YG)-A', 'FITC-A', 'Pacific Blue-A', 'APC-A'])
                threshold = getDAPIgate(plate)
                gate = ThresholdGate(threshold, 'Pacific Blue-A', region='above')
                gated_sample = plate[tube].gate(gate)
                FSC_A = gated_sample.data[['FSC-A']].values
                SSC_A = gated_sample.data[['SSC-A']].values
                PI = gated_sample.data[['PI (YG)-A']].values
                FITC = gated_sample.data[['FITC-A']].values
                FSC_H = gated_sample.data[['FSC-H']].values
                SSC_H = gated_sample.data[['SSC-H']].values
                FSC_PMT_A = gated_sample.data[['FSC PMT-A']].values
                FSC_PMT_H = gated_sample.data[['FSC PMT-H']].values
                Pacific_blue = gated_sample.data[['Pacific Blue-A']].values
                APC = gated_sample.data[['APC-A']].values

                fig = plt.figure()
                ax = fig.add_subplot(111, projection='3d')
                ax.scatter(FITC, Pacific_blue, APC , zdir='z', alpha = 0.2)
                ax.set_xlabel('FITC')
                ax.set_ylabel('Pacific blue')
                ax.set_zlabel('APC')

                name = tube + '_FITC_APC_PacificBlue_' + sample
                plt.savefig(file_folder_sample_path + name + '.png')
                plt.close()

                fig = plt.figure()
                ax = fig.add_subplot(111)
                ax.scatter(APC, FITC, alpha = 0.2)
                ax.set_xlabel('APC')
                ax.set_ylabel('FITC')

                name = tube +'_APC_FITC_' + sample
                plt.savefig(file_folder_sample_path + name + '.png')
                plt.close()


                fig = plt.figure()
                ax = fig.add_subplot(111)
                ax.scatter(Pacific_blue, FITC, alpha = 0.2)
                ax.set_xlabel('Pacific blue')
                ax.set_ylabel('FITC')

                name = tube + '_PacificBlue_FITC_' + sample
                plt.savefig(file_folder_sample_path + name + '.png')
                plt.close()

                fig = plt.figure()
                ax = fig.add_subplot(111)
                ax.scatter(FSC_A, SSC_A, alpha = 0.2)
                ax.set_xlabel('FSC-A')
                ax.set_ylabel('SSC-A')

                name = tube + '_FSC_A_SSC_A_' + sample
                plt.savefig(file_folder_sample_path + name + '.png')
                plt.close()

                fig = plt.figure()
                ax = fig.add_subplot(111)
                ax.scatter(FSC_H, SSC_H, alpha = 0.2)
                ax.set_xlabel('FSC-H')
                ax.set_ylabel('SSC-H')
                #plt.axhline(y = 250000, linewidth=2, color='darkgrey', ls='--')

                name = tube + '_FSC_H_SSC_H_' + sample
                plt.savefig(file_folder_sample_path + name + '.png')
                plt.close()

                fig = plt.figure()
                ax = fig.add_subplot(111)
                ax.scatter(FSC_PMT_A, FSC_PMT_H, alpha = 0.2)
                ax.set_xlabel('FSC PMT-A')
                ax.set_ylabel('FSC PMT-H')

                name = tube + '_FSC_PMT_A_FSC_PMT_H_' + sample
                plt.savefig(file_folder_sample_path + name + '.png')
                plt.close()
from pylab import *

import FlowCytometryTools
from FlowCytometryTools import FCPlate
from FlowCytometryTools.core.graph import plot_heat_map

# Locate sample data included with this package
datadir = os.path.join(FlowCytometryTools.__path__[0], 'tests', 'data', 'Plate01')

# datadir = '[insert path to your own folder with fcs files.]'
# Make sure that the files follow the correct naming convention for the 'name' parser.
# Alternatively, change the parser (see tutorial online for further information).

# Load plate
plate = FCPlate.from_dir(ID='Demo Plate', path=datadir, parser='name')
plate = plate.transform('hlog', channels=['Y2-A', 'B1-A'], b=500.0)

# Drop empty cols / rows
plate = plate.dropna()

# Create a threshold gates
from FlowCytometryTools import ThresholdGate
y2_gate = ThresholdGate(1000.0, 'Y2-A', region='above')

# Plot
plate = plate.gate(y2_gate)

def calculate_median_Y2(well):
    return well.data['Y2-A'].median()
Beispiel #22
0
        os.rename(file_path, rename_file_path)
        print('{} was changed to Well_{}'.format(file, file))

#%%
"""
Writing the excel file
"""

#writing the data from downloaded .fcs files
ws.write(0, 0, 'Well')
ws.write(0, 1, 'Median FL1-A')
ws.write(0, 2, 'Strain Name')
ws.write(0, 3, 'Gboxs')
ws.write(0, 4, 'beta-estradiol conc')
i = 1
plate = FCPlate.from_dir(ID='Test Plate', path=dir_path, parser='name')
plate = plate.dropna()
for well in plate:
    ws.write(i, 0, well)
    ws.write(i, 1, plate[well]['FL1-A'].median().item())
    i += 1
#%%

#writing the data from the plate item
plate_item = prod.Item.find(plate_id)
all_sample_items = plate_item.as_collection().matrix
dimensions = plate_item.as_collection().dimensions
for row in range(dimensions[0]):
    for column in range(dimensions[1]):
        sam_it = plate_item.as_collection().part(row, column)
        if sam_it is not None:
Beispiel #23
0
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KernelDensity
from sklearn.cross_validation import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.grid_search import GridSearchCV
from sklearn import metrics
#import FlowFunctions as ff
import scipy.signal as signal
import pandas as pd
from scipy.sparse import hstack
import numpy as np
from scipy.stats import gaussian_kde

plate = FCPlate.from_dir(
    ID='Demo Plate',
    path=
    '/Users/WRShoemaker/github/Task2/FlowCyto/20151203/CTC_FVD_Hoechst_test120315/',
    parser='name')
plate = plate.transform('hlog',
                        channels=[
                            'FSC-A', 'SSC-A', 'PI (B)-A', 'Alexa Fluor 488-A',
                            'Pacific Blue-A'
                        ])
plate = plate.dropna()
gate = ThresholdGate(2000.0, 'Pacific Blue-A', region='below')
gated_sample_beads_A3 = plate['A3'].gate(gate)

plateData = plate['A3'].data[['Pacific Blue-A', 'PI (B)-A']]

CTCnumpy = gated_sample_beads_A3[['PI (B)-A']].values
DNAnumpy = gated_sample_beads_A3[['Pacific Blue-A']].values
 def setUpClass(cls):
     cls.fc_measurement = FCMeasurement(ID='test', datafile=test_path)
     cls.fc_plate = FCPlate('plate',
                            measurements={'A1': cls.fc_measurement},
                            position_mapper='name')