This repository was archived by the owner on Mar 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathother_func.py
More file actions
55 lines (44 loc) · 1.81 KB
/
other_func.py
File metadata and controls
55 lines (44 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def get_gene_symbol(gene_ids: str) -> str:
"""The function that make an API request to my mygene.info, requesting for symbol using gene ID
Args:
gene_id (str): the id of the gene
Returns:
(str) the symbol of the gene
"""
headers = {"content-type": "application/x-www-form-urlencoded"}
params = f"ids={gene_ids}&fields=symbol"
try:
res = requests.post("http://mygene.info/v3/gene", data=params, headers=headers)
res.raise_for_status()
except requests.RequestException as e:
return {}
return json.loads(res.content.decode("utf-8"))
def pairUp_seq_info(value_dict: dict) -> list:
"""Pair up all the elements in each value of the dictionary
Args:
value_dict (dict): values that need to be paired up
Returns:
list[dict]: A list of dictionaries where each dict contains a pairing
of keys with corresponding elements from the value lists.
>>> pairUp_seq_info({"a": "1, 2", "b": "3, 4"})
[{'a': '1', 'b': '3'}, {'a': '2', 'b': '4'}]
>>> pairUp_seq_info({"a": '1'})
[{'a': '1'}]
"""
keys = list(value_dict.keys())
values = [str_to_list(value) for value in value_dict.values()]
# when there is mismatch gene and its symbol, we will get gene symbol form my.gene API
if not all(len(v) == len(next(iter(values))) for v in values):
gene_ids = ", ".join(
[
gene_id
for gene_id in str_to_list(value_dict["geneid"])
if gene_id.casefold() != "na" and gene_id != ""
]
)
symbols = get_gene_symbol(gene_ids)
return [
{"geneid": gene_info["_id"], "genesymbol": gene_info["symbol"]}
for gene_info in symbols
]
return [dict(zip(keys, combination)) for combination in zip(*values)]