annotate format_fasta_hairpins.py @ 4:b7ac138bb781 draft default tip

"planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit 51dc6c56c7d95fc229ffee958354211cd454fd36"
author artbio
date Sun, 09 May 2021 17:06:45 +0000
parents ee99c6374a3b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
1 import argparse
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
2 import gzip
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
3
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
4
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
5 def Parser():
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
6 the_parser = argparse.ArgumentParser()
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
7 the_parser.add_argument(
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
8 '--hairpins_path', action="store", type=str,
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
9 help="BASE url. ex: /pub/mirbase/22/")
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
10 the_parser.add_argument(
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
11 '--output', action="store", type=str,
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
12 help="parsed hairpin output in fasta format")
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
13 the_parser.add_argument(
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
14 '--basename', action="store", type=str,
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
15 help="genome basename of the parsed fasta")
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
16 args = the_parser.parse_args()
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
17 return args
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
18
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
19
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
20 def get_fasta_dic(gzipfile):
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
21 '''
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
22 gzipfile value example : 'mirbase/22/hairpin.fa.gz'
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
23 '''
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
24 item_dic = {}
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
25 with gzip.open(gzipfile, 'rb') as f:
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
26 current_item = ''
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
27 stringlist = []
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
28 for line in f:
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
29 line = line.decode('utf-8').strip('\n')
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
30 if (line[0] == ">"):
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
31 # dump the sequence of the previous item
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
32 if current_item and stringlist:
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
33 item_dic[current_item] = "".join(stringlist)
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
34 # take first word of item '''
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
35 current_item = line[1:].split()[0]
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
36 stringlist = []
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
37 else:
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
38 stringlist.append(line)
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
39 item_dic[current_item] = "".join(stringlist) # for the last item
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
40 return item_dic
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
41
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
42
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
43 def convert_and_print_hairpins(gzipfile, basename, fasta_output):
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
44 raw_fasta_dict = get_fasta_dic(gzipfile)
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
45 parsed_fasta_dict = {}
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
46 for head in raw_fasta_dict:
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
47 if basename in head:
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
48 parsed_fasta_dict[head] = raw_fasta_dict[head]
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
49 parsed_fasta_dict[head] = ''.join(
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
50 [i if i != 'u' else 't' for i in parsed_fasta_dict[head]])
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
51 parsed_fasta_dict[head] = ''.join(
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
52 [i if i != 'U' else 'T' for i in parsed_fasta_dict[head]])
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
53 with open(fasta_output, "w") as output:
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
54 for head in sorted(parsed_fasta_dict):
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
55 output.write('>%s\n%s\n' % (head, parsed_fasta_dict[head]))
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
56
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
57
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
58 def main(hairpins_path, basename, outfile):
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
59 convert_and_print_hairpins(hairpins_path, basename, outfile)
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
60
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
61
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
62 if __name__ == "__main__":
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
63 args = Parser()
ee99c6374a3b "planemo upload for repository https://github.com/ARTbio/tools-artbio/tree/master/tools/mircounts commit af48e9f6df2717ffd3731a974be1ec36e4eff779"
artbio
parents:
diff changeset
64 main(args.hairpins_path, args.basename, args.output)