Mercurial > repos > davidvanzessen > shm_csr
annotate shm_csr.py @ 90:6809c63d9161 draft
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
author | rhpvorderman |
---|---|
date | Tue, 25 Jan 2022 11:28:29 +0000 |
parents | 729738462297 |
children | 385dea3c6cb5 |
rev | line source |
---|---|
81 | 1 import argparse |
2 import logging | |
3 import sys | |
4 import os | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
5 import typing |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
6 from typing import Optional |
81 | 7 |
8 from collections import defaultdict | |
9 | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
10 REGION_FILTERS = ("leader", "FR1", "CDR1", "FR2", "CDR2") |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
11 |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
12 |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
13 class Mutation(typing.NamedTuple): |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
14 """Represent a mutation type as a tuple""" |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
15 frm: str # 'from' is a reserved python keyword. |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
16 where: int |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
17 to: str |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
18 frmAA: Optional[str] = None |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
19 whereAA: Optional[int] = None |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
20 toAA: Optional[str] = None |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
21 thing: Optional[str] = None # '(---)' or '(+-+)' etc. No idea |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
22 |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
23 @classmethod |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
24 def from_string(cls, string: str): |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
25 # Complete mutation example: a88>g,I30>V(+ - +) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
26 # Only nucleotide example: g303>t |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
27 # Including codon change: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
28 # t169>g,Y57>D(- - -); Y57 tat 169-171 [ta 169-170]>D gac |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
29 # Including codon change (synonumous mutation): |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
30 # c114>t, Y38; Y38 tac 112-114 [tact 112-115]>Y tat |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
31 if ',' in string: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
32 nucleotide_change, aa_change = string.split(',', maxsplit=1) # type: str, Optional[str] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
33 else: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
34 nucleotide_change = string |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
35 aa_change = None |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
36 frm_part, to = nucleotide_change.split('>', maxsplit=1) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
37 frm = frm_part[0] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
38 where = int(frm_part[1:]) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
39 |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
40 if aa_change is None: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
41 return cls(frm, where, to) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
42 |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
43 aa_change = aa_change.strip() |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
44 # The part after semicolon indicates the codon change. This part may |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
45 # not be present. |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
46 semi_colon_index = aa_change.find(";") |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
47 if semi_colon_index == -1: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
48 codon_change = "" |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
49 else: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
50 codon_change = aa_change[semi_colon_index:] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
51 aa_change = aa_change[:semi_colon_index] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
52 change_operator_index = aa_change.find(">") |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
53 if change_operator_index == -1: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
54 # Synonymous change |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
55 frmAA_part = aa_change |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
56 toAA_part = "" |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
57 else: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
58 frmAA_part, toAA_part = aa_change.split('>', maxsplit=1) # type: str, str |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
59 frmAA = frmAA_part[0] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
60 whereAA = int(frmAA_part[1:]) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
61 if toAA_part: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
62 brace_start = toAA_part.index('(') |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
63 toAA = toAA_part[:brace_start] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
64 thing = toAA_part[brace_start:] + codon_change |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
65 else: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
66 # Synonymous mutation |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
67 toAA = frmAA |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
68 thing = codon_change |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
69 return cls(frm, where, to, frmAA, whereAA, toAA, thing) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
70 |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
71 |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
72 class Hotspot(typing.NamedTuple): |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
73 start: int |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
74 end: int |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
75 region: str |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
76 |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
77 @classmethod |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
78 def from_string(cls, string): |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
79 # Example: aa,40-41(FR1) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
80 sequence, rest = string.split(',') # type: str, str |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
81 brace_pos = rest.index('(') |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
82 numbers = rest[:brace_pos] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
83 start, end = numbers.split('-') |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
84 region = rest[brace_pos + 1:-1] # Remove the braces |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
85 return cls(int(start), int(end), region) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
86 |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
87 |
81 | 88 def main(): |
89 parser = argparse.ArgumentParser() | |
90 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") | |
91 parser.add_argument("--genes", help="The genes available in the 'best_match' column") | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
92 parser.add_argument("--empty_region_filter", help="Where does the sequence start?", choices=REGION_FILTERS) |
81 | 93 parser.add_argument("--output", help="Output file") |
94 | |
95 args = parser.parse_args() | |
96 | |
97 infile = args.input | |
98 genes = str(args.genes).split(",") | |
99 empty_region_filter = args.empty_region_filter | |
100 outfile = args.output | |
101 | |
102 genedic = dict() | |
103 | |
104 mutationdic = dict() | |
105 NAMatchResult = (None, None, None, None, None, None, '') | |
106 linecount = 0 | |
107 | |
108 IDIndex = 0 | |
109 best_matchIndex = 0 | |
110 fr1Index = 0 | |
111 cdr1Index = 0 | |
112 fr2Index = 0 | |
113 cdr2Index = 0 | |
114 fr3Index = 0 | |
115 first = True | |
116 IDlist = [] | |
117 mutationList = [] | |
118 mutationListByID = {} | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
119 cdr1AALengthDic = {} |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
120 cdr2AALengthDic = {} |
81 | 121 |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
122 LengthDic = {} |
81 | 123 |
124 cdr1LengthIndex = 0 | |
125 cdr2LengthIndex = 0 | |
126 | |
127 tandem_sum_by_class = defaultdict(int) | |
128 expected_tandem_sum_by_class = defaultdict(float) | |
129 | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
130 with open(infile, 'r') as i: |
81 | 131 for line in i: |
132 if first: | |
133 linesplt = line.split("\t") | |
134 IDIndex = linesplt.index("Sequence.ID") | |
135 best_matchIndex = linesplt.index("best_match") | |
136 fr1Index = linesplt.index("FR1.IMGT") | |
137 cdr1Index = linesplt.index("CDR1.IMGT") | |
138 fr2Index = linesplt.index("FR2.IMGT") | |
139 cdr2Index = linesplt.index("CDR2.IMGT") | |
140 fr3Index = linesplt.index("FR3.IMGT") | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
141 fr1LengthIndex = linesplt.index("FR1.IMGT.Nb.of.nucleotides") |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
142 fr2LengthIndex = linesplt.index("FR2.IMGT.Nb.of.nucleotides") |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
143 fr3LengthIndex = linesplt.index("FR3.IMGT.Nb.of.nucleotides") |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
144 cdr1LengthIndex = linesplt.index("CDR1.IMGT.Nb.of.nucleotides") |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
145 cdr2LengthIndex = linesplt.index("CDR2.IMGT.Nb.of.nucleotides") |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
146 cdr1AALengthIndex = linesplt.index("CDR1.IMGT.length") |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
147 cdr2AALengthIndex = linesplt.index("CDR2.IMGT.length") |
81 | 148 first = False |
149 continue | |
150 linecount += 1 | |
151 linesplt = line.split("\t") | |
152 ID = linesplt[IDIndex] | |
153 genedic[ID] = linesplt[best_matchIndex] | |
154 | |
155 mutationdic[ID + "_FR1"] = [] | |
156 if len(linesplt[fr1Index]) > 5 and empty_region_filter == "leader": | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
157 mutationdic[ID + "_FR1"] = [Mutation.from_string(x) for x in linesplt[fr1Index].split("|") if x] |
81 | 158 |
159 mutationdic[ID + "_CDR1"] = [] | |
160 if len(linesplt[cdr1Index]) > 5 and empty_region_filter in ["leader", "FR1"]: | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
161 mutationdic[ID + "_CDR1"] = [Mutation.from_string(x) for x in linesplt[cdr1Index].split("|") if x] |
81 | 162 |
163 mutationdic[ID + "_FR2"] = [] | |
164 if len(linesplt[fr2Index]) > 5 and empty_region_filter in ["leader", "FR1", "CDR1"]: | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
165 mutationdic[ID + "_FR2"] = [Mutation.from_string(x) for x in linesplt[fr2Index].split("|") if x] |
81 | 166 |
167 mutationdic[ID + "_CDR2"] = [] | |
168 if len(linesplt[cdr2Index]) > 5: | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
169 mutationdic[ID + "_CDR2"] = [Mutation.from_string(x) for x in linesplt[cdr2Index].split("|") if x] |
81 | 170 |
171 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] | |
172 | |
173 mutationdic[ID + "_FR3"] = [] | |
174 if len(linesplt[fr3Index]) > 5: | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
175 mutationdic[ID + "_FR3"] = [Mutation.from_string(x) for x in linesplt[fr3Index].split("|") if x] |
81 | 176 |
177 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
178 mutationListByID[ID] = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
179 | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
180 fr1Length = int(linesplt[fr1LengthIndex]) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
181 fr2Length = int(linesplt[fr2LengthIndex]) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
182 fr3Length = int(linesplt[fr3LengthIndex]) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
183 cdr1Length = int(linesplt[cdr1LengthIndex]) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
184 cdr2Length = int(linesplt[cdr2LengthIndex]) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
185 LengthDic[ID] = (fr1Length, cdr1Length, fr2Length, cdr2Length, fr3Length) |
81 | 186 |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
187 cdr1AALengthDic[ID] = int(linesplt[cdr1AALengthIndex]) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
188 cdr2AALengthDic[ID] = int(linesplt[cdr2AALengthIndex]) |
81 | 189 |
190 IDlist += [ID] | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
191 print("len(mutationdic) =", len(mutationdic)) |
81 | 192 |
193 with open(os.path.join(os.path.dirname(os.path.abspath(infile)), "mutationdict.txt"), 'w') as out_handle: | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
194 for ID, lst in mutationdic.items(): |
81 | 195 for mut in lst: |
196 out_handle.write("{0}\t{1}\n".format(ID, "\t".join([str(x) for x in mut]))) | |
197 | |
198 #tandem mutation stuff | |
199 tandem_frequency = defaultdict(int) | |
200 mutation_frequency = defaultdict(int) | |
201 | |
202 mutations_by_id_dic = {} | |
203 first = True | |
204 mutation_by_id_file = os.path.join(os.path.dirname(outfile), "mutation_by_id.txt") | |
205 with open(mutation_by_id_file, 'r') as mutation_by_id: | |
206 for l in mutation_by_id: | |
207 if first: | |
208 first = False | |
209 continue | |
210 splt = l.split("\t") | |
211 mutations_by_id_dic[splt[0]] = int(splt[1]) | |
212 | |
213 tandem_file = os.path.join(os.path.dirname(outfile), "tandems_by_id.txt") | |
214 with open(tandem_file, 'w') as o: | |
215 highest_tandem_length = 0 | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
216 # LengthDic stores length as a tuple |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
217 # (fr1Length, cdr1Length, fr2Length, cdr2Length, fr3Length) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
218 # To get the total length, we can sum(region_lengths) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
219 # To get the total length for leader: |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
220 # sum(region_lengths[0:]) (Equivalent to everything) |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
221 # sum(region_lengths[1:]) Gets everything except FR1 etc. |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
222 # We determine the position to start summing below. |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
223 # This returns 0 for leader, 1 for FR1 etc. |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
224 length_start_pos = REGION_FILTERS.index(empty_region_filter) |
81 | 225 |
226 o.write("Sequence.ID\tnumber_of_mutations\tnumber_of_tandems\tregion_length\texpected_tandems\tlongest_tandem\ttandems\n") | |
227 for ID in IDlist: | |
228 mutations = mutationListByID[ID] | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
229 region_length = sum(LengthDic[ID][length_start_pos:]) |
81 | 230 if len(mutations) == 0: |
231 continue | |
232 last_mut = max(mutations, key=lambda x: int(x[1])) | |
233 | |
234 last_mut_pos = int(last_mut[1]) | |
235 | |
236 mut_positions = [False] * (last_mut_pos + 1) | |
237 | |
238 for mutation in mutations: | |
239 frm, where, to, frmAA, whereAA, toAA, thing = mutation | |
240 where = int(where) | |
241 mut_positions[where] = True | |
242 | |
243 tandem_muts = [] | |
244 tandem_start = -1 | |
245 tandem_length = 0 | |
246 for i in range(len(mut_positions)): | |
247 if mut_positions[i]: | |
248 if tandem_start == -1: | |
249 tandem_start = i | |
250 tandem_length += 1 | |
251 #print "".join(["1" if x else "0" for x in mut_positions[:i+1]]) | |
252 else: | |
253 if tandem_length > 1: | |
254 tandem_muts.append((tandem_start, tandem_length)) | |
255 #print "{0}{1} {2}:{3}".format(" " * (i - tandem_length), "^" * tandem_length, tandem_start, tandem_length) | |
256 tandem_start = -1 | |
257 tandem_length = 0 | |
258 if tandem_length > 1: # if the sequence ends with a tandem mutation | |
259 tandem_muts.append((tandem_start, tandem_length)) | |
260 | |
261 if len(tandem_muts) > 0: | |
262 if highest_tandem_length < len(tandem_muts): | |
263 highest_tandem_length = len(tandem_muts) | |
264 | |
265 longest_tandem = max(tandem_muts, key=lambda x: x[1]) if len(tandem_muts) else (0, 0) | |
266 num_mutations = mutations_by_id_dic[ID] # len(mutations) | |
267 f_num_mutations = float(num_mutations) | |
268 num_tandem_muts = len(tandem_muts) | |
269 expected_tandem_muts = f_num_mutations * (f_num_mutations - 1.0) / float(region_length) | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
270 # String format and round disagree slightly (see 3.605). |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
271 # So round before formatting. |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
272 o.write(f"{ID}\t{num_mutations}\t{num_tandem_muts}\t{region_length}\t" |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
273 f"{round(expected_tandem_muts, 2):.2f}\t" |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
274 f"{longest_tandem[1]}\t{tandem_muts}\n") |
81 | 275 gene = genedic[ID] |
276 if gene.find("unmatched") == -1: | |
277 tandem_sum_by_class[gene] += num_tandem_muts | |
278 expected_tandem_sum_by_class[gene] += expected_tandem_muts | |
279 | |
280 tandem_sum_by_class["all"] += num_tandem_muts | |
281 expected_tandem_sum_by_class["all"] += expected_tandem_muts | |
282 | |
283 gene = gene[:3] | |
284 if gene in ["IGA", "IGG"]: | |
285 tandem_sum_by_class[gene] += num_tandem_muts | |
286 expected_tandem_sum_by_class[gene] += expected_tandem_muts | |
287 else: | |
288 tandem_sum_by_class["unmatched"] += num_tandem_muts | |
289 expected_tandem_sum_by_class["unmatched"] += expected_tandem_muts | |
290 | |
291 | |
292 for tandem_mut in tandem_muts: | |
293 tandem_frequency[str(tandem_mut[1])] += 1 | |
294 #print "\t".join([ID, str(len(tandem_muts)), str(longest_tandem[1]) , str(tandem_muts)]) | |
295 | |
296 tandem_freq_file = os.path.join(os.path.dirname(outfile), "tandem_frequency.txt") | |
297 with open(tandem_freq_file, 'w') as o: | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
298 for frq in sorted([int(x) for x in list(tandem_frequency.keys())]): |
81 | 299 o.write("{0}\t{1}\n".format(frq, tandem_frequency[str(frq)])) |
300 | |
301 tandem_row = [] | |
302 genes_extra = list(genes) | |
303 genes_extra.append("all") | |
304 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]): | |
305 if y != 0: | |
306 tandem_row += [x, round(y, 2), round(x / y, 2)] | |
307 else: | |
308 tandem_row += [x, round(y, 2), 0] | |
309 | |
310 tandem_freq_file = os.path.join(os.path.dirname(outfile), "shm_overview_tandem_row.txt") | |
311 with open(tandem_freq_file, 'w') as o: | |
312 o.write("Tandems/Expected (ratio),{0}\n".format(",".join([str(x) for x in tandem_row]))) | |
313 | |
314 #print mutationList, linecount | |
315 | |
316 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 | |
317 if AALength < 60: | |
318 AALength = 64 | |
319 | |
320 AA_mutation = [0] * AALength | |
321 AA_mutation_dic = {"IGA": AA_mutation[:], "IGG": AA_mutation[:], "IGM": AA_mutation[:], "IGE": AA_mutation[:], "unm": AA_mutation[:], "all": AA_mutation[:]} | |
322 AA_mutation_empty = AA_mutation[:] | |
323 | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
324 print("AALength:", AALength) |
81 | 325 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt" |
326 with open(aa_mutations_by_id_file, 'w') as o: | |
327 o.write("ID\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n") | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
328 for ID in list(mutationListByID.keys()): |
81 | 329 AA_mutation_for_ID = AA_mutation_empty[:] |
330 for mutation in mutationListByID[ID]: | |
331 if mutation[4] and mutation[5] != ";": | |
332 AA_mutation_position = int(mutation[4]) | |
333 try: | |
334 AA_mutation[AA_mutation_position] += 1 | |
335 AA_mutation_for_ID[AA_mutation_position] += 1 | |
336 except Exception as e: | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
337 print(e) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
338 print(mutation) |
81 | 339 sys.exit() |
340 clss = genedic[ID][:3] | |
341 AA_mutation_dic[clss][AA_mutation_position] += 1 | |
342 o.write(ID + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n") | |
343 | |
344 | |
345 | |
346 #absent AA stuff | |
347 absentAACDR1Dic = defaultdict(list) | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
348 absentAACDR1Dic[5] = list(range(29,36)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
349 absentAACDR1Dic[6] = list(range(29,35)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
350 absentAACDR1Dic[7] = list(range(30,35)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
351 absentAACDR1Dic[8] = list(range(30,34)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
352 absentAACDR1Dic[9] = list(range(31,34)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
353 absentAACDR1Dic[10] = list(range(31,33)) |
81 | 354 absentAACDR1Dic[11] = [32] |
355 | |
356 absentAACDR2Dic = defaultdict(list) | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
357 absentAACDR2Dic[0] = list(range(55,65)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
358 absentAACDR2Dic[1] = list(range(56,65)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
359 absentAACDR2Dic[2] = list(range(56,64)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
360 absentAACDR2Dic[3] = list(range(57,64)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
361 absentAACDR2Dic[4] = list(range(57,63)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
362 absentAACDR2Dic[5] = list(range(58,63)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
363 absentAACDR2Dic[6] = list(range(58,62)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
364 absentAACDR2Dic[7] = list(range(59,62)) |
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
365 absentAACDR2Dic[8] = list(range(59,61)) |
81 | 366 absentAACDR2Dic[9] = [60] |
367 | |
368 absentAA = [len(IDlist)] * (AALength-1) | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
369 for k, cdr1Length in cdr1AALengthDic.items(): |
81 | 370 for c in absentAACDR1Dic[cdr1Length]: |
371 absentAA[c] -= 1 | |
372 | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
373 for k, cdr2Length in cdr2AALengthDic.items(): |
81 | 374 for c in absentAACDR2Dic[cdr2Length]: |
375 absentAA[c] -= 1 | |
376 | |
377 | |
378 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/absent_aa_id.txt" | |
379 with open(aa_mutations_by_id_file, 'w') as o: | |
380 o.write("ID\tcdr1length\tcdr2length\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n") | |
381 for ID in IDlist: | |
382 absentAAbyID = [1] * (AALength-1) | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
383 cdr1Length = cdr1AALengthDic[ID] |
81 | 384 for c in absentAACDR1Dic[cdr1Length]: |
385 absentAAbyID[c] -= 1 | |
386 | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
387 cdr2Length = cdr2AALengthDic[ID] |
81 | 388 for c in absentAACDR2Dic[cdr2Length]: |
389 absentAAbyID[c] -= 1 | |
390 o.write(ID + "\t" + str(cdr1Length) + "\t" + str(cdr2Length) + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in absentAAbyID]) + "\n") | |
391 | |
392 if linecount == 0: | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
393 print("No data, exiting") |
81 | 394 with open(outfile, 'w') as o: |
395 o.write("RGYW (%)," + ("0,0,0\n" * len(genes))) | |
396 o.write("WRCY (%)," + ("0,0,0\n" * len(genes))) | |
397 o.write("WA (%)," + ("0,0,0\n" * len(genes))) | |
398 o.write("TW (%)," + ("0,0,0\n" * len(genes))) | |
399 sys.exit() | |
400 | |
401 RGYWCount = {} | |
402 WRCYCount = {} | |
403 WACount = {} | |
404 TWCount = {} | |
405 | |
406 #IDIndex = 0 | |
407 ataIndex = 0 | |
408 tatIndex = 0 | |
409 aggctatIndex = 0 | |
410 atagcctIndex = 0 | |
411 first = True | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
412 with open(infile, 'r') as i: |
81 | 413 for line in i: |
414 if first: | |
415 linesplt = line.split("\t") | |
416 ataIndex = linesplt.index("X.a.t.a") | |
417 tatIndex = linesplt.index("t.a.t.") | |
418 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.") | |
419 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.") | |
420 first = False | |
421 continue | |
422 linesplt = line.split("\t") | |
423 gene = linesplt[best_matchIndex] | |
424 ID = linesplt[IDIndex] | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
425 RGYW = [Hotspot.from_string(x) for x in linesplt[aggctatIndex].split("|") if x] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
426 WRCY = [Hotspot.from_string(x) for x in linesplt[atagcctIndex].split("|") if x] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
427 WA = [Hotspot.from_string(x) for x in linesplt[ataIndex].split("|") if x] |
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
428 TW = [Hotspot.from_string(x) for x in linesplt[tatIndex].split("|") if x] |
81 | 429 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0 |
430 | |
431 with open(os.path.join(os.path.dirname(os.path.abspath(infile)), "RGYW.txt"), 'a') as out_handle: | |
432 for hotspot in RGYW: | |
433 out_handle.write("{0}\t{1}\n".format(ID, "\t".join([str(x) for x in hotspot]))) | |
434 | |
435 mutationList = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"] | |
436 for mutation in mutationList: | |
437 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation | |
438 mutation_in_RGYW = any(((start <= int(where) <= end) for (start, end, region) in RGYW)) | |
439 mutation_in_WRCY = any(((start <= int(where) <= end) for (start, end, region) in WRCY)) | |
440 mutation_in_WA = any(((start <= int(where) <= end) for (start, end, region) in WA)) | |
441 mutation_in_TW = any(((start <= int(where) <= end) for (start, end, region) in TW)) | |
442 | |
443 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW]) | |
444 | |
445 if in_how_many_motifs > 0: | |
446 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs | |
447 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs | |
448 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs | |
449 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs | |
450 | |
451 mutations_in_motifs_file = os.path.join(os.path.dirname(os.path.abspath(infile)), "mutation_in_motifs.txt") | |
452 if not os.path.exists(mutation_by_id_file): | |
453 with open(mutations_in_motifs_file, 'w') as out_handle: | |
454 out_handle.write("{0}\n".format("\t".join([ | |
455 "Sequence.ID", | |
456 "mutation_position", | |
457 "region", | |
458 "from_nt", | |
459 "to_nt", | |
460 "mutation_position_AA", | |
461 "from_AA", | |
462 "to_AA", | |
463 "motif", | |
464 "motif_start_nt", | |
465 "motif_end_nt", | |
466 "rest" | |
467 ]))) | |
468 | |
469 with open(mutations_in_motifs_file, 'a') as out_handle: | |
470 motif_dic = {"RGYW": RGYW, "WRCY": WRCY, "WA": WA, "TW": TW} | |
471 for mutation in mutationList: | |
472 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
473 for motif in list(motif_dic.keys()): |
81 | 474 |
475 for start, end, region in motif_dic[motif]: | |
476 if start <= int(where) <= end: | |
477 out_handle.write("{0}\n".format( | |
478 "\t".join([ | |
479 ID, | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
480 str(where), |
81 | 481 region, |
482 frm, | |
483 to, | |
484 str(AAwhere), | |
485 str(AAfrm), | |
486 str(AAto), | |
487 motif, | |
488 str(start), | |
489 str(end), | |
490 str(junk) | |
491 ]) | |
492 )) | |
493 | |
494 | |
495 | |
496 def mean(lst): | |
497 return (float(sum(lst)) / len(lst)) if len(lst) > 0 else 0.0 | |
498 | |
499 | |
500 def median(lst): | |
501 lst = sorted(lst) | |
502 l = len(lst) | |
503 if l == 0: | |
504 return 0 | |
505 if l == 1: | |
506 return lst[0] | |
507 | |
508 l = int(l / 2) | |
509 | |
510 if len(lst) % 2 == 0: | |
511 return float(lst[l] + lst[(l - 1)]) / 2.0 | |
512 else: | |
513 return lst[l] | |
514 | |
515 funcs = {"mean": mean, "median": median, "sum": sum} | |
516 | |
517 directory = outfile[:outfile.rfind("/") + 1] | |
518 value = 0 | |
519 valuedic = dict() | |
520 | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
521 for fname in list(funcs.keys()): |
81 | 522 for gene in genes: |
523 with open(directory + gene + "_" + fname + "_value.txt", 'r') as v: | |
524 valuedic[gene + "_" + fname] = float(v.readlines()[0].rstrip()) | |
525 with open(directory + "all_" + fname + "_value.txt", 'r') as v: | |
526 valuedic["total_" + fname] = float(v.readlines()[0].rstrip()) | |
527 | |
528 | |
529 def get_xyz(lst, gene, f, fname): | |
530 x = round(round(f(lst), 1)) | |
531 y = valuedic[gene + "_" + fname] | |
532 z = str(round(x / float(y) * 100, 1)) if y != 0 else "0" | |
533 return (str(x), str(y), z) | |
534 | |
535 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount} | |
536 arr = ["RGYW", "WRCY", "WA", "TW"] | |
537 | |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
538 for fname in list(funcs.keys()): |
81 | 539 func = funcs[fname] |
540 foutfile = outfile[:outfile.rindex("/")] + "/hotspot_analysis_" + fname + ".txt" | |
541 with open(foutfile, 'w') as o: | |
542 for typ in arr: | |
543 o.write(typ + " (%)") | |
544 curr = dic[typ] | |
545 for gene in genes: | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
546 if valuedic[gene + "_" + fname] == 0: |
81 | 547 o.write(",0,0,0") |
548 else: | |
90
6809c63d9161
"planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents:
83
diff
changeset
|
549 x, y, z = get_xyz([curr[x] for x in [y for y, z in genedic.items() if z.startswith(gene)]], gene, func, fname) |
81 | 550 o.write("," + x + "," + y + "," + z) |
83
729738462297
"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents:
81
diff
changeset
|
551 x, y, z = get_xyz([y for x, y in curr.items() if not genedic[x].startswith("unmatched")], "total", func, fname) |
81 | 552 #x, y, z = get_xyz([y for x, y in curr.iteritems()], "total", func, fname) |
553 o.write("," + x + "," + y + "," + z + "\n") | |
554 | |
555 | |
556 # for testing | |
557 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt" | |
558 with open(seq_motif_file, 'w') as o: | |
559 o.write("ID\tRGYW\tWRCY\tWA\tTW\n") | |
560 for ID in IDlist: | |
561 #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") | |
562 o.write(ID + "\t" + str(RGYWCount[ID]) + "\t" + str(WRCYCount[ID]) + "\t" + str(WACount[ID]) + "\t" + str(TWCount[ID]) + "\n") | |
563 | |
564 if __name__ == "__main__": | |
565 main() |