annotate make_unique_id.py @ 0:a3a09dd8d09a draft

"planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
author brinkmanlab
date Fri, 24 Jan 2020 17:38:28 -0500
parents
children 061c3402a977
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
1 #!/usr/bin/env python
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
2 import sys
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
3 from Bio import SeqIO
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
4 from collections import defaultdict
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
5
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
6 usage = """
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
7 make_unique_id
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
8 Makes all record ids unique across all input data.
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
9 All input data must be the same format.
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
10
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
11 Use: make_unique_id.py [-v] <format> <input1> <output1> [<input2> <output2> ... <inputn> <outputn>]
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
12 \t-v Print version and exit
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
13
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
14 Valid formats: clustal, embl, fasta, fasta-2line, fastq-sanger, fastq, fastq-solexa, fastq-illumina, genbank, gb, imgt,
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
15 nexus, phd, phylip, pir, seqxml, sff, stockholm, tab, qual
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
16 """
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
17
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
18 if __name__ == '__main__':
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
19 if '-v' in sys.argv:
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
20 print('1.0')
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
21 exit(0)
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
22
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
23 if len(sys.argv) < 4:
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
24 print("Missing arguments", file=sys.stderr)
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
25 print(usage, file=sys.stderr)
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
26 exit(1)
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
27
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
28 format = sys.argv[1]
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
29 ids = defaultdict(int)
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
30
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
31 def makeUnique(seq):
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
32 count = ids[seq.id]
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
33 ids[seq.id] += 1
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
34 if count:
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
35 suffix = "_" + str(count)
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
36 newid = seq.id
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
37 seqlenlen = len(str(len(seq)))
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
38 if len(newid) + len(suffix) + 1 + seqlenlen > 28:
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
39 # Genbank has a max length for the id and sequence length number, truncate the sequence id if too long
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
40 newid = newid[:27 - seqlenlen - len(suffix)]
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
41
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
42 print(f"{seq.id}\t{newid}{suffix}")
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
43 seq.id = newid + suffix
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
44 seq.name += suffix
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
45
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
46 return seq
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
47
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
48
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
49 paths = iter(sys.argv[2:])
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
50
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
51 for input, output in zip(paths, paths):
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
52 SeqIO.write(
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
53 map(makeUnique, SeqIO.parse(input, format)),
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
54 output,
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
55 format
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
56 )
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
57
a3a09dd8d09a "planemo upload for repository https://github.com/brinkmanlab/galaxy-tools/tree/master/make_unique_id commit 33b02e08cbc8f76fb4b8537f8c968393f85a1b5e"
brinkmanlab
parents:
diff changeset
58