47
+ − 1 import argparse
+ − 2 import logging
+ − 3 import sys
+ − 4 import os
+ − 5 import re
+ − 6
0
+ − 7 from collections import defaultdict
47
+ − 8
+ − 9 def main():
+ − 10 parser = argparse.ArgumentParser()
+ − 11 parser.add_argument("--input", help="The '7_V-REGION-mutation-and-AA-change-table' and '10_V-REGION-mutation-hotspots' merged together, with an added 'best_match' annotation")
+ − 12 parser.add_argument("--genes", help="The genes available in the 'best_match' column")
+ − 13 parser.add_argument("--empty_region_filter", help="Where does the sequence start?", choices=['leader', 'FR1', 'CDR1', 'FR2'])
+ − 14 parser.add_argument("--output", help="Output file")
0
+ − 15
47
+ − 16 args = parser.parse_args()
+ − 17
+ − 18 infile = args.input
+ − 19 genes = str(args.genes).split(",")
+ − 20 empty_region_filter = args.empty_region_filter
+ − 21 outfile = args.output
0
+ − 22
47
+ − 23 genedic = dict()
0
+ − 24
47
+ − 25 mutationdic = dict()
62
+ − 26 mutationMatcher = re.compile("^(.)(\d+).(.),?[ ]?(.)?(\d+)?.?(.)?(.?.?.?.?.?)?")
+ − 27 mutationMatcher = re.compile("^([actg])(\d+).([actg]),?[ ]?([A-Z])?(\d+)?.?([A-Z])?(.*)?")
+ − 28 mutationMatcher = re.compile("^([actg])(\d+).([actg]),?[ ]?([A-Z])?(\d+)?[>]?([A-Z;])?(.*)?")
+ − 29 mutationMatcher = re.compile("^([nactg])(\d+).([nactg]),?[ ]?([A-Z])?(\d+)?[>]?([A-Z;])?(.*)?")
47
+ − 30 NAMatchResult = (None, None, None, None, None, None, '')
+ − 31 geneMatchers = {gene: re.compile("^" + gene + ".*") for gene in genes}
+ − 32 linecount = 0
0
+ − 33
47
+ − 34 IDIndex = 0
+ − 35 best_matchIndex = 0
+ − 36 fr1Index = 0
+ − 37 cdr1Index = 0
+ − 38 fr2Index = 0
+ − 39 cdr2Index = 0
+ − 40 fr3Index = 0
+ − 41 first = True
+ − 42 IDlist = []
+ − 43 mutationList = []
+ − 44 mutationListByID = {}
+ − 45 cdr1LengthDic = {}
+ − 46 cdr2LengthDic = {}
+ − 47
+ − 48 fr1LengthDict = {}
+ − 49 fr2LengthDict = {}
+ − 50 fr3LengthDict = {}
+ − 51
+ − 52 cdr1LengthIndex = 0
+ − 53 cdr2LengthIndex = 0
0
+ − 54
47
+ − 55 fr1SeqIndex = 0
+ − 56 fr2SeqIndex = 0
+ − 57 fr3SeqIndex = 0
+ − 58
+ − 59 tandem_sum_by_class = defaultdict(int)
+ − 60 expected_tandem_sum_by_class = defaultdict(float)
0
+ − 61
47
+ − 62 with open(infile, 'ru') as i:
+ − 63 for line in i:
+ − 64 if first:
+ − 65 linesplt = line.split("\t")
+ − 66 IDIndex = linesplt.index("Sequence.ID")
+ − 67 best_matchIndex = linesplt.index("best_match")
+ − 68 fr1Index = linesplt.index("FR1.IMGT")
+ − 69 cdr1Index = linesplt.index("CDR1.IMGT")
+ − 70 fr2Index = linesplt.index("FR2.IMGT")
+ − 71 cdr2Index = linesplt.index("CDR2.IMGT")
+ − 72 fr3Index = linesplt.index("FR3.IMGT")
+ − 73 cdr1LengthIndex = linesplt.index("CDR1.IMGT.seq")
+ − 74 cdr2LengthIndex = linesplt.index("CDR2.IMGT.seq")
+ − 75 fr1SeqIndex = linesplt.index("FR1.IMGT.seq")
+ − 76 fr2SeqIndex = linesplt.index("FR2.IMGT.seq")
+ − 77 fr3SeqIndex = linesplt.index("FR3.IMGT.seq")
+ − 78 first = False
+ − 79 continue
+ − 80 linecount += 1
0
+ − 81 linesplt = line.split("\t")
47
+ − 82 ID = linesplt[IDIndex]
+ − 83 genedic[ID] = linesplt[best_matchIndex]
62
+ − 84
+ − 85 mutationdic[ID + "_FR1"] = []
+ − 86 if len(linesplt[fr1Index]) > 5 and empty_region_filter == "leader":
+ − 87 mutationdic[ID + "_FR1"] = [mutationMatcher.match(x).groups() for x in linesplt[fr1Index].split("|") if x]
+ − 88
+ − 89 mutationdic[ID + "_CDR1"] = []
+ − 90 if len(linesplt[cdr1Index]) > 5 and empty_region_filter in ["leader", "FR1"]:
+ − 91 mutationdic[ID + "_CDR1"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr1Index].split("|") if x]
+ − 92
+ − 93 mutationdic[ID + "_FR2"] = []
+ − 94 if len(linesplt[fr2Index]) > 5 and empty_region_filter in ["leader", "FR1", "CDR1"]:
+ − 95 mutationdic[ID + "_FR2"] = [mutationMatcher.match(x).groups() for x in linesplt[fr2Index].split("|") if x]
+ − 96
+ − 97 mutationdic[ID + "_CDR2"] = []
+ − 98 if len(linesplt[cdr2Index]) > 5:
+ − 99 mutationdic[ID + "_CDR2"] = [mutationMatcher.match(x).groups() for x in linesplt[cdr2Index].split("|") if x]
+ − 100
+ − 101 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
+ − 102
+ − 103 mutationdic[ID + "_FR3"] = []
+ − 104 if len(linesplt[fr3Index]) > 5:
+ − 105 mutationdic[ID + "_FR3"] = [mutationMatcher.match(x).groups() for x in linesplt[fr3Index].split("|") if x]
+ − 106
47
+ − 107 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
+ − 108 mutationListByID[ID] = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
+ − 109
+ − 110 cdr1Length = len(linesplt[cdr1LengthIndex])
+ − 111 cdr2Length = len(linesplt[cdr2LengthIndex])
+ − 112
+ − 113 #print linesplt[fr2SeqIndex]
+ − 114 fr1Length = len(linesplt[fr1SeqIndex]) if empty_region_filter == "leader" else 0
+ − 115 fr2Length = len(linesplt[fr2SeqIndex]) if empty_region_filter in ["leader", "FR1", "CDR1"] else 0
+ − 116 fr3Length = len(linesplt[fr3SeqIndex])
+ − 117
+ − 118 cdr1LengthDic[ID] = cdr1Length
+ − 119 cdr2LengthDic[ID] = cdr2Length
+ − 120
+ − 121 fr1LengthDict[ID] = fr1Length
+ − 122 fr2LengthDict[ID] = fr2Length
+ − 123 fr3LengthDict[ID] = fr3Length
+ − 124
+ − 125 IDlist += [ID]
62
+ − 126 print "len(mutationdic) =", len(mutationdic)
47
+ − 127
62
+ − 128 with open(os.path.join(os.path.dirname(os.path.abspath(infile)), "mutationdict.txt"), 'w') as out_handle:
+ − 129 for ID, lst in mutationdic.iteritems():
+ − 130 for mut in lst:
+ − 131 out_handle.write("{0}\t{1}\n".format(ID, "\t".join([str(x) for x in mut])))
47
+ − 132
+ − 133 #tandem mutation stuff
+ − 134 tandem_frequency = defaultdict(int)
+ − 135 mutation_frequency = defaultdict(int)
48
+ − 136
+ − 137 mutations_by_id_dic = {}
+ − 138 first = True
+ − 139 mutation_by_id_file = os.path.join(os.path.dirname(outfile), "mutation_by_id.txt")
+ − 140 with open(mutation_by_id_file, 'r') as mutation_by_id:
+ − 141 for l in mutation_by_id:
+ − 142 if first:
+ − 143 first = False
+ − 144 continue
+ − 145 splt = l.split("\t")
+ − 146 mutations_by_id_dic[splt[0]] = int(splt[1])
+ − 147
47
+ − 148 tandem_file = os.path.join(os.path.dirname(outfile), "tandems_by_id.txt")
+ − 149 with open(tandem_file, 'w') as o:
+ − 150 highest_tandem_length = 0
+ − 151
+ − 152 o.write("Sequence.ID\tnumber_of_mutations\tnumber_of_tandems\tregion_length\texpected_tandems\tlongest_tandem\ttandems\n")
+ − 153 for ID in IDlist:
+ − 154 mutations = mutationListByID[ID]
+ − 155 if len(mutations) == 0:
+ − 156 continue
+ − 157 last_mut = max(mutations, key=lambda x: int(x[1]))
+ − 158
+ − 159 last_mut_pos = int(last_mut[1])
+ − 160
+ − 161 mut_positions = [False] * (last_mut_pos + 1)
+ − 162
+ − 163 for mutation in mutations:
+ − 164 frm, where, to, frmAA, whereAA, toAA, thing = mutation
+ − 165 where = int(where)
+ − 166 mut_positions[where] = True
+ − 167
+ − 168 tandem_muts = []
+ − 169 tandem_start = -1
+ − 170 tandem_length = 0
+ − 171 for i in range(len(mut_positions)):
+ − 172 if mut_positions[i]:
+ − 173 if tandem_start == -1:
+ − 174 tandem_start = i
+ − 175 tandem_length += 1
+ − 176 #print "".join(["1" if x else "0" for x in mut_positions[:i+1]])
+ − 177 else:
+ − 178 if tandem_length > 1:
+ − 179 tandem_muts.append((tandem_start, tandem_length))
+ − 180 #print "{0}{1} {2}:{3}".format(" " * (i - tandem_length), "^" * tandem_length, tandem_start, tandem_length)
+ − 181 tandem_start = -1
+ − 182 tandem_length = 0
+ − 183 if tandem_length > 1: # if the sequence ends with a tandem mutation
+ − 184 tandem_muts.append((tandem_start, tandem_length))
+ − 185
+ − 186 if len(tandem_muts) > 0:
+ − 187 if highest_tandem_length < len(tandem_muts):
+ − 188 highest_tandem_length = len(tandem_muts)
0
+ − 189
47
+ − 190 region_length = fr1LengthDict[ID] + cdr1LengthDic[ID] + fr2LengthDict[ID] + cdr2LengthDic[ID] + fr3LengthDict[ID]
+ − 191 longest_tandem = max(tandem_muts, key=lambda x: x[1]) if len(tandem_muts) else (0, 0)
48
+ − 192 num_mutations = mutations_by_id_dic[ID] # len(mutations)
47
+ − 193 f_num_mutations = float(num_mutations)
+ − 194 num_tandem_muts = len(tandem_muts)
+ − 195 expected_tandem_muts = f_num_mutations * (f_num_mutations - 1.0) / float(region_length)
+ − 196 o.write("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\n".format(ID,
+ − 197 str(num_mutations),
+ − 198 str(num_tandem_muts),
+ − 199 str(region_length),
+ − 200 str(round(expected_tandem_muts, 2)),
+ − 201 str(longest_tandem[1]),
+ − 202 str(tandem_muts)))
+ − 203 gene = genedic[ID]
+ − 204 if gene.find("unmatched") == -1:
+ − 205 tandem_sum_by_class[gene] += num_tandem_muts
+ − 206 expected_tandem_sum_by_class[gene] += expected_tandem_muts
0
+ − 207
47
+ − 208 tandem_sum_by_class["all"] += num_tandem_muts
+ − 209 expected_tandem_sum_by_class["all"] += expected_tandem_muts
0
+ − 210
47
+ − 211 gene = gene[:3]
+ − 212 if gene in ["IGA", "IGG"]:
+ − 213 tandem_sum_by_class[gene] += num_tandem_muts
+ − 214 expected_tandem_sum_by_class[gene] += expected_tandem_muts
+ − 215 else:
+ − 216 tandem_sum_by_class["unmatched"] += num_tandem_muts
+ − 217 expected_tandem_sum_by_class["unmatched"] += expected_tandem_muts
+ − 218
40
+ − 219
47
+ − 220 for tandem_mut in tandem_muts:
+ − 221 tandem_frequency[str(tandem_mut[1])] += 1
+ − 222 #print "\t".join([ID, str(len(tandem_muts)), str(longest_tandem[1]) , str(tandem_muts)])
+ − 223
+ − 224 tandem_freq_file = os.path.join(os.path.dirname(outfile), "tandem_frequency.txt")
+ − 225 with open(tandem_freq_file, 'w') as o:
+ − 226 for frq in sorted([int(x) for x in tandem_frequency.keys()]):
+ − 227 o.write("{0}\t{1}\n".format(frq, tandem_frequency[str(frq)]))
0
+ − 228
47
+ − 229 tandem_row = []
+ − 230 genes_extra = list(genes)
+ − 231 genes_extra.append("all")
+ − 232 for x, y, in zip([tandem_sum_by_class[x] for x in genes_extra], [expected_tandem_sum_by_class[x] for x in genes_extra]):
+ − 233 if y != 0:
+ − 234 tandem_row += [x, round(y, 2), round(x / y, 2)]
+ − 235 else:
+ − 236 tandem_row += [x, round(y, 2), 0]
0
+ − 237
47
+ − 238 tandem_freq_file = os.path.join(os.path.dirname(outfile), "shm_overview_tandem_row.txt")
+ − 239 with open(tandem_freq_file, 'w') as o:
+ − 240 o.write("Tandems/Expected (ratio),{0}\n".format(",".join([str(x) for x in tandem_row])))
+ − 241
+ − 242 #print mutationList, linecount
+ − 243
62
+ − 244 AALength = (int(max(mutationList, key=lambda i: int(i[4]) if i[4] and i[5] != ";" else 0)[4]) + 1) # [4] is the position of the AA mutation, None if silent
47
+ − 245 if AALength < 60:
+ − 246 AALength = 64
+ − 247
+ − 248 AA_mutation = [0] * AALength
+ − 249 AA_mutation_dic = {"IGA": AA_mutation[:], "IGG": AA_mutation[:], "IGM": AA_mutation[:], "IGE": AA_mutation[:], "unm": AA_mutation[:], "all": AA_mutation[:]}
+ − 250 AA_mutation_empty = AA_mutation[:]
+ − 251
66
+ − 252 print "AALength:", AALength
47
+ − 253 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt"
+ − 254 with open(aa_mutations_by_id_file, 'w') as o:
+ − 255 o.write("ID\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n")
+ − 256 for ID in mutationListByID.keys():
+ − 257 AA_mutation_for_ID = AA_mutation_empty[:]
+ − 258 for mutation in mutationListByID[ID]:
66
+ − 259 if mutation[4] and mutation[5] != ";":
47
+ − 260 AA_mutation_position = int(mutation[4])
66
+ − 261 try:
+ − 262 AA_mutation[AA_mutation_position] += 1
+ − 263 AA_mutation_for_ID[AA_mutation_position] += 1
+ − 264 except Exception as e:
+ − 265 print e
+ − 266 print mutation
+ − 267 sys.exit()
47
+ − 268 clss = genedic[ID][:3]
+ − 269 AA_mutation_dic[clss][AA_mutation_position] += 1
+ − 270 o.write(ID + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n")
0
+ − 271
+ − 272
+ − 273
47
+ − 274 #absent AA stuff
+ − 275 absentAACDR1Dic = defaultdict(list)
+ − 276 absentAACDR1Dic[5] = range(29,36)
+ − 277 absentAACDR1Dic[6] = range(29,35)
+ − 278 absentAACDR1Dic[7] = range(30,35)
+ − 279 absentAACDR1Dic[8] = range(30,34)
+ − 280 absentAACDR1Dic[9] = range(31,34)
+ − 281 absentAACDR1Dic[10] = range(31,33)
+ − 282 absentAACDR1Dic[11] = [32]
0
+ − 283
47
+ − 284 absentAACDR2Dic = defaultdict(list)
+ − 285 absentAACDR2Dic[0] = range(55,65)
+ − 286 absentAACDR2Dic[1] = range(56,65)
+ − 287 absentAACDR2Dic[2] = range(56,64)
+ − 288 absentAACDR2Dic[3] = range(57,64)
+ − 289 absentAACDR2Dic[4] = range(57,63)
+ − 290 absentAACDR2Dic[5] = range(58,63)
+ − 291 absentAACDR2Dic[6] = range(58,62)
+ − 292 absentAACDR2Dic[7] = range(59,62)
+ − 293 absentAACDR2Dic[8] = range(59,61)
+ − 294 absentAACDR2Dic[9] = [60]
0
+ − 295
47
+ − 296 absentAA = [len(IDlist)] * (AALength-1)
+ − 297 for k, cdr1Length in cdr1LengthDic.iteritems():
+ − 298 for c in absentAACDR1Dic[cdr1Length]:
+ − 299 absentAA[c] -= 1
0
+ − 300
47
+ − 301 for k, cdr2Length in cdr2LengthDic.iteritems():
+ − 302 for c in absentAACDR2Dic[cdr2Length]:
+ − 303 absentAA[c] -= 1
0
+ − 304
+ − 305
47
+ − 306 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/absent_aa_id.txt"
+ − 307 with open(aa_mutations_by_id_file, 'w') as o:
+ − 308 o.write("ID\tcdr1length\tcdr2length\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n")
+ − 309 for ID in IDlist:
+ − 310 absentAAbyID = [1] * (AALength-1)
+ − 311 cdr1Length = cdr1LengthDic[ID]
+ − 312 for c in absentAACDR1Dic[cdr1Length]:
+ − 313 absentAAbyID[c] -= 1
0
+ − 314
47
+ − 315 cdr2Length = cdr2LengthDic[ID]
+ − 316 for c in absentAACDR2Dic[cdr2Length]:
+ − 317 absentAAbyID[c] -= 1
+ − 318 o.write(ID + "\t" + str(cdr1Length) + "\t" + str(cdr2Length) + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in absentAAbyID]) + "\n")
0
+ − 319
47
+ − 320 if linecount == 0:
+ − 321 print "No data, exiting"
+ − 322 with open(outfile, 'w') as o:
+ − 323 o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
+ − 324 o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
+ − 325 o.write("WA (%)," + ("0,0,0\n" * len(genes)))
+ − 326 o.write("TW (%)," + ("0,0,0\n" * len(genes)))
+ − 327 import sys
0
+ − 328
47
+ − 329 sys.exit()
0
+ − 330
47
+ − 331 hotspotMatcher = re.compile("[actg]+,(\d+)-(\d+)\((.*)\)")
+ − 332 RGYWCount = {}
+ − 333 WRCYCount = {}
+ − 334 WACount = {}
+ − 335 TWCount = {}
0
+ − 336
47
+ − 337 #IDIndex = 0
+ − 338 ataIndex = 0
+ − 339 tatIndex = 0
+ − 340 aggctatIndex = 0
+ − 341 atagcctIndex = 0
+ − 342 first = True
+ − 343 with open(infile, 'ru') as i:
+ − 344 for line in i:
+ − 345 if first:
+ − 346 linesplt = line.split("\t")
+ − 347 ataIndex = linesplt.index("X.a.t.a")
+ − 348 tatIndex = linesplt.index("t.a.t.")
+ − 349 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
+ − 350 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
+ − 351 first = False
+ − 352 continue
0
+ − 353 linesplt = line.split("\t")
47
+ − 354 gene = linesplt[best_matchIndex]
+ − 355 ID = linesplt[IDIndex]
+ − 356 RGYW = [(int(x), int(y), z) for (x, y, z) in
+ − 357 [hotspotMatcher.match(x).groups() for x in linesplt[aggctatIndex].split("|") if x]]
+ − 358 WRCY = [(int(x), int(y), z) for (x, y, z) in
+ − 359 [hotspotMatcher.match(x).groups() for x in linesplt[atagcctIndex].split("|") if x]]
+ − 360 WA = [(int(x), int(y), z) for (x, y, z) in
+ − 361 [hotspotMatcher.match(x).groups() for x in linesplt[ataIndex].split("|") if x]]
+ − 362 TW = [(int(x), int(y), z) for (x, y, z) in
+ − 363 [hotspotMatcher.match(x).groups() for x in linesplt[tatIndex].split("|") if x]]
+ − 364 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0
0
+ − 365
62
+ − 366 with open(os.path.join(os.path.dirname(os.path.abspath(infile)), "RGYW.txt"), 'a') as out_handle:
+ − 367 for hotspot in RGYW:
+ − 368 out_handle.write("{0}\t{1}\n".format(ID, "\t".join([str(x) for x in hotspot])))
+ − 369
47
+ − 370 mutationList = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
+ − 371 for mutation in mutationList:
+ − 372 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation
62
+ − 373 mutation_in_RGYW = any(((start <= int(where) <= end) for (start, end, region) in RGYW))
+ − 374 mutation_in_WRCY = any(((start <= int(where) <= end) for (start, end, region) in WRCY))
+ − 375 mutation_in_WA = any(((start <= int(where) <= end) for (start, end, region) in WA))
+ − 376 mutation_in_TW = any(((start <= int(where) <= end) for (start, end, region) in TW))
0
+ − 377
47
+ − 378 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW])
0
+ − 379
47
+ − 380 if in_how_many_motifs > 0:
+ − 381 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs
+ − 382 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs
+ − 383 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs
+ − 384 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs
63
+ − 385
+ − 386 mutations_in_motifs_file = os.path.join(os.path.dirname(os.path.abspath(infile)), "mutation_in_motifs.txt")
+ − 387 if not os.path.exists(mutation_by_id_file):
+ − 388 with open(mutations_in_motifs_file, 'w') as out_handle:
+ − 389 out_handle.write("{0}\n".format("\t".join([
+ − 390 "Sequence.ID",
+ − 391 "mutation_position",
+ − 392 "region",
+ − 393 "from_nt",
+ − 394 "to_nt",
+ − 395 "mutation_position_AA",
+ − 396 "from_AA",
+ − 397 "to_AA",
+ − 398 "motif",
+ − 399 "motif_start_nt",
+ − 400 "motif_end_nt",
+ − 401 "rest"
+ − 402 ])))
+ − 403
+ − 404 with open(mutations_in_motifs_file, 'a') as out_handle:
+ − 405 motif_dic = {"RGYW": RGYW, "WRCY": WRCY, "WA": WA, "TW": TW}
+ − 406 for mutation in mutationList:
+ − 407 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation
+ − 408 for motif in motif_dic.keys():
+ − 409
+ − 410 for start, end, region in motif_dic[motif]:
+ − 411 if start <= int(where) <= end:
+ − 412 out_handle.write("{0}\n".format(
+ − 413 "\t".join([
+ − 414 ID,
+ − 415 where,
+ − 416 region,
+ − 417 frm,
+ − 418 to,
+ − 419 str(AAwhere),
+ − 420 str(AAfrm),
+ − 421 str(AAto),
+ − 422 motif,
+ − 423 str(start),
+ − 424 str(end),
+ − 425 str(junk)
+ − 426 ])
+ − 427 ))
+ − 428
0
+ − 429
+ − 430
47
+ − 431 def mean(lst):
+ − 432 return (float(sum(lst)) / len(lst)) if len(lst) > 0 else 0.0
0
+ − 433
+ − 434
47
+ − 435 def median(lst):
+ − 436 lst = sorted(lst)
+ − 437 l = len(lst)
+ − 438 if l == 0:
+ − 439 return 0
+ − 440 if l == 1:
+ − 441 return lst[0]
+ − 442
+ − 443 l = int(l / 2)
0
+ − 444
47
+ − 445 if len(lst) % 2 == 0:
+ − 446 return float(lst[l] + lst[(l - 1)]) / 2.0
+ − 447 else:
+ − 448 return lst[l]
0
+ − 449
47
+ − 450 funcs = {"mean": mean, "median": median, "sum": sum}
0
+ − 451
47
+ − 452 directory = outfile[:outfile.rfind("/") + 1]
+ − 453 value = 0
+ − 454 valuedic = dict()
0
+ − 455
47
+ − 456 for fname in funcs.keys():
+ − 457 for gene in genes:
+ − 458 with open(directory + gene + "_" + fname + "_value.txt", 'r') as v:
+ − 459 valuedic[gene + "_" + fname] = float(v.readlines()[0].rstrip())
+ − 460 with open(directory + "all_" + fname + "_value.txt", 'r') as v:
+ − 461 valuedic["total_" + fname] = float(v.readlines()[0].rstrip())
+ − 462
0
+ − 463
47
+ − 464 def get_xyz(lst, gene, f, fname):
+ − 465 x = round(round(f(lst), 1))
+ − 466 y = valuedic[gene + "_" + fname]
+ − 467 z = str(round(x / float(y) * 100, 1)) if y != 0 else "0"
+ − 468 return (str(x), str(y), z)
0
+ − 469
47
+ − 470 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
+ − 471 arr = ["RGYW", "WRCY", "WA", "TW"]
0
+ − 472
47
+ − 473 for fname in funcs.keys():
+ − 474 func = funcs[fname]
+ − 475 foutfile = outfile[:outfile.rindex("/")] + "/hotspot_analysis_" + fname + ".txt"
+ − 476 with open(foutfile, 'w') as o:
+ − 477 for typ in arr:
+ − 478 o.write(typ + " (%)")
+ − 479 curr = dic[typ]
+ − 480 for gene in genes:
+ − 481 geneMatcher = geneMatchers[gene]
+ − 482 if valuedic[gene + "_" + fname] is 0:
+ − 483 o.write(",0,0,0")
+ − 484 else:
+ − 485 x, y, z = get_xyz([curr[x] for x in [y for y, z in genedic.iteritems() if geneMatcher.match(z)]], gene, func, fname)
+ − 486 o.write("," + x + "," + y + "," + z)
+ − 487 x, y, z = get_xyz([y for x, y in curr.iteritems() if not genedic[x].startswith("unmatched")], "total", func, fname)
+ − 488 #x, y, z = get_xyz([y for x, y in curr.iteritems()], "total", func, fname)
+ − 489 o.write("," + x + "," + y + "," + z + "\n")
0
+ − 490
+ − 491
47
+ − 492 # for testing
+ − 493 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
+ − 494 with open(seq_motif_file, 'w') as o:
+ − 495 o.write("ID\tRGYW\tWRCY\tWA\tTW\n")
+ − 496 for ID in IDlist:
+ − 497 #o.write(ID + "\t" + str(round(RGYWCount[ID], 2)) + "\t" + str(round(WRCYCount[ID], 2)) + "\t" + str(round(WACount[ID], 2)) + "\t" + str(round(TWCount[ID], 2)) + "\n")
+ − 498 o.write(ID + "\t" + str(RGYWCount[ID]) + "\t" + str(WRCYCount[ID]) + "\t" + str(WACount[ID]) + "\t" + str(TWCount[ID]) + "\n")
+ − 499
+ − 500 if __name__ == "__main__":
+ − 501 main()