annotate codon_switch.py @ 0:5397da1ef896 draft

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