public async Task <string> Align(string FirstSequence, string SecondSequence, string ScoringMatrixName, string Email) { if (string.IsNullOrWhiteSpace(FirstSequence) || string.IsNullOrWhiteSpace(SecondSequence)) { return("Sequence Can't be empty"); } if (FirstSequence.Length > 20000 || SecondSequence.Length > 20000) { return("Sequence length Can't be greater than 20K"); } if (!Regex.IsMatch(FirstSequence, @"^[a-zA-Z]+$") || !Regex.IsMatch(SecondSequence, @"^[a-zA-Z]+$")) { return("Sequence must contains only characters"); } IdentityUser MyUser = await UserManager.FindByEmailAsync(Email); if (MyUser == null) { return("You have to sign-up first to be able to use our alignmnet serive"); } AlignmentJob JobFound = Repo.AreExist(FirstSequence, SecondSequence); if (JobFound == null) { JobFound = new AlignmentJob() { AlignmentID = Guid.NewGuid().ToString(), Algorithm = "ParallelNeedlemanWunsch", ScoringMatrix = ScoringMatrixName.ToUpper(), FirstSequenceHash = Helper.SHA1HashStringForUTF8String(FirstSequence), SecondSequenceHash = Helper.SHA1HashStringForUTF8String(SecondSequence), FirstSequenceName = "Web Service Call", SecondSequenceName = "Web Service Call", GapOpenPenalty = -2, Gap = -8, GapExtensionPenalty = -2 }; SequenceAligner AlgorithmInstance = DynamicInvoke.GetAlgorithm(JobFound.Algorithm); ScoringMatrix ScoringMatrixInstance; try { ScoringMatrixInstance = DynamicInvoke.GetScoreMatrix(JobFound.ScoringMatrix); } catch { return("The Score Matrix Name is invalid."); } string AlignmentResult = string.Empty; float AlignmentScore = 0.0f; await Task.Run(() => { AlignedSequences Result = AlgorithmInstance.Align(FirstSequence, SecondSequence, ScoringMatrixInstance, -8); AlignmentResult = Result.StandardFormat(210); AlignmentScore = Result.AlignmentScore(ScoringMatrixInstance); }); JobFound.ByteText = Helper.GetText(AlignmentResult, AlignmentScore, JobFound.AlignmentID, "ParallelNeedlemanWunsch", ScoringMatrixName, -8, -2, -2); JobFound.UserFK = MyUser.Id; await Repo.AddAlignmentJobAsync(JobFound); return(Encoding.UTF8.GetString(JobFound.ByteText)); } else { return(Encoding.UTF8.GetString(JobFound.ByteText)); } }
public async Task <IActionResult> Align(SequenceViewModel Model, IFormFile FirstFile, IFormFile SecondFile) { if (!string.IsNullOrWhiteSpace(Model.FirstSequence)) { Model.FirstSequence = Model.FirstSequence.Trim().Replace(" ", string.Empty).ToUpper(); } if (!string.IsNullOrWhiteSpace(Model.SecondSequence)) { Model.SecondSequence = Model.SecondSequence.Trim().Replace(" ", string.Empty).ToUpper(); } if (string.IsNullOrWhiteSpace(Model.FirstSequence) && FirstFile != null) { if (FirstFile.ContentType == "text/plain") { string FirstSequence = (await Helper.ConvertFileByteToByteStringAsync(FirstFile)).Trim().Replace(" ", string.Empty).ToUpper(); if (FirstSequence.Length > 10000) { return(RedirectToAction("Grid", "Alignment")); } else if (FirstSequence.Length == 0) { return(View("Error", new ErrorViewModel { Message = "You Can't send a sequence of 0 Length", Solution = "You should send a sequence greater than 0 length" })); } else { Model.FirstSequence = FirstSequence; } } else { return(View("Error", new ErrorViewModel { Message = "You Can't upload a file of any type rather than txt file format", Solution = "You should upload a file of txt file format" })); } } if (string.IsNullOrWhiteSpace(Model.SecondSequence) && SecondFile != null) { if (SecondFile.ContentType == "text/plain") { string SecondSequence = (await Helper.ConvertFileByteToByteStringAsync(SecondFile)).Trim().Replace(" ", string.Empty).ToUpper(); if (SecondSequence.Length > 10000) { return(RedirectToAction("Grid", "Alignment")); } else if (SecondSequence.Length == 0) { return(View("Error", new ErrorViewModel { Message = "You Can't send a sequence of 0 Length", Solution = "You should send a sequence greater than 0 length" })); } else { Model.SecondSequence = SecondSequence; } } else { return(View("Error", new ErrorViewModel { Message = "You Can't upload a file of any type rather than txt file format", Solution = "You should upload a file of txt file format" })); } } if ((Model.FirstSequence == null && FirstFile == null) || (Model.SecondSequence == null && SecondFile == null)) { return(View("Error", new ErrorViewModel { Message = "You Can't enter an empty sequence", Solution = "You have to enter the sequence or either upload a file contains the sequence" })); } if (!Regex.IsMatch(Model.FirstSequence, @"^[a-zA-Z]+$") || !Regex.IsMatch(Model.SecondSequence, @"^[a-zA-Z]+$")) { return(View("Error", new ErrorViewModel { Message = "Your sequence must contains only characters", Solution = "Send sequence contains only characters" })); } AlignmentJob JobFound = Repo.AreExist(Model.FirstSequence, Model.SecondSequence, Model.ScoringMatrix, Model.Gap); if (JobFound == null) { JobFound = new AlignmentJob() { AlignmentID = Guid.NewGuid().ToString(), Algorithm = Model.Algorithm, ScoringMatrix = Model.ScoringMatrix, FirstSequenceHash = Helper.SHA1HashStringForUTF8String(Model.FirstSequence), SecondSequenceHash = Helper.SHA1HashStringForUTF8String(Model.SecondSequence), FirstSequenceName = Model.FirstSequenceName, SecondSequenceName = Model.SecomdSequenceName, GapOpenPenalty = Model.GapOpenPenalty, Gap = Model.Gap, GapExtensionPenalty = Model.GapExtensionPenalty, IsAlignmentCompleted = true, }; SequenceAligner AlgorithmInstance = DynamicInvoke.GetAlgorithm(Model.Algorithm); ScoringMatrix ScoringMatrixInstance = DynamicInvoke.GetScoreMatrix(Model.ScoringMatrix); string AlignmentResult = string.Empty; float AlignmentScore = 0.0f; AlignedSequences Result = AlgorithmInstance.Align(Model.FirstSequence, Model.SecondSequence, ScoringMatrixInstance, Model.Gap); AlignmentResult = Result.StandardFormat(210); AlignmentScore = Result.AlignmentScore(ScoringMatrixInstance); JobFound.ByteText = Helper.GetText(AlignmentResult, AlignmentScore, JobFound.AlignmentID, Model.Algorithm, Model.ScoringMatrix, Model.Gap, Model.GapOpenPenalty, Model.GapExtensionPenalty); JobFound.UserFK = UserManager.GetUserId(User); await Repo.AddAlignmentJobAsync(JobFound); if (Model.DownloadDirectly == 1) { return(File(JobFound.ByteText, "text/plain", $"{JobFound.AlignmentID}_Alignment_Result.txt")); } else { return(RedirectToAction("Display", "Profile", new { AlignmentID = JobFound.AlignmentID })); } } else { if (Model.DownloadDirectly == 1) { return(File(JobFound.ByteText, "text/plain", $"{JobFound.AlignmentID}_Alignment_Result.txt")); } else { return(RedirectToAction("Display", "Profile", new { AlignmentID = JobFound.AlignmentID })); } } }