comparison renameSequences2.py @ 3:e320ef2d105a draft

Uploaded
author petr-novak
date Thu, 05 Sep 2019 09:04:56 -0400
parents
children
comparison
equal deleted inserted replaced
2:ff658cf87f16 3:e320ef2d105a
1 #!/usr/bin/env python
2 ### this version does not use st input!!!!
3 # how to use:
4 # renameSequences.py fasta.file true index.out prefix_length # for paired sequences
5 # renameSequences.py fasta.file false index.out prefix_length # not paired sequences
6
7 import sys
8 paired = sys.argv[2] == "true"
9 index = open(sys.argv[3], 'w')
10
11 if len(sys.argv) == 4:
12 prefix = 0
13 else:
14 prefix = int(sys.argv[4])
15
16 if paired:
17 P = 2
18 suffix = "f\n"
19 else:
20 P = 1
21 suffix = "\n"
22 i = j = 0
23 reader = open(sys.argv[1], mode='r')
24 for oneline in reader:
25 if oneline == "":
26 continue
27 if oneline[0] == ">":
28 i += 1
29 j += 1
30 prefix_string = oneline[1:(1 + prefix)].strip()
31 if j == 1:
32 header = ">" + prefix_string + str(i) + suffix
33 index.write(oneline[1:].strip() + "\t" + prefix_string + str(i) +
34 suffix)
35 if j == 2:
36 i -= 1
37 header = ">" + prefix_string + str(i) + "r\n"
38 index.write(oneline[1:].strip() + "\t" + prefix_string + str(i) +
39 "r\n")
40 sys.stdout.write(header)
41 if j == P:
42 j = 0
43 else:
44 sys.stdout.write(oneline)
45 index.close()