// TODO: Fix the Calls to include Either FormValues Dictionary or DTO. public ActionResult Index(Guid?id, HttpPostedFileBase file) { if (file == null || file.ContentLength == 0 || file.ContentType == null || string.IsNullOrEmpty(file.FileName) || file.InputStream == null) { this.ModelState.AddModelError("PostedFile", "Must attach file."); return(View()); } if (file.ContentLength > 0) { string path = Path.Combine(ServerMapPath, id.ToString() + ".txt"); string fileName = Path.GetFileName(file.FileName); if (fileName.EndsWith(".zip")) { string tempPath = Path.Combine(ServerMapPath, id.ToString() + ".zip"); string extractPath = Path.Combine(ServerMapPath, id.ToString()); file.SaveAs(tempPath); System.IO.Compression.ZipFile.ExtractToDirectory(tempPath, extractPath); string[] names = Directory.GetFiles(extractPath); if (names.Length != 1) { throw new FileLoadException("Zip file must only contain one file."); } System.IO.File.Move(names[0], path); } List <SNP> snps = new List <SNP>(); int i = 0; using (StreamReader sr = new StreamReader(path)) { GeneXContext context = new GeneXContext(); while (sr.Peek() >= 0) { string line = sr.ReadLine(); if (line.StartsWith("#", true, CultureInfo.InvariantCulture)) { continue; } string[] parsed = line.Split("\t".ToCharArray()); string tmp = parsed[0]; SNP snp = context.SNP.Where(m => m.ClusterId == tmp && m.GenomeId == id.Value).FirstOrDefault(); int chromosome = 0; string chromosomeType; if (parsed[1].ToUpper().Equals("X") || parsed[1].ToUpper().Equals("Y")) { chromosome = 23; chromosomeType = parsed[1].ToUpper(); } else if (parsed[1].ToUpper().Equals("MT")) { chromosome = 0; chromosomeType = "MT"; } else { chromosome = int.Parse(parsed[1]); chromosomeType = "A"; } char?allele1 = null; char?allele2 = null; if (parsed[3].Length > 0) { allele1 = parsed[3][0]; } if (parsed[3].Length > 1) { allele2 = parsed[3][0]; } if (snp == null) { context.SNP.Add(new SNP { SNPId = Guid.NewGuid(), GenomeId = id.Value, ClusterId = parsed[0], Chromosome = chromosome, ChromosomeType = chromosomeType, Position = int.Parse(parsed[2]), Genotype = parsed[3], Allele1 = allele1.ToString(), Allele2 = allele2.ToString() }); } else { snp.ChromosomeType = chromosomeType; snp.Genotype = parsed[3]; } ++i; if (i % 1000 == 0) { context.SaveChanges(); context.Dispose(); context = new GeneXContext(); } } context.SaveChanges(); context.Dispose(); } } return(RedirectToAction("Index", "Home")); }
public ActionResult Index(Guid?id, HttpPostedFileBase file) { if (file == null || file.ContentLength == 0 || file.ContentType == null || string.IsNullOrEmpty(file.FileName) || file.InputStream == null) { this.ModelState.AddModelError("PostedFile", "Must attach file."); return(View()); } if (file.ContentLength > 0) { string fileName = Path.GetFileName(file.FileName); string path = Path.Combine(ServerMapPath, id.ToString() + ".txt"); file.SaveAs(path); List <SNPedia> snps = new List <SNPedia>(); int i = 0; using (StreamReader sr = new StreamReader(path)) { GeneXContext context = new GeneXContext(); while (sr.Peek() >= 0) { string line = sr.ReadLine(); if (line.StartsWith("#", true, CultureInfo.InvariantCulture) || string.IsNullOrWhiteSpace(line)) { continue; } string[] parsed = line.Split("\t".ToCharArray()); string chrome = parsed[0]; string reference = parsed[1]; string chiptype = parsed[2]; int pos = int.Parse(parsed[3]); string toParse = parsed[8]; int chromosome = -1; string chromosomeType; string notes = toParse.Substring(toParse.IndexOf("Note=") + 5).Replace("\"", string.Empty); int cidStart = toParse.IndexOf("ID=") + 6; int cidEnd = toParse.IndexOf(";", cidStart); string cid = toParse.Substring(cidStart, cidEnd - cidStart); if (chrome.Contains("MT")) { chromosomeType = "MT"; chromosome = 0; } else if (chrome.Contains("X")) { chromosomeType = "X"; chromosome = 23; } else if (chrome.Contains("Y")) { chromosomeType = "Y"; chromosome = 23; } else { chromosomeType = "A"; if (!int.TryParse(chrome.Substring(3), out chromosome)) { // Not Human continue; } } SNPedia snpedia = context.SNPedia.Where(m => m.Position == pos && m.Chromosome == chromosome && m.ChromosomeType == chromosomeType).FirstOrDefault(); if (snpedia == null) { context.SNPedia.Add(new SNPedia { SNPediaId = Guid.NewGuid(), ClusterId = cid, Chromosome = chromosome, ChromosomeType = chromosomeType, Position = pos, ChipType = chiptype, Source = reference, Notes = notes }); } ++i; if (i % 1000 == 0) { context.SaveChanges(); context.Dispose(); context = new GeneXContext(); } } context.SaveChanges(); context.Dispose(); } } return(RedirectToAction("Index", "Home")); }