Esempio n. 1
0
        /// <summary>
        /// Loads sequence by id from db and converts it to <see cref="Chain"/>.
        /// </summary>
        /// <param name="sequenceId">
        /// The sequence id.
        /// </param>
        /// <returns>
        /// The sequence as <see cref="Chain"/>.
        /// </returns>
        public Chain GetLibiadaChain(long sequenceId)
        {
            if (Db.CommonSequence.Any(s => s.Id == sequenceId))
            {
                var matter = Db.CommonSequence.Include(s => s.Matter).Single(s => s.Id == sequenceId).Matter;
                return(new Chain(Db.GetSequenceBuilding(sequenceId), GetAlphabet(sequenceId), sequenceId));
            }

            // if it is not "real" sequence , then it must be image "sequence"
            var imageSequence = Db.ImageSequences.Include(s => s.Matter).Single(s => s.Id == sequenceId);

            if (imageSequence.Matter.Nature != Nature.Image)
            {
                throw new Exception("Cannot find sequence to return");
            }

            var image          = Image.Load(imageSequence.Matter.Source);
            var orderExtractor = imageSequence.OrderExtractor.GetAttribute <ImageOrderExtractor, ImageOrderExtractorAttribute>().Value;
            var sequence       = ImageProcessor.ProcessImage(image, new IImageTransformer[0], new IMatrixTransformer[0], (IImageOrderExtractor)Activator.CreateInstance(orderExtractor));
            var alphabet       = new Alphabet {
                NullValue.Instance()
            };
            var incompleteAlphabet = sequence.Alphabet;

            for (int j = 0; j < incompleteAlphabet.Cardinality; j++)
            {
                alphabet.Add(incompleteAlphabet[j]);
            }

            return(new Chain(sequence.Building, alphabet));
        }
Esempio n. 2
0
        /// <summary>
        /// The to libiada alphabet.
        /// </summary>
        /// <param name="elementIds">
        /// The element ids.
        /// </param>
        /// <returns>
        /// The <see cref="Alphabet"/>.
        /// </returns>
        public Alphabet ToLibiadaAlphabet(long[] elementIds)
        {
            var alphabet = new Alphabet {
                NullValue.Instance()
            };
            List <Element> elements = GetElements(elementIds);

            foreach (long elementId in elementIds)
            {
                Element el = elements.Single(e => e.Id == elementId);
                alphabet.Add(new ValueString(el.Value));
            }

            return(alphabet);
        }
Esempio n. 3
0
        public ActionResult Index(short[] characteristicLinkIds, string[] customSequences, bool localFile, string fileType, bool toLower, bool removePunctuation)
        {
            return(CreateTask(() =>
            {
                int sequencesCount = localFile ? Request.Files.Count : customSequences.Length;
                var sequencesNames = new string[sequencesCount];
                var sequences = new Chain[sequencesCount];
                if (localFile)
                {
                    for (int i = 0; i < sequencesCount; i++)
                    {
                        Stream sequenceStream = FileHelper.GetFileStream(Request.Files[i]);
                        sequencesNames[i] = Request.Files[i].FileName;

                        switch (fileType)
                        {
                        case "text":
                            using (var sr = new StreamReader(sequenceStream))
                            {
                                string stringTextSequence = sr.ReadToEnd();
                                if (toLower)
                                {
                                    stringTextSequence = stringTextSequence.ToLower();
                                }
                                if (removePunctuation)
                                {
                                    stringTextSequence = Regex.Replace(stringTextSequence, @"[^\w\s]", "");
                                }
                                sequences[i] = new Chain(stringTextSequence);
                            }
                            break;

                        case "image":
                            var image = Image.Load(sequenceStream);
                            var sequence = ImageProcessor.ProcessImage(image, new IImageTransformer[0], new IMatrixTransformer[0], new LineOrderExtractor());
                            var alphabet = new Alphabet {
                                NullValue.Instance()
                            };
                            var incompleteAlphabet = sequence.Alphabet;
                            for (int j = 0; j < incompleteAlphabet.Cardinality; j++)
                            {
                                alphabet.Add(incompleteAlphabet[j]);
                            }

                            sequences[i] = new Chain(sequence.Building, alphabet);
                            break;

                        case "genetic":
                            ISequence fastaSequence = NcbiHelper.GetFastaSequence(sequenceStream);
                            var stringSequence = fastaSequence.ConvertToString();
                            sequences[i] = new Chain(stringSequence);
                            sequencesNames[i] = fastaSequence.ID;
                            break;

                        case "wavFile":
                            var reader = new BinaryReader(Request.Files[i].InputStream);

                            int chunkID = reader.ReadInt32();
                            int fileSize = reader.ReadInt32();
                            int riffType = reader.ReadInt32();
                            int fmtID = reader.ReadInt32();
                            int fmtSize = reader.ReadInt32();
                            int fmtCode = reader.ReadInt16();
                            int channels = reader.ReadInt16();
                            int sampleRate = reader.ReadInt32();
                            int fmtAvgBPS = reader.ReadInt32();
                            int fmtBlockAlign = reader.ReadInt16();
                            int bitDepth = reader.ReadInt16();

                            if (fmtSize == 18)
                            {
                                // Read any extra values
                                int fmtExtraSize = reader.ReadInt16();
                                reader.ReadBytes(fmtExtraSize);
                            }

                            int dataID = reader.ReadInt32();
                            int dataSize = reader.ReadInt32();
                            byte[] byteArray = reader.ReadBytes(dataSize);
                            var shortArray = new short[byteArray.Length / 2];
                            Buffer.BlockCopy(byteArray, 0, shortArray, 0, byteArray.Length);
                            //shortArray = Amplitude(shortArray, 20);
                            shortArray = Sampling(shortArray, 50);
                            //shortArray = shortArray.Select(s => (short)(s / 10)).ToArray();
                            sequences[i] = new Chain(shortArray);
                            break;

                        default:
                            throw new ArgumentException("Unknown file type", nameof(fileType));
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < sequencesCount; i++)
                    {
                        sequences[i] = new Chain(customSequences[i]);
                        sequencesNames[i] = $"Custom sequence {i + 1}. Length: {customSequences[i].Length}";
                    }
                }

                var sequencesCharacteristics = new SequenceCharacteristics[sequences.Length];
                for (int j = 0; j < sequences.Length; j++)
                {
                    var characteristics = new double[characteristicLinkIds.Length];
                    for (int k = 0; k < characteristicLinkIds.Length; k++)
                    {
                        Link link = characteristicTypeLinkRepository.GetLinkForCharacteristic(characteristicLinkIds[k]);
                        FullCharacteristic characteristic = characteristicTypeLinkRepository.GetCharacteristic(characteristicLinkIds[k]);
                        IFullCalculator calculator = FullCalculatorsFactory.CreateCalculator(characteristic);

                        characteristics[k] = calculator.Calculate(sequences[j], link);
                    }

                    sequencesCharacteristics[j] = new SequenceCharacteristics
                    {
                        MatterName = sequencesNames[j],
                        Characteristics = characteristics
                    };
                }

                var characteristicNames = new string[characteristicLinkIds.Length];
                var characteristicsList = new SelectListItem[characteristicLinkIds.Length];
                for (int k = 0; k < characteristicLinkIds.Length; k++)
                {
                    characteristicNames[k] = characteristicTypeLinkRepository.GetCharacteristicName(characteristicLinkIds[k]);
                    characteristicsList[k] = new SelectListItem
                    {
                        Value = k.ToString(),
                        Text = characteristicNames[k],
                        Selected = false
                    };
                }

                var result = new Dictionary <string, object>
                {
                    { "characteristics", sequencesCharacteristics },
                    { "characteristicNames", characteristicNames },
                    { "characteristicsList", characteristicsList }
                };

                return new Dictionary <string, object>
                {
                    { "data", JsonConvert.SerializeObject(result) }
                };
            }));
        }