Example #1
0
        /// <summary>
        /// Public method mapping Reads to Contigs.
        /// </summary>
        /// <param name="contigs">List of sequences of contigs.</param>
        /// <param name="reads">List of input reads.</param>
        /// <param name="kmerLength">Length of kmer.</param>
        /// <returns>Contig Read Map.</returns>
        public ReadContigMap Map(IList <ISequence> contigs, IList <ISequence> reads, int kmerLength)
        {
            KmerIndexerDictionary map  = SequenceToKmerBuilder.BuildKmerDictionary(contigs, kmerLength);
            ReadContigMap         maps = new ReadContigMap();

            using (ThreadLocal <char[]> rcBuilder = new ThreadLocal <char[]>(() => new char[kmerLength]))
            {
                Parallel.ForEach(reads, (ISequence readSequence) =>
                {
                    IEnumerable <string> kmers = SequenceToKmerBuilder.GetKmerStrings(readSequence, kmerLength);
                    ReadIndex read             = new ReadIndex(readSequence);
                    IList <KmerIndexer> positions;
                    foreach (string kmer in kmers)
                    {
                        if (map.TryGetValue(kmer, out positions) ||
                            map.TryGetValue(kmer.GetReverseComplement(rcBuilder.Value), out positions))
                        {
                            read.ContigReadMatchIndexes.Add(positions);
                        }
                    }

                    IList <Task <IList <ReadMap> > > tasks =
                        new List <Task <IList <ReadMap> > >();

                    //Stores information about contigs for which tasks has been generated.
                    IList <int> visitedContigs = new List <int>();

                    //Creates Task for every read in nodes for a given contig.
                    for (int index = 0; index < read.ContigReadMatchIndexes.Count; index++)
                    {
                        int readPosition = index;
                        foreach (KmerIndexer kmer in read.ContigReadMatchIndexes[index])
                        {
                            int contigIndex = kmer.SequenceIndex;
                            if (!visitedContigs.Contains(contigIndex))
                            {
                                visitedContigs.Add(contigIndex);
                                tasks.Add(Task <IList <ReadMap> > .Factory.StartNew(t =>
                                                                                    MapRead(readPosition, read.ContigReadMatchIndexes, contigIndex, read.ReadSequence.Count, kmerLength),
                                                                                    TaskCreationOptions.AttachedToParent));
                            }
                        }
                    }

                    Dictionary <ISequence, IList <ReadMap> > overlapMaps =
                        new Dictionary <ISequence, IList <ReadMap> >();
                    for (int index = 0; index < visitedContigs.Count; index++)
                    {
                        overlapMaps.Add(contigs[visitedContigs[index]], tasks[index].Result);
                    }

                    lock (maps)
                    {
                        if (!maps.ContainsKey(read.ReadSequence.DisplayID))
                        {
                            maps.Add(read.ReadSequence.DisplayID, overlapMaps);
                        }
                        else
                        {
                            throw new ArgumentException(
                                string.Format(CultureInfo.CurrentCulture, Resource.DuplicatingReadIds, read.ReadSequence.DisplayID));
                        }
                    }
                });
            }

            return(maps);
        }
Example #2
0
        /// <summary>
        /// Aligns reads to contigs using kmer method of alignment.
        /// </summary>
        /// <param name="contigs">List of contig sequences.</param>
        /// <param name="reads">List of read sequences.</param>
        /// <param name="kmerLength">Kmer Length.</param>
        /// <returns>List of Contig</returns>
        public static IList <Contig> ReadContigAlignment(IList <ISequence> contigs, IList <ISequence> reads, int kmerLength)
        {
            KmerIndexerDictionary map = SequenceToKmerBuilder.BuildKmerDictionary(reads, kmerLength);
            IList <ContigIndex>   contigDatas;

            using (ThreadLocal <char[]> rcBuilder = new ThreadLocal <char[]>(() => new char[kmerLength]))
            {
                contigDatas = contigs.AsParallel().Select(contig =>
                {
                    IEnumerable <string> kmers = SequenceToKmerBuilder.GetKmerStrings(contig, kmerLength);
                    ContigIndex index          = new ContigIndex(contig);
                    IList <KmerIndexer> positions;
                    foreach (string kmer in kmers)
                    {
                        if (map.TryGetValue(kmer, out positions) ||
                            map.TryGetValue(kmer.GetReverseComplement(rcBuilder.Value), out positions))
                        {
                            index.ContigReadMatchIndexes.Add(positions);
                        }
                        else
                        {
                            index.ContigReadMatchIndexes.Add(new List <KmerIndexer>());
                        }
                    }

                    return(index);
                }).ToList();
            }

            return(contigDatas.Select(contigData =>
            {
                IList <Task <IList <ReadMap> > > tasks =
                    new List <Task <IList <ReadMap> > >();

                //Stores information about contigs for which tasks has been generated.
                IList <int> visitedReads = new List <int>();

                //Creates Task for every read in nodes for a given contig.
                for (int index = 0; index < contigData.ContigReadMatchIndexes.Count; index++)
                {
                    int readPosition = index;
                    foreach (KmerIndexer kmer in contigData.ContigReadMatchIndexes[index])
                    {
                        int contigIndex = kmer.SequenceIndex;
                        if (!visitedReads.Contains(contigIndex))
                        {
                            visitedReads.Add(contigIndex);
                            tasks.Add(Task <IList <ReadMap> > .Factory.StartNew(t =>
                                                                                MapRead(readPosition, contigData.ContigReadMatchIndexes, contigIndex, contigData.ContigSequence.Count, kmerLength),
                                                                                TaskCreationOptions.AttachedToParent));
                        }
                    }
                }

                Contig contigOutputStructure = new Contig();
                contigOutputStructure.Consensus = contigData.ContigSequence;

                for (int index = 0; index < visitedReads.Count; index++)
                {
                    foreach (ReadMap maps in tasks[index].Result)
                    {
                        Contig.AssembledSequence assembledSeq = new Contig.AssembledSequence()
                        {
                            Length = maps.Length,
                            Position = maps.StartPositionOfContig,
                            ReadPosition = maps.StartPositionOfRead,
                            Sequence = reads[visitedReads[index]]
                        };

                        if (contigOutputStructure.Consensus.Range(assembledSeq.Position, assembledSeq.Length).ToString().
                            Equals(assembledSeq.Sequence.Range(assembledSeq.ReadPosition, assembledSeq.Length).ToString()))
                        {
                            assembledSeq.IsComplemented = false;
                            assembledSeq.IsReversed = false;
                        }
                        else
                        {
                            assembledSeq.IsComplemented = true;
                            assembledSeq.IsReversed = true;
                        }

                        contigOutputStructure.Sequences.Add(assembledSeq);
                    }
                }

                return contigOutputStructure;
            }).ToList());
        }