コード例 #1
0
ファイル: BoyerMoore.cs プロジェクト: cpatmoore/bio
        /// <summary>
        /// Find the matches for given searchStrings in sequence and returns
        /// the matched strings with indices found at.
        /// </summary>
        /// <param name="sequence">Input sequence.</param>
        /// <param name="searchPatterns">Strings to be searched.</param>
        /// <returns>Matches found in sequence.</returns>
        public IDictionary<string, IList<int>> FindMatch(ISequence sequence, IList<string> searchPatterns)
        {
            if (sequence == null)
            {
                throw new ArgumentNullException("sequence");
            }

            if (searchPatterns == null)
            {
                throw new ArgumentNullException("searchPatterns");
            }

            // Create tasks
            IList<Task<KeyValuePair<string, IList<int>>>> tasks = searchPatterns.Select(
                    searchString => Task<KeyValuePair<string, IList<int>>>.Factory.StartNew(
                            t => new KeyValuePair<string, IList<int>>(searchString, FindMatch(sequence, searchString)),
                            TaskCreationOptions.None)).ToList();

            // Wait for all the task
            Task.WaitAll(tasks.ToArray());

            IDictionary<string, IList<int>> results = new Dictionary<string, IList<int>>();
            foreach (Task<KeyValuePair<string, IList<int>>> task in tasks)
            {
                results.Add(task.Result.Key, task.Result.Value);
            }

            return results;
        }
コード例 #2
0
        /// <summary>
        /// <see cref="IWaitStrategy.WaitFor"/>
        /// </summary>
        public long WaitFor(long sequence, Sequence cursor, ISequence dependentSequence, ISequenceBarrier barrier)
        {
            long startTime = 0;
            int counter = _spinTries;

            do
            {
                long availableSequence;
                if ((availableSequence = dependentSequence.Value) >= sequence)
                    return availableSequence;

                if (0 == --counter)
                {
                    if (0 == startTime)
                    {
                        startTime = GetSystemTimeTicks();
                    }
                    else
                    {
                        var timeDelta = GetSystemTimeTicks() - startTime;
                        if (timeDelta > _yieldTimeoutTicks)
                        {
                            return _fallbackStrategy.WaitFor(sequence, cursor, dependentSequence, barrier);
                        }

                        if (timeDelta > _spinTimeoutTicks)
                        {
                            Thread.Yield();
                        }
                    }
                    counter = _spinTries;
                }
            }
            while (true);
        }
コード例 #3
0
        /// <summary>
        /// <see cref="IWaitStrategy.WaitFor"/>
        /// </summary>
        public long WaitFor(long sequence, Sequence cursor, ISequence dependentSequence, ISequenceBarrier barrier)
        {
            var timeSpan = _timeout;
            if (cursor.Value < sequence)
            {
                lock (_gate)
                {
                    while (cursor.Value < sequence)
                    {
                        barrier.CheckAlert();
                        if (!Monitor.Wait(_gate, timeSpan))
                        {
                            throw TimeoutException.Instance;
                        }
                    }
                }
            }

            long availableSequence;
            while ((availableSequence = dependentSequence.Value) < sequence)
            {
                barrier.CheckAlert();
            }

            return availableSequence;
        }
コード例 #4
0
ファイル: DeltaAlignment.cs プロジェクト: cpatmoore/bio
 ///<summary>
 /// Initializes a new instance of the DeltaAlignment class
 /// </summary>
 /// <param name="referenceSequence">Reference Sequence</param>
 /// <param name="querySequence">Query Sequence</param>
 public DeltaAlignment(ISequence referenceSequence, ISequence querySequence)
 {
     this.internalDeltas = new List<long>();
     ReferenceSequence = referenceSequence;
     QuerySequence = querySequence;
     QueryDirection = Cluster.ForwardDirection;
 }
コード例 #5
0
 private void _UseSequenceData(ISequence sequence)
 {
     if (!_persistPreFilterCache) {
         _intentPreFilter.ClearCache();
     }
     _intentPreFilter.Data = sequence.SequenceData.EffectData;
 }
コード例 #6
0
 public SequenceStartedEventArgs(ISequence sequence, ITiming timingSource, TimeSpan startTime, TimeSpan endTime)
 {
     Sequence = sequence;
     TimingSource = timingSource;
     StartTime = startTime;
     EndTime = endTime;
 }
コード例 #7
0
 public virtual RelationalTypeMapping GetTypeMapping(ISequence sequence)
     => GetTypeMapping(
         /*specifiedType:*/ null,
         sequence.Name,
         sequence.Type,
         isKey: false,
         isConcurrencyToken: false);
コード例 #8
0
        /// <summary>
        /// <see cref="IWaitStrategy.WaitFor"/>.
        /// </summary>
        public long WaitFor(long sequence, Sequence cursor, ISequence dependentSequence, ISequenceBarrier barrier)
        {
            var milliseconds = _timeoutInMilliseconds;

            long availableSequence;
            if (cursor.Value < sequence)
            {
                lock (_lock)
                {
                    while (cursor.Value < sequence)
                    {
                        Interlocked.Exchange(ref _signalNeeded, 1);

                        barrier.CheckAlert();

                         if (!Monitor.Wait(_lock, milliseconds))
                        {
                            throw TimeoutException.Instance;
                        }
                    }
                }
            }

            while ((availableSequence = dependentSequence.Value) < sequence)
            {
                barrier.CheckAlert();
            }

            return availableSequence;
        }
コード例 #9
0
        /// <summary>
        /// For input sequence, constructs k-mers by sliding 
        /// a frame of size kmerLength along the input sequence.
        /// Track positions of occurance for each kmer in sequence.
        /// Constructs KmersOfSequence for sequence and associated k-mers.
        /// </summary>
        /// <param name="sequence">Input sequence.</param>
        /// <param name="kmerLength">K-mer length.</param>
        /// <returns>KmersOfSequence constructed from sequence and associated k-mers.</returns>
        public static KmerPositionDictionary BuildKmerDictionary(ISequence sequence, int kmerLength)
        {
            if (sequence == null)
            {
                throw new ArgumentNullException("sequence");
            }

            if (kmerLength > sequence.Count)
            {
                throw new ArgumentException(Properties.Resource.KmerLengthIsTooLong);
            }

            // kmers maintains the map between k-mer strings to list of positions in sequence.
            KmerPositionDictionary kmers = new KmerPositionDictionary();

            // Sequence 'kmer' stores the k-mer in each window.
            // Construct each k-mer using range from sequence.
            for (long i = 0; i <= sequence.Count - kmerLength; ++i)
            {
                ISequence kmerString = sequence.GetSubSequence(i, kmerLength);
                if (kmers.ContainsKey(kmerString))
                {
                    kmers[kmerString].Add(i);
                }
                else
                {
                    kmers[kmerString] = new List<long>() { i };
                }
            }

            return kmers;
        }
コード例 #10
0
ファイル: Form1.cs プロジェクト: zdyne/gazebo
        public Form1()
        {
            this.displayModel = new DisplayModel(Form1.NLeds);
            this.sequence = new PowerOnStartupTest();

            InitializeComponent();
        }
コード例 #11
0
ファイル: Construct.cs プロジェクト: MelaniaNowicka/MUFASA
 /// <summary>
 /// Pre-optimized Construct constructor.
 /// </summary>
 /// <param name="fragList">Fragment list.</param>
 public Construct(List<Overlap> overlaps, ISequence sequence, DesignerSettings settings)
     : base()
 {
     this.Overlaps = overlaps;
     this.Sequence = sequence;
     this.Settings = settings;
 }
コード例 #12
0
ファイル: ExportDialog.cs プロジェクト: naztrain/vixen
        public ExportDialog(ISequence sequence)
        {
            InitializeComponent();

            ForeColor = ThemeColorTable.ForeColor;
            BackColor = ThemeColorTable.BackgroundColor;
            ThemeUpdateControls.UpdateControls(this);
            Icon = Resources.Icon_Vixen3;

            _sequence = sequence;
            _exportOps = new Export();
            _exportOps.SequenceNotify += SequenceNotify;

            _sequenceFileName = _sequence.FilePath;

            IEnumerable<string> mediaFileNames =
                (from media in _sequence.SequenceData.Media
                 where media.GetType().ToString().Contains("Audio")
                 where media.MediaFilePath.Length != 0
                 select media.MediaFilePath);

            _audioFileName = "";
            if (mediaFileNames.Count() > 0)
            {
                _audioFileName = mediaFileNames.First();
            }

            exportProgressBar.Visible = false;
            currentTimeLabel.Visible = false;

            _cancelled = false;

            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }
コード例 #13
0
ファイル: Transcription.cs プロジェクト: cpatmoore/bio
        /// <summary>
        /// Transcribes a DNA sequence into an RNA sequence. The length
        /// of the resulting sequence will equal the length of the source
        /// sequence. Gap and ambiguous characters will also be transcribed.
        /// For example:
        /// Sequence dna = new Sequence(Alphabets.DNA, "TACCGC");
        /// Sequence rna = Transcription.Transcribe(dna);
        /// rna.ToString() would produce "AUGGCG"
        /// </summary>
        /// <param name="dnaSource">The dna source sequence to be transcribed.</param>
        /// <returns>The transcribed RNA sequence.</returns>
        public static ISequence Transcribe(ISequence dnaSource)
        {
            if (dnaSource == null)
            {
                throw new ArgumentNullException("dnaSource");
            }

            if (dnaSource.Alphabet != Alphabets.DNA && dnaSource.Alphabet != Alphabets.AmbiguousDNA)
            {
                throw new InvalidOperationException(Properties.Resource.InvalidAlphabetType);
            }

            byte[] transcribedResult = new byte[dnaSource.Count];
            long counter = 0;

            foreach (byte n in dnaSource)
            {
                transcribedResult[counter] = GetRnaComplement(n);
                counter++;
            }

            var alphabet = dnaSource.Alphabet == Alphabets.DNA ? Alphabets.RNA : Alphabets.AmbiguousRNA;
            Sequence result = new Sequence(alphabet, transcribedResult);
            result.ID = "Complement: " + dnaSource.ID;

            return result;
        }
コード例 #14
0
        public static bool RemoveSequence(ref ISequence[] sequences, ISequence sequence)
        {
            int numToRemove;
            ISequence[] oldSequences;
            ISequence[] newSequences;

            do
            {
                oldSequences = Volatile.Read(ref sequences);

                numToRemove = CountMatching(oldSequences, sequence);

                if (numToRemove == 0)
                    break;

                var oldSize = oldSequences.Length;
                newSequences = new ISequence[oldSize - numToRemove];

                for (int i = 0, pos = 0; i < oldSize; i++)
                {
                    var testSequence = oldSequences[i];
                    if (sequence != testSequence)
                    {
                        newSequences[pos++] = testSequence;
                    }
                }
            }
            while (Interlocked.CompareExchange(ref sequences, newSequences, oldSequences) != oldSequences);

            return numToRemove != 0;
        }
コード例 #15
0
        public static void AddSequences(ref ISequence[] sequences, ICursored cursor, params ISequence[] sequencesToAdd)
        {
            long cursorSequence;
            ISequence[] updatedSequences;
            ISequence[] currentSequences;

            do
            {
                currentSequences = Volatile.Read(ref sequences);
                updatedSequences = new ISequence[currentSequences.Length + sequencesToAdd.Length];
                Array.Copy(currentSequences, updatedSequences, currentSequences.Length);
                cursorSequence = cursor.Cursor;

                var index = currentSequences.Length;
                foreach (var sequence in sequencesToAdd)
                {
                    sequence.SetValue(cursorSequence);
                    updatedSequences[index++] = sequence;
                }
            }
            while (Interlocked.CompareExchange(ref sequences, updatedSequences, currentSequences) != currentSequences);

            cursorSequence = cursor.Cursor;
            foreach (var sequence in sequencesToAdd)
            {
                sequence.SetValue(cursorSequence);
            }
        }
コード例 #16
0
ファイル: ExcelFormatter.cs プロジェクト: cpatmoore/bio
 /// <summary>
 /// Convert fasta sequence id to a string array
 /// </summary>
 public static string[,] SequenceIDHeaderToRange(ISequence sequence)
 {
     var formattedData = new string[1,2];
     formattedData[0, 0] = Properties.Resources.Sequence_ID;
     formattedData[0, 1] = (sequence != null) ? sequence.ID : "";
     return formattedData;
 }
コード例 #17
0
ファイル: Fragment.cs プロジェクト: MelaniaNowicka/MUFASA
 /// <summary>
 /// Fragment constructor.
 /// </summary>
 /// <param name="source">Filename or URL.</param>
 /// <param name="name">Fragment name.</param>
 public Fragment(String source, String name, ISequence sequence, bool vector = false)
 {
     this.Source = source;
     this.Name = name;
     this.Sequence = sequence;
     this.Length = sequence.Count;
     this.IsVector = vector;
 }
コード例 #18
0
        /// <summary>
        /// Method to align two sequences, this sample code uses NeedlemanWunschAligner.
        /// </summary>
        /// <param name="referenceSequence">Reference sequence for alignment</param>
        /// <param name="querySequence">Query sequence for alignment</param>
        /// <returns>List of IPairwiseSequenceAlignment</returns>
        public static IList<IPairwiseSequenceAlignment> AlignSequences(ISequence referenceSequence, ISequence querySequence)
        {
            // Initialize the Aligner
            NeedlemanWunschAligner aligner = new NeedlemanWunschAligner();

            // Calling align method to do the alignment.
            return aligner.Align(referenceSequence, querySequence);
        }
コード例 #19
0
ファイル: SequencePersistor.cs プロジェクト: stewmc/vixen
        public void SaveToFile(ISequence obj, string filePath)
        {
            IFileWriter fileWriter = FileWriterFactory.CreateFileWriter();
            IObjectContentReader contentReader = ObjectContentReaderFactory.Instance.CreateSequenceContentReader(filePath);
            ObjectPersistorService.Instance.SaveToFile(obj, fileWriter, contentReader, filePath);

            if (obj != null) obj.FilePath = filePath;
        }
コード例 #20
0
 private EnumeratorSequence(IEnumerator enumerator, object first, ISequence rest, IPersistentMap metadata)
     : base(metadata)
 {
     this.enumerator = enumerator;
     this.first = first;
     this.rest = rest;
     this.restWasCalculated = true;
 }
コード例 #21
0
ファイル: SequenceService.cs プロジェクト: Jchuchla/vixen
        public void Save(ISequence sequence, string filePath)
        {
            if (sequence == null) throw new ArgumentNullException("sequence");
            if (string.IsNullOrWhiteSpace(filePath))
                throw new InvalidOperationException("File path must be a file name or path.");

            FileService.Instance.SaveSequenceFile(sequence, filePath);
        }
コード例 #22
0
ファイル: Synteny.cs プロジェクト: cpatmoore/bio
 /// <summary>
 /// Initializes a new instance of the Synteny class
 /// </summary>
 /// <param name="referenceSequence">Reference sequence</param>
 /// <param name="querySequence">Query sequence</param>
 public Synteny(
         ISequence referenceSequence,
         ISequence querySequence)
 {
     internalReferenceSequence = referenceSequence;
     internalQuerySequence = querySequence;
     internalClusters = new List<Cluster>();
 }
コード例 #23
0
ファイル: Util.cs プロジェクト: disruptor-net/Disruptor-net
 /// <summary>
 /// Get the minimum sequence from an array of <see cref="Sequence"/>s.
 /// </summary>
 /// <param name="sequences">sequences to compare.</param>
 /// <param name="minimum">an initial default minimum.  If the array is empty this value will returned.</param>
 /// <returns>the minimum sequence found or lon.MaxValue if the array is empty.</returns>
 public static long GetMinimumSequence(ISequence[] sequences, long minimum = long.MaxValue)
 {
     for (var i = 0; i < sequences.Length; i++)
     {
         var sequence = sequences[i].Value;
         minimum = Math.Min(minimum, sequence);
     }
     return minimum;
 }
コード例 #24
0
        public static float PairWiseScoreFunction(ISequence sequenceA, ISequence sequenceB, SimilarityMatrix similarityMatrix,
                                                int gapOpenPenalty, int gapExtensionPenalty)
        {
            if (sequenceA.Count != sequenceB.Count)
            {
                throw new Exception("Unaligned sequences");
            }
            float result = 0;

            bool isGapA = false;
            bool isGapB = false;

            for (int i = 0; i < sequenceA.Count; ++i)
            {
                if (sequenceA.Alphabet.CheckIsGap(sequenceA[i]) && sequenceB.Alphabet.CheckIsGap(sequenceB[i]))
                {
                    continue;
                }
                if (sequenceA.Alphabet.CheckIsGap(sequenceA[i]) && !sequenceB.Alphabet.CheckIsGap(sequenceB[i]))
                {
                    if (isGapB)
                    {
                        isGapB = false;
                    }
                    if (isGapA)
                    {
                        result += gapExtensionPenalty;
                    }
                    else
                    {
                        result += gapOpenPenalty;
                        isGapA = true;
                    }
                    continue;
                }
                if (!sequenceA.Alphabet.CheckIsGap(sequenceA[i]) && sequenceB.Alphabet.CheckIsGap(sequenceB[i]))
                {
                    if (isGapA)
                    {
                        isGapA = false;
                    }
                    if (isGapB)
                    {
                        result += gapExtensionPenalty;
                    }
                    else
                    {
                        result += gapOpenPenalty;
                        isGapB = true;
                    }
                    continue;
                }

                result += similarityMatrix[sequenceA[i], sequenceB[i]];
            }
            return result;
        }
コード例 #25
0
ファイル: MediaTimingProvider.cs プロジェクト: Jchuchla/vixen
 public ITiming GetTimingSource(ISequence sequence, string sourceName)
 {
     IMediaModuleInstance mediaModule =
         sequence.GetAllMedia().FirstOrDefault(x => x.TimingSource != null && x.MediaFilePath == sourceName);
     if (mediaModule != null) {
         return mediaModule.TimingSource;
     }
     return null;
 }
コード例 #26
0
ファイル: StringSequence.cs プロジェクト: Altaxo/Altaxo
		public bool Equals(int i, ISequence other, int j)
		{
			StringSequence seq = other as StringSequence;
			
			if (seq == null)
				return false;
			
			return content[i] == seq.content[j];
		}
コード例 #27
0
		public bool Equals(int thisIdx, ISequence other, int otherIdx)
		{
			DocumentSequence seq = other as DocumentSequence;
			
			if (seq == null)
				return false;
			
			return hashes[thisIdx] == seq.hashes[otherIdx];
		}
コード例 #28
0
ファイル: Mismatcher.cs プロジェクト: krendil/mismatcher
 /// <summary>
 /// Converts a portion of an ISequence to a string. Intended to match the behaviour of Sequence.ConvertToString.
 /// </summary>
 /// <returns>A string </returns>
 /// <param name="sequence">Sequence.</param>
 /// <param name="startIndex">Start index.</param>
 /// <param name="length">Length.</param>
 private static string ConvertToString(ISequence sequence, long startIndex, long length)
 {
     StringBuilder sb = new StringBuilder((int)length);
     for (long i = startIndex; i < startIndex + length; i++)
     {
         sb.Append(sequence[i]);
     }
     return sb.ToString();
 }
コード例 #29
0
ファイル: Codons.cs プロジェクト: cpatmoore/bio
        /// <summary>
        /// Lookup an amino acid within a sequence starting a certain offset.
        /// </summary>
        /// <param name="sequence">The source sequence to lookup from.</param>
        /// <param name="offset">
        /// The offset within the sequence from which to look at the next three
        /// nucleotides. Note that this offset begins its count at zero. Thus
        /// looking at a sequence described by "AUGGCG" and using a offset of 0
        /// would lookup the amino acid for codon "AUG" while using a offset of 1
        /// would lookup the amino acid for codon "UGG".
        /// </param>
        /// <returns>An amino acid from the protein alphabet</returns>
        public static byte Lookup(ISequence sequence, int offset)
        {
            byte value;
            if (TryLookup(sequence, offset, out value))
                return value;

            throw new InvalidOperationException(
                string.Format(null, "({0},{1},{2}) not found.", (char)sequence[offset], (char)sequence[offset + 1], (char)sequence[offset + 2]));
        }
コード例 #30
0
 public ITiming GetTimingSource(ISequence sequence, string sourceName)
 {
     IModuleDescriptor moduleDescriptor =
         Modules.GetDescriptors<ITimingModuleInstance>().FirstOrDefault(x => x.TypeName == sourceName);
     if (moduleDescriptor != null) {
         return Modules.ModuleManagement.GetTiming(moduleDescriptor.TypeId);
     }
     return null;
 }
コード例 #31
0
    public void SplitterTestMultiThread()
    {
        var context = TestExecuter.GetContext();

        var splitter = new Splitter <DefaultRowQueue>(context)
        {
            InputProcess = new EnumerableImporter(context)
            {
                InputGenerator = caller => TestData.Person(context).TakeRowsAndReleaseOwnership(caller),
            },
        };

        var processes = new ISequence[4];

        for (var i = 0; i < 3; i++)
        {
            processes[i] = new CustomMutator(context)
            {
                Input  = splitter,
                Action = row =>
                {
                    Thread.Sleep(new Random().Next(10));
                    row["ThreadIndex"] = i;
                    return(true);
                },
            };
        }

        var threads = new List <Thread>();
        var results = new List <ISlimRow> [3];

        for (var i = 0; i < 3; i++)
        {
            var threadIndex = i;
            var thread      = new Thread(() =>
            {
                var rows             = processes[threadIndex].TakeRowsAndReleaseOwnership(null);
                results[threadIndex] = new List <ISlimRow>(rows);
            });

            thread.Start();
            threads.Add(thread);
        }

        for (var i = 0; i < 3; i++)
        {
            threads[i].Join();
        }

        Assert.AreEqual(7, results[0].Count + results[1].Count + results[2].Count);
        foreach (var p in TestData.Person(context).TakeRowsAndReleaseOwnership(null))
        {
            Assert.IsTrue(
                results[0].Any(m => m.GetAs <int>("id") == p.GetAs <int>("id")) ||
                results[1].Any(m => m.GetAs <int>("id") == p.GetAs <int>("id")) ||
                results[2].Any(m => m.GetAs <int>("id") == p.GetAs <int>("id")));
        }

        var exceptions = context.GetExceptions();

        Assert.AreEqual(0, exceptions.Count);
    }
コード例 #32
0
        /// <summary>
        /// Calculates all measures needed for plotting a sequence within the given space constraints
        /// Adds sequence items (TextBlocks) to this seuqnnce line depending on the available width
        /// </summary>
        /// <param name="constraint"></param>
        /// <returns></returns>
        protected override Size MeasureOverride(Size constraint)
        {
            long sequencePlotStartColumn = AlignStartIndex - ReadAlignStartIndex + BasePaddingLeft;
            long sequencePlotEndColumn   = sequencePlotStartColumn + sequence.Count;

            if (!(sequencePlotStartColumn < DisplayEndIndex && sequencePlotEndColumn + 1 > DisplayStartIndex))
            {
                return(new Size(0, containerGrid.RowDefinitions[0].Height.Value + containerGrid.RowDefinitions[1].Height.Value));
            }

            long   startCharIndex = 0, endCharIndex;
            double leftMargin = 0;

            sequenceItemsPanel.Children.Clear();
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                sequence = new Sequence(Alphabets.DNA, "ACGT");
            }

            if (DisplayStartIndex > sequencePlotStartColumn)
            {
                startCharIndex = DisplayStartIndex - sequencePlotStartColumn;
                endCharIndex   = DisplayEndIndex - DisplayStartIndex + startCharIndex;

                // Keep metadata text left aligned with the sequence start point.
                metadataBlock.Margin = new Thickness((startCharIndex * CharWidth * -1), 0, 0, 0);
            }
            else
            {
                endCharIndex = DisplayEndIndex - DisplayStartIndex - (sequencePlotStartColumn - DisplayStartIndex);
                leftMargin   = (sequencePlotStartColumn - DisplayStartIndex) * CharWidth;
            }

            if (endCharIndex > sequence.Count)
            {
                endCharIndex = sequence.Count;
            }

            this.Margin = new Thickness(leftMargin, 0, 0, 0);
            sequenceItemsBorder.Width = (endCharIndex - startCharIndex) * CharWidth;

            // Color code sequences if complemented , reversed or reverse complemented
            if (IsComplemented && IsReversed)
            {
                sequenceItemsBorder.BorderBrush = Brushes.Crimson;
            }
            else if (IsComplemented)
            {
                sequenceItemsBorder.BorderBrush = Brushes.LightSeaGreen;
            }
            else if (IsReversed)
            {
                sequenceItemsBorder.BorderBrush = Brushes.BurlyWood;
            }

            // If font size is less than 4, skip this so that the sequences will appear as a line.
            // Any font size below 4 is not readable so no point in displaying it.
            if (FontSize > 4)
            {
                TextBlock block = null;
                for (long pos = startCharIndex; pos < endCharIndex; pos++)
                {
                    block = new TextBlock();

                    if (pos < ReadAlignStartIndex || pos >= ReadAlignStartIndex + AlignmentLength)
                    {
                        string symbol = new string(new char[] { (char)sequence[pos] });
                        block.Text    = symbol.ToLower();
                        block.Opacity = .6;
                    }
                    else
                    {
                        string symbol = new string(new char[] { (char)sequence[pos] });
                        block.Text = symbol.ToString();
                    }

                    block.Width             = CharWidth;
                    block.Height            = CharHeight;
                    block.TextAlignment     = TextAlignment.Center;
                    block.UseLayoutRounding = true;

                    Brush colorBrush;

                    if (ColorMap != null && ColorMap.TryGetValue(char.ToUpper((char)sequence[pos]), out colorBrush))
                    {
                        block.Background = colorBrush;
                    }
                    else if (ColorMap != null && ColorMap.TryGetValue('*', out colorBrush))
                    {
                        block.Background = colorBrush;
                    }

                    sequenceItemsPanel.Children.Add(block);
                }

                metadataBlock.Text = sequence.ID;
            }
            else
            {
                metadataRow.Height = new GridLength(0);
                sequenceRow.Height = new GridLength(sequenceItemsBorder.BorderThickness.Top + sequenceItemsBorder.BorderThickness.Bottom + (isReferenceSequence ? 1 : 0));
            }

            return(base.MeasureOverride(constraint));
        }
コード例 #33
0
 public OracleSequenceValueGeneratorState([NotNull] ISequence sequence)
     : base(Check.NotNull(sequence, nameof(sequence)).IncrementBy)
 {
     Sequence = sequence;
 }
コード例 #34
0
        private static void WriteLocus(ISequence sequence, TextWriter txtWriter)
        {
            // determine molecule and sequence type
            GenBankMetadata metadata = (GenBankMetadata)sequence.Metadata[Helper.GenBankMetadataKey];

            GenBankLocusInfo locusInfo = null;
            string           molType   = sequence.Alphabet.Name;

            if (metadata != null)
            {
                locusInfo = metadata.Locus;
                molType   = locusInfo.MoleculeType.ToString();
            }

            string seqType;

            if (sequence.Alphabet.Name != null)
            {
                if (molType == Alphabets.Protein.Name)
                {
                    seqType = "aa";
                    molType = string.Empty; // protein files don't use molecule type
                }
                else
                {
                    seqType = "bp";
                }
            }
            else
            {
                if (sequence.Alphabet == Alphabets.Protein)
                {
                    seqType = "aa";
                    molType = string.Empty; // protein files don't use molecule type
                }
                else
                {
                    seqType = "bp";

                    if (sequence.Alphabet == Alphabets.DNA)
                    {
                        molType = Alphabets.DNA.Name;
                    }
                    else
                    {
                        molType = Alphabets.RNA.Name;
                    }
                }
            }

            // retrieve metadata fields
            string   strandType     = string.Empty;
            string   strandTopology = string.Empty;
            string   division       = string.Empty;
            DateTime date           = DateTime.Now;

            if (locusInfo != null)
            {
                strandType = Helper.GetStrandType(locusInfo.Strand);

                strandTopology = Helper.GetStrandTopology(locusInfo.StrandTopology);
                if (locusInfo.DivisionCode != SequenceDivisionCode.None)
                {
                    division = locusInfo.DivisionCode.ToString();
                }

                date = locusInfo.Date;
            }

            txtWriter.WriteLine("{0,-12}{1,-16} {2,11} {3} {4,3}{5,-6}  {6,-8} {7,3} {8}",
                                "LOCUS",
                                sequence.ID,
                                sequence.Count,
                                seqType,
                                strandType,
                                molType,
                                strandTopology,
                                division,
                                date.ToString("dd-MMM-yyyy", CultureInfo.InvariantCulture).ToUpperInvariant());
        }
コード例 #35
0
 private void _UseSequenceFilters(ISequence sequence)
 {
     _intentPreFilter.Filters = sequence.GetAllSequenceFilters();
 }
コード例 #36
0
        public ActionResult Index(
            bool importGenes,
            bool importPartial,
            bool filterMinLength,
            int minLength,
            bool filterMaxLength,
            int maxLength)
        {
            return(CreateTask(() =>
            {
                var genBankSearchResultsStream = FileHelper.GetFileStream(Request.Files[0]);
                string searchResults = FileHelper.ReadSequenceFromStream(genBankSearchResultsStream);
                string[] accessions;
                if (filterMinLength)
                {
                    accessions = filterMaxLength ?
                                 NcbiHelper.GetIdsFromNcbiSearchResults(searchResults, importPartial, minLength, maxLength) :
                                 NcbiHelper.GetIdsFromNcbiSearchResults(searchResults, importPartial, minLength);
                }
                else
                {
                    accessions = filterMaxLength ?
                                 NcbiHelper.GetIdsFromNcbiSearchResults(searchResults, importPartial, 1, maxLength) :
                                 NcbiHelper.GetIdsFromNcbiSearchResults(searchResults, importPartial);
                }
                accessions = accessions.Distinct().Select(a => a.Split('.')[0]).ToArray();
                var importResults = new List <MatterImportResult>(accessions.Length);

                using (var db = new LibiadaWebEntities())
                {
                    var matterRepository = new MatterRepository(db);
                    var dnaSequenceRepository = new GeneticSequenceRepository(db);

                    var(existingAccessions, accessionsToImport) = dnaSequenceRepository.SplitAccessionsIntoExistingAndNotImported(accessions);

                    importResults.AddRange(existingAccessions.ConvertAll(existingAccession => new MatterImportResult
                    {
                        MatterName = existingAccession,
                        Result = "Sequence already exists",
                        Status = "Exists"
                    }));

                    foreach (string accession in accessionsToImport)
                    {
                        var importResult = new MatterImportResult()
                        {
                            MatterName = accession
                        };

                        try
                        {
                            ISequence bioSequence = NcbiHelper.DownloadGenBankSequence(accession);
                            GenBankMetadata metadata = NcbiHelper.GetMetadata(bioSequence);
                            importResult.MatterName = metadata.Version.CompoundAccession;

                            Matter matter = matterRepository.CreateMatterFromGenBankMetadata(metadata);

                            importResult.SequenceType = matter.SequenceType.GetDisplayValue();
                            importResult.Group = matter.Group.GetDisplayValue();
                            importResult.MatterName = matter.Name;
                            importResult.AllNames = $"Common name = {metadata.Source.CommonName}, "
                                                    + $"Species = {metadata.Source.Organism.Species}, "
                                                    + $"Definition = {metadata.Definition}, "
                                                    + $"Saved matter name = {importResult.MatterName}";

                            var sequence = new CommonSequence
                            {
                                Matter = matter,
                                Notation = Notation.Nucleotides,
                                RemoteDb = RemoteDb.GenBank,
                                RemoteId = metadata.Version.CompoundAccession
                            };
                            bool partial = metadata.Definition.ToLower().Contains("partial");
                            dnaSequenceRepository.Create(sequence, bioSequence, partial);

                            (importResult.Result, importResult.Status) = importGenes ?
                                                                         ImportFeatures(metadata, sequence) :
                                                                         ("Successfully imported sequence", "Success");
                        }
                        catch (Exception exception)
                        {
                            importResult.Status = "Error";
                            importResult.Result = $"Error: {exception.Message}";
                            while (exception.InnerException != null)
                            {
                                exception = exception.InnerException;
                                importResult.Result += $" {exception.Message}";
                            }

                            foreach (var dbEntityEntry in db.ChangeTracker.Entries())
                            {
                                if (dbEntityEntry.Entity != null)
                                {
                                    dbEntityEntry.State = EntityState.Detached;
                                }
                            }
                        }
                        finally
                        {
                            importResults.Add(importResult);
                        }
                    }

                    string[] names = importResults.Select(r => r.MatterName).ToArray();

                    // removing matters for which adding of sequence failed
                    Matter[] orphanMatters = db.Matter
                                             .Include(m => m.Sequence)
                                             .Where(m => names.Contains(m.Name) && m.Sequence.Count == 0)
                                             .ToArray();

                    if (orphanMatters.Length > 0)
                    {
                        db.Matter.RemoveRange(orphanMatters);
                        db.SaveChanges();
                    }
                }

                var result = new Dictionary <string, object> {
                    { "result", importResults }
                };

                return new Dictionary <string, string> {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }
コード例 #37
0
        /// <summary>
        /// Parses all test cases related to ParseOne() method based on the
        /// parameters passed and validates the same.
        /// </summary>
        /// <param name="nodeName">Xml Node name to be read.</param>
        /// <param name="isFilePath">Is file path passed as parameter?</param>
        void ValidateParseOneGeneralTestCases(string nodeName,
                                              bool isFilePath)
        {
            // Gets the expected sequence from the Xml
            string filePath = _utilityObj._xmlUtil.GetTextValue(nodeName,
                                                                Constants.FilePathNode);

            Assert.IsTrue(File.Exists(filePath));

            // Logs information to the log file
            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                   "Gff Parser BVT : File Exists in the Path '{0}'.", filePath));

            ISequence originalSeq = null;
            GffParser parserObj   = new GffParser();

            if (isFilePath)
            {
                originalSeq = parserObj.ParseOne(filePath);
            }
            else
            {
                using (StreamReader reader = File.OpenText(filePath))
                {
                    originalSeq = parserObj.ParseOne(reader);
                }
            }

            Assert.IsNotNull(originalSeq);
            Assert.IsTrue(ValidateFeatures(originalSeq, nodeName));
            ApplicationLog.WriteLine(
                "Gff Parser BVT : Successfully validated all the Features for a give Sequence in GFF File.");
            Console.WriteLine(
                "Gff Parser BVT : Successfully validated all the Features for a give Sequence in GFF File.");

            string expectedSequence = _utilityObj._xmlUtil.GetTextValue(nodeName,
                                                                        Constants.ExpectedSequenceNode);

            Sequence seq = (Sequence)originalSeq;

            Assert.IsNotNull(seq);
            Assert.AreEqual(expectedSequence, seq.ToString());
            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                   "Gff Parser BVT: The Gff sequence '{0}' validation after ParseOne() is found to be as expected.",
                                                   seq.ToString()));

            // Logs to the NUnit GUI (Console.Out) window
            Console.WriteLine(string.Format((IFormatProvider)null,
                                            "Gff Parser BVT: The Gff sequence '{0}' validation after ParseOne() is found to be as expected.",
                                            seq.ToString()));

            Assert.AreEqual(expectedSequence.Length, seq.EncodedValues.Length);
            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                   "Gff Parser BVT: The Gff Length sequence '{0}' is as expected.",
                                                   expectedSequence.Length));

            string expectedAlphabet = _utilityObj._xmlUtil.GetTextValue(nodeName,
                                                                        Constants.AlphabetNameNode).ToLower(CultureInfo.CurrentCulture);

            Assert.IsNotNull(seq.Alphabet);
            Assert.AreEqual(seq.Alphabet.Name.ToLower(CultureInfo.CurrentCulture),
                            expectedAlphabet);
            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                   "Gff Parser BVT: The Sequence Alphabet is '{0}' and is as expected.",
                                                   seq.Alphabet.Name));

            string expectedSequenceId = _utilityObj._xmlUtil.GetTextValue(nodeName,
                                                                          Constants.SequenceIdNode);

            Assert.AreEqual(expectedSequenceId, seq.DisplayID);
            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                   "Gff Parser BVT: The Sequence ID is '{0}' and is as expected.",
                                                   seq.DisplayID));
            // Logs to the NUnit GUI (Console.Out) window
            Console.WriteLine(string.Format((IFormatProvider)null,
                                            "Gff Parser BVT: The Sequence ID is '{0}' and is as expected.",
                                            seq.DisplayID));
        }
コード例 #38
0
        /// <summary>
        /// Performs initializations and validations required
        /// before carrying out sequence alignment.
        /// Initializes only gap open penalty. Initialization for
        /// gap extension, if required, has to be done seperately.
        /// </summary>
        /// <param name="similarityMatrix">Scoring matrix.</param>
        /// <param name="gapPenalty">Gap open penalty (by convention, use a negative number for this.)</param>
        /// <param name="aInput">First input sequence.</param>
        /// <param name="bInput">Second input sequence.</param>
        private void SimpleAlignPrimer(SimilarityMatrix similarityMatrix, int gapPenalty, ISequence aInput, ISequence bInput)
        {
            InitializeAlign(aInput);
            ResetSpecificAlgorithmMemberVariables();

            // Set Gap Penalty and Similarity Matrix
            _gapOpenCost = gapPenalty;

            // note that _gapExtensionCost is not used for simple gap penalty
            _similarityMatrix = similarityMatrix;

            ValidateAlignInput(aInput, bInput);  // throws exception if input not valid

            // Convert input strings to 0-based int arrays using similarity matrix mapping
            _a = similarityMatrix.ToByteArray(aInput.ToString());
            _b = similarityMatrix.ToByteArray(bInput.ToString());
        }
コード例 #39
0
        /// <summary>
        /// Pairwise alignment of two sequences using a single gap penalty.  The various algorithms in derived classes (NeedlemanWunsch,
        /// SmithWaterman, and PairwiseOverlap) all use this general engine for alignment with a single gap penalty.
        /// </summary>
        /// <param name="similarityMatrix">Scoring matrix.</param>
        /// <param name="gapPenalty">Gap penalty (by convention, use a negative number for this.)</param>
        /// <param name="aInput">First input sequence.</param>
        /// <param name="bInput">Second input sequence.</param>
        /// <returns>A list of sequence alignments.</returns>
        public IList <IPairwiseSequenceAlignment> AlignSimple(SimilarityMatrix similarityMatrix, int gapPenalty, ISequence aInput, ISequence bInput)
        {
            // Initialize and perform validations for simple alignment
            SimpleAlignPrimer(similarityMatrix, gapPenalty, aInput, bInput);

            FillMatrixSimple();

            ////DumpF();  // Writes F-matrix to application log, used for development and testing

            List <byte[]> alignedSequences;
            List <int>    offsets;
            List <int>    endOffsets;
            List <int>    insertions;
            List <int>    startOffsets;
            int           optScore = Traceback(out alignedSequences, out offsets, out startOffsets, out endOffsets, out insertions);

            return(CollateResults(aInput, bInput, alignedSequences, offsets, optScore, startOffsets, endOffsets, insertions));
        }
コード例 #40
0
 /// <summary>
 /// Aligns two sequences using the affine gap metric, a gap open penalty and a gap extension penalty.
 /// This method uses the existing gap open and extension penalties and similarity matrix.
 /// Set these using GapOpenCost, GapExtensionCost and SimilarityMatrix properties before calling this method.
 /// </summary>
 /// <param name="sequence1">First input sequence.</param>
 /// <param name="sequence2">Second input sequence.</param>
 /// <returns>A list of sequence alignments.</returns>
 public IList <IPairwiseSequenceAlignment> Align(ISequence sequence1, ISequence sequence2)
 {
     return(Align(_similarityMatrix, _gapOpenCost, _gapExtensionCost, sequence1, sequence2));
 }
コード例 #41
0
        /// <summary>
        /// This method is considered as main execute method which defines the
        /// step by step algorithm. Derived class flows the defined flow by this
        /// method.
        /// </summary>
        /// <param name="referenceSequenceList">Reference sequence.</param>
        /// <param name="originalQuerySequences">List of input sequences.</param>
        /// <returns>A list of sequence alignment.</returns>
        private IEnumerable <IPairwiseSequenceAlignment> Alignment(IEnumerable <ISequence> referenceSequenceList, IEnumerable <ISequence> originalQuerySequences)
        {
            ConsensusResolver = new SimpleConsensusResolver(referenceSequenceList.ElementAt(0).Alphabet);

            IEnumerable <ISequence> querySequenceList =
                ForwardOnly ? originalQuerySequences
                    : (ReverseOnly
                        ? ReverseComplementSequenceList(originalQuerySequences)
                        : AddReverseComplementsToSequenceList(originalQuerySequences));

            IList <IPairwiseSequenceAlignment> results = new List <IPairwiseSequenceAlignment>();

            var deltas = new List <DeltaAlignment>();

            foreach (ISequence refSequence in referenceSequenceList)
            {
                this.nucmerAlgo = new NUCmer(refSequence);

                if (GapOpenCost != DefaultGapOpenCost)
                {
                    this.nucmerAlgo.GapOpenCost = GapOpenCost;
                }
                if (GapExtensionCost != DefaultGapExtensionCost)
                {
                    this.nucmerAlgo.GapExtensionCost = GapExtensionCost;
                }
                if (LengthOfMUM != DefaultLengthOfMUM)
                {
                    this.nucmerAlgo.LengthOfMUM = LengthOfMUM;
                }

                // Set the ClusterBuilder properties to defaults
                if (FixedSeparation != ClusterBuilder.DefaultFixedSeparation)
                {
                    this.nucmerAlgo.FixedSeparation = FixedSeparation;
                }
                if (MaximumSeparation != ClusterBuilder.DefaultMaximumSeparation)
                {
                    this.nucmerAlgo.MaximumSeparation = MaximumSeparation;
                }
                if (MinimumScore != ClusterBuilder.DefaultMinimumScore)
                {
                    this.nucmerAlgo.MinimumScore = MinimumScore;
                }
                if (SeparationFactor != ClusterBuilder.DefaultSeparationFactor)
                {
                    this.nucmerAlgo.SeparationFactor = SeparationFactor;
                }
                if (BreakLength != ModifiedSmithWaterman.DefaultBreakLength)
                {
                    this.nucmerAlgo.BreakLength = BreakLength;
                }

                this.nucmerAlgo.ConsensusResolver = ConsensusResolver;
                if (SimilarityMatrix != null)
                {
                    this.nucmerAlgo.SimilarityMatrix = SimilarityMatrix;
                }

                foreach (ISequence querySequence in querySequenceList)
                {
                    //  Check for parameters that would prevent an alignment from being returned.
                    if (Math.Min(querySequence.Count, refSequence.Count) < MinimumScore)
                    {
                        var msg = "Bad parameter settings for NucmerPairwiseAligner. " +
                                  "Tried to align a reference of length " + refSequence.Count.ToString() +
                                  " to a sequence of length " + querySequence.Count.ToString() +
                                  " while requiring a minimum score of MinimumScore = " + MinimumScore +
                                  ". This will prevent any alignments from being returned.";
                        throw new ArgumentException(msg);
                    }
                    IEnumerable <DeltaAlignment> deltaAlignment = this.nucmerAlgo.GetDeltaAlignments(querySequence, !MaxMatch, querySequence.IsMarkedAsReverseComplement());
                    deltas.AddRange(deltaAlignment);
                }
            }

            if (deltas.Count > 0)
            {
                ISequence concatReference = referenceSequenceList.ElementAt(0);
                //// concat all the sequences into one sequence
                if (referenceSequenceList.Count() > 1)
                {
                    concatReference = ConcatSequence(referenceSequenceList);
                }

                foreach (ISequence querySequence in querySequenceList)
                {
                    List <DeltaAlignment>      qDelta            = deltas.Where(d => d.QuerySequence.Equals(querySequence)).ToList();
                    IPairwiseSequenceAlignment sequenceAlignment = new PairwiseSequenceAlignment(concatReference, querySequence);

                    // Convert delta alignments to sequence alignments
                    IList <PairwiseAlignedSequence> alignments = ConvertDeltaToAlignment(qDelta);

                    if (alignments.Count > 0)
                    {
                        foreach (PairwiseAlignedSequence align in alignments)
                        {
                            // Calculate the score of alignment
                            align.Score = CalculateScore(
                                align.FirstSequence,
                                align.SecondSequence);

                            // Make Consensus
                            align.Consensus = MakeConsensus(
                                align.FirstSequence,
                                align.SecondSequence);

                            sequenceAlignment.PairwiseAlignedSequences.Add(align);
                        }
                    }

                    results.Add(sequenceAlignment);
                }
            }

            return(results);
        }
コード例 #42
0
        /// <summary>
        /// Calculate the score of alignment.
        /// </summary>
        /// <param name="referenceSequence">Reference sequence.</param>
        /// <param name="querySequence">Query sequence.</param>
        /// <returns>Score of the alignment.</returns>
        protected int CalculateScore(
            ISequence referenceSequence,
            ISequence querySequence)
        {
            if (referenceSequence == null)
            {
                throw new ArgumentNullException("referenceSequence");
            }

            if (querySequence == null)
            {
                throw new ArgumentNullException("querySequence");
            }

            int index;

            int score = 0;

            // For each pair of symbols (characters) in reference and query sequence
            // 1. If the character are different and not alignment character "-",
            //      then find the cost form Similarity Matrix
            // 2. If Gap Extension cost needs to be used
            //      a. Find how many gaps exists in appropriate sequence (reference / query)
            //          and calculate the score
            // 3. Add the gap open cost
            for (index = 0; index < referenceSequence.Count; index++)
            {
                byte referenceCharacter = referenceSequence[index];
                byte queryCharacter     = querySequence[index];

                if (DnaAlphabet.Instance.Gap != referenceCharacter &&
                    DnaAlphabet.Instance.Gap != queryCharacter)
                {
                    score += SimilarityMatrix[referenceCharacter, queryCharacter];
                }
                else
                {
                    if (IsAlign)
                    {
                        int gapCount;
                        if (DnaAlphabet.Instance.Gap == referenceCharacter)
                        {
                            gapCount = FindExtensionLength(referenceSequence, index);
                        }
                        else
                        {
                            gapCount = FindExtensionLength(querySequence, index);
                        }

                        score += GapOpenCost + (gapCount * GapExtensionCost);

                        // move the index pointer to end of extension
                        index = index + gapCount - 1;
                    }
                    else
                    {
                        score += GapOpenCost;
                    }
                }
            }

            return(score);
        }
コード例 #43
0
        /// <summary>
        /// Validates the Metadata Features of a Gff Sequence for the sequence and node name specified.
        /// </summary>
        /// <param name="seq">Sequence that needs to be validated.</param>
        /// <param name="nodeName">Xml Node name to be read.</param>
        /// <returns>True/false</returns>
        bool ValidateFeatures(ISequence seq, string nodeName)
        {
            // Gets all the Features from the Sequence for Validation
            List <MetadataListItem <List <string> > > featureList =
                (List <MetadataListItem <List <string> > >)seq.Metadata[Constants.Features];

            // Gets all the xml values for validation
            string[] sequenceNames = _utilityObj._xmlUtil.GetTextValues(nodeName,
                                                                        Constants.SequenceNameNodeName);
            string[] sources = _utilityObj._xmlUtil.GetTextValues(nodeName,
                                                                  Constants.SourceNodeName);
            string[] featureNames = _utilityObj._xmlUtil.GetTextValues(nodeName,
                                                                       Constants.FeatureNameNodeName);
            string[] startValues = _utilityObj._xmlUtil.GetTextValues(nodeName,
                                                                      Constants.StartNodeName);
            string[] endValues = _utilityObj._xmlUtil.GetTextValues(nodeName,
                                                                    Constants.EndNodeName);
            string[] scoreValues = _utilityObj._xmlUtil.GetTextValues(nodeName,
                                                                      Constants.ScoreNodeName);
            string[] strandValues = _utilityObj._xmlUtil.GetTextValues(nodeName,
                                                                       Constants.StrandNodeName);
            string[] frameValues = _utilityObj._xmlUtil.GetTextValues(nodeName,
                                                                      Constants.FrameNodeName);
            string[] attributeValues = _utilityObj._xmlUtil.GetTextValues(nodeName,
                                                                          Constants.AttributesNodeName);
            int i = 0;

            // Loop through each and every feature and validate the same.
            foreach (MetadataListItem <List <string> > feature in featureList)
            {
                Dictionary <string, List <string> > itemList = feature.SubItems;

                // Read specific feature Item and validate
                // Validate Start
                try
                {
                    List <string> st = itemList[Constants.FeatureStart];
                    foreach (string sin in st)
                    {
                        if (0 != string.Compare(startValues[i], sin,
                                                true, CultureInfo.CurrentCulture))
                        {
                            return(false);
                        }
                    }
                }
                catch (KeyNotFoundException) { }

                // Validate Score
                try
                {
                    List <string> st = itemList[Constants.FeatureScore];
                    foreach (string sin in st)
                    {
                        if (0 != string.Compare(scoreValues[i],
                                                sin, true, CultureInfo.CurrentCulture))
                        {
                            return(false);
                        }
                    }
                }
                catch (KeyNotFoundException) { }


                // Validate Strand
                try
                {
                    List <string> st = itemList[Constants.FeatureStrand];
                    foreach (string sin in st)
                    {
                        if (0 != string.Compare(strandValues[i],
                                                sin, true, CultureInfo.CurrentCulture))
                        {
                            return(false);
                        }
                    }
                }
                catch (KeyNotFoundException) { }

                // Validate Source
                try
                {
                    List <string> st = itemList[Constants.FeatureSource];
                    foreach (string sin in st)
                    {
                        if (0 != string.Compare(sources[i],
                                                sin, true, CultureInfo.CurrentCulture))
                        {
                            return(false);
                        }
                    }
                }
                catch (KeyNotFoundException) { }

                // Validate End
                try
                {
                    List <string> st = itemList[Constants.FeatureEnd];
                    foreach (string sin in st)
                    {
                        if (0 != string.Compare(endValues[i],
                                                sin, true, CultureInfo.CurrentCulture))
                        {
                            return(false);
                        }
                    }
                }
                catch (KeyNotFoundException) { }


                // Validate Frame
                try
                {
                    List <string> st = itemList[Constants.FeatureFrame];
                    foreach (string sin in st)
                    {
                        if (0 != string.Compare(frameValues[i],
                                                sin, true, CultureInfo.CurrentCulture))
                        {
                            return(false);
                        }
                    }
                }
                catch (KeyNotFoundException) { }

                if (0 != string.Compare(feature.FreeText, attributeValues[i],
                                        true, CultureInfo.CurrentCulture))
                {
                    return(false);
                }

                if (0 != string.Compare(feature.Key, featureNames[i],
                                        true, CultureInfo.CurrentCulture))
                {
                    return(false);
                }

                if (0 != string.Compare(seq.DisplayID, sequenceNames[i],
                                        true, CultureInfo.CurrentCulture))
                {
                    return(false);
                }

                i++;
            }

            return(true);
        }
コード例 #44
0
        /// <summary>
        /// Returns a sequence which contains bases from the specified sequence as specified by the location.
        /// If the location contains accession then the sequence from the referredSequences which matches the
        /// accession of the location will be considered.
        ///
        /// For example,
        /// if location is "join(100..200, J00089.1:10..50, J00090.2:30..40)"
        /// then bases from 100 to 200 will be considered from the sequence parameter and referredSequences will
        /// be searched for the J00089.1 and J00090.2 accession if found then those sequences will be considered
        /// for constructing the output sequence.
        /// If the referred sequence is not found in the referredSequences then an exception will occur.
        /// </summary>
        /// <param name="location">Location instance.</param>
        /// <param name="sequence">Sequence instance from which the sub sequence has to be returned.</param>
        /// <param name="referredSequences">A dictionary containing Accession numbers as keys and Sequences as values, this will be used when
        /// the location or sub-locations contains accession.</param>
        public ISequence GetSubSequence(ILocation location, ISequence sequence, Dictionary <string, ISequence> referredSequences)
        {
            if (location == null)
            {
                throw new ArgumentNullException(Properties.Resource.ParameterNameLocation);
            }

            if (sequence == null)
            {
                throw new ArgumentNullException(Properties.Resource.ParameterNameSequence);
            }

            DerivedSequence basicDerSeq;

            if (location.Operator == LocationOperator.Complement)
            {
                if (location.SubLocations.Count > 1)
                {
                    throw new ArgumentException(Properties.Resource.ComplementWithMorethanOneSubLocs);
                }

                if (location.SubLocations.Count > 0)
                {
                    basicDerSeq = new DerivedSequence(location.SubLocations[0].GetSubSequence(sequence, referredSequences), false, true);
                }
                else
                {
                    basicDerSeq = new DerivedSequence(GetSubSequence(location.LocationStart, location.LocationEnd, location.Accession, location.Separator, sequence, referredSequences), false, true);
                }

                byte[] tempSeqData = new byte[basicDerSeq.Count];
                for (int i = 0; i < basicDerSeq.Count; i++)
                {
                    tempSeqData[i] = basicDerSeq[i];
                }

                return(new Sequence(sequence.Alphabet, tempSeqData));
            }

            if (location.Operator == LocationOperator.Order)
            {
                List <ISequence> subSequences = new List <ISequence>();
                if (location.SubLocations.Count > 0)
                {
                    foreach (ILocation loc in location.SubLocations)
                    {
                        subSequences.Add(loc.GetSubSequence(sequence, referredSequences));
                    }
                }
                else
                {
                    basicDerSeq = new DerivedSequence(GetSubSequence(location.LocationStart, location.LocationEnd, location.Accession, location.Separator, sequence, referredSequences), false, false);
                    byte[] seqData = new byte[basicDerSeq.Count];
                    for (long i = 0; i < basicDerSeq.Count; i++)
                    {
                        seqData[i] = basicDerSeq[i];
                    }

                    subSequences.Add(new Sequence(sequence.Alphabet, seqData));
                }

                long totalSubSequenceLength = 0;
                long j = 0;
                foreach (ISequence seq in subSequences)
                {
                    totalSubSequenceLength += seq.Count;
                }
                byte[] tempSeqData = new byte[totalSubSequenceLength];
                totalSubSequenceLength = 0;
                IAlphabet alphabet = null;
                int       m        = 0;
                foreach (ISequence seq in subSequences)
                {
                    totalSubSequenceLength += seq.Count;
                    while (j < totalSubSequenceLength)
                    {
                        tempSeqData[j] = seq[m];
                        j++;
                        m++;
                    }
                    m        = 0;
                    alphabet = seq.Alphabet;
                }

                //return Segmented sequence.
                return(new Sequence(alphabet, tempSeqData));
            }

            if (location.Operator == LocationOperator.Join || location.Operator == LocationOperator.Bond)
            {
                if (location.SubLocations.Count > 0)
                {
                    List <ISequence> subSequences = new List <ISequence>();
                    foreach (ILocation loc in location.SubLocations)
                    {
                        subSequences.Add(loc.GetSubSequence(sequence, referredSequences));
                    }


                    long i            = 0;
                    long subSeqLength = 0;
                    foreach (ISequence subSeq in subSequences)
                    {
                        subSeqLength += subSeq.Count;
                    }
                    byte[] seqData = new byte[subSeqLength];
                    subSeqLength = 0;
                    int m = 0;
                    foreach (ISequence subSeq in subSequences)
                    {
                        subSeqLength += subSeq.Count;
                        while (i < subSeqLength)
                        {
                            seqData[i] = subSeq[m];
                            i++;
                            m++;
                        }
                        m = 0;
                    }

                    Sequence seq = new Sequence(sequence.Alphabet, seqData);

                    return(seq);
                }
                else
                {
                    return(GetSubSequence(location.LocationStart, location.LocationEnd, location.Accession, location.Separator, sequence, referredSequences));
                }
            }

            if (location.SubLocations.Count > 0)
            {
                throw new ArgumentException(Properties.Resource.NoneWithSubLocs);
            }

            return(GetSubSequence(location.LocationStart, location.LocationEnd, location.Accession, location.Separator, sequence, referredSequences));
        }
コード例 #45
0
        /// <summary>
        /// Returns the sequence for the specified start and end positions.
        /// If the accession is null or empty then the source sequence is used to construct the output sequence,
        /// otherwise appropriate sequence from the referred sequence is used to construct output sequence.
        /// </summary>
        /// <param name="start">Start position.</param>
        /// <param name="end">End position.</param>
        /// <param name="accession">Accession number.</param>
        /// <param name="sepataror">Start and End separator.</param>
        /// <param name="source">Source sequence.</param>
        /// <param name="referredSequences">Referred Sequences.</param>
        private static ISequence GetSubSequence(int start, int end, string accession, string sepataror, ISequence source, Dictionary <string, ISequence> referredSequences)
        {
            if (string.Compare(sepataror, "^", StringComparison.OrdinalIgnoreCase) == 0)
            {
                return(new Sequence(source.Alphabet, string.Empty));
            }

            if (string.Compare(sepataror, "..", StringComparison.OrdinalIgnoreCase) != 0 &&
                string.Compare(sepataror, ".", StringComparison.OrdinalIgnoreCase) != 0 &&
                !string.IsNullOrEmpty(sepataror))
            {
                string str = string.Format(CultureInfo.CurrentCulture, Properties.Resource.InvalidSeparator, sepataror);
                throw new ArgumentException(str);
            }

            if (!string.IsNullOrEmpty(accession) && (referredSequences == null || !referredSequences.ContainsKey(accession)))
            {
                string str = string.Format(CultureInfo.CurrentCulture, Properties.Resource.AccessionSequenceNotFound, accession);
                throw new ArgumentException(str);
            }

            if (!string.IsNullOrEmpty(accession))
            {
                if (source.Alphabet != referredSequences[accession].Alphabet)
                {
                    string str = string.Format(CultureInfo.CurrentCulture, Properties.Resource.InvalidReferredAlphabet, accession);
                    throw new ArgumentException(str);
                }

                source = referredSequences[accession];
            }

            // as location.start is one based where as Range accepts zero based index.
            start = start - 1;
            int length = end - start;

            if (string.IsNullOrEmpty(sepataror) || string.Compare(sepataror, ".", StringComparison.OrdinalIgnoreCase) == 0)
            {
                length = 1;
            }

            ISequence newSequence = source.GetSubSequence(start, length);

            byte[] seqData = new byte[newSequence.Count];
            for (long i = 0; i < newSequence.Count; i++)
            {
                seqData[i] = newSequence[i];
            }

            return(new Sequence(source.Alphabet, seqData));
        }
コード例 #46
0
 /// <summary>
 /// Averages the <paramref name="values"/>.
 /// </summary>
 /// <typeparam name="TEnumerator">The type of the enumerator of the <paramref name="values"/>.</typeparam>
 /// <param name="values">The values to find the mean of.</param>
 /// <returns>The mean of the <paramref name="values"/>.</returns>
 public static Double GeometricMean <TEnumerator>([AllowNull] this ISequence <Single, TEnumerator> values) where TEnumerator : notnull, ICount, ICurrent <Single>, IMoveNext, IReset => Math.Pow(values.Product(), 1.0 / (values?.Count ?? 0));
コード例 #47
0
        /// <summary>
        /// Align the Gap by executing pairwise alignment.
        /// </summary>
        /// <param name="referenceSequence">Reference sequence.</param>
        /// <param name="querySequence">Query Sequence.</param>
        /// <param name="sequenceResult1">Editable sequence containing alignment first result.</param>
        /// <param name="sequenceResult2">Editable sequence containing alignment second result.</param>
        /// <param name="consensusResult">Editable sequence containing consensus sequence.</param>
        /// <param name="mum1">First MUM of Gap.</param>
        /// <param name="mum2">Second MUM of Gap.</param>
        /// <param name="insertions">Insertions made to the aligned sequences.</param>
        /// <returns>Score of alignment.</returns>
        private long AlignGap(
            ISequence referenceSequence,
            ISequence querySequence,
            List <byte> sequenceResult1,
            List <byte> sequenceResult2,
            List <byte> consensusResult,
            Match mum1,
            Match mum2,
            out List <long> insertions)
        {
            long      score     = 0;
            ISequence sequence1 = null;
            ISequence sequence2 = null;
            IList <IPairwiseSequenceAlignment> sequenceAlignment = null;

            byte[] mum1String;
            byte[] mum2String;

            insertions = new List <long>(2);
            insertions.Add(0);
            insertions.Add(0);

            long mum1ReferenceStartIndex = 0;
            long mum1QueryStartIndex     = 0;
            long mum1Length = 0;
            long mum2ReferenceStartIndex = 0;
            long mum2QueryStartIndex     = 0;
            long mum2Length = 0;

            if (mum1.Length != 0)
            {
                mum1ReferenceStartIndex = mum1.ReferenceSequenceOffset;
                mum1QueryStartIndex     = mum1.QuerySequenceOffset;
                mum1Length = mum1.Length;
            }

            if (mum2.Length != 0)
            {
                mum2ReferenceStartIndex = mum2.ReferenceSequenceOffset;
                mum2QueryStartIndex     = mum2.QuerySequenceOffset;
                mum2Length = mum2.Length;
            }
            else
            {
                mum2ReferenceStartIndex = referenceSequence.Count;
                mum2QueryStartIndex     = querySequence.Count;
            }

            long referenceGapStartIndex = mum1ReferenceStartIndex + mum1Length;
            long queryGapStartIndex     = mum1QueryStartIndex + mum1Length;

            if (mum2ReferenceStartIndex > referenceGapStartIndex &&
                mum2QueryStartIndex > queryGapStartIndex)
            {
                sequence1 = referenceSequence.GetSubSequence(
                    referenceGapStartIndex,
                    mum2ReferenceStartIndex - referenceGapStartIndex);
                sequence2 = querySequence.GetSubSequence(
                    queryGapStartIndex,
                    mum2QueryStartIndex - queryGapStartIndex);

                sequenceAlignment = this.RunPairWise(sequence1, sequence2);

                if (sequenceAlignment != null)
                {
                    foreach (IPairwiseSequenceAlignment pairwiseAlignment in sequenceAlignment)
                    {
                        foreach (PairwiseAlignedSequence alignment in pairwiseAlignment.PairwiseAlignedSequences)
                        {
                            sequenceResult1.InsertRange(
                                sequenceResult1.Count,
                                alignment.FirstSequence);
                            sequenceResult2.InsertRange(
                                sequenceResult2.Count,
                                alignment.SecondSequence);
                            consensusResult.InsertRange(
                                consensusResult.Count,
                                alignment.Consensus);

                            score += alignment.Score;

                            if (alignment.Metadata.ContainsKey("Insertions"))
                            {
                                List <int> gapinsertions = alignment.Metadata["Insertions"] as List <int>;
                                if (gapinsertions != null)
                                {
                                    if (gapinsertions.Count > 0)
                                    {
                                        insertions[0] += gapinsertions[0];
                                    }

                                    if (gapinsertions.Count > 1)
                                    {
                                        insertions[1] += gapinsertions[1];
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (mum2ReferenceStartIndex > referenceGapStartIndex)
            {
                sequence1 = referenceSequence.GetSubSequence(
                    referenceGapStartIndex,
                    mum2ReferenceStartIndex - referenceGapStartIndex);

                sequenceResult1.InsertRange(sequenceResult1.Count, sequence1);
                sequenceResult2.InsertRange(sequenceResult2.Count, CreateDefaultGap(sequence1.Count));
                consensusResult.InsertRange(consensusResult.Count, sequence1);

                insertions[1] += sequence1.Count;

                if (this.UseGapExtensionCost)
                {
                    score = this.GapOpenCost + ((sequence1.Count - 1) * this.GapExtensionCost);
                }
                else
                {
                    score = sequence1.Count * this.GapOpenCost;
                }
            }
            else if (mum2QueryStartIndex > queryGapStartIndex)
            {
                sequence2 = querySequence.GetSubSequence(
                    queryGapStartIndex,
                    mum2QueryStartIndex - queryGapStartIndex);

                sequenceResult1.InsertRange(sequenceResult1.Count, CreateDefaultGap(sequence2.Count));
                sequenceResult2.InsertRange(sequenceResult2.Count, sequence2);
                consensusResult.InsertRange(consensusResult.Count, sequence2);

                insertions[0] += sequence2.Count;

                if (this.UseGapExtensionCost)
                {
                    score = this.GapOpenCost + ((sequence2.Count - 1) * this.GapExtensionCost);
                }
                else
                {
                    score = sequence2.Count * this.GapOpenCost;
                }
            }

            // Add the MUM to the result
            if (0 < mum2Length)
            {
                mum1String = referenceSequence.GetSubSequence(
                    mum2ReferenceStartIndex,
                    mum2Length).ToArray();
                sequenceResult1.InsertRange(sequenceResult1.Count, mum1String);

                mum2String = querySequence.GetSubSequence(
                    mum2QueryStartIndex,
                    mum2Length).ToArray();
                sequenceResult2.InsertRange(sequenceResult2.Count, mum2String);
                consensusResult.InsertRange(consensusResult.Count, mum1String);

                foreach (byte index in mum1String)
                {
                    score += SimilarityMatrix[index, index];
                }
            }

            return(score);
        }
コード例 #48
0
        /// <summary>
        /// General method to validate creation of Segmented sequence.
        /// <param name="nodeName">xml node name.</param>
        /// <param name="methodName">Method Name</param>
        /// </summary>
        void ValidateSegmentedsequenceCreation(
            string nodeName, segmentedSequenceParameters methodName)
        {
            // Gets the alphabet from the Xml
            string alphabet = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.AlphabetNameNode);
            string expectedSegmentedSeq = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.ExpectedSequence);
            string inputSequence1 = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.Sequence1);
            string inputSequence2 = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.Sequence2);
            string inputSequence3 = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.Sequence3);
            string inputSequence4 = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.Sequence4);
            string inputSequence5 = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.Sequence5);
            string inputSequence6 = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.Sequence6);
            string inputSequence7 = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.Sequence7);
            string expectedSegSeqCount = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.SegmetedSeqCount);
            string expectedSeguencesCount = _utilityObj._xmlUtil.GetTextValue(
                nodeName, Constants.SequencesCount);

            ISequence         seq          = null;
            ISequence         seq1         = null;
            ISequence         seq2         = null;
            ISequence         seq3         = null;
            ISequence         seq4         = null;
            ISequence         seq5         = null;
            ISequence         seq6         = null;
            ISequence         seq7         = null;
            List <ISequence>  seqList      = new List <ISequence>();
            SegmentedSequence segmentedSeq = null;

            // Logs information to the log file
            ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                   "Segmented Sequence BVT: Sequence {0} is expected.", alphabet));

            // create a Isequence.
            seq1 = new Sequence(Utility.GetAlphabet(alphabet), inputSequence1);
            seq2 = new Sequence(Utility.GetAlphabet(alphabet), inputSequence2);
            seq3 = new Sequence(Utility.GetAlphabet(alphabet), inputSequence3);
            seq4 = new Sequence(Utility.GetAlphabet(alphabet), inputSequence4);
            seq5 = new Sequence(Utility.GetAlphabet(alphabet), inputSequence5);
            seq6 = new Sequence(Utility.GetAlphabet(alphabet), inputSequence6);
            seq7 = new Sequence(Utility.GetAlphabet(alphabet), inputSequence7);

            // Add all sequences to sequence list.
            seqList.Add(seq1);
            seqList.Add(seq2);
            seqList.Add(seq3);
            seqList.Add(seq4);
            seqList.Add(seq5);
            seqList.Add(seq6);
            seqList.Add(seq7);
            switch (methodName)
            {
            case segmentedSequenceParameters.SegmentedsequenceList:
                // create a Segmented sequence with sequence list.
                segmentedSeq = new SegmentedSequence(seqList);

                // validate created segmented sequence list.
                Assert.AreEqual(expectedSegmentedSeq, segmentedSeq.ToString());
                Assert.AreSame(seq1, segmentedSeq.Sequences[0]);
                Assert.AreSame(seq2, segmentedSeq.Sequences[1]);
                Assert.AreSame(seq3, segmentedSeq.Sequences[2]);
                Assert.AreSame(seq4, segmentedSeq.Sequences[3]);
                Assert.AreSame(seq5, segmentedSeq.Sequences[4]);
                Assert.AreSame(seq6, segmentedSeq.Sequences[5]);
                Assert.AreSame(seq7, segmentedSeq.Sequences[6]);
                Console.WriteLine(string.Format((IFormatProvider)null,
                                                "Segmented Sequence BVT: Segmented Sequence{0}  is as expected.",
                                                segmentedSeq.ToString()));
                break;

            case segmentedSequenceParameters.SegmentedSequence:
                // create a Isequence.
                seq = new Sequence(Utility.GetAlphabet(alphabet), inputSequence1);

                // create a Segmented sequence
                segmentedSeq = new SegmentedSequence(seq);

                // validate expected segmented sequence.
                Assert.AreEqual(seq.ToString(), segmentedSeq.ToString());
                Console.WriteLine(string.Format((IFormatProvider)null,
                                                "Segmented Sequence BVT: Segmented Sequence{0}  is as expected.",
                                                segmentedSeq.ToString()));
                break;

            case segmentedSequenceParameters.Clone:
                // create a Segmented sequence with sequence list.
                segmentedSeq = new SegmentedSequence(seqList);

                // create a segmented sequences clone.
                SegmentedSequence segSequenceClone = segmentedSeq.Clone();

                // validate Clone sequence.
                Assert.AreEqual(segSequenceClone.ToString(), segmentedSeq.ToString());
                Assert.AreEqual(segSequenceClone.Count, segmentedSeq.Count);
                Assert.AreEqual(segSequenceClone.Sequences.Count, 7);
                Console.WriteLine(string.Format((IFormatProvider)null,
                                                "Segmented Sequence BVT: Segmented Sequence{0}  is as expected.",
                                                segmentedSeq.ToString()));
                break;

            default:
                break;
            }

            // Validate a created segmented Sequence
            Assert.AreEqual(segmentedSeq.Count.ToString((IFormatProvider)null),
                            expectedSegSeqCount);
            Assert.AreEqual(segmentedSeq.Sequences.Count.ToString((IFormatProvider)null),
                            expectedSeguencesCount);
            Assert.AreSame(Utility.GetAlphabet(alphabet), segmentedSeq.Alphabet);

            // Logs to the NUnit GUI (Console.Out) window
            Console.WriteLine(string.Format((IFormatProvider)null,
                                            "Segmented Sequence BVT: Segmented Sequence count{0} is as expected.",
                                            segmentedSeq.Sequences.Count.ToString((IFormatProvider)null)));
            Console.WriteLine(string.Format((IFormatProvider)null,
                                            "Segmented Sequence BVT: Segmented Sequences count{0} is as expected.",
                                            segmentedSeq.Count.ToString((IFormatProvider)null)));

            // Logs to the NUnit GUI (Console.Out) window
            ApplicationLog.WriteLine(
                "Segmented Sequence BVT: Segmented Sequence validation is completed successfully.");
        }
コード例 #49
0
ファイル: Sequencer.cs プロジェクト: zyhong/Disruptor-net
 /// <summary>
 /// Remove the specified sequence from this sequencer.
 /// </summary>
 /// <param name="sequence">to be removed.</param>
 /// <returns>true if this sequence was found, false otherwise.</returns>
 public bool RemoveGatingSequence(ISequence sequence)
 {
     return(SequenceGroups.RemoveSequence(ref _gatingSequences, sequence));
 }
コード例 #50
0
 public void SetSequence(ISequence sequence)
 {
     _UseSequenceData(sequence);
     _UseSequenceFilters(sequence);
 }
コード例 #51
0
        /// <summary>
        /// Get all the gaps in each sequence and call pairwise alignment.
        /// </summary>
        /// <param name="referenceSequence">Reference sequence.</param>
        /// <param name="sequence">Query sequence.</param>
        /// <param name="mums">List of MUMs.</param>
        /// <returns>Aligned sequences.</returns>
        private PairwiseAlignedSequence ProcessGaps(
            ISequence referenceSequence,
            ISequence sequence,
            IList <Match> mums)
        {
            List <byte>             sequenceResult1 = new List <byte>();
            List <byte>             sequenceResult2 = new List <byte>();
            List <byte>             consensusResult = new List <byte>();
            PairwiseAlignedSequence alignedSequence = new PairwiseAlignedSequence();
            Match mum1;
            Match mum2;

            // Run the alignment for gap before first MUM
            List <long> insertions = new List <long>(2);

            insertions.Add(0);
            insertions.Add(0);

            List <long> gapInsertions;

            mum1 = mums.First();
            alignedSequence.Score += this.AlignGap(
                referenceSequence,
                sequence,
                sequenceResult1,
                sequenceResult2,
                consensusResult,
                new Match()
            {
                Length = 0
            },                                  // Here the first MUM does not exist
                mum1,
                out gapInsertions);

            insertions[0] += gapInsertions[0];
            insertions[1] += gapInsertions[1];

            // Run the alignment for all the gaps between MUM
            for (int index = 1; index < mums.Count; index++)
            {
                mum2 = mums[index];

                alignedSequence.Score += this.AlignGap(
                    referenceSequence,
                    sequence,
                    sequenceResult1,
                    sequenceResult2,
                    consensusResult,
                    mum1,
                    mum2,
                    out gapInsertions);

                insertions[0] += gapInsertions[0];
                insertions[1] += gapInsertions[1];

                mum1 = mum2;
            }

            // Run the alignment for gap after last MUM
            alignedSequence.Score += this.AlignGap(
                referenceSequence,
                sequence,
                sequenceResult1,
                sequenceResult2,
                consensusResult,
                mum1,
                new Match()
            {
                Length = 0
            },
                out gapInsertions);

            insertions[0] += gapInsertions[0];
            insertions[1] += gapInsertions[1];

            byte[]    result1  = sequenceResult1.ToArray();
            IAlphabet alphabet = Alphabets.AutoDetectAlphabet(result1, 0, result1.LongLength, referenceSequence.Alphabet);

            alignedSequence.FirstSequence = new Sequence(
                alphabet,
                result1)
            {
                ID = referenceSequence.ID,
                // Do not shallow copy dictionary
                //Metadata = referenceSequence.Metadata
            };

            byte[] result2 = sequenceResult2.ToArray();
            alphabet = Alphabets.AutoDetectAlphabet(result2, 0, result2.LongLength, sequence.Alphabet);

            alignedSequence.SecondSequence = new Sequence(
                alphabet,
                result2)
            {
                ID = sequence.ID,
                // Do not shallow copy dictionary
                //Metadata = sequence.Metadata
            };

            byte[] consensus = consensusResult.ToArray();
            alphabet = Alphabets.AutoDetectAlphabet(consensus, 0, consensus.LongLength, referenceSequence.Alphabet);
            alignedSequence.Consensus = new Sequence(
                alphabet,
                consensus);

            // Offset is not required as Smith Waterman will  fragmented alignment.
            // Offset is the starting position of alignment of sequence1 with respect to sequence2.
            if (this.PairWiseAlgorithm is NeedlemanWunschAligner)
            {
                alignedSequence.FirstOffset = alignedSequence.FirstSequence.IndexOfNonGap() -
                                              referenceSequence.IndexOfNonGap();
                alignedSequence.SecondOffset = alignedSequence.SecondSequence.IndexOfNonGap() -
                                               sequence.IndexOfNonGap();
            }

            List <long> startOffsets = new List <long>(2);
            List <long> endOffsets   = new List <long>(2);

            startOffsets.Add(0);
            startOffsets.Add(0);

            endOffsets.Add(referenceSequence.Count - 1);
            endOffsets.Add(sequence.Count - 1);

            alignedSequence.Metadata["StartOffsets"] = startOffsets;
            alignedSequence.Metadata["EndOffsets"]   = endOffsets;
            alignedSequence.Metadata["Insertions"]   = insertions;

            // return the aligned sequence
            return(alignedSequence);
        }
コード例 #52
0
        private void GenerateSequence(ISequence sequence)
        {
            var methodName = nameof(RelationalModelBuilderExtensions.HasSequence);

            if (sequence.ClrType != Sequence.DefaultClrType)
            {
                methodName += $"<{_code.Reference(sequence.ClrType)}>";
            }

            var parameters = _code.Literal(sequence.Name);

            if (!string.IsNullOrEmpty(sequence.Schema) &&
                sequence.Model.GetDefaultSchema() != sequence.Schema)
            {
                parameters += $", {_code.Literal(sequence.Schema)}";
            }

            var lines = new List <string> {
                $"modelBuilder.{methodName}({parameters})"
            };

            if (sequence.StartValue != Sequence.DefaultStartValue)
            {
                lines.Add($".{nameof(SequenceBuilder.StartsAt)}({sequence.StartValue})");
            }

            if (sequence.IncrementBy != Sequence.DefaultIncrementBy)
            {
                lines.Add($".{nameof(SequenceBuilder.IncrementsBy)}({sequence.IncrementBy})");
            }

            if (sequence.MinValue != Sequence.DefaultMinValue)
            {
                lines.Add($".{nameof(SequenceBuilder.HasMin)}({sequence.MinValue})");
            }

            if (sequence.MaxValue != Sequence.DefaultMaxValue)
            {
                lines.Add($".{nameof(SequenceBuilder.HasMax)}({sequence.MaxValue})");
            }

            if (sequence.IsCyclic != Sequence.DefaultIsCyclic)
            {
                lines.Add($".{nameof(SequenceBuilder.IsCyclic)}()");
            }

            if (lines.Count == 2)
            {
                lines = new List <string> {
                    lines[0] + lines[1]
                };
            }

            _sb.AppendLine();
            _sb.Append(lines[0]);

            using (_sb.Indent())
            {
                foreach (var line in lines.Skip(1))
                {
                    _sb.AppendLine();
                    _sb.Append(line);
                }
            }

            _sb.AppendLine(";");
        }
コード例 #53
0
ファイル: DatabaseGenerator.cs プロジェクト: dave1374/DbUtils
        protected override string GetNextValueRawSql(ISequence sequence)
        {
            var sqlServerSequence = (Sequence)sequence;

            return($"SELECT NEXT VALUE FOR [{sqlServerSequence.Schema}].[{sequence.Name}];");
        }
コード例 #54
0
        /// <summary>
        /// Find the matches of sequence in suffix tree
        /// </summary>
        /// <param name="suffixTree">Suffix Tree</param>
        /// <param name="searchSequence">Query searchSequence</param>
        /// <param name="lengthOfMUM">Mininum length of MUM</param>
        /// <returns>Matches found</returns>
        public IList <MaxUniqueMatch> FindMatches(
            SequenceSuffixTree suffixTree,
            ISequence searchSequence,
            long lengthOfMUM)
        {
            if (suffixTree == null)
            {
                throw new ArgumentNullException("suffixTree");
            }

            if (searchSequence == null)
            {
                throw new ArgumentNullException("searchSequence");
            }

            // Initialize
            _referenceString    = string.Empty;
            _minimumLengthOfMUM = lengthOfMUM;
            _suffixTree         = suffixTree;
            _searchSequence     = searchSequence;
            _queryString        = _searchSequence.ToString();
            SegmentedSequence referenceSequence = _suffixTree.Sequence as SegmentedSequence;

            if (null != referenceSequence)
            {
                foreach (Sequence sequence in referenceSequence.Sequences)
                {
                    _referenceString += sequence.ToString() + CONCATENATING_SYMBOL;
                }

                // remove the concatenating symbol form end and add terminating symbol
                _referenceString  = _referenceString.TrimEnd(CONCATENATING_SYMBOL);
                _referenceString += TERMINATING_SYMBOL;
            }
            else
            {
                _referenceString = _suffixTree.Sequence.ToString() + TERMINATING_SYMBOL;
            }

            int interval = (int)(_queryString.Length - (_minimumLengthOfMUM - 1)) / Environment.ProcessorCount;

            IList <Task <List <MaxUniqueMatch> > > result = new List <Task <List <MaxUniqueMatch> > >();

            for (int index = 0; index < _queryString.Length - (_minimumLengthOfMUM - 1); index += interval)
            {
                int taskIndex = index;
                result.Add(
                    Task.Factory.StartNew <List <MaxUniqueMatch> >(
                        o => FindMUMs(taskIndex, interval),
                        TaskCreationOptions.None));
            }

            List <MaxUniqueMatch> mergedList = new List <MaxUniqueMatch>();

            foreach (List <MaxUniqueMatch> local in result.Select(l => l.Result))
            {
                // Check if there is overlap, last MUM of mergedList overlaps with first MUM of local
                if (0 == mergedList.Count)
                {
                    mergedList.AddRange(local.Select(m => m));
                }
                else
                {
                    if (0 < local.Count)
                    {
                        MaxUniqueMatch previous = mergedList.Last();
                        MaxUniqueMatch current  = local.First();

                        if ((current.SecondSequenceStart >= previous.SecondSequenceStart &&
                             current.SecondSequenceStart <= previous.SecondSequenceStart + previous.Length) &&
                            (current.SecondSequenceStart + current.Length >= previous.SecondSequenceStart &&
                             current.SecondSequenceStart + current.Length <= previous.SecondSequenceStart + previous.Length))
                        {
                            local.RemoveAt(0);
                        }

                        if (0 < local.Count)
                        {
                            mergedList.AddRange(local.Select(m => m));
                        }
                    }
                }
            }
            // Order the mum list with query sequence order
            for (int index = 0; index < mergedList.Count; index++)
            {
                mergedList[index].FirstSequenceMumOrder  = index + 1;
                mergedList[index].SecondSequenceMumOrder = index + 1;
            }

            return(mergedList);
        }
コード例 #55
0
 public InvocableSequenceExceptionsOrReturns(ISetup <TMock, TReturn> wrapped, Mock mock, ISequence sequence,
                                             IInvocationResponder <ISetup <TMock, TReturn>, ExceptionOrReturn <TReturn> > invocationResponder) : base(wrapped, mock, sequence, invocationResponder)
 {
 }
コード例 #56
0
ファイル: Discarder.cs プロジェクト: kcha/seqcos
 /// <summary>
 /// Abstract method for discarding reads
 /// </summary>
 abstract public bool CanDiscard(ISequence seqObj);
コード例 #57
0
        /// <summary>
        /// Builds the Suffix Tree using Kurtz Algorithm(using Hash Table)
        /// </summary>
        /// <example>
        /// --------------------------------------------------
        /// Create the Sequence from string (let say DNA sequence "CACCAS")
        /// --------------------------------------------------
        /// string aOriginalStr = "CACCAS";
        /// Sequence aInput = new Sequence(Alphabets.DNA, aOriginalStr);
        /// --------------------------------------------------
        /// Instantiate and run the suffix tree builder
        /// --------------------------------------------------
        /// ISuffixTreeBuilder suffixTreeBuilder = new KurtzSuffixTreeBuilder();
        /// SuffixTree suffixTree = suffixTreeBuilder.BuildSuffixTree(aInput);
        /// </example>
        /// <param name="sequence">Input Sequence</param>
        /// <returns>Suffix Tree</returns>
        public SequenceSuffixTree BuildSuffixTree(ISequence sequence)
        {
            // Initialize
            Edge.NodeCount = 1;
            SegmentedSequence referenceSequence = sequence as SegmentedSequence;

            if (null != referenceSequence)
            {
                foreach (Sequence subSequence in referenceSequence.Sequences)
                {
                    _referenceString += subSequence.ToString() + CONCATENATING_SYMBOL;
                }

                // remove the concatenating symbol form end and add terminating symbol
                _referenceString  = _referenceString.TrimEnd(CONCATENATING_SYMBOL);
                _referenceString += TERMINATING_SYMBOL;
            }
            else
            {
                _referenceString = sequence.ToString() + TERMINATING_SYMBOL;
            }

            _suffixTree = new SequenceSuffixTree(sequence);

            // Create Tasks
            Dictionary <char, IList <int> > treeTasks = new Dictionary <char, IList <int> >();

            // Loop through subset of sequence string and build the suffix tree
            for (int index = 0; index < _referenceString.Length; index++)
            {
                IList <int> startIndices = null;

                if (!treeTasks.TryGetValue(_referenceString[index], out startIndices))
                {
                    startIndices = new List <int>();
                    treeTasks.Add(_referenceString[index], startIndices);
                }

                startIndices.Add(index);
            }

            IList <Task <SequenceSuffixTree> > tasks = treeTasks.Values.Select(
                indices => Task <SequenceSuffixTree> .Factory.StartNew(
                    t => AppendSuffix(indices, sequence), TaskCreationOptions.None)).ToList();

            // Wait for all the task
            Task.WaitAll(tasks.ToArray());

            // Merge the branches of tree
            Edge edgeFound = null;

            foreach (Task <SequenceSuffixTree> task in tasks)
            {
                foreach (KeyValuePair <int, Edge> edge in task.Result.Edges)
                {
                    if (_suffixTree.Edges.TryGetValue(edge.Key, out edgeFound))
                    {
                        Insert(edgeFound, _suffixTree);
                    }
                    else
                    {
                        _suffixTree.Edges.Add(edge.Key, edge.Value);
                    }
                }
            }

            // return the suffix tree
            return(_suffixTree);
        }
コード例 #58
0
 public StringPathPartsSequence(IProperty <string> path, ISequence <ITextRange> sequence = null)
 {
     _path     = path;
     _sequence = sequence ?? new RunOnceSequence <ITextRange>(new TextRangeSequence(new CachedValue <IText>(() => new StringText(path.Value)), new CachedValue <int>(() => path.Value.Length), Path.DirectorySeparatorChar));
 }
コード例 #59
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="sequence">Sequence to wrap</param>
 public SequenceViewModel(ISequence sequence)
 {
     _sequence = sequence;
 }
コード例 #60
0
        public static void TryAddPosition(Dictionary <IChromosome, List <int> > chromPositions, IChromosome chromosome,
                                          int position, string refAllele, string altAllele, ISequence refSequence)
        {
            if (!chromPositions.ContainsKey(chromosome))
            {
                chromPositions.Add(chromosome, new List <int>(16 * 1024));
            }

            foreach (string allele in altAllele.OptimizedSplit(','))
            {
                if (allele.OptimizedStartsWith('<') && allele != "<NON_REF>" || allele.Contains('[') || altAllele.Contains(']'))
                {
                    continue;
                }

                (int shiftedPos, string _, string _) =
                    VariantUtils.TrimAndLeftAlign(position, refAllele, allele, refSequence);
                chromPositions[chromosome].Add(shiftedPos);
            }
        }