summaryrefslogtreecommitdiff
path: root/Code/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'Code/utils.py')
-rw-r--r--Code/utils.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/Code/utils.py b/Code/utils.py
index 2451b91..d4bb3e6 100644
--- a/Code/utils.py
+++ b/Code/utils.py
@@ -3,6 +3,8 @@
# Created by Hui on 20 July 2021
import os
+import shutil
+import mmap
from datetime import datetime
def get_edge_set(fname):
@@ -43,6 +45,66 @@ def make_paths(s):
os.makedirs(s)
+def write_first_column(lst, fname):
+ with open(fname, 'w') as f:
+ for x in lst:
+ f.write(x + '\n')
+
+
+def append_column_fast(basefile, col):
+ ''' Append col to basefile. If basefile does not exist, then create it and col will be basefile's first column.'''
+ if not os.path.exists(basefile):
+ with open(basefile, 'w') as f:
+ count = 0
+ for x in col:
+ f.write(x + '\n')
+ count += 1
+ return count
+
+ with open(basefile) as f:
+ lines = f.readlines()
+ if len(lines) != len(col):
+ return
+
+ count = 0
+ with open(basefile, 'w') as f:
+ for line in lines:
+ line = line.strip()
+ new_line = line + '\t' + col[count] + '\n'
+ count += 1
+ f.write(new_line)
+
+ return count
+
+
+def append_column_fast2(basefile, col):
+ ''' Append col to basefile. If basefile does not exist, then create it and col will be basefile's first column.'''
+ if not os.path.exists(basefile):
+ with open(basefile, 'w') as f:
+ count = 0
+ for x in col:
+ f.write(x + '\n')
+ count += 1
+ return count
+
+ with open(basefile) as f:
+ with mmap.mmap(f.fileno(), length=0, access=mmap.ACCESS_READ) as mmap_f:
+ lines = mmap_f.read().split(b'\n')[:-1]
+ if len(lines) != len(col):
+ return
+
+ count = 0
+ content = ''
+ with open(basefile, 'w') as f:
+ for line in lines:
+ line = line.decode().strip()
+ new_line = line + '\t' + col[count] + '\n'
+ content += new_line
+ count += 1
+ f.write(content)
+ return count
+
+
if __name__ == '__main__':
S2 = get_edge_set('/home/lanhui/brain/Data/temp/edges.txt')
S1 = get_edge_set('/home/lanhui/brain/Data/temp/edges.txt.old')