Mercurial > repos > davidvanzessen > sff_extract_demultiplex
comparison trim.py @ 4:8e3d95d7f342 draft
Uploaded
author | davidvanzessen |
---|---|
date | Mon, 07 Jul 2014 05:49:20 -0400 |
parents | 79be0752711d |
children | 35b55f1c0c59 |
comparison
equal
deleted
inserted
replaced
3:79be0752711d | 4:8e3d95d7f342 |
---|---|
1 import argparse | 1 import argparse |
2 | 2 |
3 #docs.python.org/dev/library/argparse.html | 3 #docs.python.org/dev/library/argparse.html |
4 parser = argparse.ArgumentParser() | 4 parser = argparse.ArgumentParser() |
5 parser.add_argument("--input", help="Input folder with files") | 5 parser.add_argument("--input", help="Input fasta") |
6 parser.add_argument("--output", help="Output file") | 6 parser.add_argument("--output", help="Output fasta") |
7 parser.add_argument("--start", help="How many nucleotides to trim from the start", type=int) | 7 parser.add_argument("--start", help="How many nucleotides to trim from the start", type=int) |
8 parser.add_argument("--end", help="How many nucleotides to trim from the end", type=int) | 8 parser.add_argument("--end", help="How many nucleotides to trim from the end", type=int) |
9 | 9 |
10 args = parser.parse_args() | 10 args = parser.parse_args() |
11 start = int(args.start) | 11 start = int(args.start) |
12 end = int(args.end) | 12 end = int(args.end) |
13 | 13 |
14 if end <= 0: | 14 if end <= 0 and start <= 0: |
15 import shutil | 15 import shutil |
16 shutil.copy(args.input, args.output) | 16 shutil.copy(args.input, args.output) |
17 import sys | 17 import sys |
18 sys.exit() | 18 sys.exit() |
19 | |
20 | |
19 | 21 |
20 currentSeq = "" | 22 currentSeq = "" |
21 currentId = "" | 23 currentId = "" |
22 | 24 |
23 with open(args.input, 'r') as i: | 25 if end is 0: |
24 with open(args.output, 'w') as o: | 26 with open(args.input, 'r') as i: |
25 for line in i.readlines(): | 27 with open(args.output, 'w') as o: |
26 if line[0] is ">": | 28 for line in i.readlines(): |
27 if currentSeq is not "" or currentId is not "": | 29 if line[0] is ">": |
28 o.write(currentId) | 30 currentSeq = currentSeq[start:] |
29 o.write(currentSeq[start:-end] + "\n") | 31 if currentSeq is not "" and currentId is not "": |
30 currentId = line | 32 o.write(currentId) |
31 currentSeq = "" | 33 o.write(currentSeq + "\n") |
32 else: | 34 currentId = line |
33 currentSeq += line.rstrip() | 35 currentSeq = "" |
34 o.write(currentId) | 36 else: |
35 o.write(currentSeq.rstrip()[start:-end] + "\n") | 37 currentSeq += line.rstrip() |
38 o.write(currentId) | |
39 o.write(currentSeq[start:] + "\n") | |
40 else: | |
41 with open(args.input, 'r') as i: | |
42 with open(args.output, 'w') as o: | |
43 for line in i.readlines(): | |
44 if line[0] is ">": | |
45 currentSeq = currentSeq[start:-end] | |
46 if currentSeq is not "" and currentId is not "": | |
47 o.write(currentId) | |
48 o.write(currentSeq + "\n") | |
49 currentId = line | |
50 currentSeq = "" | |
51 else: | |
52 currentSeq += line.rstrip() | |
53 o.write(currentId) | |
54 o.write(currentSeq[start:-end] + "\n") |