public ActionResult GetBlastSearchResult(string projectId)
        {
            ObjectId        projId  = ObjectId.Parse(projectId);
            SequenceProject project = db.GetItem <SequenceProject>(x => x._id == projId);

            return(Json(bioService.GetBlastSearchResult(project, Server.MapPath("~/ServiceResults/"), GetActualUserProject())));
        }
        private IList <AlignRecord> GetRecordsFromResult(ObjectId projectId, IList <string> records)
        {
            IList <AlignRecord> alignRecords = new List <AlignRecord>();
            SequenceProject     sProject     = db.GetItem <SequenceProject>(x => x._id == projectId);

            if (sProject != null)
            {
                IList <Sequence> sequences = GetSequenceForProject(projectId.ToString(), null);

                int index = 0;
                while (index < records.Count)
                {
                    if (records[index].StartsWith(">"))
                    {
                        AlignRecord record = new AlignRecord();
                        record.AlignedSequence = records[index + 1];
                        record.SequenceHeader  = records[index];
                        Sequence findedSeq = sequences.FirstOrDefault(x => x.FastaProtein.StartsWith(records[index]));
                        index += 2;
                        if (findedSeq != null)
                        {
                            record.SequenceId   = findedSeq._id;
                            record.SequenceName = findedSeq.Name;
                            record.Organism     = findedSeq.Organism;
                            alignRecords.Add(record);
                        }
                    }
                }
            }
            return(alignRecords);
        }
        public DataSourceResult GetAlignmentForProject([DataSourceRequest] DataSourceRequest request, string projectId)
        {
            foreach (Kendo.Mvc.SortDescriptor sd in request.Sorts)
            {
                sd.Member = ConvertSequenceViewModel(sd.Member);
            }
            foreach (Kendo.Mvc.FilterDescriptor fd in request.Filters.Where(x => x is Kendo.Mvc.FilterDescriptor))
            {
                fd.Member = ConvertSequenceViewModel(fd.Member);
            }
            foreach (
                Kendo.Mvc.CompositeFilterDescriptor fd in request.Filters.Where(x => x is Kendo.Mvc.CompositeFilterDescriptor))
            {
                ConvertSequenceFilters(fd);
            }
            ObjectId id = ObjectId.Empty;

            if (ObjectId.TryParse(projectId, out id))
            {
                SequenceProject project = db.GetItem <SequenceProject>(x => x._id == id);
                IQueryable <AlignmentSequence> querySeq = project != null && project.Alligments != null?project.Alligments.AsQueryable() : new List <AlignmentSequence>().AsQueryable();

                return(querySeq.ToDataSourceResult(request));
            }
            return(new DataSourceResult());
        }
        public ActionResult AddTreePicture(FormCollection col, TreeModel model)
        {
            if (Request.Files.Count > 0)
            {
                var file = Request.Files[0];

                if (file != null && file.ContentLength > 0)
                {
                    string fileName = Path.GetFileName(file.FileName);

                    var path = Path.Combine(Server.MapPath("~/ServiceResults/Trees/"), fileName);
                    file.SaveAs(path);
                    ObjectId        projId  = ObjectId.Parse(model.ProjectId);
                    SequenceProject project = db.GetItem <SequenceProject>(x => x._id == projId);
                    if (project != null && model.AlignmentId != String.Empty)
                    {
                        AlignmentSequence alignment = project.Alligments.First(x => x.IdString == model.AlignmentId);
                        if (alignment != null)
                        {
                            alignment.TreePicture = path;
                            db.UpdateItem(project);
                        }
                    }
                }
            }
            return(RedirectToAction("ViewTree", new { projectId = model.ProjectId, alignmentId = model.AlignmentId }));
        }
        public ActionResult ViewTree(string projectId, string alignmentId)
        {
            ObjectId        projId  = ObjectId.Parse(projectId);
            SequenceProject project = db.GetItem <SequenceProject>(x => x._id == projId);

            if (project != null && alignmentId != String.Empty)
            {
                AlignmentSequence alignment = project.Alligments.First(x => x.IdString == alignmentId);
                if (alignment != null && !String.IsNullOrEmpty(alignment.PhylogeneticTree))
                {
                    string fileContent = String.Empty;
                    if (System.IO.File.Exists(alignment.TreePicture))
                    {
                        using (StreamReader sr = new StreamReader(alignment.TreePicture))
                        {
                            fileContent = sr.ReadToEnd();
                        }
                    }
                    return(View("ViewTree", new TreeModel()
                    {
                        TreeString = alignment.PhylogeneticTree, AlignmentName = alignment.Description, ProjectId = projectId, AlignmentId = alignmentId, TreeFile = fileContent
                    }));
                }
            }
            return(View());
        }
        public ActionResult GetTreeResult(string projectId, string alignmentId)
        {
            ObjectId        projId  = ObjectId.Parse(projectId);
            SequenceProject project = db.GetItem <SequenceProject>(x => x._id == projId);

            return(Json(bioService.GetSimplePhyloResult(project, alignmentId)));
        }
 public void SaveProject(SequenceProject project, ObjectId creatorId)
 {
     if (project._id == ObjectId.Empty)
     {
         db.AddItem(project, creatorId);
     }
     else
     {
         db.UpdateItem(project);
     }
 }
        public DataSourceResult GetSequenceForProject([DataSourceRequest] DataSourceRequest request, string projectId, string[] taxons, bool isBlast = false)
        {
            foreach (Kendo.Mvc.SortDescriptor sd in request.Sorts)
            {
                sd.Member = ConvertSequenceViewModel(sd.Member);
            }
            foreach (Kendo.Mvc.FilterDescriptor fd in request.Filters.Where(x => x is Kendo.Mvc.FilterDescriptor))
            {
                fd.Member = ConvertSequenceViewModel(fd.Member);
            }
            foreach (
                Kendo.Mvc.CompositeFilterDescriptor fd in request.Filters.Where(x => x is Kendo.Mvc.CompositeFilterDescriptor))
            {
                ConvertSequenceFilters(fd);
            }
            ObjectId id = ObjectId.Empty;

            if (ObjectId.TryParse(projectId, out id))
            {
                IQueryable <Sequence> querySeq = null;
                if (taxons != null)
                {
                    querySeq = db.GetCollection <Sequence>(x => x.ProjectId == id && x.Lineage != null).AsQueryable();
                    IList <Sequence> sequences    = querySeq.ToList();
                    IList <Sequence> hlpSequences = new List <Sequence>();
                    foreach (string taxon in taxons)
                    {
                        hlpSequences.AddRange(sequences.Where(x => x.Lineage.Contains(taxon)).ToList());
                    }
                    SequenceProject project = db.GetItem <SequenceProject>(x => x._id == id);
                    project.ActualTaxons = taxons.ToList();
                    db.UpdateItem(project);
                    querySeq = hlpSequences.AsQueryable();
                }
                else
                {
                    querySeq = db.GetCollection <Sequence>(x => x.ProjectId == id).AsQueryable();
                }
                if (isBlast)
                {
                    querySeq = querySeq.Where(x => !String.IsNullOrEmpty(x.BlastAlignedSequence)).ToList().AsQueryable();
                }
                else
                {
                    querySeq = querySeq.Where(x => String.IsNullOrEmpty(x.BlastAlignedSequence)).ToList().AsQueryable();
                }

                return(querySeq.ToDataSourceResult(request));
            }
            return(new DataSourceResult());
        }
        public void GenerateBlastSearch(ObjectId projectId, Models.BlastModels.BlastViewModel model)
        {
            SequenceProject project = db.GetItem <SequenceProject>(x => x._id == projectId);

            if (project != null)
            {
                blastService.SetParams(new ServiceConnector.BlastConnector.InputParameters()
                {
                    stype = "protein", program = "blastp", matrix = "BLOSUM62", sequence = model.Sequence, database = new string[] { model.DatabaseParam }, scores = Int32.Parse(model.ScoresParam)
                });
                project.BlastSearchGuid = blastService.Run("BlastSearch");
                db.UpdateItem(project);
            }
        }
        public bool SelUnSelAllSequence(string projectId, bool sel, bool isBlast)
        {
            ObjectId id = ObjectId.Empty;

            if (ObjectId.TryParse(projectId, out id))
            {
                var querySeq = isBlast ? db.GetCollection <Sequence>(x => x.ProjectId == id && !String.IsNullOrEmpty(x.BlastAlignedSequence)).ToList() :
                               db.GetCollection <Sequence>(x => x.ProjectId == id && String.IsNullOrEmpty(x.BlastAlignedSequence)).ToList();
                querySeq.ForEach(x => x.Selected = sel);
                SequenceProject project = db.GetItem <SequenceProject>(x => x._id == id);
                project.ActualTaxons = new List <string>();
                db.UpdateItem(project);
                UpdateSequenceData(querySeq);
            }
            return(false);
        }
        public ActionResult ViewAlignment(string projectId, string alignmentId)
        {
            ObjectId        projId  = ObjectId.Parse(projectId);
            SequenceProject project = db.GetItem <SequenceProject>(x => x._id == projId);

            if (project != null && alignmentId != String.Empty)
            {
                AlignmentSequence alignment = project.Alligments.First(x => x.IdString == alignmentId);
                if (alignment != null && alignment.AllignedSequences != null && alignment.AllignedSequences.Any())
                {
                    return(View("ViewAlignmentRaw", new AlignmentModel()
                    {
                        AlignmentSequences = alignment.AllignedSequences, AlignmentName = alignment.Description, AlignmentId = alignmentId, ProjectId = projectId
                    }));
                }
            }
            return(View(new HtmlString("")));
        }
        public bool GetSimplePhyloResult(SequenceProject project, string alignmentId)
        {
            IList <string> records = new List <string>();

            if (project != null && project.Alligments != null)
            {
                AlignmentSequence alignment = project.Alligments.First(x => x._id == ObjectId.Parse(alignmentId));
                string            result    = alignment.PhylogeneticFilename + ".tree.ph";
                if (alignment != null)
                {
                    if (!File.Exists(result))
                    {
                        result = phylologyService.GetResults(alignment.PhylogeneticGuid, alignment.PhylogeneticFilename);
                        if (String.IsNullOrEmpty(result))
                        {
                            return(false);
                        }
                        result = result + ".tree.ph";
                    }
                    if (!String.IsNullOrEmpty(result))
                    {
                        using (StreamReader sr = new StreamReader(result))
                        {
                            StringBuilder sb = new StringBuilder();
                            while (!sr.EndOfStream)
                            {
                                string hlpString = sr.ReadLine();
                                sb.AppendLine(hlpString);
                                records.Add(hlpString);
                            }
                            foreach (string s in records)
                            {
                                alignment.PhylogeneticTree += s;
                            }
                            db.UpdateItem(project);
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
        public bool GetClustalResult(SequenceProject project, string alignmentId)
        {
            IList <string> records = new List <string>();

            if (project != null && project.Alligments != null)
            {
                AlignmentSequence alignment = project.Alligments.First(x => x._id == ObjectId.Parse(alignmentId));
                string            result    = alignment.AlignmentFilename + ".vienna.aln-vienna.vienna";
                if (alignment != null)
                {
                    if (!File.Exists(result))
                    {
                        result = clustalService.GetResults(alignment.AlignmentGuid, alignment.AlignmentFilename);
                        if (String.IsNullOrEmpty(result))
                        {
                            return(false);
                        }
                        result = result + ".aln-vienna.vienna";
                    }
                    if (!String.IsNullOrEmpty(result))
                    {
                        using (StreamReader sr = new StreamReader(result))
                        {
                            StringBuilder sb = new StringBuilder();
                            while (!sr.EndOfStream)
                            {
                                string hlpString = sr.ReadLine();
                                sb.AppendLine(hlpString);
                                records.Add(hlpString);
                            }
                            alignment.AllignedSequences = GetRecordsFromResult(project._id, records);
                            alignment.IsFinished        = true;
                            db.UpdateItem(project);
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
        private IList <Sequence> CreateBlastSequencies(hits hits, SequenceProject project, UserProject userProject)
        {
            IList <Sequence> sequencies = new List <Sequence>();

            if (hits != null && hits.seqHits != null)
            {
                WebClient webClient = new WebClient();
                foreach (hit hit in hits.seqHits)
                {
                    if (IsReadyForUpdate(hit.alignmets))
                    {
                        Sequence sequence = new Sequence();
                        sequence.ProjectId = userProject.ProjectId;
                        //výsledek zarovnání blast
                        sequence.BlastAlignedSequence = hit.alignmets.alignment[0].patternSeq.matchSeqValue;
                        //fasta organismu
                        sequence.FastaProtein = hit.alignmets.alignment[0].matchSeq.matchSeqValue;
                        sequence.Created      = DateTime.Now;
                        sequence.Description  = hit.description;
                        PopulateLineageBlast(sequence, webClient);
                        if (!String.IsNullOrEmpty(sequence.Organism) && sequence.Lineage != null && sequence.Lineage.Any())
                        {
                            if (!sequence.Name.Contains("Fragment"))
                            {
                                if (!sequencies.Select(x => x.Organism).Contains(sequence.Organism))
                                {
                                    sequencies.Add(sequence);
                                }
                            }
                        }
                    }
                }
                db.AddItems(sequencies, userProject.UserId);
            }
            return(sequencies);
        }
        public void GenerateTree(ObjectId projectId, string alignmentId, string path)
        {
            SequenceProject project = db.GetItem <SequenceProject>(x => x._id == projectId);

            if (project != null)
            {
                AlignmentSequence aligment = project.Alligments.FirstOrDefault(x => x.IdString == alignmentId);
                if (aligment != null)
                {
                    string seq = String.Empty;
                    foreach (AlignRecord rec in aligment.AllignedSequences)
                    {
                        seq += ">" + rec.Organism.Replace(" ", "_") + "\n" + rec.AlignedSequence + "\n";
                    }
                    phylologyService.SetParams(new ServiceConnector.PhylogenyConnector.InputParameters()
                    {
                        sequence = seq, tree = "phylip"
                    });
                    aligment.PhylogeneticGuid     = phylologyService.Run(aligment.Description);
                    aligment.PhylogeneticFilename = path + aligment.PhylogeneticGuid;
                    db.UpdateItem(project);
                }
            }
        }
 public bool GetBlastSearchResult(SequenceProject project, string path, UserProject userProject)
 {
     if (project != null)
     {
         //XmlResult
         string result = path + project.BlastSearchGuid + ".xml.xml";
         if (!File.Exists(result))
         {
             result = blastService.GetResults(project.BlastSearchGuid, result);
             if (String.IsNullOrEmpty(result))
             {
                 return(false);
             }
         }
         if (!String.IsNullOrEmpty(result))
         {
             using (StreamReader sr = new StreamReader(result))
             {
                 StringBuilder        sb        = new StringBuilder();
                 XmlReader            reader    = XmlReader.Create(sr);
                 EBIApplicationResult ebiResult = GenerateSequencesFromXml(reader);
                 if (ebiResult != null && ebiResult.SequenceSimilaritySearchResult != null)
                 {
                     IList <Sequence> sequencies = CreateBlastSequencies(ebiResult.SequenceSimilaritySearchResult.hits, project, userProject);
                     if (project.Alligments == null)
                     {
                         project.Alligments = new List <AlignmentSequence>();
                     }
                     if (sequencies != null && sequencies.Any())
                     {
                         AlignmentSequence allign = new AlignmentSequence()
                         {
                             Created           = DateTime.Now,
                             CreatorId         = project.CreatorId,
                             Description       = "BlastAlignment",
                             IsFinished        = false,
                             _id               = ObjectId.GenerateNewId(),
                             Source            = "Blast",
                             AllignedSequences = new List <AlignRecord>()
                         };
                         foreach (Sequence seq in sequencies)
                         {
                             allign.AllignedSequences.Add(new AlignRecord()
                             {
                                 AlignedSequence = seq.BlastAlignedSequence,
                                 Organism        = seq.Organism,
                                 SequenceId      = seq._id,
                                 SequenceName    = seq.Name,
                                 SequenceHeader  = seq.Description
                             });
                         }
                         project.Alligments.Add(allign);
                         db.UpdateItem(project);
                         return(true);
                     }
                 }
             }
         }
     }
     return(false);
 }