-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcecolisummary.py
executable file
·68 lines (54 loc) · 2.05 KB
/
qcecolisummary.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
## Ecoli related analysis
## Summarizes the contents of various .tsv files by keeping 1 header and concatenating the contents.
import argparse
import os
import subprocess
parser = argparse.ArgumentParser(description='qcecolisummary.py')
parser.add_argument("-i", "--input_dir", default='input')
parser.add_argument("-o", "--output_dir", default='output')
args = parser.parse_args()
# Functions
def make_folder_if_not_exists(folder_name):
"""This function creates output folders if they don't exists."""
if not os.path.exists(folder_name):
os.makedirs(folder_name)
def find_dirs(path):
"""This function finds dirs in a path,
excluding files."""
dirs = [os.path.join(path, dir) for dir in os.listdir(path) if os.path.isdir(os.path.join(path, dir))]
return dirs
def find_files(path, extension=None):
"""This function finds files in a path with optional extension filtering,
excluding directories."""
if extension:
files = [os.path.join(path, file) for file in os.listdir(path)
if os.path.isfile(os.path.join(path, file)) and file.endswith(extension)]
else:
files = [os.path.join(path, file) for file in os.listdir(path)
if os.path.isfile(os.path.join(path, file))]
return files
# Main
# Create outputs
make_folder_if_not_exists(args.output_dir)
sampledirs = find_dirs(args.input_dir)
for sampledir in sampledirs:
outfile_path = f"{os.path.join(args.output_dir, os.path.basename(args.input_dir))}.tsv"
if os.path.exists(outfile_path):
outfile=open(outfile_path,"a")
header="true"
line_ending='\n'
else:
outfile=open(outfile_path,"w")
header="false"
files = find_files(sampledir, '.tsv')
for file in files:
print(file)
if header=="false":
proc=subprocess.Popen("".join(["head -1 ", file]), stdout=subprocess.PIPE, shell=True)
(out, err)=proc.communicate()
outfile.write(out.decode())
header="true"
proc=subprocess.Popen("".join(["tail -n +2 ", file]), stdout=subprocess.PIPE, shell=True)
(out, err)=proc.communicate()
outfile.write(out.decode() + '\n')