-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrimCPseq.py
More file actions
executable file
·81 lines (54 loc) · 1.75 KB
/
Copy pathtrimCPseq.py
File metadata and controls
executable file
·81 lines (54 loc) · 1.75 KB
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
#!/usr/bin/env python
""" Trim an overly-long CPseq file
Inputs:
Sequence data (.CPseq files)
Outputs:
trimmed Sequence data
Ben Ober-Reynolds, boberrey@stanford.edu
20160805
"""
import sys
import os
import argparse
### MAIN ###
def main():
################ Parse input parameters ################
#set up command line argument parser
parser = argparse.ArgumentParser(description='Script for trimming CPseq files')
group = parser.add_argument_group('required arguments for processing data')
group.add_argument('-f', '--file', required=True,
help='file to be trimmed (CPseq)')
group.add_argument('-n', '--nth_line', required=True,
help='Every nth line will be trimmed')
group = parser.add_argument_group('optional arguments')
# Currently this option doesn't do anything. May be worth implementing in the future
#group.add_argument('-rf','--respect_filter', default=True,
# help='do not trim clusters that have a filter')
if not len(sys.argv) > 1:
parser.print_help()
sys.exit()
args = parser.parse_args()
seqFileName = args.file
n = int(args.nth_line)
if not os.path.isfile(seqFileName):
print "File "+seqFileName+" is not a valid file! Exiting..."
sys.exit()
with open(seqFileName, 'r') as f:
seqData = f.readlines()
print "Trimming every "+str(n)+"th line from file "+seqFileName+"..."
newFileName = "trimmed_"+str(n)+"_"+seqFileName
with open(newFileName, 'w') as f:
count = 1
for line in seqData:
fields = len(line.split())
if count == n:
if fields > 9:
# Don't throw away clusters with an assigned filter
f.write(line)
count = 1
else:
f.write(line)
count += 1
print "...done"
if __name__ == '__main__':
main()