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