-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon_methylation_ids.py
54 lines (47 loc) · 1.68 KB
/
common_methylation_ids.py
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
import sys
from soft import get_ids
directory = sys.argv[1]
#Use this function for a complete scan of all files (Very slow)
def get_files_in_subdirectories(base_dir):
import os
files = []
for item in os.listdir(base_dir):
item_path = os.path.join(base_dir, item)
if os.path.isdir(item_path):
for nested_file in os.listdir(item_path):
file_path = os.path.join (item_path, nested_file)
if os.path.isfile(file_path):
files.append(file_path)
return files
#Use this function for a scan of first files in each experiment (Fast but assumes all samples have the same cg ids)
def get_first_file_in_directories(base_dir):
import os
files = []
for item in os.listdir(base_dir):
item_path = os.path.join(base_dir, item)
if os.path.isdir(item_path):
file_path = os.path.join (item_path, os.listdir(item_path)[0])
files.append(file_path)
return files
def save_file(id_dict):
try:
with open("data/training/methylation_ids.txt", 'w') as methylids_file:
methylids_file.write("0,(Intercept)")
for k, v in id_dict.items():
methylids_file.write(f"{v},{k}\n")
except FileNotFoundError:
os.mkdir("data/training")
save_file(id_dict)
ids = set()
for sample in get_first_file_in_directories(directory):
print(f"Processing file: {sample}")
curr_ids = set()
get_ids(sample, curr_ids)
if len(ids) != 0:
ids = ids.intersection(curr_ids)
print(f"Current amount of ids: {len(ids)}")
else:
ids = curr_ids
id_dict = dict()
for num, sid in enumerate(ids):
id_dict[sid] = num