annotate CodonSwitchTool/codon_switch.py @ 2:aad5e435e4dc draft default tip

Uploaded
author gianmarco_piccinno
date Tue, 21 May 2019 05:24:56 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
1 #!/usr/bin/env python
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
2
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
3 __author__= "Gianmarco Piccinno"
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
4 __version__ = "1.0.0"
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
5
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
6 from syngenic import *
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
7 from functions import *
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
8 from Bio import *
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
9 import argparse as ap
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
10
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
11 if __name__ == '__main__':
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
12
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
13 parser = ap.ArgumentParser(description="", formatter_class=ap.RawTextHelpFormatter)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
14
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
15 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
16 '-i', '--input_plasmid', help='Input plasmid', required=True)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
17 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
18 '-l', '--plasmid_format', help='Format of the plasmid: {fasta, genbank}', required=True)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
19 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
20 '-p', '--input_patterns', help='Input patterns separated by new_line', required=True)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
21 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
22 '-g', '--input_genome', help='Input annotated genome', required=True)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
23 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
24 '-q', '--genome_format', help='Format of the annotated genome: {fasta, gbk}', required=True)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
25 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
26 '-c', '--codon_table', help='Codon table to be used {Bacterial}', required=True)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
27 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
28 '-m', '--max_row', help='Max row length when print', required=False)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
29 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
30 '-d', '--demonstration', help='Use demonstration simplication', required=False)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
31 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
32 '-f', '--n_plasmids', help='Use demonstration simplication', required=False)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
33 parser.add_argument(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
34 '-o', '--output_folder', help='Folder for writing the output file', required=True)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
35 args = vars(parser.parse_args())
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
36
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
37 """
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
38
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
39 python codon_switch_v2.py
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
40 -i ./pEPSA5_annotated.gb
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
41 -l genbank
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
42 -p ./patterns.txt
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
43 -g S_aureus_JE2.gbf
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
44 -q gbk -c Bacterial
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
45 -o ./output
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
46
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
47 python codon_switch_v2.py -i ./pEPSA5_annotated.gb -l genbank -p ./patterns.txt -g S_aureus_JE2.gbf -q genbank -c Bacterial -o ./output
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
48
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
49 """
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
50
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
51
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
52 pl = SeqIO.read(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
53 open(args['input_plasmid'], "r"), args['plasmid_format'])
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
54
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
55 if args['demonstration'] == "demonstration":
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
56 pl = pl[0:3000]
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
57 pats = read_patterns(args['input_patterns'])
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
58
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
59
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
60 #############################################################
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
61 #
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
62 #############################################################
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
63
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
64 #pl = fake_from_real(path = "./data/pEPSA5_annotated.gb", id_ = "Trial", name = "Fake_plasmid")
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
65 print(type(pl))
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
66 print(pl); print(pl.seq); print(pl.features)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
67
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
68 #for feat in pl.features:
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
69 # print(str(feat.extract(pl)))
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
70 # print(str(pl[feat.location.start:feat.location.end]))
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
71 # print("\n")
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
72
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
73
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
74 n_pl = plasmid(pl)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
75 print(n_pl); print(len(n_pl))
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
76 print(n_pl.features)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
77
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
78
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
79 patts, n_patts = all_patterns(input_ = pats)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
80
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
81
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
82 f_patts = n_pl.findpatterns(n_patts, patts)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
83 print(f_patts)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
84 print(pl.seq)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
85 print(len(pl.seq))
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
86
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
87
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
88 n_poss = punctuate_targets(f_patts, n_pl)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
89 print(n_poss)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
90
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
91 print_seq(n_pl.seq)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
92
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
93 synonims_tables = synonims_(table_name=args['codon_table'])
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
94
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
95 synonims_tables
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
96
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
97 plasmids = generalization(n_poss, n_pl, synonims_tables)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
98
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
99 print(len(plasmids))
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
100
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
101 #plasmids
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
102
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
103 #if len(plasmids) > 5000000:
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
104 #redo generalization without considering internal bases
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
105 #in target sites that are not in CDS
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
106 #this means considering only the outer bases of the target
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
107 # plasmids = generalization(n_poss, n_pl, synonims_tables,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
108 # reduced = True)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
109
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
110 #########################################################
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
111 # Read plasmid and compute codon usage
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
112 #########################################################
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
113
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
114 genome = annotated_genome(read_annotated_genome(
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
115 data=args['input_genome'], type_=args['genome_format']))
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
116
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
117 out_genome = genome.codon_usage(args['codon_table'])
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
118 print(out_genome.keys())
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
119 print(out_genome["Table"])
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
120
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
121 print(out_genome["Table"].loc["GCA"]["Proportion"])
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
122 print(type(out_genome["Table"].loc["GCA"]["Proportion"]))
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
123
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
124
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
125 #########################################################
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
126 # Evaluate the plasmid
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
127 #########################################################
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
128
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
129 useful_plasmids = evaluate_plasmids(plasmids = plasmids,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
130 original_plasmid = n_pl,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
131 codon_usage_table = out_genome["Table"],
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
132 n_patts = n_patts,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
133 f_patts = patts)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
134
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
135 dat_plasmids = rank_plasmids(original_useful_plasmids = useful_plasmids)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
136
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
137 def_pls = dat_plasmids.index[:int(args['n_plasmids'])]
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
138
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
139 for to_save in def_pls:
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
140 #print(to_save)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
141 #print(useful_plasmids[to_save])
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
142 with open(to_save+".fa", "w") as handle:
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
143 handle.write(">"+to_save+"\n")
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
144 handle.write(useful_plasmids[to_save]["sequence"])
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
145
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
146
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
147
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
148 if args['max_row'] != None:
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
149 tmp_max_row = int(args['max_row'])
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
150 else:
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
151 tmp_max_row = 27
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
152
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
153 print_color_seq(original = n_pl,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
154 others = def_pls,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
155 annotation_information = useful_plasmids,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
156 tot = useful_plasmids,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
157 ind_range = None,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
158 patterns = n_poss,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
159 f_patterns = f_patts,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
160 patts = patts,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
161 max_row = tmp_max_row)
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
162
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
163
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
164 print_to_pdf(original = n_pl,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
165 others = def_pls,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
166 annotation_information = useful_plasmids,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
167 tot = useful_plasmids,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
168 ind_range = None,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
169 patterns = n_poss,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
170 f_patterns = f_patts,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
171 patts = patts,
aad5e435e4dc Uploaded
gianmarco_piccinno
parents:
diff changeset
172 max_row = tmp_max_row)