Пример #1
0
        public int[] SeqParse(int rowIndex)
        {
            string seq = FetchSeq(rowIndex);

            int[] freq = new int[reg.Length];
            Multithreading.StartLoops(0, reg.Length,
                                      i => { freq[i] = reg[i].Matches(seq).Count; });
            return(freq);
        }
Пример #2
0
        public int[] SeqParse2(uint seqIdx, uint seqLen)
        {
            string seq = FetchSeq(seqIdx, seqLen);

            int[] freq = new int[reg.Length];
            if (seq == null)
            {
                return(freq);
            }
            Multithreading.StartLoops(0, reg.Length,
                                      i => { freq[i] = reg[i].Matches(seq).Count; });
            return(freq);
        }
Пример #3
0
        public uint[] KmerFrequency(byte[] seq, int k)
        {
            uint N    = (uint)(1 << (2 * k));
            uint mask = (uint)(N - 1);

            uint[] freq  = new uint[N];
            uint[] freq2 = new uint[freq.Length];
            uint[] tmp;
            uint   idx  = 0;
            uint   kLen = 0;

            for (uint i = 0; i < seq.Length; i++)
            {
                byte nb = seq[i];
                if (nb == 4)    // unknown nucliotide
                {
                    kLen = idx = 0;
                    continue;
                }
                idx = ((idx << 2) & mask) | (uint)(nb & 3);
                kLen++;
                if (kLen >= k)
                {
                    freq[idx]++;
                }
            }

            Multithreading.StartLoops(0, (int)N, delegate(int n) {
                // Flip the index n to n3.
                uint n2 = ((uint)n) ^ 0xFFFFFFFF; // A->T, G->C
                uint n3 = 0;                      // Reverse the bi-bits order
                for (int i = 0; i < k; i++)
                {
                    n3 <<= 2;
                    n3  |= (n2 & 0x3);
                    n2 >>= 2;
                }

                freq2[n] = freq[n] + freq[n3];
            });
            tmp = freq2; freq2 = freq; freq = tmp;


            if (!allowMismatch)
            {
                return(freq);
            }


            // including the single nb mutation.
            Multithreading.StartLoops(0, (int)N, delegate(int n) {
                uint sum = freq[n];
                for (int i = 0; i < 2 * k; i += 2)
                {
                    sum += freq[n ^ (1 << i)] + freq2[n ^ (2 << i)] + freq2[n ^ (3 << i)];
                }
                freq2[n] = sum;
            });

            // including the single nb deletions.
            tmp = freq2; freq2 = freq; freq = tmp;
            Multithreading.StartLoops(0, (int)N, delegate(int n) {
                uint sum = freq[n];
                for (int i = 0; i < k; i++)
                {
                    for (int nb = 0; nb < 4; nb++)
                    {
                        for (int leftFlank = 0; leftFlank < 2; leftFlank++)
                        {
                            int i2    = 2 * i;
                            int m     = 0;
                            int rMask = (1 << i2) - 1;  // mask for the section on the right side of the deleted nb.
                            if (leftFlank == 1)
                            {
                                // the (k-1) will be extend to k-mer by add nb to the left side.
                                int rightSec = n & rMask;
                                int leftSec  = (n >> 2) & ~rMask;
                                m            = (nb << (2 * k - 2)) | leftSec | rightSec;
                            }
                            else
                            {
                                // the (k-1) will be extend to k-mer by add nb to the right side.
                                int rightSec = (n & rMask) << 2;
                                int leftSec  = ((n >> (i2 + 2)) << (i2 + 2));
                                m            = leftSec | rightSec | nb;
                            }
                            sum += freq[m];
                        }
                    }
                }
                freq2[n] = sum;
            });

            return(freq2);
        }