Created
February 5, 2020 10:54
-
-
Save tbrittoborges/db9e4b40e8fb171bb0323d7b289eae93 to your computer and use it in GitHub Desktop.
deletes files in the rrna_free_reads and trimmed_reads to save space
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
#!/usr/bin/env python | |
# coding: utf-8 | |
from pathlib import Path | |
import os | |
from itertools import chain | |
from datetime import datetime | |
__author__: tbrittoborges | |
__date__: 05/02/2020 | |
# RNA-Seq workflow | |
expected_lines = { | |
0: 'SHELL := /bin/bash\n', | |
1: '\n', | |
2: 'all: TRIMMING RRNA MAPPING\n' | |
} | |
def check_makefile(file_path): | |
'Check makefile for RNA-Seq type with its three first lines' | |
with open(file_path) as fin: | |
for i, line in enumerate(fin): | |
if i == 3: | |
return True | |
elif line != expected_lines[i]: | |
return False | |
def calculate_dir_size(dir_path): | |
'Computes directory size in Gb' | |
return sum(x.stat().st_size for x in dir_path.rglob('*')) / 1024e6 | |
def delete_and_annotate(path): | |
# scan recursively and deletes files | |
for f in Path(path).rglob('*'): | |
os.unlink(f) | |
# documents the deletion time | |
with open(Path(path) / 'DELETED') as fout: | |
now = datetime.now() | |
dt_string = now.strftime("%d/%m/%Y %H:%M:%S") | |
fout.write(f'Content removed on {dt_stringdt_string}') | |
prj = Path('/beegfs/prj/') | |
with open('/scratch/tbrittoborges/report_on_dir_size.txt', 'w') as fou: | |
for f in prj.rglob('workflow/Makefile'): | |
print('=====', | |
'path: ', f, | |
'makefile check? ', check_makefile(f), | |
'accessible? ', os.access(f.parent, os.W_OK), | |
'rrna_free size (Gb)', round(calculate_dir_size(f.parent / 'rrna_free_reads/'), 2), | |
'trimmed_reads size (Gb)', round(calculate_dir_size(f.parent / 'trimmed_reads/'), 2), | |
'=====', sep='\n', file=fou, flush=True) | |
if os.access(f.parent, os.W_OK) & check_makefile(f): | |
delete_and_annotate(f.parent / 'rrna_free_reads/') | |
delete_and_annotate(f.parent / 'trimmed_reads/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment