-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbowtie2index.py
86 lines (66 loc) · 2.75 KB
/
bowtie2index.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
"""
Author : Pierre Levy <[email protected]>
Date : 2024-09-13
Purpose: Bowtie2 indexing for Ag lib
"""
import argparse
import subprocess
import os
import sys
import logging
# --------------------------------------------------
def get_args():
"""Get command-line arguments"""
parser = argparse.ArgumentParser(
description='Ag library screen processing',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('lib',
metavar='str',
help='fasta file containing library ag sequences')
parser.add_argument('-l',
'--log',
help='Name of the log file',
metavar='\b',
type=str,
default='log.txt')
return parser.parse_args()
# --------------------------------------------------
def exec_command(cmd):
logger = logging.getLogger()
logger.info(f'{cmd}\n')
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, executable='/bin/bash')
output, error = p.communicate()
if p.returncode != 0:
for line in output.decode("utf-8").split("\n") if output else "":
logger.error(line.rstrip())
for line in error.decode("utf-8").split("\n") if error else "":
logger.error(line.rstrip())
sys.exit()
# --------------------------------------------------
def main():
"""Make a jazz noise here"""
args = get_args()
lib = os.path.abspath(args.lib)
log = args.log
# create output directory out and set as current wd
if not os.path.exists(f'{os.path.dirname(lib)}/bowtie2_indices'):
os.mkdir(f'{os.path.dirname(lib)}/bowtie2_indices') # create dir
# Create log file
logging.basicConfig(format='%(asctime)s - %(message)s',
datefmt='%d-%b-%y %H:%M:%S',
level=logging.DEBUG,
handlers=[
logging.FileHandler(f'{os.path.dirname(lib)}/bowtie2_indices/{log}'),
logging.StreamHandler() # these 2 handlers allow 1) to have the log file created and 2) to stream to the terminal
])
logger = logging.getLogger() # creates logger to add entries to the log
# bowtie2 indexing (if not done before)
if not os.path.exists(f"{os.path.dirname(lib)}/bowtie2_indices/aglib.1.bt2"):
logger.info("****** Bowtie2: library fasta indexing ******\n\n")
cmd = f"bowtie2-build {lib} {os.path.dirname(lib)}/bowtie2_indices/aglib"
exec_command(cmd)
# --------------------------------------------------
if __name__ == '__main__':
main()