internal IMsDataFile <IMsDataScan <IMzSpectrum <IMzPeak> > > LoadFile(string origDataFile, int?topNpeaks, double?minRatio, bool trimMs1Peaks, bool trimMsMsPeaks) { FilteringParams filter = new FilteringParams(topNpeaks, minRatio, null, trimMs1Peaks, trimMsMsPeaks); if (myMsDataFiles.TryGetValue(origDataFile, out IMsDataFile <IMsDataScan <IMzSpectrum <IMzPeak> > > value) && value != null) { return(value); } // By now know that need to load this file!!! lock (fileLoadingLock) // Lock because reading is sequential if (Path.GetExtension(origDataFile).Equals(".mzML", StringComparison.OrdinalIgnoreCase)) { myMsDataFiles[origDataFile] = Mzml.LoadAllStaticData(origDataFile, filter); } else { #if NETFRAMEWORK myMsDataFiles[origDataFile] = ThermoStaticData.LoadAllStaticData(origDataFile, filter); #else Warn("No capability for reading " + origDataFile); #endif } return(myMsDataFiles[origDataFile]); }
public MsDataFile LoadFile(string origDataFile, int?topNpeaks, double?minRatio, bool trimMs1Peaks, bool trimMsMsPeaks, CommonParameters commonParameters) { FilteringParams filter = new FilteringParams(topNpeaks, minRatio, 1, trimMs1Peaks, trimMsMsPeaks); if (MyMsDataFiles.TryGetValue(origDataFile, out MsDataFile value) && value != null) { return(value); } // By now know that need to load this file!!! lock (FileLoadingLock) // Lock because reading is sequential { if (Path.GetExtension(origDataFile).Equals(".mzML", StringComparison.OrdinalIgnoreCase)) { MyMsDataFiles[origDataFile] = Mzml.LoadAllStaticData(origDataFile, filter, commonParameters.MaxThreadsToUsePerFile); } else if (Path.GetExtension(origDataFile).Equals(".mgf", StringComparison.OrdinalIgnoreCase)) { MyMsDataFiles[origDataFile] = Mgf.LoadAllStaticData(origDataFile, filter); } else { #if NETFRAMEWORK MyMsDataFiles[origDataFile] = ThermoStaticData.LoadAllStaticData(origDataFile, filter); #else Warn("No capability for reading " + origDataFile); #endif } return(MyMsDataFiles[origDataFile]); } }
public static void ProcessXcorrInB6MzSpectrum() { Dictionary <string, MsDataFile> MyMsDataFiles = new Dictionary <string, MsDataFile>(); string origDataFile = Path.Combine(TestContext.CurrentContext.TestDirectory, @"DatabaseTests\sliced_b6.mzML"); FilteringParams filter = new FilteringParams(200, 0.01, null, 1, false, false, false); string expectedResultFile = Path.Combine(TestContext.CurrentContext.TestDirectory, @"DatabaseTests\Working_86.tsv"); List <string> expectedResults = File.ReadAllLines(expectedResultFile, Encoding.UTF8).ToList(); MyMsDataFiles[origDataFile] = Mzml.LoadAllStaticData(origDataFile, filter, 1); var scans = MyMsDataFiles[origDataFile].GetAllScansList(); List <double> xArrayProcessed = new List <double>(); foreach (MsDataScan scan in scans.Where(s => s.MsnOrder > 1)) { if (scan.OneBasedScanNumber == 86) { scan.MassSpectrum.XCorrPrePreprocessing(0, 1969, scan.IsolationMz.Value); xArrayProcessed = scan.MassSpectrum.XArray.ToList(); } } for (int i = 0; i < expectedResults.Count; i++) { Assert.That(double.Parse(expectedResults[i]), Is.EqualTo(xArrayProcessed[i]).Within(0.001)); } }
public List <ResponseModel> GetMovies(FilteringParams filteringParams) { var predicate = PredicateBuilder.True <Movie>(); if (!string.IsNullOrEmpty(filteringParams.title)) { predicate = predicate.And(i => i.Title.ToLower().Contains(filteringParams.title)); } if (!string.IsNullOrEmpty(filteringParams.genres)) { predicate = predicate.And(i => i.Genres.ToLower().Contains(filteringParams.genres)); } if (filteringParams.YearOfRelease > 0) { predicate = predicate.And(i => i.YearOfRelease == filteringParams.YearOfRelease); } var data = from item in _dbContext.Movies .Include("Ratings") .Where(predicate) .ToList() select new ResponseModel { Id = item.MovieId, Title = item.Title, YearOfRelease = item.YearOfRelease, RunningTime = item.RunningTime, Genres = item.Genres, AverageRating = item.Ratings.Count > 0 ? Math.Round(item.Ratings.Average(s => s.GivenRating)) : 0 }; return(data.ToList()); }
public static void TestFilteringPeaksTopN_MultipleWindows(int peaksToKeep, int?nominalWindowWidthDaltons, int peakCount, bool normalize) { double[] mzArray = new double[100]; double[] intArray = new double[100]; for (int i = 0; i < 100; i++) { mzArray[i] = 100d + ((double)i / 100d) * 1900; intArray[i] = i; } FilteringParams f = new FilteringParams(peaksToKeep, null, nominalWindowWidthDaltons, null, normalize, false, false); MsDataFile.WindowModeHelper(ref intArray, ref mzArray, f, 100, 2000, false); if (nominalWindowWidthDaltons.HasValue) { Assert.LessOrEqual((decimal)mzArray.Count(), (decimal)peaksToKeep * (decimal)nominalWindowWidthDaltons); } else { Assert.LessOrEqual((decimal)mzArray.Count(), (decimal)peaksToKeep * (decimal)1.0); } if (normalize) { Assert.That(50, Is.EqualTo(intArray.Max()).Within(0.1)); } }
public static void TestFilterKeepsPeaksWithHighestIntensity() { double[] mzArray = new double[200]; double[] intArray = new double[200]; Random r = new Random(); for (int i = 0; i < 200; i++) { mzArray[i] = (double)(i + 1); intArray[i] = (i * Math.Abs(r.Next(1, 100)) + 1d); } List <double> l = intArray.ToList(); l.Sort((x, y) => y.CompareTo(x)); l = l.Take(100).ToList(); FilteringParams f = new FilteringParams(100, null, null, null, false, false, false); MsDataFile.WindowModeHelper(ref intArray, ref mzArray, f, mzArray.Min(), mzArray.Max()); List <double> myOut = intArray.ToList(); myOut.Sort((x, y) => y.CompareTo(x)); Assert.IsTrue(l.SequenceEqual(myOut)); }
public static void testingTMTonBigScan() { Dictionary <string, MsDataFile> MyMsDataFiles = new Dictionary <string, MsDataFile>(); var origDataFile = Path.Combine(TestContext.CurrentContext.TestDirectory, @"TestData\tmt18test.mzML"); FilteringParams filter = new FilteringParams(); MyMsDataFiles[origDataFile] = Mzml.LoadAllStaticData(origDataFile, filter, 1); var scans2 = MyMsDataFiles[origDataFile].GetAllScansList(); var trimmedScans = scans2.Where(m => m.MsnOrder == 3).ToList(); Protein p = new Protein("LAALNPESNTAGLDIFAK", "accession"); List <Modification> fixedModifications = new List <Modification>(); fixedModifications.AddRange(GlobalVariables.AllModsKnown); List <Modification> tmt18Mods = fixedModifications.Where(m => m.ModificationType == "Multiplex Label" && m.IdWithMotif.Contains("TMT18")).ToList(); DigestionParams digestionParams = new DigestionParams(minPeptideLength: 1); var aPeptideWithSetModifications = p.Digest(digestionParams, tmt18Mods, new List <Modification>()).First(); var theseTheoreticalFragments = new List <Product>(); aPeptideWithSetModifications.Fragment(DissociationType.HCD, FragmentationTerminus.Both, theseTheoreticalFragments); theseTheoreticalFragments = theseTheoreticalFragments.Where(n => n.ProductType == ProductType.D).ToList(); double ppmTolerance = 20; int roundTo = 5; List <double> diagnosticIons = new List <double>(); for (int k = 0; k < theseTheoreticalFragments.Count(); k++) { diagnosticIons.Add(Math.Round(theseTheoreticalFragments[k].NeutralMass, roundTo)); } int i = 460; do { if (i == trimmedScans.Count()) { break; } var massList = trimmedScans[i].MassSpectrum.XArray.ToList(); for (int j = 0; j < massList.Count(); j++) { massList[j] = Math.Round(massList[j], roundTo); } for (int l = 0; l < massList.Count(); l++) { for (int m = 0; m < diagnosticIons.Count(); m++) { double toleranceValue = ppmTolerance / Math.Pow(10, 6) * massList[l]; if ((massList[l] < diagnosticIons[m] + toleranceValue) & (massList[l] > diagnosticIons[m] - toleranceValue)) { diagnosticIons.Remove(diagnosticIons[m]); } } } i++; } while (diagnosticIons.Count() > 0); Assert.AreEqual(0, diagnosticIons.Count()); }
public MsDataFile LoadFile(string origDataFile, CommonParameters commonParameters) { FilteringParams filter = new FilteringParams(commonParameters.NumberOfPeaksToKeepPerWindow, commonParameters.MinimumAllowedIntensityRatioToBasePeak, commonParameters.WindowWidthThomsons, commonParameters.NumberOfWindows, commonParameters.NormalizePeaksAccrossAllWindows, commonParameters.TrimMs1Peaks, commonParameters.TrimMsMsPeaks); if (commonParameters.DissociationType == DissociationType.LowCID || commonParameters.MS2ChildScanDissociationType == DissociationType.LowCID || commonParameters.MS3ChildScanDissociationType == DissociationType.LowCID) { filter = null; } if (MyMsDataFiles.TryGetValue(origDataFile, out MsDataFile value) && value != null) { return(value); } // By now know that need to load this file!!! lock (FileLoadingLock) // Lock because reading is sequential { if (Path.GetExtension(origDataFile).Equals(".mzML", StringComparison.OrdinalIgnoreCase)) { MyMsDataFiles[origDataFile] = Mzml.LoadAllStaticData(origDataFile, filter, commonParameters.MaxThreadsToUsePerFile); } else if (Path.GetExtension(origDataFile).Equals(".mgf", StringComparison.OrdinalIgnoreCase)) { MyMsDataFiles[origDataFile] = Mgf.LoadAllStaticData(origDataFile, filter); } else { MyMsDataFiles[origDataFile] = ThermoRawFileReaderData.LoadAllStaticData(origDataFile, filter, commonParameters.MaxThreadsToUsePerFile); } return(MyMsDataFiles[origDataFile]); } }
public IActionResult FilterMovies([FromQuery] FilteringParams filteringParams) { if (string.IsNullOrEmpty(filteringParams.title) && filteringParams.YearOfRelease == 0 && string.IsNullOrEmpty(filteringParams.genres)) { return(BadRequest("invalid / no criteria is given.")); } var model = _service.GetMovies(filteringParams); return(model.Count > 0? Ok(model.ToList()): StatusCode(404, "no movie is found based on the criteria")); }
public IActionResult Get(FilteringParams filteringParams) { var model = _service.GetMovies(filteringParams); var outputModel = new MovieOutputModel { Count = model.Count, Items = model.Select(m => ToMovieInfo(m)).ToList(), }; return(Ok(outputModel)); }
public IActionResult Index() { HomesListViewModel model = new HomesListViewModel(); FilteringParams filterModel = new FilteringParams(); model.Homes = homeRepo.GetAll().Select(x => HomeViewModel.MapHomeToHomeViewModel(x)).ToList(); filterModel.Cities = homeRepo.Cities.Select(x => CityViewModel.MapCityToCityViewModel(x)).ToList(); filterModel.HomeTypes = homeRepo.HomeTypes.Select(x => HomeTypeViewModel.MapHomeTypeToHomeTypeViewModel(x)).ToList(); filterModel.AdverTypes = homeRepo.AdverTypes.Select(x => AdverTypeViewModel.MapAdverTypeToAdverTypeViewModel(x)).ToList(); model.FilterParams = filterModel; model.PageInfo = new PagingInfo(); return(View("Index", model)); }
public List <Home> GetByFilterParams(FilteringParams filterParam) { if (filterParam != null) { IQueryable <Home> homes = context.Homes; if (filterParam.HomeTypeId != null) { homes = context.Homes.Where(x => x.HomeTypeId == filterParam.HomeTypeId); } if (filterParam.CityId != null) { homes = homes.Where(x => x.CityId == filterParam.CityId); } if (filterParam.AdverTypeId != null) { homes = homes.Where(x => x.AdverTypeId == filterParam.AdverTypeId); } //if (filterParam.Credit != null) //{ // homes = homes.Where(x => x.Credit == filterParam.Credit); //} if (filterParam.Price1 != null) { homes = homes.Where(x => x.Price >= filterParam.Price1); } if (filterParam.Price2 != null) { homes = homes.Where(x => x.Price <= filterParam.Price2); } if (filterParam.Area1 != null) { homes = homes.Where(x => x.Area >= filterParam.Area1); } if (filterParam.Area2 != null) { homes = homes.Where(x => x.Area <= filterParam.Area2); } if (filterParam.RoomCount1 != null) { homes = homes.Where(x => x.Area >= filterParam.RoomCount1); } if (filterParam.RoomCount2 != null) { homes = homes.Where(x => x.Area <= filterParam.RoomCount2); } return(homes.Include(x => x.City).Include(x => x.AdverType).Include(x => x.HomeType).ToList()); } throw new Exception("NOT ANY HOME FOUND"); }
public async Task <IActionResult> GetModels([FromQuery] VehicleParams vehicleModelParams) { var page = new PagingParams(vehicleModelParams.PageNumber, vehicleModelParams.PageSize); var filter = new FilteringParams(vehicleModelParams.Search, vehicleModelParams.MakeId ?? Guid.Empty); var sort = new SortingParams(vehicleModelParams.SortBy); var models = await modelService.FindModelAsync(filter, sort, page); var modelsDto = models.ToMappedPagedList <IVehicleModel, VehicleModelDto>(mapper); Response.AddPagination(models.PageNumber, models.PageSize, models.TotalItemCount, models.PageCount); return(Ok(modelsDto)); }
public List <Movie> GetMovies(FilteringParams filteringParams) { var query = this.movies.AsQueryable(); var filterBy = filteringParams.FilterBy.Trim().ToLowerInvariant(); if (!string.IsNullOrEmpty(filterBy)) { query = query .Where(m => m.LeadActor.ToLowerInvariant().Contains(filterBy) || m.Title.ToLowerInvariant().Contains(filterBy) || m.Summary.ToLowerInvariant().Contains(filterBy)); } return(query.ToList()); }
public async Task <IActionResult> GetBook(FilteringParams filteringParams) { var filterBy = filteringParams.FilterBy.Trim().ToLowerInvariant(); if (string.IsNullOrEmpty(filterBy)) { return(BadRequest()); } var books = await _context.Book.Where(x => x.Author.ToLowerInvariant().Contains(filterBy) || x.Name.ToLowerInvariant().Contains(filterBy)).ToListAsync(); if (books.Count() == 0) { return(NotFound()); } return(Ok(books)); }
/// <summary> /// Tests peak filtering for ThermoRawFileReader /// </summary> public static void TestPeakFilteringRawFileReader(string infile) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var filterParams = new FilteringParams(200, 0.01, 0, 1, false, true, true); var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "DataFiles", infile); var a = ThermoRawFileReader.LoadAllStaticData(path, filterParams, maxThreads: 1); var rawScans = a.GetAllScansList(); foreach (var scan in rawScans) { Assert.That(scan.MassSpectrum.XArray.Length <= 200); } string outfile1 = Path.Combine(TestContext.CurrentContext.TestDirectory, "DataFiles", Path.GetFileNameWithoutExtension(infile) + ".mzML"); MzmlMethods.CreateAndWriteMyMzmlWithCalibratedSpectra(a, outfile1, false); var mzml = Mzml.LoadAllStaticData(outfile1, filterParams, maxThreads: 1); var mzmlScans = mzml.GetAllScansList(); for (int i = 0; i < mzmlScans.Count; i++) { var mzmlScan = mzmlScans[i]; var rawScan = rawScans[i]; for (int j = 0; j < mzmlScan.MassSpectrum.XArray.Length; j++) { double roundedMzmlMz = Math.Round(mzmlScan.MassSpectrum.XArray[j], 2); double roundedRawMz = Math.Round(rawScan.MassSpectrum.XArray[j], 2); Assert.AreEqual(roundedMzmlMz, roundedRawMz); double roundedMzmlIntensity = Math.Round(mzmlScan.MassSpectrum.XArray[j], 0); double roundedRawIntensity = Math.Round(rawScan.MassSpectrum.XArray[j], 0); Assert.AreEqual(roundedMzmlIntensity, roundedRawIntensity); } } Console.WriteLine($"Analysis time for TestPeakFilteringRawFileReader: {stopwatch.Elapsed.Hours}h " + $"{stopwatch.Elapsed.Minutes}m {stopwatch.Elapsed.Seconds}s"); }
public static void ProcessXcorrInMzSpectrum() { Dictionary <string, MsDataFile> MyMsDataFiles = new Dictionary <string, MsDataFile>(); string origDataFile = Path.Combine(TestContext.CurrentContext.TestDirectory, "DataFiles", "BinGenerationTest.mzML"); FilteringParams filter = new FilteringParams(200, 0.01, null, 1, false, false, true); MyMsDataFiles[origDataFile] = Mzml.LoadAllStaticData(origDataFile, filter, 1); var scans = MyMsDataFiles[origDataFile].GetAllScansList(); foreach (MsDataScan scan in scans.Where(s => s.MsnOrder > 1)) { scan.MassSpectrum.XCorrPrePreprocessing(0, 1968 * 1.0005079, scan.IsolationMz.Value); } Assert.AreEqual(6, scans[0].MassSpectrum.XArray.Count()); Assert.AreEqual(20, scans[1].MassSpectrum.XArray.Count()); }
public IActionResult Count([FromQuery] string filterBy, [FromQuery] string search) { FilteringParams filteringParams = new FilteringParams(filterBy, search); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var model = _service.GetPages(filteringParams); if (model == null) { return(NotFound()); } return(Ok(model.Count())); }
public static void TestFilterLowIntensity() { double[] mzArray = new double[100]; double[] intArray = new double[100]; for (int i = 0; i < 100; i++) { mzArray[i] = (double)i; intArray[i] = (double)i; } FilteringParams f = new FilteringParams(null, 0.05, null, null, false, false, false); MsDataFile.WindowModeHelper(ref intArray, ref mzArray, f, mzArray.Min(), mzArray.Max()); //The first five intensities are below 5% and therefore removed. Assert.AreEqual(95, intArray.Count()); Assert.AreEqual(95, mzArray.Count()); }
public IActionResult GetFiltro( [FromQuery] string filterBy, [FromQuery] string search, [FromQuery] int limit, [FromQuery] int skip) { FilteringParams filteringParams = new FilteringParams(filterBy, search, limit, skip); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var model = _service.GetPages(filteringParams); if (model == null) { return(NotFound()); } return(Ok(model)); }
public async Task <IActionResult> GetMakers([FromQuery] VehicleParams vehicleMakeParams) { try { var page = new PagingParams(vehicleMakeParams.PageNumber, vehicleMakeParams.PageSize); var filter = new FilteringParams(vehicleMakeParams.Search); var sort = new SortingParams(vehicleMakeParams.SortBy); var makers = await makeService.FindMakeAsync(filter, sort, page); var mapped = mapper.Map <IEnumerable <VehicleMakeDto> >(makers); Response.AddPagination(makers.PageNumber, makers.PageSize, makers.TotalItemCount, makers.PageCount); return(Ok(mapped)); } catch (Exception) { return(NotFound()); } }
public static void TestFilterNumberPeaksPerWindowTenWindows() { double[] mzArray = new double[200]; double[] intArray = new double[200]; for (int i = 0; i < 200; i++) { mzArray[i] = (double)(i + 1); intArray[i] = (double)(i + 1); } FilteringParams f = new FilteringParams(10, null, 20, 10, false, false, false); MsDataFile.WindowModeHelper(ref intArray, ref mzArray, f, mzArray.Min(), mzArray.Max()); Assert.AreEqual(100, intArray.Count()); Assert.AreEqual(100, mzArray.Count()); Assert.AreEqual(11, intArray.Min()); Assert.AreEqual(200, intArray.Max()); Assert.AreEqual(11, mzArray.Min()); Assert.AreEqual(200, mzArray.Max()); }
public static Mgf LoadAllStaticData(string filePath, FilteringParams filterParams = null) { if (!File.Exists(filePath)) { throw new FileNotFoundException(); } Loaders.LoadElements(); List <MsDataScan> scans = new List <MsDataScan>(); HashSet <int> checkForDuplicateScans = new HashSet <int>(); using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (BufferedStream bs = new BufferedStream(fs)) { using (StreamReader sr = new StreamReader(bs)) { while (sr.Peek() > 0) { string line = sr.ReadLine(); if (line != "BEGIN IONS") { continue; } var scan = GetNextMsDataOneBasedScanFromConnection(sr, checkForDuplicateScans, filterParams); scans.Add(scan); } } } } SourceFile sourceFile = new SourceFile("no nativeID format", "mgf format", null, null, null); return(new Mgf(scans.OrderBy(x => x.OneBasedScanNumber).ToArray(), sourceFile)); }
public static Mzml LoadAllStaticData(string filePath, FilteringParams filterParams = null) { Generated.mzMLType _mzMLConnection; try { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { var _indexedmzMLConnection = (Generated.indexedmzML)MzmlMethods.indexedSerializer.Deserialize(fs); _mzMLConnection = _indexedmzMLConnection.mzML; } } catch { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) _mzMLConnection = (Generated.mzMLType)MzmlMethods.mzmlSerializer.Deserialize(fs); } SourceFile sourceFile; if (_mzMLConnection.fileDescription.sourceFileList != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile[0] != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile[0].cvParam != null) { var simpler = _mzMLConnection.fileDescription.sourceFileList.sourceFile[0]; string nativeIdFormat = null; string fileFormat = null; string checkSum = null; string checkSumType = null; foreach (var cv in simpler.cvParam) { if (cv.accession.Equals(@"MS:1000563")) { fileFormat = "Thermo RAW format"; } if (cv.accession.Equals(@"MS:1000584")) { fileFormat = "mzML format"; } if (cv.accession.Equals(@"MS:1000768")) { nativeIdFormat = "Thermo nativeID format"; } if (cv.accession.Equals(@"MS:1000776")) { nativeIdFormat = "scan number only nativeID format"; } if (cv.accession.Equals(@"MS:1000824")) { nativeIdFormat = "no nativeID format"; } if (cv.accession.Equals(@"MS:1000568")) { checkSum = cv.value; checkSumType = "MD5"; } if (cv.accession.Equals(@"MS:1000569")) { checkSum = cv.value; checkSumType = "SHA-1"; } } sourceFile = new SourceFile( nativeIdFormat, fileFormat, checkSum, checkSumType, new Uri(simpler.location), simpler.id, simpler.name); } else { string sendCheckSum; using (FileStream stream = File.OpenRead(filePath)) { using (SHA1Managed sha = new SHA1Managed()) { byte[] checksum = sha.ComputeHash(stream); sendCheckSum = BitConverter.ToString(checksum) .Replace("-", string.Empty); } } sourceFile = new SourceFile( @"no nativeID format", @"mzML format", sendCheckSum, @"SHA-1", Path.GetFullPath(filePath), Path.GetFileNameWithoutExtension(filePath)); } var numSpecta = _mzMLConnection.run.spectrumList.spectrum.Length; IMzmlScan[] scans = new IMzmlScan[numSpecta]; Parallel.ForEach(Partitioner.Create(0, numSpecta), fff => { for (int i = fff.Item1; i < fff.Item2; i++) { scans[i] = GetMsDataOneBasedScanFromConnection(_mzMLConnection, i + 1, filterParams); } }); return(new Mzml(scans, sourceFile)); }
public static Mzml LoadAllStaticData(string filePath, FilteringParams filterParams = null, int maxThreads = -1) { if (!File.Exists(filePath)) { throw new FileNotFoundException(); } Loaders.LoadElements(); Generated.mzMLType _mzMLConnection; try { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { var _indexedmzMLConnection = (Generated.indexedmzML)MzmlMethods.indexedSerializer.Deserialize(fs); _mzMLConnection = _indexedmzMLConnection.mzML; } } catch { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) _mzMLConnection = (Generated.mzMLType)MzmlMethods.mzmlSerializer.Deserialize(fs); } SourceFile sourceFile; if (_mzMLConnection.fileDescription.sourceFileList != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile[0] != null && _mzMLConnection.fileDescription.sourceFileList.sourceFile[0].cvParam != null) { var simpler = _mzMLConnection.fileDescription.sourceFileList.sourceFile[0]; string nativeIdFormat = null; string fileFormat = null; string checkSum = null; string checkSumType = null; foreach (var cv in simpler.cvParam) { if (cv.accession.Equals(@"MS:1000563")) { fileFormat = "Thermo RAW format"; } if (cv.accession.Equals(@"MS:1000584")) { fileFormat = "mzML format"; } if (cv.accession.Equals(@"MS:1000768")) { nativeIdFormat = "Thermo nativeID format"; } if (cv.accession.Equals(@"MS:1000776")) { nativeIdFormat = "scan number only nativeID format"; } if (cv.accession.Equals(@"MS:1000824")) { nativeIdFormat = "no nativeID format"; } if (cv.accession.Equals(@"MS:1000568")) { checkSum = cv.value; checkSumType = "MD5"; } if (cv.accession.Equals(@"MS:1000569")) { checkSum = cv.value; checkSumType = "SHA-1"; } } sourceFile = new SourceFile( nativeIdFormat, fileFormat, checkSum, checkSumType, new Uri(simpler.location), simpler.id, simpler.name); } else { string sendCheckSum; using (FileStream stream = File.OpenRead(filePath)) { using (SHA1Managed sha = new SHA1Managed()) { byte[] checksum = sha.ComputeHash(stream); sendCheckSum = BitConverter.ToString(checksum) .Replace("-", string.Empty); } } sourceFile = new SourceFile( @"no nativeID format", @"mzML format", sendCheckSum, @"SHA-1", Path.GetFullPath(filePath), Path.GetFileNameWithoutExtension(filePath)); } var numSpecta = _mzMLConnection.run.spectrumList.spectrum.Length; MsDataScan[] scans = new MsDataScan[numSpecta]; Parallel.ForEach(Partitioner.Create(0, numSpecta), new ParallelOptions { MaxDegreeOfParallelism = maxThreads }, fff => { for (int i = fff.Item1; i < fff.Item2; i++) { scans[i] = GetMsDataOneBasedScanFromConnection(_mzMLConnection, i + 1, filterParams); } }); //Mzml sometimes have scan numbers specified, but usually not. //In the event that they do, the iterator above unintentionally assigned them to an incorrect index. //Check to make sure that the scans are in order and that there are no duplicate scans HashSet <int> checkForDuplicateScans = new HashSet <int>(); bool ordered = true; int previousScanNumber = -1; foreach (MsDataScan scan in scans) { //check if no duplicates if (!checkForDuplicateScans.Add(scan.OneBasedScanNumber)) //returns false if the scan already exists { throw new MzLibException("Scan number " + scan.OneBasedScanNumber.ToString() + " appeared multiple times in " + filePath); } //check if scans are in order if (previousScanNumber > scan.OneBasedScanNumber) { ordered = false; } previousScanNumber = scan.OneBasedScanNumber; } if (!ordered) //reassign indexes if not ordered { MsDataScan[] indexedScans = new MsDataScan[checkForDuplicateScans.Max()]; foreach (MsDataScan scan in scans) { indexedScans[scan.OneBasedScanNumber - 1] = scan; } scans = indexedScans; } //make reference pervious ms1 scan // we weren't able to get the precursor scan number, so we'll have to guess; // loop back to find precursor scan // (assumed to be the first scan before this scan with an MS order of this scan's MS order - 1) // e.g., if this is an MS2 scan, find the first MS1 scan before this and assume that's the precursor scan for (int i = 0; i < scans.Length; i++) { if (scans[i].MsnOrder > 1 && scans[i].OneBasedPrecursorScanNumber == null) { for (int j = i; j >= 0; j--) { if (scans[i].MsnOrder - scans[j].MsnOrder == 1) { scans[i].SetOneBasedPrecursorScanNumber(scans[j].OneBasedScanNumber); break; } } } } return(new Mzml(scans, sourceFile)); }
public static Mgf LoadAllStaticData(string filePath, FilteringParams filterParams = null) { if (!File.Exists(filePath)) { throw new FileNotFoundException(); } List <MsDataScan> scans = new List <MsDataScan>(); HashSet <int> checkForDuplicateScans = new HashSet <int>(); using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (BufferedStream bs = new BufferedStream(fs)) { using (StreamReader sr = new StreamReader(bs)) { string s; while ((s = sr.ReadLine()) != null && !s.Equals("BEGIN IONS")) { //do nothing with the first few scans } bool readingPeaks = false; List <double> mzs = new List <double>(); List <double> intensities = new List <double>(); double precursorMz = 0; int charge = 2; int scanNumber = 0; int oldScanNumber = 0; double rtInMinutes = 0; while ((s = sr.ReadLine()) != null) { if (s.Equals("END IONS")) { if (!checkForDuplicateScans.Add(scanNumber)) //returns false if the scan already exists { throw new MzLibException("Scan number " + scanNumber.ToString() + " appeared multiple times in " + filePath + ", which is not allowed because we assume all scan numbers are unique."); } readingPeaks = false; MzSpectrum spectrum = new MzSpectrum(mzs.ToArray(), intensities.ToArray(), false); scans.Add(new MsDataScan(spectrum, scanNumber, 2, true, charge > 0 ? Polarity.Positive : Polarity.Negative, rtInMinutes, new MzRange(mzs[0], mzs[mzs.Count - 1]), null, MZAnalyzerType.Unknown, intensities.Sum(), 0, null, null, precursorMz, charge, null, precursorMz, null, DissociationType.Unknown, null, precursorMz)); mzs = new List <double>(); intensities = new List <double>(); oldScanNumber = scanNumber; charge = 2; //default when unknown //skip the next two lines which are "" and "BEGIN IONS" while ((s = sr.ReadLine()) != null && !s.Equals("BEGIN IONS")) { //do nothing } } else { if (readingPeaks) { string[] sArray = s.Split(' '); mzs.Add(Convert.ToDouble(sArray[0])); intensities.Add(Convert.ToDouble(sArray[1])); } else { string[] sArray = s.Split('='); if (sArray.Length == 1) { readingPeaks = true; sArray = s.Split(' '); mzs.Add(Convert.ToDouble(sArray[0])); intensities.Add(Convert.ToDouble(sArray[1])); if (oldScanNumber == scanNumber) //if there's no recorded scan number, simply index them. { scanNumber++; } } else { switch (sArray[0]) { case "PEPMASS": sArray = sArray[1].Split(' '); precursorMz = Convert.ToDouble(sArray[0]); break; case "CHARGE": string entry = sArray[1]; charge = Convert.ToInt32(entry.Substring(0, entry.Length - 1)); if (entry[entry.Length - 1].Equals("-")) { charge *= -1; } break; case "SCANS": scanNumber = Convert.ToInt32(sArray[1]); break; case "RTINSECONDS": rtInMinutes = Convert.ToDouble(sArray[sArray.Length - 1]) / 60.0; break; default: break; } } } } } } } } SourceFile sourceFile = new SourceFile("no nativeID format", "mgf format", null, null, null); return(new Mgf(scans.OrderBy(x => x.OneBasedScanNumber).ToArray(), sourceFile)); }