annotate trim.py @ 1:cbce7f35f8b0 draft

Uploaded
author davidvanzessen
date Wed, 21 Dec 2016 10:07:42 -0500
parents cb08a27e5fc2
children 423d320bc1ba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
1 import argparse
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
2
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
3 #docs.python.org/dev/library/argparse.html
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
4 parser = argparse.ArgumentParser()
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
5 parser.add_argument("--input", help="Input fasta")
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
6 parser.add_argument("--output", help="Output fasta")
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
7 parser.add_argument("--start", help="How many nucleotides to trim from the start", type=int)
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
8 parser.add_argument("--end", help="How many nucleotides to trim from the end", type=int)
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
9
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
10 args = parser.parse_args()
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
11 start = int(args.start)
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
12 end = int(args.end)
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
13
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
14 print args.input
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
15 print args.output
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
16 print start
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
17 print end
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
18
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
19 if end <= 0 and start <= 0:
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
20 import shutil
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
21 shutil.copy(args.input, args.output)
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
22 import sys
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
23 sys.exit()
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
24
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
25
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
26
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
27 currentSeq = ""
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
28 currentId = ""
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
29
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
30 if end is 0:
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
31 with open(args.input, 'r') as i:
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
32 with open(args.output, 'w') as o:
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
33 for line in i.readlines():
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
34 if line[0] is ">":
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
35 currentSeq = currentSeq[start:]
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
36 if currentSeq is not "" and currentId is not "":
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
37 o.write(currentId)
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
38 o.write(currentSeq + "\n")
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
39 currentId = line
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
40 currentSeq = ""
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
41 else:
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
42 currentSeq += line.rstrip()
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
43 o.write(currentId)
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
44 o.write(currentSeq[start:] + "\n")
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
45 else:
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
46 with open(args.input, 'r') as i:
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
47 with open(args.output, 'w') as o:
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
48 for line in i.readlines():
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
49 if line[0] is ">":
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
50 currentSeq = currentSeq[start:-end]
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
51 if currentSeq is not "" and currentId is not "":
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
52 o.write(currentId)
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
53 o.write(currentSeq + "\n")
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
54 currentId = line
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
55 currentSeq = ""
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
56 else:
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
57 currentSeq += line.rstrip()
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
58 o.write(currentId)
cb08a27e5fc2 Uploaded
davidvanzessen
parents:
diff changeset
59 o.write(currentSeq[start:-end] + "\n")