annotate shm_csr.py @ 96:385dea3c6cb5 draft

planemo upload commit 423a48569c69301fdbf893ac3a649128404dfff5
author rhpvorderman
date Fri, 05 Jan 2024 08:53:22 +0000
parents 6809c63d9161
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
1 import argparse
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
2 import logging
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
3 import sys
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
8
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
9 from collections import defaultdict
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
95 def main():
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
96 parser = argparse.ArgumentParser()
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
100 parser.add_argument("--output", help="Output file")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
101
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
102 args = parser.parse_args()
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
103
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
104 infile = args.input
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
105 genes = str(args.genes).split(",")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
106 empty_region_filter = args.empty_region_filter
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
107 outfile = args.output
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
108
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
109 genedic = dict()
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
110
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
111 mutationdic = dict()
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
112 NAMatchResult = (None, None, None, None, None, None, '')
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
113 linecount = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
114
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
115 IDIndex = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
116 best_matchIndex = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
117 fr1Index = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
118 cdr1Index = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
119 fr2Index = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
120 cdr2Index = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
121 fr3Index = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
122 first = True
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
123 IDlist = []
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
124 mutationList = []
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
128
90
6809c63d9161 "planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents: 83
diff changeset
129 LengthDic = {}
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
130
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
131 cdr1LengthIndex = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
132 cdr2LengthIndex = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
133
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
134 tandem_sum_by_class = defaultdict(int)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
135 expected_tandem_sum_by_class = defaultdict(float)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
136
83
729738462297 "planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents: 81
diff changeset
137 with open(infile, 'r') as i:
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
138 for line in i:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
139 if first:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
140 linesplt = line.split("\t")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
141 IDIndex = linesplt.index("Sequence.ID")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
142 best_matchIndex = linesplt.index("best_match")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
143 fr1Index = linesplt.index("FR1.IMGT")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
144 cdr1Index = linesplt.index("CDR1.IMGT")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
145 fr2Index = linesplt.index("FR2.IMGT")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
146 cdr2Index = linesplt.index("CDR2.IMGT")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
155 first = False
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
156 continue
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
157 linecount += 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
158 linesplt = line.split("\t")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
159 ID = linesplt[IDIndex]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
160 genedic[ID] = linesplt[best_matchIndex]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
161
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
162 mutationdic[ID + "_FR1"] = []
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
165
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
166 mutationdic[ID + "_CDR1"] = []
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
169
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
170 mutationdic[ID + "_FR2"] = []
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
173
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
174 mutationdic[ID + "_CDR2"] = []
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
177
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
178 mutationdic[ID + "_FR2-CDR2"] = mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
179
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
180 mutationdic[ID + "_FR3"] = []
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
183
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
184 mutationList += mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
185 mutationListByID[ID] = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
196
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
197 IDlist += [ID]
83
729738462297 "planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents: 81
diff changeset
198 print("len(mutationdic) =", len(mutationdic))
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
199
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
202 for mut in lst:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
203 out_handle.write("{0}\t{1}\n".format(ID, "\t".join([str(x) for x in mut])))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
204
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
205 #tandem mutation stuff
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
206 tandem_frequency = defaultdict(int)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
207 mutation_frequency = defaultdict(int)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
208
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
209 mutations_by_id_dic = {}
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
210 first = True
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
211 mutation_by_id_file = os.path.join(os.path.dirname(outfile), "mutation_by_id.txt")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
212 with open(mutation_by_id_file, 'r') as mutation_by_id:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
213 for l in mutation_by_id:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
214 if first:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
215 first = False
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
216 continue
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
217 splt = l.split("\t")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
218 mutations_by_id_dic[splt[0]] = int(splt[1])
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
219
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
220 tandem_file = os.path.join(os.path.dirname(outfile), "tandems_by_id.txt")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
221 with open(tandem_file, 'w') as o:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
234
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
235 o.write("Sequence.ID\tnumber_of_mutations\tnumber_of_tandems\tregion_length\texpected_tandems\tlongest_tandem\ttandems\n")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
236 for ID in IDlist:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
239 if len(mutations) == 0:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
240 continue
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
241 last_mut = max(mutations, key=lambda x: int(x[1]))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
242
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
243 last_mut_pos = int(last_mut[1])
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
244
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
245 mut_positions = [False] * (last_mut_pos + 1)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
246
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
247 for mutation in mutations:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
248 frm, where, to, frmAA, whereAA, toAA, thing = mutation
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
249 where = int(where)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
250 mut_positions[where] = True
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
251
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
252 tandem_muts = []
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
253 tandem_start = -1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
254 tandem_length = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
255 for i in range(len(mut_positions)):
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
256 if mut_positions[i]:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
257 if tandem_start == -1:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
258 tandem_start = i
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
259 tandem_length += 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
260 #print "".join(["1" if x else "0" for x in mut_positions[:i+1]])
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
261 else:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
262 if tandem_length > 1:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
263 tandem_muts.append((tandem_start, tandem_length))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
264 #print "{0}{1} {2}:{3}".format(" " * (i - tandem_length), "^" * tandem_length, tandem_start, tandem_length)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
265 tandem_start = -1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
266 tandem_length = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
267 if tandem_length > 1: # if the sequence ends with a tandem mutation
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
268 tandem_muts.append((tandem_start, tandem_length))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
269
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
270 if len(tandem_muts) > 0:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
271 if highest_tandem_length < len(tandem_muts):
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
272 highest_tandem_length = len(tandem_muts)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
273
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
274 longest_tandem = max(tandem_muts, key=lambda x: x[1]) if len(tandem_muts) else (0, 0)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
275 num_mutations = mutations_by_id_dic[ID] # len(mutations)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
276 f_num_mutations = float(num_mutations)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
277 num_tandem_muts = len(tandem_muts)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
284 gene = genedic[ID]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
285 if gene.find("unmatched") == -1:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
286 tandem_sum_by_class[gene] += num_tandem_muts
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
287 expected_tandem_sum_by_class[gene] += expected_tandem_muts
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
288
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
289 tandem_sum_by_class["all"] += num_tandem_muts
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
290 expected_tandem_sum_by_class["all"] += expected_tandem_muts
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
291
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
292 gene = gene[:3]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
293 if gene in ["IGA", "IGG"]:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
294 tandem_sum_by_class[gene] += num_tandem_muts
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
295 expected_tandem_sum_by_class[gene] += expected_tandem_muts
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
296 else:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
297 tandem_sum_by_class["unmatched"] += num_tandem_muts
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
298 expected_tandem_sum_by_class["unmatched"] += expected_tandem_muts
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
299
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
300
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
301 for tandem_mut in tandem_muts:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
302 tandem_frequency[str(tandem_mut[1])] += 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
303 #print "\t".join([ID, str(len(tandem_muts)), str(longest_tandem[1]) , str(tandem_muts)])
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
304
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
305 tandem_freq_file = os.path.join(os.path.dirname(outfile), "tandem_frequency.txt")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
308 o.write("{0}\t{1}\n".format(frq, tandem_frequency[str(frq)]))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
309
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
310 tandem_row = []
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
311 genes_extra = list(genes)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
312 genes_extra.append("all")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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]):
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
314 if y != 0:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
315 tandem_row += [x, round(y, 2), round(x / y, 2)]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
316 else:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
317 tandem_row += [x, round(y, 2), 0]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
318
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
319 tandem_freq_file = os.path.join(os.path.dirname(outfile), "shm_overview_tandem_row.txt")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
320 with open(tandem_freq_file, 'w') as o:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
321 o.write("Tandems/Expected (ratio),{0}\n".format(",".join([str(x) for x in tandem_row])))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
322
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
323 #print mutationList, linecount
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
324
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
326 if AALength < 60:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
327 AALength = 64
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
328
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
329 AA_mutation = [0] * AALength
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
330 AA_mutation_dic = {"IGA": AA_mutation[:], "IGG": AA_mutation[:], "IGM": AA_mutation[:], "IGE": AA_mutation[:], "unm": AA_mutation[:], "all": AA_mutation[:]}
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
331 AA_mutation_empty = AA_mutation[:]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
332
83
729738462297 "planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents: 81
diff changeset
333 print("AALength:", AALength)
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
334 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/aa_id_mutations.txt"
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
335 with open(aa_mutations_by_id_file, 'w') as o:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
338 AA_mutation_for_ID = AA_mutation_empty[:]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
339 for mutation in mutationListByID[ID]:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
340 if mutation[4] and mutation[5] != ";":
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
341 AA_mutation_position = int(mutation[4])
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
342 try:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
343 AA_mutation[AA_mutation_position] += 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
344 AA_mutation_for_ID[AA_mutation_position] += 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
348 sys.exit()
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
349 clss = genedic[ID][:3]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
350 AA_mutation_dic[clss][AA_mutation_position] += 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
351 o.write(ID + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in AA_mutation_for_ID[1:]]) + "\n")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
352
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
353
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
354
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
355 #absent AA stuff
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
363 absentAACDR1Dic[11] = [32]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
364
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
375 absentAACDR2Dic[9] = [60]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
376
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
379 for c in absentAACDR1Dic[cdr1Length]:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
380 absentAA[c] -= 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
381
90
6809c63d9161 "planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents: 83
diff changeset
382 for k, cdr2Length in cdr2AALengthDic.items():
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
383 for c in absentAACDR2Dic[cdr2Length]:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
384 absentAA[c] -= 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
385
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
386
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
387 aa_mutations_by_id_file = outfile[:outfile.rindex("/")] + "/absent_aa_id.txt"
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
388 with open(aa_mutations_by_id_file, 'w') as o:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
389 o.write("ID\tcdr1length\tcdr2length\tbest_match\t" + "\t".join([str(x) for x in range(1,AALength)]) + "\n")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
390 for ID in IDlist:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
391 absentAAbyID = [1] * (AALength-1)
90
6809c63d9161 "planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents: 83
diff changeset
392 cdr1Length = cdr1AALengthDic[ID]
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
393 for c in absentAACDR1Dic[cdr1Length]:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
394 absentAAbyID[c] -= 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
395
90
6809c63d9161 "planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents: 83
diff changeset
396 cdr2Length = cdr2AALengthDic[ID]
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
397 for c in absentAACDR2Dic[cdr2Length]:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
398 absentAAbyID[c] -= 1
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
399 o.write(ID + "\t" + str(cdr1Length) + "\t" + str(cdr2Length) + "\t" + genedic[ID] + "\t" + "\t".join([str(x) for x in absentAAbyID]) + "\n")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
400
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
401 if linecount == 0:
83
729738462297 "planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents: 81
diff changeset
402 print("No data, exiting")
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
403 with open(outfile, 'w') as o:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
404 o.write("RGYW (%)," + ("0,0,0\n" * len(genes)))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
405 o.write("WRCY (%)," + ("0,0,0\n" * len(genes)))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
406 o.write("WA (%)," + ("0,0,0\n" * len(genes)))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
407 o.write("TW (%)," + ("0,0,0\n" * len(genes)))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
408 sys.exit()
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
409
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
410 RGYWCount = {}
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
411 WRCYCount = {}
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
412 WACount = {}
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
413 TWCount = {}
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
414
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
415 #IDIndex = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
416 ataIndex = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
417 tatIndex = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
418 aggctatIndex = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
419 atagcctIndex = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
420 first = True
83
729738462297 "planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents: 81
diff changeset
421 with open(infile, 'r') as i:
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
422 for line in i:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
423 if first:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
424 linesplt = line.split("\t")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
425 ataIndex = linesplt.index("X.a.t.a")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
426 tatIndex = linesplt.index("t.a.t.")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
427 aggctatIndex = linesplt.index("X.a.g.g.c.t..a.t.")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
428 atagcctIndex = linesplt.index("X.a.t..a.g.c.c.t.")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
429 first = False
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
430 continue
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
431 linesplt = line.split("\t")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
432 gene = linesplt[best_matchIndex]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
438 RGYWCount[ID], WRCYCount[ID], WACount[ID], TWCount[ID] = 0, 0, 0, 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
439
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
440 with open(os.path.join(os.path.dirname(os.path.abspath(infile)), "RGYW.txt"), 'a') as out_handle:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
441 for hotspot in RGYW:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
442 out_handle.write("{0}\t{1}\n".format(ID, "\t".join([str(x) for x in hotspot])))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
443
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
444 mutationList = mutationdic[ID + "_FR1"] + mutationdic[ID + "_CDR1"] + mutationdic[ID + "_FR2"] + mutationdic[ID + "_CDR2"] + mutationdic[ID + "_FR3"]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
445 for mutation in mutationList:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
446 frm, where, to, AAfrm, AAwhere, AAto, junk = mutation
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
447 mutation_in_RGYW = any(((start <= int(where) <= end) for (start, end, region) in RGYW))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
448 mutation_in_WRCY = any(((start <= int(where) <= end) for (start, end, region) in WRCY))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
449 mutation_in_WA = any(((start <= int(where) <= end) for (start, end, region) in WA))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
450 mutation_in_TW = any(((start <= int(where) <= end) for (start, end, region) in TW))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
451
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
452 in_how_many_motifs = sum([mutation_in_RGYW, mutation_in_WRCY, mutation_in_WA, mutation_in_TW])
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
453
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
454 if in_how_many_motifs > 0:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
455 RGYWCount[ID] += (1.0 * int(mutation_in_RGYW)) / in_how_many_motifs
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
456 WRCYCount[ID] += (1.0 * int(mutation_in_WRCY)) / in_how_many_motifs
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
457 WACount[ID] += (1.0 * int(mutation_in_WA)) / in_how_many_motifs
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
458 TWCount[ID] += (1.0 * int(mutation_in_TW)) / in_how_many_motifs
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
459
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
460 mutations_in_motifs_file = os.path.join(os.path.dirname(os.path.abspath(infile)), "mutation_in_motifs.txt")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
461 if not os.path.exists(mutation_by_id_file):
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
462 with open(mutations_in_motifs_file, 'w') as out_handle:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
463 out_handle.write("{0}\n".format("\t".join([
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
464 "Sequence.ID",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
465 "mutation_position",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
466 "region",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
467 "from_nt",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
468 "to_nt",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
469 "mutation_position_AA",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
470 "from_AA",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
471 "to_AA",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
472 "motif",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
473 "motif_start_nt",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
474 "motif_end_nt",
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
475 "rest"
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
476 ])))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
477
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
478 with open(mutations_in_motifs_file, 'a') as out_handle:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
479 motif_dic = {"RGYW": RGYW, "WRCY": WRCY, "WA": WA, "TW": TW}
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
480 for mutation in mutationList:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
483
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
484 for start, end, region in motif_dic[motif]:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
485 if start <= int(where) <= end:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
486 out_handle.write("{0}\n".format(
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
487 "\t".join([
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
488 ID,
90
6809c63d9161 "planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents: 83
diff changeset
489 str(where),
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
490 region,
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
491 frm,
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
492 to,
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
493 str(AAwhere),
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
494 str(AAfrm),
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
495 str(AAto),
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
496 motif,
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
497 str(start),
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
498 str(end),
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
499 str(junk)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
500 ])
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
501 ))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
502
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
503
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
504
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
505 def mean(lst):
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
506 return (float(sum(lst)) / len(lst)) if len(lst) > 0 else 0.0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
507
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
508
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
509 def median(lst):
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
510 lst = sorted(lst)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
511 l = len(lst)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
512 if l == 0:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
513 return 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
514 if l == 1:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
515 return lst[0]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
516
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
517 l = int(l / 2)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
518
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
519 if len(lst) % 2 == 0:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
520 return float(lst[l] + lst[(l - 1)]) / 2.0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
521 else:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
522 return lst[l]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
523
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
524 funcs = {"mean": mean, "median": median, "sum": sum}
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
525
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
526 directory = outfile[:outfile.rfind("/") + 1]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
527 value = 0
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
528 valuedic = dict()
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
529
83
729738462297 "planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents: 81
diff changeset
530 for fname in list(funcs.keys()):
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
531 for gene in genes:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
532 with open(directory + gene + "_" + fname + "_value.txt", 'r') as v:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
533 valuedic[gene + "_" + fname] = float(v.readlines()[0].rstrip())
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
534 with open(directory + "all_" + fname + "_value.txt", 'r') as v:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
535 valuedic["total_" + fname] = float(v.readlines()[0].rstrip())
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
536
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
537
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
538 def get_xyz(lst, gene, f, fname):
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
539 x = round(round(f(lst), 1))
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
540 y = valuedic[gene + "_" + fname]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
541 z = str(round(x / float(y) * 100, 1)) if y != 0 else "0"
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
542 return (str(x), str(y), z)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
543
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
544 dic = {"RGYW": RGYWCount, "WRCY": WRCYCount, "WA": WACount, "TW": TWCount}
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
545 arr = ["RGYW", "WRCY", "WA", "TW"]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
546
83
729738462297 "planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
rhpvorderman
parents: 81
diff changeset
547 for fname in list(funcs.keys()):
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
548 func = funcs[fname]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
549 foutfile = outfile[:outfile.rindex("/")] + "/hotspot_analysis_" + fname + ".txt"
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
550 with open(foutfile, 'w') as o:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
551 for typ in arr:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
552 o.write(typ + " (%)")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
553 curr = dic[typ]
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
554 for gene in genes:
90
6809c63d9161 "planemo upload commit fd64827ff6e63df008f6f50ddb8576ad2b1dbb26"
rhpvorderman
parents: 83
diff changeset
555 if valuedic[gene + "_" + fname] == 0:
81
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
556 o.write(",0,0,0")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
561 #x, y, z = get_xyz([y for x, y in curr.iteritems()], "total", func, fname)
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
562 o.write("," + x + "," + y + "," + z + "\n")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
563
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
564
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
565 # for testing
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
566 seq_motif_file = outfile[:outfile.rindex("/")] + "/motif_per_seq.txt"
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
567 with open(seq_motif_file, 'w') as o:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
568 o.write("ID\tRGYW\tWRCY\tWA\tTW\n")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
569 for ID in IDlist:
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
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")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
571 o.write(ID + "\t" + str(RGYWCount[ID]) + "\t" + str(WRCYCount[ID]) + "\t" + str(WACount[ID]) + "\t" + str(TWCount[ID]) + "\n")
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
572
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
573 if __name__ == "__main__":
b6f9a640e098 Uploaded
davidvanzessen
parents:
diff changeset
574 main()