annotate shm_csr.py @ 91:f387cc1580c6 draft

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