-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_splice_junction_library_size.py
51 lines (39 loc) · 1.59 KB
/
get_splice_junction_library_size.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import argparse
def cmdline_args():
# Make parser object
p = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
p.add_argument("-f", "--folder", help="folder containing splicejunctions")
p.add_argument("-s", "--suffix", help="suffix of the splicejunctions",
default=".SJ.out.tab")
p.add_argument("-c", "--column", help="column to sum use 4 for beds,6 for STar",
default=4,type=int)
p.add_argument("-o", "--output", help="output file name",
default="splice_junction_sums.txt")
return(p.parse_args())
def main():
""" Main program """
args = cmdline_args()
#collect the names of the files in the given folder
files_to_count = args.folder
#take only the files which contain the suffix
onlysuffix = [f for f in os.listdir(files_to_count) if args.suffix in f]
#loop through all the files and get the sum of column 4(counting frm 0)
count_column = args.column
output_file = open(args.output, 'w')
for file in onlysuffix:
file_name = file.replace(args.suffix,'')
with open(os.path.join(files_to_count,file)) as infile:
try:
tot = sum(int(line.split("\t")[count_column]) for line in infile)
except ValueError:
print("%s does not have a column %d" % (file_name,count_column))
output_file.write("%s,%d\n" % (file_name,tot))
output_file.close()
return(0)
if __name__ == "__main__":
main()