aboutsummaryrefslogtreecommitdiffstats
path: root/SNPDict.sc
blob: 155dad7b26368c4f32ea39ec111720258db0baf2 (plain)
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
SNPDict{
	const <emptyBasePair = #[\none,\none];
	const <emptyBase = #[\none];
	var <snpDict;
	var <snpArr;
	var <positionLookup;
	var <unknownLookup;
	var <unknownArr;
	var <unknownToDelete;
	var <>length;
	var <positions = 0;
	var <unknowns = 0;
	var <userID;

	*new{
		arg aLength, aUserID = 0;
		^super.new.init(aLength, aUserID);
	}

	init{
		arg aLength, aUserID;
		userID = aUserID.asInt;
		length = aLength.asInt;
		snpDict = Dictionary.new(length);
		//snpArr = Array.new(length);
		positionLookup = Dictionary.new(length);
		unknownLookup = Dictionary.new(length);
		//unknownArr = Array.new(length);
	}
	
	updatePositionLookup{//sort the dictionary's position lookup table when updating a new position
		arg position, positionAdd;
		if(positionAdd,{//add or remove a position in the lookup table
			positionLookup.add(position.asFloat -> true);
		},{
			positionLookup.removeAt(position.asFloat);
		});
	}

	updateUnknownLookup{//sort the dictionary's unknown lookup table when updating a SNP with unknown resolver
		arg position, positionAdd;
		if(positionAdd,{
			unknownLookup.add(position.asFloat -> true);
		},{
			unknownLookup.removeAt(position.asFloat);
		});
	}

	orderLookup{//order the lookup tables (position and unknown) and store them to Arrays (for sequential access)
		arg table;
		switch(table,
			0,{
				snpArr = positionLookup.order;
				("Done sorting position lookup table. Positions: "++snpArr.size.asString).postln;
				^true;
			},
			1,{
				unknownArr = unknownLookup.order;
				("Done sorting unknown lookup table. Unknowns: "++snpArr.size.asString).postln;
				^true;
			},
			2,{
				snpArr = positionLookup.order;
				("Done sorting position lookup table. Positions: "++snpArr.size.asString).postln;
				unknownArr = unknownLookup.order;
				("Done sorting unknown lookup table. Unknowns: "++snpArr.size.asString).postln;
				unknownLookup = nil;
				positionLookup = nil;
				^true;
			},{
				("Unknown operation on sorting lookup tables: "++table.asString).postln;
				^false;
			}
		);
	}

	storeSNP{//store a combo to the snpDict and the lookup tables when reading from opensnp.org -file
		arg snp, position;
		var positionHolder, new = 0;//the SNP holder in snpDict
		if(snpDict.includesKey(position.asSymbol), {//if there is an entry already, add this one after it
			snpDict.at(position.asSymbol).add(snp.chromosome -> snp);
			new = -1;
		},{//else create the first one in this slot
			positionHolder = Dictionary.new(25);	
			positionHolder.add(snp.chromosome -> snp);
			snpDict.put(position.asSymbol, positionHolder);
			this.updatePositionLookup(position, true);
			new = 1;
			positions = positions+1;
		});
		if(snp.hasResolver.not,{//if the SNP has no resolver, update the unknown lookup table
			this.updateUnknownLookup(position, true);
		});
		^new;
	}

	storeSNPFromFile{//store a combo to the snpDict and the lookup tables when reading from own file
		arg snp, position;
		var positionHolder, new = 0;//the SNP holder in snpDict
		if(snpDict.includesKey(position.asSymbol), {//if there is an entry already, add this one after it
			snpDict.at(position.asSymbol).add(snp.chromosome -> snp);
		},{//else create the first one in this slot
			positionHolder = Dictionary.new(25);
			positionHolder.add(snp.chromosome -> snp);
			snpDict.put(position.asSymbol, positionHolder);
		});
	}
	
	initLookupFromFile{//init lookup table from file with size given
		arg posLookup, unLookup;
		snpArr = Array.new(posLookup);
		unknownArr = Array.new(unLookup);
	}

	addPositionLookupFromFile{//put a position to the position lookup table when reading from own file
		arg posArr, posSNP;
		snpArr.add(posSNP);
	}

	addUnknownLookupFromFile{//put a position to the position lookup table when reading from own file
		arg posArr, posSNP;
		unknownArr.add(posSNP);
	}
	
	updateResolver{//update a SNP resolver at a given position
		arg position, id, resolver;
		var resolverLeft;
		if(resolver!=SNPInfo.emptyBasePair && resolver!=SNPInfo.emptyBase,{//if the resolver is none, don't do anything
			snpDict.at(position.asSymbol).do({
				arg snpItem;
				if(id==snpItem.id, {
					snpItem.resolver_(resolver);
					("Resolver updated.").postln;
					^true;
				});
			});
//			resolverLeft = this.noneResolverAtPosition(position);
//			if(resolverLeft == false,{//if there are no unknowns left, delete position from lookup table
//				unknownArr.removeAt(unknownArr.indexOf(position.asFloat));
//			});
//			resolverLeft = nil;
		},{
			("Not updating position "++position++". Resolver is none.").postln;
			^false;
		});
	}

	deleteSNP{//delete a SNP at a given position
		arg snp, position;
		var snpCounter = 0, deleted = false;
		snpDict.at(position.asSymbol).do({
			arg snpItem, i;
//			if(snpItem.notNil, {
				snpCounter = snpCounter + 1;
				if(snp.id == snpItem.id{
					snpItem.removeAt(position.asSymbol);
					snpCounter = snpCounter - 1;
					deleted = true;
				});
//			});
		});
		if(snpCounter < 1,{//delete the complete position if there is no SNP left
			snpDict.removeAt(position.asSymbol);
			this.sortPositionLookup(-1, position);
			this.sortUnknownLookup(-1, position);
		});
		^deleted;
	}

	noneResolverAtPosition{//return a Dictionary of Chromosome -> SNP pairs that don't have a resolver or false if none found
		arg position;
		var noneResolver = Dictionary.new(25), noneResolverCount = 0;
		if(snpDict.includesKey(position.asSymbol),{//if the position exists, check it
			snpDict.at(position.asSymbol).keysValuesDo({
				arg chromosome, snp;
				if(snp.hasResolver.not,{
					noneResolver.add(chromosome -> snp);
					noneResolverCount = noneResolverCount + 1;
				});
			});
			if(noneResolverCount>0,{
				^noneResolver;
			},{
				^false;
			});
		},{
			^false;
		});
	}

	snpAtPosition{//return a Dictionary of chromosome -> SNP pairs at a given position, or false if none available
		arg position;
		if(snpDict.includesKey(position.asSymbol),{//check if the key is there
			^snpDict.at(position.asSymbol);//return the Dictionary at position
		},{
			^false;
		});
	}
	
	distanceToNextPosition{//calculate the distance from current position, to the one following
		arg position;
		var now; 
		if(position==0.0,{//if it's the beginning, return first position later on
			now = -1;
		},{
			now = snpArr.indexOf(position.asFloat);
		});
		switch(now,
			nil,{^position},
			snpArr.size-1,{^0},
			-1,{^snpArr[0]},
			{^snpArr[(snpArr.indexOf(position.asFloat)+1)]-position;}
		);
	}

	queryStatistics{//query statistics about this SNPDict
		//TODO: shortest/longest/mean distance between SNPs
		//TODO: unknown/total SNPs
		//TODO: number of SNPs per chromosome
		//TODO: unknown/total SNPs per chromosome
		//TODO: total positions
	}
}