-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping_func.c
63 lines (53 loc) · 1.78 KB
/
mapping_func.c
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
// By: Lisheng Zhou
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "mapping_func.h"
// This function will convert a number into a multi-locus genotype array (coded by 0, 1, 2 for each locus) according to the number of loci
// base 10 (number) to base 3 (vector)
// Genotype code:
// 0 - homozygous with 2 common alleles
// 1 - heterozygous with 1 common allele and 1 rare allele
// 2 - homozygous with 2 rare alleles
void map_in(int InNum, int NumLocus, int GenomArray[]){
// InNum - number you want to convert
// NumLocus - number of loci
// GenomArray[] - output array of the multi-locus genotype
int remain = 0;
int TempIn = InNum;
int TempGeno = 0;
for (int i = 0; i < NumLocus; i++){
if ( i != NumLocus-1){
remain = TempIn % (int)pow(3, (NumLocus-1-i));
TempGeno = (TempIn - remain)/(int)pow(3,(NumLocus-1-i));
TempIn = remain;
}else{
TempGeno=remain;
}
GenomArray[i] = TempGeno;
}
}
// This function will convert a multi-locus genotype array (coded by 0, 1, 2 for each locus) into a number according to the number of loci
// base 3 (vector) to base 10 (number)
int map_out(int GenomArray[], int NumLocus){
int sum = 0;
for (int i = 0; i < NumLocus; i++){
sum += GenomArray[i] * (int)pow(3,i);
}
return sum;
}
// Example
//int main(){
// int OutNum;
// int n = 3;
// int *p;
// p = (int*)malloc(sizeof(int)*n);
// p[0] = 3;
// p[1] = 3;
// p[2] = 3;
// OutNum = map_out(p, n);
// printf("%i\n", OutNum);
// free(p);
// return 0;
//}