-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSNPDatabase.cs
More file actions
123 lines (105 loc) · 2.87 KB
/
SNPDatabase.cs
File metadata and controls
123 lines (105 loc) · 2.87 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
namespace Personal_Genome_Explorer
{
/** Encapsulates the global SNP database. */
public class SNPDatabaseManager
{
public static SNPDatabase localDatabase = InternalLoadDatabase();
private static SNPDatabase InternalLoadDatabase()
{
// Load the default database.
using(var defaultDatabaseStream = new MemoryStream(Properties.Resources.DefaultSNPDatabase,false))
{
SNPDatabase result = null;
if(SNPDatabase.Load(defaultDatabaseStream, ref result))
{
return result;
}
else
{
return new SNPDatabase();
}
}
}
public static void RevertDatabase()
{
localDatabase = InternalLoadDatabase();
}
public static bool ImportDatabase(Stream stream)
{
SNPDatabase newDatabase = null;
if(SNPDatabase.Load(stream,ref newDatabase))
{
localDatabase = newDatabase;
return true;
}
else
{
return false;
}
}
public static void ExportDatabase(Stream stream)
{
localDatabase.Save(stream);
}
};
public class SNPDatabase
{
public Dictionary<string, SNPInfo> snpToInfoMap = new Dictionary<string, SNPInfo>();
public List<TraitInfo> traits = new List<TraitInfo>();
private static char[] referenceFileMagic = new char[] { 'S', 'N', 'P', 'D', 'B', '1' };
public void Save(Stream stream)
{
var writer = new BinaryWriter(stream);
// Write the file magic and version.
writer.Write(referenceFileMagic);
// Write the SNP IDs and values to the file.
writer.Write((Int32)snpToInfoMap.Count);
foreach (var pair in snpToInfoMap)
{
writer.Write(pair.Key);
pair.Value.Write(writer);
}
// Write the traits to the file.
writer.Write((Int32)traits.Count);
foreach(var trait in traits)
{
trait.Write(writer);
}
}
public static bool Load(Stream stream, ref SNPDatabase outResult)
{
var reader = new BinaryReader(stream);
// Read the file magic and version.
char[] fileMagic = reader.ReadChars(referenceFileMagic.Length);
// If the file doesn't have the expected magic header, abort and return an error.
if (!Utilities.ArrayCompare(fileMagic, referenceFileMagic))
{
return false;
}
// Create the SNP info database that's about to be loaded.
outResult = new SNPDatabase();
// Read the SNPs in the database.
int numSNPs = reader.ReadInt32();
for(int snpIndex = 0;snpIndex < numSNPs;snpIndex++)
{
// Read a SNP ID and value pair, and add them to the database.
var Key = reader.ReadString();
var Value = SNPInfo.Read(reader);
outResult.snpToInfoMap.Add(Key, Value);
}
// Read the traits in the database.
int numTraits = reader.ReadInt32();
for(int traitIndex = 0;traitIndex < numTraits;traitIndex++)
{
outResult.traits.Add(TraitInfo.Read(reader));
}
return true;
}
}
}