Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
master_project_JLU2018/bin/3.1_create_gtf/Modules/Ensembl/ActivityCategorizer.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
149 lines (103 sloc)
5.49 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
class ActivityCategorizer: | |
""" | |
Class that categorizes activitydata based on json config and binary activitydata (table.bin). | |
@author: Sebastian Beyvers | |
@contact: sebastian.beyvers@med.uni-giessen.de | |
""" | |
def __init__(self, release, organism, wd, data_dir): | |
# Constructor for ActivityCategorizer | |
# input_parameter: organism = input organism | |
# release = current used Ensembl release | |
# wd = working dir (default working directory, data_dir is used if specified) | |
# data_dir = data directory (this is used as directory if specified) | |
# List of all folders with activity Tables | |
self.folderlist = [] | |
# Dictionary from celltypes_organism.json mit key = category and Value = [directory] | |
self.c_dict = self.read_config(organism, wd) | |
# Activity table from all files as dict | |
self.activity = {} | |
self.get_activity_data(release, organism, wd, data_dir) | |
# Categorized Activity from Json-config | |
print("Categorization: This may take a while") | |
self.categorization = self.generate_categorized_activity() | |
print("Categorization finished !") | |
def get_categorization(self): | |
# Getter method to return the self.categorization variable | |
return self.categorization | |
def read_config(self, organism, wd): | |
# Method to read the celltypes_organism.json config file | |
# input_parameter: organism = input organism | |
# wd = working directory to find the config files. | |
# return_value: Dictionary with ensembl aliases based on config | |
# -> Key = type (from config), value = list of ensembl aliases | |
c_dict = {} | |
path_to_config = os.path.join(wd +"/config/celltypes_"+organism+".json") | |
with open(path_to_config) as input_file: | |
data = json.loads(input_file.read()) | |
for x in data: | |
c_dict[x["type"]] = x["alias_ensembl"] | |
self.folderlist.extend(x["alias_ensembl"]) | |
return c_dict | |
def get_activity_data(self, release, organism, wd, data_dir): | |
# Method to read the binary table.bin file and return its content as bytearray | |
# input_parameter: organism = input organism | |
# release = current used Ensembl release | |
# wd = working dir (default working directory, data_dir is used if specified) | |
# data_dir = data directory (this is used as directory if specified) | |
# return_value: bytearray with activitystatus | |
for folder in self.folderlist: | |
# Generate path to binary File | |
if data_dir: | |
file = os.path.join(data_dir + "/EnsemblData", release, organism, "activity", folder, "table.bin") | |
else: | |
file = os.path.join(wd + "/data/EnsemblData", release, organism, "activity", folder, "table.bin") | |
with open(file, "rb") as tables: | |
self.activity[folder] = bytearray(tables.read()) | |
def generate_categorized_activity(self): | |
# Categorizes the activity by config defined categories. | |
category_activity = {} | |
for category, aliases in self.c_dict.items(): | |
# If an alias exists | |
if aliases: | |
# If theres only one alias | |
if len(aliases) == 1: | |
category_activity[category] = self.activity[aliases[0]] | |
# If there are multiple alias | |
else: | |
category_activity[category] = self.activity_comparator(aliases) | |
# If theres no alias all bytes were set to 4 = NA | |
else: | |
category_activity[category] = bytearray([4]*len(self.activity[self.folderlist[0]])) | |
return category_activity | |
def activity_comparator(self, aliaslist): | |
# Method to determine the resulting activitystatus if the entry contains | |
# multiple differing activitystatus from aliases | |
# e.g. if one alias is ACTIVE and one INACTIVE the result will be ACTIVE -> see wiki for more detailed info | |
# input_parameter: aliaslist = list of aliases for activity_data | |
# return_value: Array of Activitystatus by category (type in config) | |
concatenated_array = bytearray([]) | |
length = len(self.activity[aliaslist[0]]) | |
input_arrays = [self.activity[index] for index in aliaslist] | |
for x in range(length): | |
# This try-catch block is needed because of inconsistency in file-lengths in Ensembl-release-95 | |
try: | |
if any(y[x] == 0 for y in input_arrays): | |
concatenated_array.append(0) | |
elif any(y[x] == 1 for y in input_arrays): | |
concatenated_array.append(1) | |
elif any(y[x] == 2 for y in input_arrays): | |
concatenated_array.append(2) | |
elif any(y[x] == 3 for y in input_arrays): | |
concatenated_array.append(3) | |
elif any(y[x] == 4 for y in input_arrays): | |
concatenated_array.append(4) | |
except IndexError: | |
concatenated_array.append(4) | |
return concatenated_array | |
# Debugging | |
# e = ActivityCategorizer("../../config/celltypes_human.json", "release-94", "homo_sapiens") | |
# print(len(e.categorization)) | |
# for x in e.categorization.values(): | |
# print(len(x)) |