Skip to content

Commit 92c0d40

Browse files
committed
Добавил два варианта простейших мотивов.
1 parent 7bcb68f commit 92c0d40

File tree

5 files changed

+593
-0
lines changed

5 files changed

+593
-0
lines changed
Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace MotifSeeker2.Helpers
6+
{
7+
/// <summary>
8+
/// Коллекция расширений над коллекциями.
9+
/// </summary>
10+
public static class LinqExt
11+
{
12+
#region firstWhere
13+
public static T FirstWhereMax<T>(this IEnumerable<T> lst, Func<T, int> f)
14+
{
15+
var data = default(T);
16+
var val = 0;
17+
var get = false;
18+
foreach (var item in lst)
19+
{
20+
var v = f(item);
21+
if (v <= val && get) continue;
22+
get = true;
23+
val = v;
24+
data = item;
25+
}
26+
if(!get)
27+
throw new Exception("Коллекция пуста");
28+
return data;
29+
}
30+
31+
public static T FirstWhereMaxOrDefault<T>(this IEnumerable<T> lst, Func<T, int> f)
32+
{
33+
var data = default(T);
34+
if (lst == null)
35+
return data;
36+
var val = 0;
37+
var get = false;
38+
foreach (var item in lst)
39+
{
40+
var v = f(item);
41+
if (v <= val && get) continue;
42+
get = true;
43+
val = v;
44+
data = item;
45+
}
46+
return data;
47+
}
48+
49+
public static T FirstWhereMax<T,TV>(this IEnumerable<T> lst, Func<T, TV> f) where TV: IComparable
50+
{
51+
var data = default(T);
52+
var val = default(TV);
53+
var get = false;
54+
foreach (var item in lst)
55+
{
56+
var v = f(item);
57+
if (v.CompareTo(val) <= 0 && get) continue;
58+
get = true;
59+
val = v;
60+
data = item;
61+
}
62+
if (!get)
63+
throw new Exception("Коллекция пуста");
64+
return data;
65+
}
66+
67+
public static T FirstWhereMaxOrDefault<T, TV>(this IEnumerable<T> lst, Func<T, TV> f) where TV : IComparable
68+
{
69+
var data = default(T);
70+
if (lst == null)
71+
return data;
72+
var val = default(TV);
73+
var get = false;
74+
foreach (var item in lst)
75+
{
76+
var v = f(item);
77+
if (v.CompareTo(val) <= 0 && get) continue;
78+
get = true;
79+
val = v;
80+
data = item;
81+
}
82+
return data;
83+
}
84+
85+
public static T FirstWhereMin<T>(this IEnumerable<T> lst, Func<T, int> f)
86+
{
87+
var data = default(T);
88+
var val = 0;
89+
var get = false;
90+
foreach (var item in lst)
91+
{
92+
var v = f(item);
93+
if (v >= val && get) continue;
94+
get = true;
95+
val = v;
96+
data = item;
97+
}
98+
if (!get)
99+
throw new Exception("Коллекция пуста");
100+
return data;
101+
}
102+
103+
public static T FirstWhereMinOrDefault<T>(this IEnumerable<T> lst, Func<T, int> f)
104+
{
105+
var data = default(T);
106+
if (lst == null)
107+
return data;
108+
var val = 0;
109+
var get = false;
110+
foreach (var item in lst)
111+
{
112+
var v = f(item);
113+
if (v >= val && get) continue;
114+
get = true;
115+
val = v;
116+
data = item;
117+
}
118+
return data;
119+
}
120+
121+
public static T FirstWhereMin<T,TV>(this IEnumerable<T> lst, Func<T, TV> f) where TV: IComparable
122+
{
123+
var data = default(T);
124+
var val = default(TV);
125+
var get = false;
126+
foreach (var item in lst)
127+
{
128+
var v = f(item);
129+
if (v.CompareTo(val) < 0 || !get)
130+
{
131+
get = true;
132+
val = v;
133+
data = item;
134+
}
135+
}
136+
if (!get)
137+
throw new Exception("Коллекция пуста");
138+
return data;
139+
}
140+
141+
public static T FirstWhereMinOrDefault<T, TV>(this IEnumerable<T> lst, Func<T, TV> f) where TV : IComparable
142+
{
143+
var data = default(T);
144+
if (lst == null)
145+
return data;
146+
var val = default(TV);
147+
var get = false;
148+
foreach (var item in lst)
149+
{
150+
var v = f(item);
151+
if (v.CompareTo(val) < 0 || !get)
152+
{
153+
get = true;
154+
val = v;
155+
data = item;
156+
}
157+
}
158+
return data;
159+
}
160+
#endregion firstWhere
161+
162+
public static int FirstIndexWhereMin<T, TV>(this T[] lst, Func<T, TV> f) where TV : IComparable
163+
{
164+
TV val = default(TV);
165+
bool get = false;
166+
int id = 0;
167+
for (int i = 0; i < lst.Length; i++)
168+
{
169+
var item = lst[i];
170+
var v = f(item);
171+
if (v.CompareTo(val) < 0 || !get)
172+
{
173+
get = true;
174+
val = v;
175+
id = i;
176+
}
177+
}
178+
if (!get)
179+
throw new Exception("Коллекция пуста");
180+
return id;
181+
}
182+
183+
#region enumerate
184+
public static IEnumerable<List<T>> EnumerateByPacks<T>(this IEnumerable<T> data, int packSize)
185+
{
186+
var pack = new List<T>(packSize);
187+
foreach(var t in data)
188+
{
189+
pack.Add(t);
190+
if(pack.Count == packSize)
191+
{
192+
yield return pack;
193+
pack = new List<T>(packSize);
194+
}
195+
}
196+
if (pack.Count != 0)
197+
yield return pack;
198+
}
199+
200+
/// <summary>
201+
/// Разбивает последовательность на пачки. Чётные содержат packSizeEven элементов, нечётные - packSizeOdd.
202+
/// Нулевая пачка - чётная. Размер последней пачки неопределён.
203+
/// </summary>
204+
public static IEnumerable<List<T>> EnumerateByPacks<T>(this IEnumerable<T> data, int packSizeEven, int packSizeOdd)
205+
{
206+
bool even = true;
207+
int curPackSize = packSizeEven;
208+
var pack = new List<T>(curPackSize);
209+
foreach (var t in data)
210+
{
211+
pack.Add(t);
212+
if (pack.Count == curPackSize)
213+
{
214+
yield return pack;
215+
even = !even;
216+
curPackSize = even ? packSizeEven : packSizeOdd;
217+
pack = new List<T>(curPackSize);
218+
}
219+
}
220+
if (pack.Count != 0)
221+
yield return pack;
222+
}
223+
224+
public static IEnumerable<T[]> EnumerateGroups<T>(this IEnumerable<IEnumerable<T>> flows)
225+
{
226+
var data = flows.Select(p => p.GetEnumerator()).ToList();
227+
while(true)
228+
{
229+
var ret = new T[data.Count];
230+
bool rem = false;
231+
for(int i=0;i<ret.Length;i++)
232+
{
233+
if(data[i] == null)
234+
continue;
235+
if (data[i].MoveNext())
236+
{
237+
ret[i] = data[i].Current;
238+
}
239+
else
240+
{
241+
data[i] = null;
242+
rem = true;
243+
}
244+
}
245+
if (rem)
246+
data.RemoveAll(p => p == null);
247+
if(data.Count == 0)
248+
yield break;
249+
yield return ret;
250+
}
251+
}
252+
#endregion enumerate
253+
254+
/// <summary>
255+
/// Расщепляет поток на два списка.
256+
/// Увы, ленивым его не сделать.
257+
/// [ToDo] реализовать ленивый вариант. :)
258+
/// </summary>
259+
/// <typeparam name="T"></typeparam>
260+
/// <param name="flow">Конечный поток данных.</param>
261+
/// <param name="pred">Условие. Если верно, то в первый список. Если нет - то во второй.</param>
262+
public static Tuple<List<T>, List<T>> Split<T>(this IEnumerable<T> flow, Predicate<T> pred)
263+
{
264+
var tmp1 = new List<T>();
265+
var tmp2 = new List<T>();
266+
foreach(var f in flow)
267+
if(pred(f))
268+
tmp1.Add(f);
269+
else
270+
tmp2.Add(f);
271+
272+
return new Tuple<List<T>, List<T>>(tmp1, tmp2);
273+
}
274+
275+
#region deNullable
276+
public static IEnumerable<T> DeNullabe<T>(this IEnumerable<T?> flow) where T: struct
277+
{
278+
if (flow == null)
279+
yield break;
280+
foreach (var t in flow)
281+
if (t.HasValue)
282+
yield return t.Value;
283+
}
284+
285+
public static IEnumerable<TV> DeNullabe<T,TV>(this IEnumerable<T> flow, Func<T, TV?> f) where TV : struct
286+
{
287+
if (flow == null)
288+
yield break;
289+
foreach (var t in flow)
290+
{
291+
var v = f(t);
292+
if (v.HasValue)
293+
yield return v.Value;
294+
}
295+
}
296+
#endregion deNullable
297+
298+
public static void ForEach<T>(this IEnumerable<T> c, Action<T> action)
299+
{
300+
if (c == null || action == null)
301+
return;
302+
303+
foreach (T obj in c)
304+
action(obj);
305+
}
306+
307+
public static IEnumerable<T> Prepare<T>(this IEnumerable<T> c, Action<T> action)
308+
{
309+
if (c == null)
310+
yield break;
311+
312+
foreach (T obj in c)
313+
{
314+
if (action != null)
315+
action(obj);
316+
317+
yield return obj;
318+
}
319+
}
320+
321+
public static TValue TryGetValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
322+
{
323+
TValue value;
324+
return dictionary.TryGetValue(key, out value) ? value : default(TValue);
325+
}
326+
327+
public static Queue<T> ToQueue<T>(this IEnumerable<T> flow)
328+
{
329+
var ret = new Queue<T>();
330+
flow.ForEach(ret.Enqueue);
331+
return ret;
332+
}
333+
334+
public static Stack<T> ToStack<T>(this IEnumerable<T> flow)
335+
{
336+
return new Stack<T>(flow);
337+
}
338+
339+
//public static SortedQueue<TK,TV> ToSortedQueue<TK,TV>(this IEnumerable<TV> flow, Func<TV,TK> f)
340+
//{
341+
// var ret = new SortedQueue<TK, TV>(f);
342+
// flow.ForEach(ret.EnQueue);
343+
// return ret;
344+
//}
345+
346+
public static double Prod(this IEnumerable<double> flow)
347+
{
348+
return flow.Aggregate(1.0, (current, f) => current * f);
349+
}
350+
}
351+
}

Sources/MotifSeeker2/Motif/IMotif.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using MotifSeeker2.Dna;
2+
3+
namespace MotifSeeker2.Motif
4+
{
5+
/// <summary>
6+
/// Интерфейс мотива.
7+
/// </summary>
8+
public interface IMotif
9+
{
10+
/// <summary>
11+
/// Длина мотива. Минимальное количество нуклеотид, необходимых для работы мотива.
12+
/// </summary>
13+
int Length { get; }
14+
15+
/// <summary>
16+
/// Получение оценки комплиментарности мотива к заданной последовательности.
17+
/// Длина последовательности должна быть равна Length мотива.
18+
/// </summary>
19+
float GetScore(Nucleotide[] nucs);
20+
}
21+
}

0 commit comments

Comments
 (0)