1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
#!/usr/bin/python
"""
Returns a bed-like translation of a CDS in which each record corresponds to
a single site in the CDS and includes additional fields for site degenaracy,
position ind CDS, and amino acid encoded.
usage: %prog nibdir genefile [options]
-o, --outfile=o: output file
-f, --format=f: format bed (default), or gtf|gff
-a, --allpositions: 1st, 2nd and 3rd positions are evaluated for degeneracy given the sequence at the other two positions. Many 1d sites in 1st codon positions become 2d sites when considered this way.
-n, --include_name: include the 'name' or 'id' field from the source file on every line of output
"""
import os
import re
import sys
from bx.cookbook import doc_optparse
from bx.gene_reader import CDSReader
from bx.seq import nib
GENETIC_CODE = """
TTT (Phe/F)Phenylalanine
TTC (Phe/F)Phenylalanine
TTA (Leu/L)Leucine
TTG (Leu/L)Leucine, Start
TCT (Ser/S)Serine
TCC (Ser/S)Serine
TCA (Ser/S)Serine
TCG (Ser/S)Serine
TAT (Tyr/Y)Tyrosine
TAC (Tyr/Y)Tyrosine
TAA Ochre (Stop)
TAG Amber (Stop)
TGT (Cys/C)Cysteine
TGC (Cys/C)Cysteine
TGA Opal (Stop)
TGG (Trp/W)Tryptophan
CTT (Leu/L)Leucine
CTC (Leu/L)Leucine
CTA (Leu/L)Leucine
CTG (Leu/L)Leucine, Start
CCT (Pro/P)Proline
CCC (Pro/P)Proline
CCA (Pro/P)Proline
CCG (Pro/P)Proline
CAT (His/H)Histidine
CAC (His/H)Histidine
CAA (Gln/Q)Glutamine
CAG (Gln/Q)Glutamine
CGT (Arg/R)Arginine
CGC (Arg/R)Arginine
CGA (Arg/R)Arginine
CGG (Arg/R)Arginine
ATT (Ile/I)Isoleucine, Start2
ATC (Ile/I)Isoleucine
ATA (Ile/I)Isoleucine
ATG (Met/M)Methionine, Start1
ACT (Thr/T)Threonine
ACC (Thr/T)Threonine
ACA (Thr/T)Threonine
ACG (Thr/T)Threonine
AAT (Asn/N)Asparagine
AAC (Asn/N)Asparagine
AAA (Lys/K)Lysine
AAG (Lys/K)Lysine
AGT (Ser/S)Serine
AGC (Ser/S)Serine
AGA (Arg/R)Arginine
AGG (Arg/R)Arginine
GTT (Val/V)Valine
GTC (Val/V)Valine
GTA (Val/V)Valine
GTG (Val/V)Valine, Start2
GCT (Ala/A)Alanine
GCC (Ala/A)Alanine
GCA (Ala/A)Alanine
GCG (Ala/A)Alanine
GAT (Asp/D)Aspartic acid
GAC (Asp/D)Aspartic acid
GAA (Glu/E)Glutamic acid
GAG (Glu/E)Glutamic acid
GGT (Gly/G)Glycine
GGC (Gly/G)Glycine
GGA (Gly/G)Glycine
GGG (Gly/G)Glycine
"""
def translate(codon, genetic_code):
c1, c2, c3 = codon
return genetic_code[c1][c2][c3]
""" parse the doc string to hash the genetic code"""
GEN_CODE = {}
for line in GENETIC_CODE.split("\n"):
if line.strip() == "":
continue
f = re.split(r"\s|\(|\)|\/", line)
codon = f[0]
c1, c2, c3 = codon
aminoacid = f[3]
if c1 not in GEN_CODE:
GEN_CODE[c1] = {}
if c2 not in GEN_CODE[c1]:
GEN_CODE[c1][c2] = {}
GEN_CODE[c1][c2][c3] = aminoacid
def getnib(nibdir):
seqs = {}
for nibf in os.listdir(nibdir):
if not nibf.endswith(".nib"):
continue
chr = nibf.replace(".nib", "")
file = os.path.join(nibdir, nibf)
seqs[chr] = nib.NibFile(open(file))
return seqs
REVMAP = str.maketrans("ACGTacgt", "TGCAtgca")
def revComp(seq):
return seq[::-1].translate(REVMAP)
def Comp(seq):
return seq.translate(REVMAP)
def main():
options, args = doc_optparse.parse(__doc__)
try:
if options.outfile:
out = open(options.outfile, "w")
else:
out = sys.stdout
if options.format:
format = options.format
else:
format = "bed"
allpositions = bool(options.allpositions)
include_name = bool(options.include_name)
nibdir = args[0]
bedfile = args[1]
except Exception:
doc_optparse.exit()
nibs = getnib(nibdir)
for chrom, strand, cds_exons, name in CDSReader(open(bedfile), format=format):
cds_seq = ""
# genome_seq_index maps the position in CDS to position on the genome
genome_seq_index = []
for c_start, c_end in cds_exons:
cds_seq += nibs[chrom].get(c_start, c_end - c_start)
for i in range(c_start, c_end):
genome_seq_index.append(i)
cds_seq = cds_seq.upper()
if strand == "+":
frsts = range(0, len(cds_seq), 3)
offsign = 1
else:
cds_seq = Comp(cds_seq)
frsts = range(2, len(cds_seq), 3)
offsign = -1
offone = 1 * offsign
offtwo = 2 * offsign
all = ["A", "C", "G", "T"]
for first_pos in frsts:
c1 = first_pos
c2 = first_pos + offone
c3 = first_pos + offtwo
try:
assert c3 < len(cds_seq)
except AssertionError:
print("out of sequence at %d for %s, %d" % (c3, chrom, genome_seq_index[first_pos]), file=sys.stderr)
continue
codon = cds_seq[c1], cds_seq[c2], cds_seq[c3]
aa = translate(codon, GEN_CODE)
degeneracy3 = str(list(GEN_CODE[codon[0]][codon[1]].values()).count(aa)) + "d"
if not include_name:
name_text = ""
else:
name_text = name.replace(" ", "_")
if allpositions:
try:
degeneracy1 = str([GEN_CODE[k][codon[1]][codon[2]] for k in all].count(aa)) + "d"
degeneracy2 = str([GEN_CODE[codon[0]][k][codon[2]] for k in all].count(aa)) + "d"
except TypeError as s:
print(list(GEN_CODE.values()), file=sys.stderr)
raise TypeError(s)
if strand == "+":
print(
chrom,
genome_seq_index[c1],
genome_seq_index[c1] + 1,
cds_seq[c1],
degeneracy1,
aa,
name_text,
file=out,
)
print(
chrom,
genome_seq_index[c2],
genome_seq_index[c2] + 1,
cds_seq[c2],
degeneracy2,
aa,
name_text,
file=out,
)
print(
chrom,
genome_seq_index[c3],
genome_seq_index[c3] + 1,
cds_seq[c3],
degeneracy3,
aa,
name_text,
file=out,
)
else:
print(
chrom,
genome_seq_index[c3],
genome_seq_index[c3] + 1,
cds_seq[c3],
degeneracy3,
aa,
name_text,
file=out,
)
print(
chrom,
genome_seq_index[c2],
genome_seq_index[c2] + 1,
cds_seq[c2],
degeneracy2,
aa,
name_text,
file=out,
)
print(
chrom,
genome_seq_index[c1],
genome_seq_index[c1] + 1,
cds_seq[c1],
degeneracy1,
aa,
name_text,
file=out,
)
else:
if strand == "+":
for b in c1, c2:
print(
chrom,
genome_seq_index[b],
genome_seq_index[b] + 1,
cds_seq[b],
"1d",
aa,
name_text,
file=out,
)
print(
chrom,
genome_seq_index[c3],
genome_seq_index[c3] + 1,
cds_seq[c3],
degeneracy3,
aa,
name_text,
file=out,
)
else:
print(
chrom,
genome_seq_index[c3],
genome_seq_index[c3] + 1,
cds_seq[c3],
degeneracy3,
aa,
name_text,
file=out,
)
for b in c2, c1:
print(
chrom,
genome_seq_index[b],
genome_seq_index[b] + 1,
cds_seq[b],
"1d",
aa,
name_text,
file=out,
)
out.close()
if __name__ == "__main__":
main()
|