#!/usr/bin/env python # coding: utf-8 # # Single-cell cooler format Python API examples # The here provided examples are using the cooler Python API with version 0.8.9. Earlier versions do not support single-cell cooler files. # The single-cell cooler format uses an HDF5 container and can therefore be accessed with any HDF5 library of any programming language. However, the programmer needs to take care of all the operations on his/her own and make sure the single-cell cooler definitions are met. # Please consider our documentation on https://cooler.readthedocs.io/ # In[1]: import cooler import os # ## Create a single-cell cooler file # #### Load cooler files # In[2]: path_file1 = 'data/single-cell/GSM2687248_41669_ACAGTG-R1-DpnII.100000.cool' # In[3]: path_file2 = 'data/single-cell/GSM2687249_41670_GGCTAC-R1-DpnII.100000.cool' # In[4]: path_file3 = 'data/single-cell/GSM2687250_41671_TTAGGC-R1-DpnII.100000.cool' # In[5]: cooler_file1 = cooler.Cooler(path_file1) # In[6]: cooler_file2 = cooler.Cooler(path_file2) # In[7]: cooler_file3 = cooler.Cooler(path_file3) # #### Load pixels # In[8]: pixel1 = cooler_file1.pixels()[:] # In[9]: pixel2 = cooler_file2.pixels()[:] # In[10]: pixel3 = cooler_file3.pixels()[:] # #### Load bins # In[11]: bin1 = cooler_file1.bins()[:] # In[12]: bin2 = cooler_file2.bins()[:] # In[13]: bin3 = cooler_file3.bins()[:] # #### Cell names # In[14]: cell_name_list = [os.path.basename(cooler_file1.filename), os.path.basename(cooler_file2.filename), os.path.basename(cooler_file3.filename)] # #### Create input # Bins either as pandas pixels or as a dict of name:bins (as pandas pixels) # In[15]: bins = bin1 # In[16]: bins_dictionary = {cell_name_list[0]:bin1, cell_name_list[1]:bin2, cell_name_list[2]:bin3} # Pixels as dictionary name:pixels # In[17]: pixels_dictionary = {cell_name_list[0]:pixel1, cell_name_list[1]:pixel2, cell_name_list[2]:pixel3} # #### Create scool with bins as pandas dataframe # In[18]: cooler.create_scool('outfile_test.scool', bins, pixels_dictionary) # #### Create scool with bins as dictonary # In[19]: cooler.create_scool('outfile_test.scool', bins_dictionary, pixels_dictionary) # ## Read scool # ### Get paths of the cells # In[20]: content_of_scool = cooler.fileops.list_scool_cells('outfile_test.scool') # In[21]: content_of_scool # ### Load the individual cells # In[22]: cell1 = cooler.Cooler('outfile_test.scool' + '::' + content_of_scool[0]) # In[23]: cell2 = cooler.Cooler('outfile_test.scool' + '::' + content_of_scool[1]) # In[24]: cell3 = cooler.Cooler('outfile_test.scool' + '::' + content_of_scool[2]) # ### Bin content # The bins are shared and for all cells identical # In[25]: cell1.bins()[:10] # In[26]: cell2.bins()[:10] # In[27]: cell3.bins()[:10] # ### Pixel content # The content of the pixels are in all cells different # In[28]: cell1.pixels()[:10] # In[29]: cell2.pixels()[:10] # In[30]: cell3.pixels()[:10]