-
Notifications
You must be signed in to change notification settings - Fork 0
/
DNACalcFrm.cs
342 lines (304 loc) · 11.4 KB
/
DNACalcFrm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DNA_Calculator
{
public partial class DNACalcFrm : Form
{
Dictionary<string, string[]> rsid = new Dictionary<string, string[]>();
long total_bp = 0;
string filename1 = null;
string filename2 = null;
long total_base_pairs = 0;
bool m_match_no_call = true;
long m_AllowedErrors=5;
long m_BasePairs = 5000000;
long m_GapToBreak=100000;
long m_SNPs=500;
long m_match_error_count=0;
public DNACalcFrm()
{
InitializeComponent();
}
private void DNACalcFrm_Load(object sender, EventArgs e)
{
}
public byte[] Zip(byte[] bytes)
{
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(mso, CompressionMode.Compress))
{
//msi.CopyTo(gs);
CopyTo(msi, gs);
}
return mso.ToArray();
}
}
public byte[] Unzip(byte[] bytes)
{
using (var msi = new MemoryStream(bytes))
using (var mso = new MemoryStream())
{
using (var gs = new GZipStream(msi, CompressionMode.Decompress))
{
//gs.CopyTo(mso);
CopyTo(gs, mso);
}
return mso.ToArray();
}
}
public void CopyTo(Stream src, Stream dest)
{
byte[] bytes = new byte[4096];
int cnt;
while ((cnt = src.Read(bytes, 0, bytes.Length)) != 0)
{
dest.Write(bytes, 0, cnt);
}
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog(this) == DialogResult.OK)
{
filename2 = dialog.FileName;
total_bp = 0;
neanPercent.Text = "0.00%";
neanPercent.ForeColor = Color.Gray;
statusLbl.Text = "Calculating ...";
button1.Enabled = false;
backgroundWorker2.RunWorkerAsync();
}
}
private string getAutosomalText(string file)
{
string text = null;
if (file.EndsWith(".gz"))
{
StringReader reader = new StringReader(Encoding.UTF8.GetString(Unzip(File.ReadAllBytes(file))));
text = reader.ReadToEnd();
reader.Close();
}
else if (file.EndsWith(".zip"))
{
using (var fs = new MemoryStream(File.ReadAllBytes(file)))
using (var zf = new ZipFile(fs))
{
var ze = zf[0];
if (ze == null)
{
throw new ArgumentException("file not found in Zip");
}
using (var s = zf.GetInputStream(ze))
{
using (StreamReader sr = new StreamReader(s))
{
text = sr.ReadToEnd();
}
}
}
}
else
text = File.ReadAllText(file);
return text;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("http://www.y-str.org/2014/12/dna-calculator.html");
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
StringReader reader = new StringReader(getAutosomalText(filename1));
string line = null;
string[] data = null;
long pos_start = 0;
long pos_end = 0;
string chr = null;
string pchr = null;
int i_chr = -1;
total_base_pairs = 0;
while((line=reader.ReadLine())!=null)
{
if (line.StartsWith("#") || line.StartsWith("RSID") || line.Trim().StartsWith("rsid"))
continue;
line = line.Replace("\"", "").Replace("\t", ",");
data = line.Split(new char[] { ',' });
if (data.Length == 5)
data[3] = data[3] + data[4];
chr = data[1];
if (!int.TryParse(chr, out i_chr))
continue;
if (i_chr > 22 || i_chr <= 0)
continue;
if (!rsid.ContainsKey(data[0]))
rsid.Add(data[0], data);
if (chr != pchr || long.Parse(data[2]) - pos_end >= m_GapToBreak)
{
total_base_pairs = total_base_pairs + (pos_end - pos_start);
pos_start = long.Parse(data[2]);
}
pos_end = long.Parse(data[2]);
pchr = chr;
}
reader.Close();
//
if(pchr==chr)
total_base_pairs = total_base_pairs + (pos_end - pos_start);
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Enabled = true;
statusLbl.Text = "Please select second file ...";
}
private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
StringReader reader = new StringReader(getAutosomalText(filename2));
string line = null;
string[] data = null;
long segment_start = 0;
long segment_end = 0;
string chr=null;
string pchr=null;
int i_chr=-1;
int snp_count = 0;
bool doub = true;
m_match_error_count = 0;
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("#") || line.StartsWith("RSID"))
continue;
line = line.Replace("\"", "").Replace("\t", ",");
data = line.Split(new char[] { ',' });
if (data.Length == 5)
data[3] = data[3] + data[4];
if(rsid.ContainsKey(data[0]))
{
chr=data[1];
if (!isDoubleMatch(rsid[data[0]][3], data[3]))
doub = false;
if (!int.TryParse(chr, out i_chr))
continue;
if (i_chr > 22 || i_chr <= 0)
continue;
else if (!isMatch(rsid[data[0]][3], data[3]) || chr != pchr)
{
if (segment_end - segment_start >= m_BasePairs && snp_count > m_SNPs) // 100000 bp
{
if (doub)
total_bp = total_bp + (segment_end - segment_start)*2;
else
total_bp = total_bp + (segment_end - segment_start);
}
doub = true;
segment_start = long.Parse(rsid[data[0]][2]);
snp_count = 0;
m_match_error_count = 0;
}
segment_end = long.Parse(rsid[data[0]][2]);
pchr=chr;
snp_count++;
}
}
reader.Close();
}
public static string ReverseString(string s)
{
char[] arr = s.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
private bool isDoubleMatch(string p1, string p2)
{
if (p1 == p2 || p1==ReverseString(p2))
return true;
return false;
}
private bool isMatch(string p1, string p2)
{
foreach (char c1 in p1.ToCharArray())
foreach (char c2 in p2.ToCharArray())
if (c1 == c2)
return true;
else if (m_match_no_call && (c1 == '-' || c2 == '-' || c1 == '?' || c2 == '?' || c1 == '0' || c2 == '0'))
return true;
m_match_error_count++;
if (m_match_error_count <= m_AllowedErrors)
return true;
return false;
}
private void backgroundWorker2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// because total_bp is HIR, we need to calcualate percentage for each allele.
double percent = total_bp * 100.0 / (total_base_pairs * 2);// excluding X chromosome and positions not tested by DNA companies and mtdna otherwise it is 3.2 billion bp
if (percent > 100)
percent = 100;
neanPercent.Text = percent.ToString("#0.00") + "%";
neanPercent.ForeColor = Color.Black;
statusLbl.Text = "";
//button1.Enabled = true;
//
tbAllowedErrors.Enabled = true;
tbBasePairs.Enabled = true;
tbGapToBreak.Enabled = true;
tbSNPs.Enabled = true;
cbMatchNoCalls.Enabled = true;
button3.Enabled = true;
button4.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
if (dialog.ShowDialog(this) == DialogResult.OK)
{
filename1 = dialog.FileName;
total_bp = 0;
rsid.Clear();
neanPercent.Text = "0.00%";
neanPercent.ForeColor = Color.Gray;
statusLbl.Text = "Loading ...";
button1.Enabled = false;
tbAllowedErrors.Enabled = false;
tbBasePairs.Enabled = false;
tbGapToBreak.Enabled = false;
tbSNPs.Enabled = false;
cbMatchNoCalls.Enabled = false;
button3.Enabled = false;
button4.Enabled = false;
long.TryParse(tbAllowedErrors.Text, out m_AllowedErrors);
long.TryParse(tbBasePairs.Text, out m_BasePairs);
long.TryParse(tbGapToBreak.Text, out m_GapToBreak);
long.TryParse(tbSNPs.Text, out m_SNPs);
m_match_no_call = cbMatchNoCalls.Checked;
backgroundWorker1.RunWorkerAsync();
}
}
private void button3_Click(object sender, EventArgs e)
{
tbAllowedErrors.Text = "1";
tbBasePairs.Text = "100000";
tbGapToBreak.Text = "100000";
tbSNPs.Text = "60";
cbMatchNoCalls.Checked = false;
}
private void button4_Click(object sender, EventArgs e)
{
tbAllowedErrors.Text = "5";
tbBasePairs.Text = "5000000";
tbGapToBreak.Text = "100000";
tbSNPs.Text = "500";
cbMatchNoCalls.Checked = true;
}
}
}