예제 #1
0
        public void SaveLoad_16Bit()
        {
            Random rnd = new Random(0);

            int size      = 1024;
            int stripSize = 32;

            float[] data = new float[size];
            for (int i = 0; i < size; i++)
            {
                data[i] = (float)rnd.Next(0, ushort.MaxValue) / (float)ushort.MaxValue;
            }

            try
            {
                RawFile.Save16Bit(fileName, data);
                RawFile raw = new RawFile(fileName, 16, BYTE_ORDER.WINDOWS, stripSize);

                Assert.AreEqual(fileName, raw.FileName);
                Assert.AreEqual(size, raw.ElementCount, "Element count incorrect");
                Assert.AreEqual(size * 2, raw.ByteCount, "Byte count incorrect");
                Assert.AreEqual(stripSize, raw.StripSize, "Strip size incorrect");
                Assert.AreEqual(16, raw.BitDepth);
                Assert.AreEqual(BYTE_ORDER.WINDOWS, raw.ByteOrder);

                CollectionAssert.AreEqual(data, ToArray(raw));
            }
            finally
            {
                File.Delete(fileName);
            }
        }
예제 #2
0
        public void SaveLoad_8Bit()
        {
            Random rnd = new Random(0);

            int size      = 1024;
            int stripSize = 32;

            float[] data = new float[size];
            for (int i = 0; i < size; i++)
            {
                data[i] = (float)rnd.Next(0, byte.MaxValue) / byte.MaxValue;
            }

            try
            {
                RawFile.Save8Bit(fileName, data);
                RawFile raw = new RawFile(fileName, 8, BYTE_ORDER.WINDOWS, stripSize);

                Assert.AreEqual(fileName, raw.FileName);
                Assert.AreEqual(size, raw.ElementCount);
                Assert.AreEqual(size, raw.ByteCount);
                Assert.AreEqual(stripSize, raw.StripSize);
                Assert.AreEqual(8, raw.BitDepth);
                Assert.AreEqual(BYTE_ORDER.WINDOWS, raw.ByteOrder);

                CollectionAssert.AreEqual(data, ToArray(raw));
            }
            finally
            {
                File.Delete(fileName);
            }
        }
 public void Dispose()
 {
     if (RawFile != null && RawFile.IsOpen)
     {
         RawFile.Dispose();
     }
 }
        public IActionResult CreateFile([FromForm] CreateExternalFileViewModel model) {
            ApplicationUser user = userRepository.GetApplicationUserByUsername(User.Identity.Name);
            Post post = postRepository.GetPostByPostId(model.PostId, true);
            if(post == null) {
                return NotFound();
            }
            if( user == null) {
                return Unauthorized();
            }
            var time = DateTime.Now.ToBinary();
            ExternalFile file = new ExternalFile() {
                FileName = model.RawFile.FileName+time,
                MIMEType = model.RawFile.ContentType,
                PostId = post.PostId,
                PostedTime = DateTime.Now
            };

            RawFile rawFile = new RawFile() {
                Data = GetByteArrayFromFile(model.RawFile),
                FileName = model.RawFile.FileName+time,
            };
            file.RawFile = rawFile;
            try {
                externalFileRepository.CreateExternalFile(file);
            }
            catch (Exception) {
                return BadRequest();
            }
            return new OkObjectResult(Conversions.ExternalFileViewModelFromExternalFile(file));
        }
예제 #5
0
        public static object Contruct(Type type, RawFile reader)
        {
            object target = null;



            return(target);
        }
예제 #6
0
        public async Task <RawFile> ProcessImage(RawFile rawImage)
        {
            rawImage.Content = await this.imageProcessor
                               .Resize(rawImage.Content, GlobalConstants.ResizedImageWidth);

            rawImage.PreviewContent = await this.imageProcessor
                                      .Resize(rawImage.Content, GlobalConstants.ResizedImageThumbnailWidth);

            return(rawImage);
        }
예제 #7
0
        public static T Contruct <T>(RawFile reader)
        {
            T target = default(T);

            var type        = typeof(T);
            var constructer = type.GetConstructor(new Type[0]);



            return(target);
        }
예제 #8
0
 public void Save(string path, RawFile file)
 {
     try
     {
         fileIO.Save(path, file);
     }
     catch (Exception ex)
     {
         errorReporter.Add(ex);
     }
 }
예제 #9
0
        float[] ToArray(RawFile raw)
        {
            int size = raw.ElementCount;

            float[] data = new float[size];
            for (int i = 0; i < size; i++)
            {
                data[i] = raw.Read(i);
            }

            return(data);
        }
        public async Task <T> SaveFileInfo <T>(RawFile file)
            where T : FileInfo, new()
        {
            var processedFileName = string.Join(WhiteSpace.ToString(), file.OriginalFileName.Split(new[] { WhiteSpace }, StringSplitOptions.RemoveEmptyEntries));
            var databaseFile      = new T {
                OriginalFileName = processedFileName, FileExtension = file.FileExtension
            };

            var filesContext = this.objectFactory.GetInstance <ShowcaseDbContext>();

            filesContext.Set <T>().Add(databaseFile);
            await filesContext.SaveChangesAsync();

            databaseFile.UrlPath = databaseFile.Id.ToUrlPath();
            await filesContext.SaveChangesAsync();

            return(databaseFile);
        }
예제 #11
0
        public static RawFile ReadRaw(string dbFile, DBEntry entry)
        {
            // figure out what file version exists
            string db2file = Path.ChangeExtension(dbFile, ".db2");

            if (File.Exists(db2file))
            {
                dbFile = db2file;
            }
            else
            {
                dbFile = Path.ChangeExtension(dbFile, ".dbc");
            }

            using (var fs = new FileStream(dbFile, FileMode.Open, FileAccess.Read))
                using (var br = new BinaryReader(fs, Encoding.UTF8))
                {
                    DBHeader header = ReadHeader(br, "", 0);
                    if (!ValidationChecks(header, dbFile, out string error))
                    {
                        return(null);
                    }

                    RawFile rawFile = new RawFile()
                    {
                        Entry = entry
                    };

                    // stringtable stuff
                    long pos = br.BaseStream.Position;
                    long stringTableStart = br.BaseStream.Position += header.RecordCount * header.RecordSize;
                    rawFile.StringTable = ReadStringTable(br, stringTableStart);             //Get stringtable
                    br.Scrub(pos);

                    // store data
                    rawFile.RawRecords = new List <byte[]>();
                    for (int i = 0; i < header.RecordCount; i++)
                    {
                        rawFile.RawRecords.Add(br.ReadBytes((int)header.RecordSize));
                    }

                    return(rawFile);
                }
        }
예제 #12
0
        public ActionResult InsertFile(RawFile file_item)
        {
            Value temp_file = new Value();

            temp_file = Session["TxtFile"] as Value;

            if (file_item.file != null && file_item.file.ContentLength > 0)
            {
                var InputFileName  = Path.GetFileName(date_ff);
                var ServerSavePath = Path.Combine("D:/File/Topic/" + InputFileName);
                file_item.file.SaveAs(ServerSavePath);
                if (file_item.description == "null" || file_item.description == null)
                {
                    file_item.description = " ";
                }
                M_Req.InsertFile(file_item.file, (long)Session["TopicID"], "Topic", file_item.description, Session["User"].ToString(), Session["TopicCode"].ToString(), Session["Department"].ToString());
            }
            return(Json((string)Session["TopicCode"]));
        }
예제 #13
0
        //[TestMethod]
        public void BigFileTest()
        {
            string directory = "C:/Users/Justin/Desktop/Residuals/";
            string fileName  = directory + "YellowStone_5m_Ortho_8192_8_3.raw";

            RawFileFormat fileFormat = new RawFileFormat();

            fileFormat.bitDepth  = 8;
            fileFormat.byteOrder = BYTE_ORDER.WINDOWS;
            fileFormat.fileName  = fileName;

            RawFile raw = new RawFile(fileFormat);

            int count = raw.ElementCount;

            for (int i = 0; i < count; i++)
            {
                raw.Read(i);
            }
        }
예제 #14
0
 /// <summary>
 /// Insert detail into Raw File detail table
 /// </summary>
 /// <param name="runId">Run number Id</param>
 /// <param name="file">Raw File Name</param>
 /// <param name="desLoc"></param>
 /// <returns></returns>
 public bool InsertRawFileDetails(int runId, string file, string desLoc)
 {
     try
     {
         using (IRawFileRepository objRawFileRepository = new RawFileRepository())
         {
             RawFile rawFile = new RawFile();
             rawFile.RunNumberId = runId;
             rawFile.FileName    = Path.GetFileName(file);
             rawFile.HotFolder   = desLoc;
             rawFile.Status      = true;
             objRawFileRepository.Save(rawFile);
         }
     }
     catch (Exception ex)
     {
         throw new Exception("Error while inserting record in Raw file : " + ex);
     }
     return(true);
 }
예제 #15
0
        public bool IsValid()
        {
            if (RawFile != null)
            {
                Filename = Filename ?? RawFile.FileName;
                using (var stream = new MemoryStream())
                {
                    RawFile.CopyTo(stream);
                    FileBytes = stream.ToArray();
                    var data  = Convert.ToBase64String(FileBytes);
                    var index = data.LastIndexOf("base64", StringComparison.Ordinal);
                    Data = index == -1 ? data : data.Substring(index + 7);
                }
            }

            if (string.IsNullOrEmpty(FileType))
            {
                if (!string.IsNullOrEmpty(Filename))
                {
                    FileType = Path.GetExtension(Filename).ToLower();
                }

                if (string.IsNullOrEmpty(FileType))
                {
                    var match = Regex.Match(Data, "data:(.*?);");
                    if (!match.Success)
                    {
                        return(false);
                    }

                    FileType = match.Groups[1].Value;
                }
            }

            if (string.IsNullOrEmpty(FileType))
            {
                return(false);
            }

            return(true);
        }
예제 #16
0
        public void StaticSaveLoad_32Bit()
        {
            Random rnd = new Random(0);

            int size = 1024;

            float[] data = new float[size];
            for (int i = 0; i < size; i++)
            {
                data[i] = (float)rnd.NextDouble();
            }

            try
            {
                RawFile.Save32Bit(fileName, data);
                CollectionAssert.AreEqual(data, RawFile.Load32Bit(fileName));
            }
            finally
            {
                File.Delete(fileName);
            }
        }
예제 #17
0
        public void StaticSaveLoad_16Bit()
        {
            Random rnd = new Random(0);

            int size = 1024;

            float[] data = new float[size];
            for (int i = 0; i < size; i++)
            {
                data[i] = (float)rnd.Next(0, ushort.MaxValue) / ushort.MaxValue;
            }

            try
            {
                RawFile.Save16Bit(fileName, data);
                CollectionAssert.AreEqual(data, RawFile.Load16Bit(fileName, BYTE_ORDER.WINDOWS));
            }
            finally
            {
                File.Delete(fileName);
            }
        }
예제 #18
0
        private int GetFileStats()
        {
            TextReader instream = null;

            try
            {
                instream = (ReadStdin ? Console.In : new StreamReader(FilenameIn));

                bool firstZero = true;
                for (int row = 0; ; row++)
                {
                    bool   saveLine    = true;
                    bool   processLine = true;
                    string line        = instream.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    if (firstZero && row == 0 && Header == PrintHeaderType.PrintRow1)
                    {
                        row        = -1;
                        HeaderLine = line;
                        saveLine   = false;
                        // still process it
                        firstZero = false;
                    }

                    // if we still want to save it, it's not a header
                    if (saveLine && SkipRows > 0)
                    {
                        SkipRows--;
                        saveLine    = false;
                        processLine = false;
                    }

                    if (processLine)
                    {
                        SaveFieldCount(line);
                    }

                    if (saveLine)
                    {
                        MaxReadLine++;
                        if (!UseTwoPassMethod)
                        {
                            RawFile.Add(line);
                        }
                        if (LineCountToPrint > 0)
                        {
                            LineCountToPrint--;
                            if (LineCountToPrint <= 0)
                            {
                                break;
                            }
                        }
                    }
                }

                int ret = ReadExternHeader();
                if (ret > 0)
                {
                    return(ret);
                }

                VerifyFieldLengths();

                return(0);
            }
            finally
            {
                if (instream != null && instream != Console.In)
                {
                    try { instream.Close(); }
                    catch { }
                    try { instream.Dispose(); }
                    catch { }
                }
            }
        }
예제 #19
0
        public QMatch FindBestScore(double MZ, int Charge, double IsoRatio, double MinRT, double MaxRT)
        {
            MSMatch First = null, Best = null;

            QMatch Res = new QMatch();

            Res.Clean();
            Res.MSMatches = new List <MSMatch>();

            double FirstIso = 0.0, SecondIso = 0.0;
            double BestScore = 0.0, fs = 0.0;
            int    BestIndex = 0;

            int    Scan  = 0;
            double AveRT = (MinRT + MaxRT) / 2;

            RawFile.ScanNumFromRT(AveRT, ref Scan);
            Scan = ms2index[Scan];

            //отступаем до минимального времени RT в массиве спектров
            double LowMZ = (MZ * Charge + (float)1.007277) / (Charge + 1);
            double HighMZ;

            if (Charge > 1)
            {
                HighMZ = (MZ * Charge - 1.007277) / (Charge - 1);
            }
            else
            {
                HighMZ = 0.0;
            }

            while (Scan > 0 && RawSpectra[Scan].RT >= MinRT)
            {
                Scan = IndexRev[Scan];
            }

            //цикл поиска лучшего
            while (Scan != -1 && RawSpectra[Scan].RT <= MaxRT)
            {
                if (Settings.Default.Deconvolution) //прихватываем другие зарядовые состояния
                {
                    First = FindMatchwithCharges(Scan, MZ, Charge, LowMZ, HighMZ);
                }
                else
                {
                    First = FindMatch(Scan, MZ, Charge);
                }

                //если не найден сам пик или его первый изотоп - сбрасываем
                if (First.Score == 0.0 || First.SecondIsotope == 0.0)
                {
                    Scan = IndexDir[Scan];
                    continue;
                }

                FirstIso = First.FirstIsotope +
                           (First.UpperCharges == null ? 0 : First.UpperCharges.FirstIsotope) +
                           (First.LowerCharges == null ? 0 : First.LowerCharges.FirstIsotope);
                SecondIso = First.SecondIsotope +
                            (First.UpperCharges == null ? 0 : First.UpperCharges.SecondIsotope) +
                            (First.LowerCharges == null ? 0 : First.LowerCharges.SecondIsotope);

                fs = SecondIso / FirstIso;

                double CurrentScore = First.Score +
                                      (First.UpperCharges == null ? 0 : First.UpperCharges.Score) +
                                      (First.LowerCharges == null ? 0 : First.LowerCharges.Score);

                //пик может распространяться за границы, но апекс должен быть внутри
                if (CurrentScore > BestScore && fs > IsoRatio * 0.5 && fs < IsoRatio * 2)
                {
                    BestScore = CurrentScore;
                    BestIndex = Scan;
                    Best      = First;
                }
                //переходим к следующему full-ms
                Scan = IndexDir[Scan];
            }

            if (BestScore == 0.0)
            {
                return(null);
            }

            //если хоть что-то нашли
            //добавляем наибольшее
            LowMZ = (Best.MZ * Charge + (float)1.007277) / (Charge + 1);
            if (Charge > 1)
            {
                HighMZ = (MZ * Charge - 1.007277) / (Charge - 1);
            }
            else
            {
                HighMZ = 0.0;
            }


            Res.ApexRT = Best.RT;
            Res.MSMatches.Add(Best);

            //берем сплошную область вокруг Best - вперед
            for (Scan = IndexDir[BestIndex]; Scan > 0; Scan = IndexDir[Scan])
            {
                if (Settings.Default.Deconvolution) //прихватываем другие зарядовые состояния
                {
                    First = FindMatchwithCharges(Scan, Best.MZ, Charge, LowMZ, HighMZ);
                }
                else
                {
                    First = FindMatch(Scan, Best.MZ, Charge);
                }

                if (First.Score == 0.0 || First.SecondIsotope == 0.0)
                {
                    Res.MSMatches.Add(First);
                    break;
                }
                fs = First.SecondIsotope / First.FirstIsotope;
                //включаем здесь контроль по соотношению изотопов
                //if (fs < IsoRatio*0.5 || fs > IsoRatio * 2 ){
                //    break;
                //}
                Res.MSMatches.Add(First);
            }

            //берем сплошную область вокруг Best - назад
            for (Scan = IndexRev[BestIndex]; Scan > 0; Scan = IndexRev[Scan])
            {
                if (Settings.Default.Deconvolution) //прихватываем другие зарядовые состояния
                {
                    First = FindMatchwithCharges(Scan, Best.MZ, Charge, LowMZ, HighMZ);
                }
                else
                {
                    First = FindMatch(Scan, Best.MZ, Charge);
                }

                if (First.Score == 0.0 || First.SecondIsotope == 0.0)
                {
                    Res.MSMatches.Add(First);
                    break;
                }
                fs = First.SecondIsotope / First.FirstIsotope;
                //включаем здесь контроль по соотношению изотопов
                //if (fs < IsoRatio*0.5 || fs > IsoRatio * 2 ){
                //    break;
                //}
                Res.MSMatches.Insert(0, First);
            }

            Res.Estimate(IsoRatio);
            Res.CleanUp();

            return(Res);
        }
예제 #20
0
        /// <summary>
        /// Remove a section from the PE file.
        /// </summary>
        /// <param name="name">Name of the section to remove.</param>
        /// <param name="removeContent">Flag if the content should be removed or only the section header entry.</param>
        public void RemoveSection(string name, bool removeContent = true)
        {
            var sectionToRemove = ImageSectionHeaders.First(s => s.Name == name);

            // Remove section from list of sections
            var newSections = ImageSectionHeaders.Where(s => s.Name != name).ToArray();

            // Change number of sections in the file header
            ImageNtHeaders !.FileHeader.NumberOfSections--;

            if (removeContent)
            {
                // Reloc the physical address of all sections
                foreach (var s in newSections)
                {
                    if (s.PointerToRawData > sectionToRemove.PointerToRawData)
                    {
                        s.PointerToRawData -= sectionToRemove.SizeOfRawData;
                    }
                }

                // Remove section content
                RawFile.RemoveRange(sectionToRemove.PointerToRawData, sectionToRemove.SizeOfRawData);
            }

            // Fix virtual size
            for (var i = 1; i < newSections.Count(); i++)
            {
                if (newSections[i - 1].VirtualAddress < sectionToRemove.VirtualAddress)
                {
                    newSections[i - 1].VirtualSize = newSections[i].VirtualAddress - newSections[i - 1].VirtualAddress;
                }
            }

            // Replace old section headers with new section headers
            var sectionHeaderOffset = ImageDosHeader !.E_lfanew + ImageNtHeaders !.FileHeader.SizeOfOptionalHeader + 0x18;
            var sizeOfSection       = 0x28;
            var newRawSections      = new byte[newSections.Count() * sizeOfSection];

            for (var i = 0; i < newSections.Count(); i++)
            {
                Array.Copy(newSections[i].ToArray(), 0, newRawSections, i * sizeOfSection, sizeOfSection);
            }

            // Null the data directory entry if any available
            var de = ImageNtHeaders
                     .OptionalHeader
                     .DataDirectory
                     .FirstOrDefault(d => d.VirtualAddress == sectionToRemove.VirtualAddress &&
                                     d.Size == sectionToRemove.VirtualSize);

            if (de != null)
            {
                de.Size           = 0;
                de.VirtualAddress = 0;
            }

            // Null the old section headers
            RawFile.WriteBytes(sectionHeaderOffset, new byte[ImageSectionHeaders.Count() * sizeOfSection]);

            // Write the new sections headers
            RawFile.WriteBytes(sectionHeaderOffset, newRawSections);

            // Reparse section header
            _nativeStructureParsers.ReparseSectionHeaders();
        }
예제 #21
0
        /// <summary>
        /// Add a new section to the PE file.
        /// </summary>
        /// <param name="name">Name of the section to add. At max. 8 characters.</param>
        /// <param name="size">Size in bytes of the new section.</param>
        /// <param name="characteristics">Section characteristics.</param>
        public void AddSection(string name, int size, ScnCharacteristicsType characteristics)
        {
            if (ImageNtHeaders is null)
            {
                throw new Exception("IMAGE_NT_HEADERS must not be null.");
            }
            if (ImageDosHeader is null)
            {
                throw new Exception("IMAGE_DOS_HEADER must not be null");
            }

            uint getNewSizeOfImage()
            {
                var factor         = size / (double)ImageNtHeaders.OptionalHeader.SectionAlignment;
                var additionalSize = (uint)Math.Ceiling(factor) * ImageNtHeaders !.OptionalHeader.SectionAlignment;

                return(ImageNtHeaders.OptionalHeader.SizeOfImage + additionalSize);
            }

            uint getNewSecHeaderOffset()
            {
                var sizeOfSection        = 0x28;
                var x                    = (uint)ImageNtHeaders !.FileHeader.SizeOfOptionalHeader + 0x18;
                var startOfSectionHeader = ImageDosHeader.E_lfanew + x;

                return((uint)(startOfSectionHeader + (ImageNtHeaders.FileHeader.NumberOfSections * sizeOfSection)));
            }

            uint getNewSecVA()
            {
                var lastSec      = ImageSectionHeaders.OrderByDescending(sh => sh.VirtualAddress).First();
                var vaLastSecEnd = lastSec.VirtualAddress + lastSec.VirtualSize;
                var factor       = vaLastSecEnd / (double)ImageNtHeaders.OptionalHeader.SectionAlignment;

                return((uint)(Math.Ceiling(factor) * ImageNtHeaders.OptionalHeader.SectionAlignment));
            }

            // Append new section to end of file
            var paNewSec = RawFile.AppendBytes(new Byte[size]);

            // Add new entry in section table
            var newSection = new ImageSectionHeader(RawFile, getNewSecHeaderOffset(), ImageNtHeaders.OptionalHeader.ImageBase)
            {
                Name                 = name,
                VirtualSize          = (uint)size,
                VirtualAddress       = getNewSecVA(),
                SizeOfRawData        = (uint)size,
                PointerToRawData     = (uint)paNewSec,
                PointerToRelocations = 0,
                PointerToLinenumbers = 0,
                NumberOfRelocations  = 0,
                NumberOfLinenumbers  = 0,
                Characteristics      = characteristics
            };

            // Increase number of sections
            ImageNtHeaders.FileHeader.NumberOfSections = (ushort)(ImageNtHeaders.FileHeader.NumberOfSections + 1);

            // Adjust image size by image alignment
            ImageNtHeaders.OptionalHeader.SizeOfImage = getNewSizeOfImage();

            // Reparse section headers
            _nativeStructureParsers.ReparseSectionHeaders();
        }
예제 #22
0
        public async Task <int> Add(
            string title,
            string autenticatedUserId,
            string description,
            bool isPrivate,
            RawFile rawImage,
            IEnumerable <Tag> imageTags     = null,
            IEnumerable <Album> imageAlbums = null)
        {
            var currentUser = this.Users
                              .All()
                              .FirstOrDefault(u => u.Id == autenticatedUserId && u.IsDeleted == false);

            if (currentUser == null)
            {
                return(GlobalConstants.ItemNotFoundReturnValue);
            }

            var newImageInfo = new ImageInfo()
            {
                OriginalName      = rawImage.OriginalFileName,
                OriginalExtension = rawImage.FileExtension,
            };

            var newImage = new Image()
            {
                Title       = title,
                Author      = currentUser,
                Description = description,
                IsPrivate   = isPrivate,
                UploadedOn  = DateTime.Now,
                ImageInfo   = newImageInfo,
            };

            var guid = Guid.NewGuid().ToString();

            newImage.Url = await this.webSrorageService
                           .UploadImageToCloud(
                rawImage.Content,
                guid,
                rawImage.FileExtension,
                currentUser.Id);

            newImage.ThumbnailUrl = await this.webSrorageService
                                    .UploadImageToCloud(
                rawImage.PreviewContent,
                guid + ThumbnailExtension,
                rawImage.FileExtension,
                currentUser.Id);

            if (imageTags != null)
            {
                imageTags.ForEach(t =>
                {
                    newImage.Tags.Add(t);
                });
            }

            if (imageAlbums != null)
            {
                imageAlbums.ForEach(a =>
                {
                    newImage.Albums.Add(a);
                });
            }

            try
            {
                this.images.Add(newImage);
                await this.images.SaveChangesAsync();
            }
            catch (Exception e)
            {
                var er = e.Message;
            }



            try
            {
                string notification = string.Format("{0} added picture {1}", currentUser.UserName, title);
                this.notifier.Notify(notification);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(newImage.Id);
        }
예제 #23
0
        private void WriteFile(string filepath)
        {
            onStartingFile(new FilepathEventArgs(filepath));

            txt_outputs = new Dictionary<string, StreamWriter>();
            mgf_outputs = new Dictionary<string, StreamWriter>();
            SortedDictionary<string, int> spectrum_counts = new SortedDictionary<string, int>();
            SortedDictionary<string, int> dta_counts = new SortedDictionary<string, int>();
            SortedDictionary<int, double> retention_times = new SortedDictionary<int, double>();
            SortedDictionary<int, double> scan_filter_mzs = new SortedDictionary<int, double>();
            SortedDictionary<int, double> precursor_mzs = new SortedDictionary<int, double>();
            SortedDictionary<int, double> precursor_intensities = new SortedDictionary<int, double>();
            SortedDictionary<int, double> precursor_denormalized_intensities = new SortedDictionary<int, double>();
            SortedDictionary<int, int> precursor_charge_states = new SortedDictionary<int, int>();
            SortedDictionary<int, string> precursor_fragmentation_methods = new SortedDictionary<int, string>();
            SortedDictionary<int, double> elapsed_scan_times = new SortedDictionary<int, double>();
            SortedDictionary<int, double> ion_injection_times = new SortedDictionary<int, double>();
            SortedDictionary<int, double?> precursor_sns = new SortedDictionary<int, double?>();
            SortedDictionary<int, int> precursor_peak_depths = new SortedDictionary<int, int>();

            using (RawFile raw = new RawFile(filepath))
            {
                // Open connection to the raw file
                raw.Open();

                // Can generate massive background spectrum here... to remove from all the other spectra

                using (StreamWriter writer = new StreamWriter(Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(filepath) + ".txt")))
                {
                    int count = 0;
                    foreach (Spectrum msms in raw.GetNextMSMS())
                    {
                        // Clean the spectrum
                        CleanSpectrum(msms);

                        // Write the spectrum out
                        WriteSpectrumToFile(writer, msms);

                        // Change Progress
                        if (count > 100)
                        {
                            count = 0;
                            double progress = (double)(msms.ScanNumber - raw.FirstScanNumber + 1) / (raw.LastScanNumber - raw.FirstScanNumber + 1);
                            onUpdateProgress(new ProgressEventArgs((int)(progress * 100)));
                        }
                        count++;
                    }
                }
            }

            //            for (int scan_number = first_scan_number; scan_number <= last_scan_number; scan_number++)
            //            {
            //                string scan_filter = null;
            //                raw.GetFilterForScanNum(scan_number, ref scan_filter);

            //                if (scan_filter != null && scan_filter.Contains("@"))
            //                {
            //*                    retention_times.Add(scan_number, time);
            //*                    scan_filter_mzs.Add(scan_number, precursor_mz);

            //                    if (!no_precursor_scan)
            //                    {
            //                  /

            //                        int? precursor_index = null;
            //                        int precusor_data_index = (int)RawLabelDataColumn.MZ;
            //                        int min = precursor_data.GetLowerBound(1);
            //                        int max = precursor_data.GetUpperBound(1);
            //                        for (int i = min; i <= max; i++)
            //                        {
            //                            if (Math.Abs(precursor_data[precusor_data_index, i] - precursor_mz) <= PEAK_IDENTIFICATION_MASS_TOLERANCE)
            //                            {
            //                                if (!precursor_index.HasValue || precursor_data[(int)RawLabelDataColumn.Intensity, i] > precursor_data[(int)RawLabelDataColumn.Intensity, precursor_index.Value])
            //                                {
            //                                    precursor_index = i;
            //                                }
            //                            }
            //                        }

            //                        if (!precursor_index.HasValue)
            //                        {
            //                            for (int i = precursor_data.GetLowerBound(1); i <= precursor_data.GetUpperBound(1); i++)
            //                            {
            //                                if (!precursor_index.HasValue || Math.Abs(precursor_data[(int)RawLabelDataColumn.MZ, i] - precursor_mz) < Math.Abs(precursor_data[(int)RawLabelDataColumn.MZ, precursor_index.Value] - precursor_mz))
            //                                {
            //                                    precursor_index = i;
            //                                }
            //                            }
            //                        }

            //                        if (precursor_index.HasValue)
            //                        {
            //                            precursor_mz = precursor_data[(int)RawLabelDataColumn.MZ, precursor_index.Value];

            //                            precursor_mzs.Add(scan_number, precursor_mz);
            //                            precursor_intensities.Add(scan_number, precursor_data[(int)RawLabelDataColumn.Intensity, precursor_index.Value]);

            //                            object precursor_header_labels = null;
            //                            object precursor_header_values = null;
            //                            int precursor_array_size = -1;
            //                            raw.GetTrailerExtraForScanNum(precursor_scan_number, ref precursor_header_labels, ref precursor_header_values, ref precursor_array_size);
            //                            string[] precursor_header_label_strings = (string[])precursor_header_labels;
            //                            string[] precursor_header_value_strings = (string[])precursor_header_values;
            //                            if (precursor_header_label_strings != null && precursor_header_value_strings != null)
            //                            {
            //                                for (int header_i = precursor_header_label_strings.GetLowerBound(0); header_i <= precursor_header_label_strings.GetUpperBound(0); header_i++)
            //                                {
            //                                    if (precursor_header_label_strings[header_i].StartsWith("Ion Injection Time (ms)"))
            //                                    {
            //                                        precursor_denormalized_intensities.Add(scan_number, precursor_data[(int)RawLabelDataColumn.Intensity, precursor_index.Value] * double.Parse(precursor_header_value_strings[header_i]) / 1000.0);
            //                                    }
            //                                }
            //                            }

            //                            if (precursor_data.GetLength(0) > 2)
            //                            {
            //                                precursor_sns.Add(scan_number, (precursor_data[(int)RawLabelDataColumn.Intensity, precursor_index.Value] - precursor_data[(int)RawLabelDataColumn.NoiseBaseline, precursor_index.Value]) / precursor_data[(int)RawLabelDataColumn.NoiseLevel, precursor_index.Value]);
            //                            }
            //                            else
            //                            {
            //                                precursor_sns.Add(scan_number, null);
            //                            }

            //                            int peak_depth = 1;
            //                            int precursor_data_index = (int)RawLabelDataColumn.Intensity;
            //                            double prec_value = precursor_data[precursor_data_index, precursor_index.Value];
            //                            min = precursor_data.GetLowerBound(1);
            //                            max = precursor_data.GetUpperBound(1);
            //                            for (int i = min; i <= max; i++)
            //                            {
            //                                if (precursor_data[precursor_data_index, i] > prec_value && i != precursor_index.Value)
            //                                {
            //                                    peak_depth++;
            //                                }
            //                            }

            //                            precursor_peak_depths.Add(scan_number, peak_depth);
            //                        }
            //                    }

            //                    object header_labels = null;
            //                    object header_values = null;
            //                    int array_size = -1;
            //                    raw.GetTrailerExtraForScanNum(scan_number, ref header_labels, ref header_values, ref array_size);
            //                    string[] header_label_strings = (string[])header_labels;
            //                    string[] header_value_strings = (string[])header_values;
            //                    int charge = 0;
            //                    if (header_label_strings != null && header_value_strings != null)
            //                    {
            //                        for (int header_i = header_label_strings.GetLowerBound(0); header_i <= header_label_strings.GetUpperBound(0); header_i++)
            //                        {
            //                            if (header_label_strings[header_i].StartsWith("Charge"))
            //                            {
            //                                charge = int.Parse(header_value_strings[header_i]);
            //                                if (scan_filter.Contains(" - "))
            //                                {
            //                                    charge = -charge;
            //                                }
            //                                precursor_charge_states.Add(scan_number, charge);
            //                            }
            //                            else if (header_label_strings[header_i].StartsWith("Elapsed Scan Time (sec)"))
            //                            {
            //                                elapsed_scan_times.Add(scan_number, double.Parse(header_value_strings[header_i]));
            //                            }
            //                            else if (header_label_strings[header_i].StartsWith("Ion Injection Time (ms)"))
            //                            {
            //                                ion_injection_times.Add(scan_number, double.Parse(header_value_strings[header_i]));
            //                            }
            //                        }
            //                    }

            //                    object labels = null;
            //                    object flags = null;
            //                    raw.GetLabelData(ref labels, ref flags, ref scan_number);

            //                    double[,] data = (double[,])labels;
            //                    if (data.Length == 0)
            //                    {
            //                        double centroid_peak_width = -1.0;
            //                        labels = null;
            //                        flags = null;
            //                        int mass_list_array_size = -1;
            //                        raw.GetMassListFromScanNum(ref scan_number, null, 0, 0, 0, 1, ref centroid_peak_width, ref labels, ref flags, ref mass_list_array_size);
            //                        data = (double[,])labels;
            //                    }

            //                    double total_ion_current = 0.0;
            //                    double base_peak_mz = -1.0;
            //                    double base_peak_intensity = -1.0;
            //                    for (int data_i = data.GetLowerBound(1); data_i <= data.GetUpperBound(1); data_i++)
            //                    {
            //                        total_ion_current += data[(int)RawLabelDataColumn.Intensity, data_i];

            //                        if (base_peak_mz < 0.0 ||
            //                            data[(int)RawLabelDataColumn.Intensity, data_i] > base_peak_intensity)
            //                        {
            //                            base_peak_mz = data[(int)RawLabelDataColumn.MZ, data_i];
            //                            base_peak_intensity = data[(int)RawLabelDataColumn.Intensity, data_i];
            //                        }
            //                    }

            //                    List<int> charges = new List<int>();
            //                    if (charge == 0 || no_precursor_scan)
            //                    {
            //                        for (int assumed_charge_state = minimumAssumedPrecursorChargeState; assumed_charge_state <= maximumAssumedPrecursorChargeState; assumed_charge_state++)
            //                        {
            //                            charges.Add(assumed_charge_state);
            //                        }
            //                    }
            //                    else
            //                    {
            //                        charges.Add(charge);
            //                    }

            //                    string mass_analyzer = scan_filter.Substring(0, 4).ToUpper();
            //                    if (!mass_analyzer.Contains("MS"))
            //                    {
            //                        mass_analyzer = "TQMS";
            //                    }

            //                    string fragmentation_method = null;
            //                    if (groupByActivationEnergyTime)
            //                    {
            //                        foreach (int i in AllIndicesOf(scan_filter, '@'))
            //                        {
            //                            string temp_scan_filter = scan_filter.Substring(i + 1);
            //                            temp_scan_filter = temp_scan_filter.Substring(0, temp_scan_filter.IndexOf(' '));
            //                            fragmentation_method += temp_scan_filter.ToUpper() + '-';
            //                        }
            //                    }
            //                    else
            //                    {
            //                        foreach (int i in AllIndicesOf(scan_filter, '@'))
            //                        {
            //                            fragmentation_method += scan_filter.Substring(i + 1, 3).ToUpper() + '-';
            //                        }
            //                    }
            //                    fragmentation_method = fragmentation_method.Substring(0, fragmentation_method.Length - 1);

            //                    string base_output_filename = Path.GetFileNameWithoutExtension(filepath) + '_' + mass_analyzer + '_' + fragmentation_method;

            //                    precursor_fragmentation_methods.Add(scan_number, fragmentation_method);

            //                    if (!spectrum_counts.ContainsKey(mass_analyzer))
            //                    {
            //                        spectrum_counts.Add(mass_analyzer, 0);
            //                    }
            //                    spectrum_counts[mass_analyzer]++;

            //                    if (!dta_counts.ContainsKey(mass_analyzer))
            //                    {
            //                        dta_counts.Add(mass_analyzer, 0);
            //                    }
            //                    dta_counts[mass_analyzer] += charges.Count;

            //                    if (!spectrum_counts.ContainsKey(fragmentation_method))
            //                    {
            //                        spectrum_counts.Add(fragmentation_method, 0);
            //                    }
            //                    spectrum_counts[fragmentation_method]++;

            //                    if (!dta_counts.ContainsKey(fragmentation_method))
            //                    {
            //                        dta_counts.Add(fragmentation_method, 0);
            //                    }
            //                    dta_counts[fragmentation_method] += charges.Count;

            //                    if (!spectrum_counts.ContainsKey(mass_analyzer + ' ' + fragmentation_method))
            //                    {
            //                        spectrum_counts.Add(mass_analyzer + ' ' + fragmentation_method, 0);
            //                    }
            //                    spectrum_counts[mass_analyzer + ' ' + fragmentation_method]++;

            //                    if (!dta_counts.ContainsKey(mass_analyzer + ' ' + fragmentation_method))
            //                    {
            //                        dta_counts.Add(mass_analyzer + ' ' + fragmentation_method, 0);
            //                    }
            //                    dta_counts[mass_analyzer + ' ' + fragmentation_method] += charges.Count;

            //                    if (sequestDtaOutput || omssaTxtOutput || mascotMgfOutput)
            //                    {
            //                        List<MSPeak> all_peaks = new List<MSPeak>();
            //                        for (int data_i = data.GetLowerBound(1); data_i <= data.GetUpperBound(1); data_i++)
            //                        {
            //                            double mz = data[(int)RawLabelDataColumn.MZ, data_i];

            //                            all_peaks.Add(new MSPeak(mz, data[(int)RawLabelDataColumn.Intensity, data_i]));
            //                        }

            //                        foreach (int charge_i in charges)
            //                        {
            //                            double retention_time_min = double.NaN;
            //                            raw.RTFromScanNum(scan_number, ref retention_time_min);
            //                            double retention_time_s = retention_time_min * 60;

            //                            string dta_filepath = Path.GetFileNameWithoutExtension(filepath) +
            //                                '.' + mass_analyzer + '.' + fragmentation_method +
            //                                '.' + scan_number.ToString() + '.' +
            //                                scan_number.ToString() + '.' +
            //                                charge_i.ToString() + '.' +
            //                                "RT_" + retention_time_min.ToString("0.000") + "_min_" + retention_time_s.ToString("0.0") + "_s" +
            //                                ".dta";

            //                            double precursor_mass = MassFromMZ(precursor_mz, charge_i);

            //                            StringBuilder dta_content_sb = new StringBuilder();

            //                            StringBuilder mgf_content_sb = new StringBuilder();

            //                            dta_content_sb.AppendLine((precursor_mass + PROTON_MASS).ToString("0.00000") + ' ' + charge_i.ToString());

            //                            mgf_content_sb.AppendLine("BEGIN IONS");
            //                            mgf_content_sb.AppendLine("Title=" + Path.GetFileNameWithoutExtension(dta_filepath));
            //                            mgf_content_sb.AppendLine("SCANS=" + scan_number.ToString());
            //                            mgf_content_sb.AppendLine("RTINSECONDS=" + retention_time_s.ToString());
            //                            mgf_content_sb.AppendLine("PEPMASS=" + precursor_mz.ToString("0.00000"));
            //                            mgf_content_sb.AppendLine("CHARGE=" + charge_i.ToString("0+;0-"));

            //                            List<MSPeak> peaks = new List<MSPeak>(all_peaks);
            //                            bool isHCD = fragmentation_method.StartsWith("HCD");
            //                            bool isETD = fragmentation_method.StartsWith("ETD") || fragmentation_method.StartsWith("ECD");

            //                            // FOR AARON
            //#if Aaron_Experiment
            //                                    isETD = isETD || scan_filter.Contains("hcd0.5") || scan_filter.Contains("hcd1.5");
            //#endif
            //                            //

            //                            if (neutralLossesIncluded)
            //                            {
            //                                List<KeyValuePair<double, double>> mzs = new List<KeyValuePair<double, double>>();
            //                                foreach (double nl_mass in this.neutralLosses)
            //                                {
            //                                    double mz = precursor_mz - MZFromMass(nl_mass, charge_i);
            //                                    double min = mz - LOW_PRECURSOR_CLEANING_WINDOW_MZ;
            //                                    double max = mz + HIGH_PRECURSOR_CLEANING_WINDOW_MZ;
            //                                    mzs.Add(new KeyValuePair<double, double>(min, max));
            //                                }
            //                                int p = 0;
            //                                while (p < peaks.Count)
            //                                {
            //                                    double mz = peaks[p].MZ;
            //                                    if (mz >= precursor_mz)
            //                                    {
            //                                        break;
            //                                    }
            //                                    bool removed = false;
            //                                    foreach (KeyValuePair<double, double> minmax in mzs)
            //                                    {
            //                                        if (mz >= minmax.Key && mz <= minmax.Value)
            //                                        {
            //                                            peaks.RemoveAt(p);
            //                                            removed = true;
            //                                            break;
            //                                        }
            //                                    }
            //                                    if (!removed)
            //                                    {
            //                                        p++;
            //                                    }
            //                                }
            //                            }

            //                            // ETD pre-processing
            //                            if (enableEtdPreProcessing && isETD)
            //                            {
            //                                // negative ETD
            //                                if (scan_filter.Contains(" - "))
            //                                {
            //                                    int p1 = 0;
            //                                    while (p1 < peaks.Count)
            //                                    {
            //                                        double mz = peaks[p1].MZ;

            //                                        bool clean = false;

            //                                        for (int reduced_precursor_charge = -2; reduced_precursor_charge >= charge_i + 1; reduced_precursor_charge--)
            //                                        {
            //                                            if (mz >= MZFromMass(precursor_mass - NETD_LOW_NEUTRAL_LOSS_CLEANING_WINDOW_DA, reduced_precursor_charge) &&
            //                                                mz <= MZFromMass(precursor_mass + NETD_HIGH_NEUTRAL_LOSS_CLEANING_WINDOW_DA, reduced_precursor_charge))
            //                                            {
            //                                                clean = true;
            //                                                break;
            //                                            }

            //                                            if (mz >= MZFromMass(precursor_mass + NETD_ADDUCT_CLEANING_WINDOW_DA, reduced_precursor_charge) - NETD_ADDUCT_LOW_CLEANING_WINDOW_MZ &&
            //                                                mz <= MZFromMass(precursor_mass + NETD_ADDUCT_CLEANING_WINDOW_DA, reduced_precursor_charge) + NETD_ADDUCT_LOW_CLEANING_WINDOW_MZ)
            //                                            {
            //                                                clean = true;
            //                                                break;
            //                                            }
            //                                        }

            //                                        if (!clean)
            //                                        {
            //                                            if (mz >= MZFromMass(precursor_mass, -1) - NETD_SINGLY_CHARGED_LOW_NEUTRAL_LOSS_CLEANING_WINDOW_MZ)
            //                                            {
            //                                                clean = true;
            //                                            }
            //                                        }

            //                                        if (clean)
            //                                        {
            //                                            peaks.RemoveAt(p1);
            //                                        }
            //                                        else
            //                                        {
            //                                            p1++;
            //                                        }
            //                                    }
            //                                }
            //                                // positive ETD
            //                                else
            //                                {
            //                                    int p1 = 0;
            //                                    while (p1 < peaks.Count)
            //                                    {
            //                                        double mz = peaks[p1].MZ;

            //                                        bool clean = false;

            //                                        for (int reduced_precursor_charge = 1; reduced_precursor_charge <= charge_i - 1; reduced_precursor_charge++)
            //                                        {
            //                                            if (mz >= MZFromMass(precursor_mass - LOW_NEUTRAL_LOSS_CLEANING_WINDOW_DA, reduced_precursor_charge) &&
            //                                                mz < MZFromMass(precursor_mass, reduced_precursor_charge) + HIGH_PRECURSOR_CLEANING_WINDOW_MZ)
            //                                            {
            //                                                clean = true;
            //                                                break;
            //                                            }
            //                                        }

            //                                        if (clean)
            //                                        {
            //                                            peaks.RemoveAt(p1);
            //                                        }
            //                                        else
            //                                        {
            //                                            p1++;
            //                                        }
            //                                    }
            //                                }
            //                            }

            //                            // TMT duplex cleaning
            //                            if (cleanTmtDuplex)
            //                            {
            //                                if (fragmentation_method.StartsWith("CID") || fragmentation_method.StartsWith("PQD") || fragmentation_method.StartsWith("HCD"))
            //                                {
            //                                    for (int reduced_charge_i = charge_i - 1; reduced_charge_i >= 1; reduced_charge_i--)
            //                                    {
            //                                        double precursor_tmt_duplex_tag_cleaning_mz = precursor_mz * reduced_charge_i - TMT_DUPLEX_CAD_TAG_LOSS_DA / reduced_charge_i;

            //                                        int p1 = 0;
            //                                        while (p1 < peaks.Count)
            //                                        {
            //                                            double mz = peaks[p1].MZ;

            //                                            if ((mz >= MINIMUM_TMT_DUPLEX_CAD_REPORTER_MZ - TMT_DUPLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= MAXIMUM_TMT_DUPLEX_CAD_REPORTER_MZ + TMT_DUPLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= TMT_DUPLEX_CAD_TAG_MZ - TMT_DUPLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= TMT_DUPLEX_CAD_TAG_MZ + TMT_DUPLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= precursor_tmt_duplex_tag_cleaning_mz - TMT_DUPLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= precursor_tmt_duplex_tag_cleaning_mz + TMT_DUPLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ))
            //                                            {
            //                                                peaks.RemoveAt(p1);
            //                                            }
            //                                            else
            //                                            {
            //                                                p1++;
            //                                            }
            //                                        }
            //                                    }
            //                                }
            //                                else if (fragmentation_method.StartsWith("ETD"))
            //                                {
            //                                    for (int reduced_charge_i = charge_i - 1; reduced_charge_i >= 1; reduced_charge_i--)
            //                                    {
            //                                        double precursor_tmt_duplex_reporter_loss_cleaning_mz = precursor_mz * charge_i - TMT_DUPLEX_ETD_REPORTER_LOSS_DA / reduced_charge_i;
            //                                        double precursor_tmt_duplex_tag_loss_cleaning_mz = precursor_mz * charge_i - TMT_DUPLEX_ETD_TAG_LOSS_DA / reduced_charge_i;

            //                                        int p1 = 0;
            //                                        while (p1 < peaks.Count)
            //                                        {
            //                                            double mz = peaks[p1].MZ;
            //                                            if ((mz >= MINIMUM_TMT_DUPLEX_ETD_REPORTER_MZ - TMT_DUPLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= MAXIMUM_TMT_DUPLEX_ETD_REPORTER_MZ + TMT_DUPLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= TMT_DUPLEX_ETD_TAG_MZ - TMT_DUPLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= TMT_DUPLEX_ETD_TAG_MZ + TMT_DUPLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= precursor_tmt_duplex_reporter_loss_cleaning_mz - TMT_DUPLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= precursor_tmt_duplex_reporter_loss_cleaning_mz + TMT_DUPLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= precursor_tmt_duplex_tag_loss_cleaning_mz - TMT_DUPLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= precursor_tmt_duplex_tag_loss_cleaning_mz + TMT_DUPLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ))
            //                                            {
            //                                                peaks.RemoveAt(p1);
            //                                            }
            //                                            else
            //                                            {
            //                                                p1++;
            //                                            }
            //                                        }
            //                                    }
            //                                }
            //                            }
            //                            // iTRAQ 4-plex cleaning
            //                            if (cleanItraq4Plex)
            //                            {
            //                                if (fragmentation_method.StartsWith("CID") || fragmentation_method.StartsWith("PQD") || fragmentation_method.StartsWith("HCD"))
            //                                {
            //                                    double precursor_itraq_4plex_tag_cleaning_mz = precursor_mz * charge_i - ITRAQ_4PLEX_CAD_TAG_LOSS_DA;

            //                                    int p1 = 0;
            //                                    while (p1 < peaks.Count)
            //                                    {
            //                                        double mz = peaks[p1].MZ;

            //                                        if ((mz >= MINIMUM_ITRAQ_4PLEX_CAD_REPORTER_MZ - ITRAQ_4PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= MAXIMUM_ITRAQ_4PLEX_CAD_REPORTER_MZ + ITRAQ_4PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                            || (mz >= ITRAQ_4PLEX_CAD_TAG_MZ - ITRAQ_4PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= ITRAQ_4PLEX_CAD_TAG_MZ + ITRAQ_4PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                            || (mz >= precursor_itraq_4plex_tag_cleaning_mz - ITRAQ_4PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= precursor_itraq_4plex_tag_cleaning_mz + ITRAQ_4PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ))
            //                                        {
            //                                            peaks.RemoveAt(p1);
            //                                        }
            //                                        else
            //                                        {
            //                                            p1++;
            //                                        }
            //                                    }
            //                                }
            //                                else if (fragmentation_method.StartsWith("ETD"))
            //                                {
            //                                    double precursor_itraq_4plex_reporter_loss_cleaning_mz = precursor_mz * charge_i - ITRAQ_4PLEX_ETD_REPORTER_LOSS_DA;
            //                                    double precursor_itraq_4plex_tag_loss_cleaning_mz = precursor_mz * charge_i - ITRAQ_4PLEX_ETD_TAG_LOSS_DA;

            //                                    int p1 = 0;
            //                                    while (p1 < peaks.Count)
            //                                    {
            //                                        double mz = peaks[p1].MZ;
            //                                        if ((mz >= MINIMUM_ITRAQ_4PLEX_ETD_REPORTER_MZ - ITRAQ_4PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= MAXIMUM_ITRAQ_4PLEX_ETD_REPORTER_MZ + ITRAQ_4PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                            || (mz >= ITRAQ_4PLEX_ETD_TAG_MZ - ITRAQ_4PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= ITRAQ_4PLEX_ETD_TAG_MZ + ITRAQ_4PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                            || (mz >= precursor_itraq_4plex_reporter_loss_cleaning_mz - ITRAQ_4PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= precursor_itraq_4plex_reporter_loss_cleaning_mz + ITRAQ_4PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ)
            //                                            || (mz >= precursor_itraq_4plex_tag_loss_cleaning_mz - ITRAQ_4PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= precursor_itraq_4plex_tag_loss_cleaning_mz + ITRAQ_4PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ))
            //                                        {
            //                                            peaks.RemoveAt(p1);
            //                                        }
            //                                        else
            //                                        {
            //                                            p1++;
            //                                        }
            //                                    }
            //                                }
            //                            }
            //                            // TMT 6-plex cleaning
            //                            if (cleanTmt6Plex)
            //                            {
            //                                if (fragmentation_method.StartsWith("CID") || fragmentation_method.StartsWith("PQD") || fragmentation_method.StartsWith("HCD"))
            //                                {
            //                                    for (int reduced_charge_i = charge_i - 1; reduced_charge_i >= 1; reduced_charge_i--)
            //                                    {
            //                                        double precursor_tmt_6plex_tag_cleaning_mz = precursor_mz * reduced_charge_i - TMT_6PLEX_CAD_TAG_LOSS_DA / reduced_charge_i;

            //                                        int p1 = 0;
            //                                        while (p1 < peaks.Count)
            //                                        {
            //                                            double mz = peaks[p1].MZ;

            //                                            if ((mz >= MINIMUM_TMT_6PLEX_CAD_REPORTER_MZ - TMT_6PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= MAXIMUM_TMT_6PLEX_CAD_REPORTER_MZ + TMT_6PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= TMT_6PLEX_CAD_TAG_MZ - TMT_6PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= TMT_6PLEX_CAD_TAG_MZ + TMT_6PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= precursor_tmt_6plex_tag_cleaning_mz - TMT_6PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= precursor_tmt_6plex_tag_cleaning_mz + TMT_6PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ))
            //                                            {
            //                                                peaks.RemoveAt(p1);
            //                                            }
            //                                            else
            //                                            {
            //                                                p1++;
            //                                            }
            //                                        }
            //                                    }
            //                                }
            //                                else if (fragmentation_method.StartsWith("ETD"))
            //                                {
            //                                    for (int reduced_charge_i = charge_i - 1; reduced_charge_i >= 1; reduced_charge_i--)
            //                                    {
            //                                        double precursor_tmt_6plex_reporter_loss_cleaning_mz = precursor_mz * charge_i - TMT_6PLEX_ETD_REPORTER_LOSS_DA / reduced_charge_i;
            //                                        double precursor_tmt_6plex_tag_loss_cleaning_mz = precursor_mz * charge_i - TMT_6PLEX_ETD_TAG_LOSS_DA / reduced_charge_i;

            //                                        int p1 = 0;
            //                                        while (p1 < peaks.Count)
            //                                        {
            //                                            double mz = peaks[p1].MZ;
            //                                            if ((mz >= MINIMUM_TMT_6PLEX_ETD_REPORTER_MZ - TMT_6PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= MAXIMUM_TMT_6PLEX_ETD_REPORTER_MZ + TMT_6PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= TMT_6PLEX_ETD_TAG_MZ - TMT_6PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= TMT_6PLEX_ETD_TAG_MZ + TMT_6PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= precursor_tmt_6plex_reporter_loss_cleaning_mz - TMT_6PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= precursor_tmt_6plex_reporter_loss_cleaning_mz + TMT_6PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ)
            //                                                || (mz >= precursor_tmt_6plex_tag_loss_cleaning_mz - TMT_6PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                                && mz <= precursor_tmt_6plex_tag_loss_cleaning_mz + TMT_6PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ))
            //                                            {
            //                                                peaks.RemoveAt(p1);
            //                                            }
            //                                            else
            //                                            {
            //                                                p1++;
            //                                            }
            //                                        }
            //                                    }
            //                                }
            //                            }
            //                            // iTRAQ 8-plex cleaning
            //                            if (cleanItraq8Plex)
            //                            {
            //                                if (fragmentation_method.StartsWith("CID") || fragmentation_method.StartsWith("PQD") || fragmentation_method.StartsWith("HCD"))
            //                                {
            //                                    double precursor_itraq_4plex_tag_cleaning_mz = precursor_mz * charge_i - ITRAQ_8PLEX_CAD_TAG_LOSS_DA;

            //                                    int p1 = 0;
            //                                    while (p1 < peaks.Count)
            //                                    {
            //                                        double mz = peaks[p1].MZ;

            //                                        if ((mz >= MINIMUM_ITRAQ_8PLEX_CAD_REPORTER_MZ - ITRAQ_8PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= MAXIMUM_ITRAQ_8PLEX_CAD_REPORTER_MZ + ITRAQ_8PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                            || (mz >= ITRAQ_8PLEX_CAD_TAG_MZ - ITRAQ_8PLEX_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= ITRAQ_8PLEX_CAD_TAG_MZ + ITRAQ_8PLEX_CLEANING_MASS_TOLERANCE_MZ)
            //                                            || (mz >= precursor_itraq_4plex_tag_cleaning_mz - ITRAQ_8PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ
            //                                            && mz <= precursor_itraq_4plex_tag_cleaning_mz + ITRAQ_8PLEX_LOSS_CLEANING_MASS_TOLERANCE_MZ))
            //                                        {
            //                                            peaks.RemoveAt(p1);
            //                                        }
            //                                        else
            //                                        {
            //                                            p1++;
            //                                        }
            //                                    }
            //                                }
            //                            }

            //                            MSPeak base_peak = null;
            //                            foreach (MSPeak peak in peaks)
            //                            {
            //                                if (base_peak == null || peak.Intensity > base_peak.Intensity)
            //                                {
            //                                    base_peak = peak;
            //                                }
            //                            }

            //                            //if (omssaTxtOutput)
            //                            //{
            //                            //foreach (MSPeak peak in peaks)
            //                            //{
            //                            //   dta_content_sb.AppendFormat(" {0:0.00000} {1:0.000}", peak.MZ, peak.Intensity).AppendLine();
            //                            //dta_content_sb.AppendLine(string.Format(" {0:0.00000} {1:0.00}", peak.MZ, peak.Intensity));
            //                            //}
            //                            //}

            //                            if (mascotMgfOutput)
            //                            {
            //                                foreach (MSPeak peak in peaks)
            //                                {
            //                                    mgf_content_sb.AppendLine(string.Format("{0:0.00000} {1:0.00}", peak.MZ, peak.Intensity));
            //                                }
            //                            }

            //                            if (sequestDtaOutput)
            //                            {
            //                                StreamWriter dta = new StreamWriter(Path.Combine(outputFolder, dta_filepath));

            //                                if (dta_content_sb.Length > 0)
            //                                {
            //                                    dta.Write(dta_content_sb.ToString());
            //                                }

            //                                dta.Close();
            //                            }

            //                            if (omssaTxtOutput)
            //                            {
            //                                string txt_filepath = Path.Combine(outputFolder,
            //                                    base_output_filename + ".txt");

            //                                if (!txt_outputs.ContainsKey(txt_filepath))
            //                                {
            //                                    txt_outputs.Add(txt_filepath, new StreamWriter(txt_filepath));
            //                                }

            //                                StreamWriter txt = txt_outputs[txt_filepath];

            //                                txt.WriteLine("<dta id=\"" + scan_number.ToString() + "\" name=\"" + dta_filepath + "\">");
            //                                txt.WriteLine();

            //                                foreach (MSPeak peak in peaks)
            //                                {
            //                                    txt.WriteLine(" {0:0.00000} {1:0.000}", peak.MZ, peak.Intensity);
            //                                    // dta_content_sb.AppendFormat(" {0:0.00000} {1:0.000}", peak.MZ, peak.Intensity).AppendLine();
            //                                    //dta_content_sb.AppendLine(string.Format(" {0:0.00000} {1:0.00}", peak.MZ, peak.Intensity));
            //                                }
            //                                // if(dta_content_sb.Length > 0)
            //                                // {
            //                                //    txt.Write(dta_content_sb.ToString());
            //                                // }

            //                                txt.WriteLine();
            //                                txt.WriteLine();
            //                            }

            //                            if (mascotMgfOutput)
            //                            {
            //                                string mgf_filepath = Path.Combine(outputFolder,
            //                                    base_output_filename + ".mgf");

            //                                if (!mgf_outputs.ContainsKey(mgf_filepath))
            //                                {
            //                                    mgf_outputs.Add(mgf_filepath, new StreamWriter(mgf_filepath));
            //                                }

            //                                StreamWriter mgf = mgf_outputs[mgf_filepath];

            //                                if (mgf_content_sb.Length > 0)
            //                                {
            //                                    mgf.Write(mgf_content_sb.ToString());
            //                                }

            //                                mgf.WriteLine("END IONS");
            //                                mgf.WriteLine();
            //                            }
            //                        }
            //                    }
            //                }

            //    // Perform update progress every 100 scans so not to slow down with all the callbacks
            //    if (scan_number % 100 == 0)
            //    {
            //        double progress = (double)(scan_number - first_scan_number + 1) / (last_scan_number - first_scan_number + 1);
            //        onUpdateProgress(new ProgressEventArgs((int)(progress * 100)));
            //    }
            //}

            //raw.Close();

            //if (txt_outputs != null)
            //{
            //    foreach (StreamWriter sw in txt_outputs.Values)
            //    {
            //        if (sw != null)
            //        {
            //            sw.Close();
            //        }
            //    }
            //}
            //if (mgf_outputs != null)
            //{
            //    foreach (StreamWriter sw in mgf_outputs.Values)
            //    {
            //        if (sw != null)
            //        {
            //            sw.Close();
            //        }
            //    }
            //}

            //using (StreamWriter log = new StreamWriter(Path.Combine(LogFolder, Path.GetFileNameWithoutExtension(filepath) + "_log.txt")))
            //{
            //    log.WriteLine("DTA Generator PARAMETERS");
            //    log.WriteLine("Assumed Precursor Charge State Range: " + minimumAssumedPrecursorChargeState.ToString() + '-' + maximumAssumedPrecursorChargeState.ToString());
            //    log.WriteLine("Clean Precursor: " + cleanPrecursor.ToString());
            //    log.WriteLine("Enable ETD Pre-Processing: " + enableEtdPreProcessing.ToString());
            //    log.WriteLine("Clean TMT Duplex: " + cleanTmtDuplex.ToString());
            //    log.WriteLine("Clean iTRAQ 4-Plex: " + cleanItraq4Plex.ToString());
            //    log.WriteLine("Clean TMT 6-Plex: " + cleanTmt6Plex.ToString());
            //    log.WriteLine("Clean iTRAQ 8-Plex: " + cleanItraq8Plex.ToString());
            //    log.WriteLine();

            //    log.WriteLine("Spectrum Type\tNumber of Scans");
            //    foreach (KeyValuePair<string, int> kvp in spectrum_counts)
            //    {
            //        log.WriteLine(kvp.Key + '\t' + kvp.Value.ToString());
            //    }
            //    log.WriteLine();

            //    log.WriteLine("Spectrum Type\tNumber of DTAs");
            //    foreach (KeyValuePair<string, int> kvp in dta_counts)
            //    {
            //        log.WriteLine(kvp.Key + '\t' + kvp.Value.ToString());
            //    }
            //    log.WriteLine();

            //    double? min_elapsed = null;
            //    double? max_elapsed = null;
            //    double mean_elapsed = 0.0;
            //    foreach (double elapsed_scan_time in elapsed_scan_times.Values)
            //    {
            //        if (!min_elapsed.HasValue || elapsed_scan_time < min_elapsed)
            //        {
            //            min_elapsed = elapsed_scan_time;
            //        }

            //        if (!max_elapsed.HasValue || elapsed_scan_time > max_elapsed)
            //        {
            //            max_elapsed = elapsed_scan_time;
            //        }

            //        mean_elapsed += elapsed_scan_time;
            //    }
            //    mean_elapsed /= elapsed_scan_times.Count;

            //    if (min_elapsed.HasValue)
            //    {
            //        log.WriteLine("Minimum Fragmentation Elapsed Scan Time (sec): " + min_elapsed.Value.ToString());
            //    }
            //    if (max_elapsed.HasValue)
            //    {
            //        log.WriteLine("Maximum Fragmentation Elapsed Scan Time (sec): " + max_elapsed.Value.ToString());
            //    }
            //    if (!Double.IsNaN(mean_elapsed))
            //    {
            //        log.WriteLine("Average Fragmentation Elapsed Scan Time (sec): " + mean_elapsed.ToString());
            //    }

            //    log.WriteLine();

            //    double? min_injection = null;
            //    double? max_injection = null;
            //    double mean_injection = 0.0;
            //    foreach (double ion_injection_time in ion_injection_times.Values)
            //    {
            //        if (!min_injection.HasValue || ion_injection_time < min_injection)
            //        {
            //            min_injection = ion_injection_time;
            //        }

            //        if (!max_injection.HasValue || ion_injection_time > max_injection)
            //        {
            //            max_injection = ion_injection_time;
            //        }

            //        mean_injection += ion_injection_time;
            //    }
            //    mean_injection /= ion_injection_times.Count;

            //    if (min_injection.HasValue)
            //    {
            //        log.WriteLine("Minimum Fragmentation Ion Injection Time (msec): " + min_injection.Value.ToString());
            //    }
            //    if (max_injection.HasValue)
            //    {
            //        log.WriteLine("Maximum Fragmentation Ion Injection Time (msec): " + max_injection.Value.ToString());
            //    }
            //    if (!Double.IsNaN(mean_injection))
            //    {
            //        log.WriteLine("Average Fragmentation Ion Injection Time (msec): " + mean_injection.ToString());
            //    }

            //    log.WriteLine();

            //    log.WriteLine("Fragmentation Scan Summary");
            //    log.Write("Fragmentation Scan Number\t");
            //    log.Write("Retention Time (min.)\t");
            //    log.Write("Scan Filter m/z\t");
            //    log.Write("Precursor m/z\t");
            //    log.Write("Precursor Intensity\t");
            //    log.Write("Precursor Denormalized Intensity\t");
            //    log.Write("Precursor Charge State\t");
            //    log.Write("Precursor S/N Ratio\t");
            //    log.Write("Precursor Peak Depth\t");
            //    log.Write("Fragmentation Method\t");
            //    log.Write("Elapsed Scan Time (sec)\t");
            //    log.Write("Ion Injection Time (msec)");
            //    log.WriteLine();
            //    foreach (int sn2 in retention_times.Keys)
            //    {
            //        log.Write(sn2.ToString() + '\t');
            //        log.Write(retention_times[sn2].ToString("0.00") + '\t');
            //        log.Write(scan_filter_mzs[sn2].ToString("0.00") + '\t');
            //        log.Write((precursor_mzs.ContainsKey(sn2) ? precursor_mzs[sn2].ToString("0.00000") : "n/a") + '\t');
            //        log.Write((precursor_intensities.ContainsKey(sn2) ? precursor_intensities[sn2].ToString("0.0") : "n/a") + '\t');
            //        log.Write((precursor_denormalized_intensities.ContainsKey(sn2) ? precursor_denormalized_intensities[sn2].ToString("0.0") : "n/a") + '\t');
            //        log.Write((precursor_charge_states.ContainsKey(sn2) ? precursor_charge_states[sn2].ToString() : "n/a") + '\t');
            //        log.Write((precursor_sns.ContainsKey(sn2) && precursor_sns[sn2].HasValue ? precursor_sns[sn2].Value.ToString() : "n/a") + '\t');
            //        log.Write((precursor_peak_depths.ContainsKey(sn2) ? precursor_peak_depths[sn2].ToString() : "n/a") + '\t');
            //        log.Write((precursor_fragmentation_methods.ContainsKey(sn2) ? precursor_fragmentation_methods[sn2] : "n/a") + '\t');
            //        log.Write((elapsed_scan_times.ContainsKey(sn2) ? elapsed_scan_times[sn2].ToString() : "n/a") + '\t');
            //        log.Write((ion_injection_times.ContainsKey(sn2) ? ion_injection_times[sn2].ToString() : "n/a") + '\t');
            //        log.WriteLine();
            //    }

            //}

            onFinishedFile(new FilepathEventArgs(filepath));
        }
예제 #24
0
 public ValueOrError<Ticker> Read(RawFile rawTicker)
 {
     return executor.SafeExecute<Ticker>(engine => engine.GetVariable("Read")(rawTicker));
 }
예제 #25
0
        static double MZError = 10; //ppm

        static int Main(string[] args)
        {
            //Параметры соответствуют вводу в форму RawtoMGF
            //args[0] - имя Raw-файла
            //args[1] - имя MGF-файла
            //args[2] - minimal MZ
            //args[3] - maximal MZ
            //args[4] - minimal RT
            //args[5] - maximal RT
            //args[6] - minimal Charge
            //args[7] - maximal Charge
            //args[8] - number of top peaks
            //args[9] - CleanETD:yes / CleanETD:no
            //args[10] - Instrument:yes / Instrument:no
            //дополнительно (к версии 2.0.6)
            //args[11] - CheckSpectra:yes / CheckSpectra:no
            try{
                Console.ReadLine();
                //Thread.Sleep(30000);
                string InFileName      = args[0];
                string OutFileName     = args[1];
                double MinMZ           = Convert.ToDouble(args[2]);
                double MaxMZ           = Convert.ToDouble(args[3]);
                double MinRT           = Convert.ToDouble(args[4]);
                double MaxRT           = Convert.ToDouble(args[5]);
                int    MinCharge       = Convert.ToInt32(args[6]);
                int    MaxCharge       = Convert.ToInt32(args[7]);
                int    MaxPeakNumber   = Convert.ToInt32(args[8]);
                bool   CleanETD        = args[9].Contains("CleanETD:yes");
                bool   MarkInstrtument = args[10].Contains("Instrument:yes");
                bool   CheckSpectra    = args[11].Contains("CheckSpectra:yes");
                bool   RTApex          = args[12].Contains("RTApex:yes");

                string[] Refs = { "@cid", "@hcd", "@etd", "@ecd", "FTMS", "ITMS" };

                MSFileReader_XRawfile RawFile;
                int              Spectra, LocalCount = 0;
                string           Filter;
                MGFFile          MGF;
                MGFSpectrum      ms;
                Childs           ch;
                ChildbyIntensity ci = new ChildbyIntensity();
                ChildbyMass      cm = new ChildbyMass();


                int    ArraySize = 0;
                Object MassList, EmptyRef;
                Object Labels, Values;
                double temp      = 0.0;
                int    Progress  = 0;
                int    MSCount   = 0;
                int    MSMSCount = 0;

                if (RTApex)
                {
                    MSFileBox = new RawFileBox();
                    MSFileBox.LoadIndex(InFileName);
                    RawFileBox.RepProgress = RepProgress;
                    RawFile = MSFileBox.RawFile;
                }
                else
                {
                    RawFile = new MSFileReader_XRawfile();
                    RawFile.Open(InFileName);
                    RawFile.SetCurrentController(0, 1);
                }
                Spectra = 0;
                RawFile.GetNumSpectra(ref Spectra);
                if (Spectra == 0)
                {
                    throw new Exception("Cannot get spectra from the file " + InFileName + ", File is invalid, broken or empty");
                }
                MGF = new MGFFile();
                for (int j = 1; j <= Spectra; j++)
                {
                    Filter = null;
                    RawFile.GetFilterForScanNum(j, ref Filter);
                    if (Filter.IndexOf("ms2") == -1)
                    {
                        MSCount++;
                        if (!CheckSpectra)
                        {
                            continue;
                        }
                        else
                        {
                            ArraySize = 0;
                            MassList  = null;
                            EmptyRef  = null;
                            temp      = 0.0;
                            try {
                                RawFile.GetMassListFromScanNum(ref j, null, 0, 0, 0, 0,
                                                               ref temp, ref MassList, ref EmptyRef, ref ArraySize);
                                continue;
                            }catch {
                                Exception e = new Exception(string.Format("Scan #{0} cannot be loaded, probably RAW file is corrupted!", j));
                                throw e;
                            }
                        }
                    }
                    MSMSCount++;
                    ms = new MGFSpectrum();
                    //определяем родительскую массу, заряд и RT
                    Labels    = null;
                    Values    = null;
                    ArraySize = 0;
                    LocalCount++;
                    RawFile.GetTrailerExtraForScanNum(j, ref Labels, ref Values, ref ArraySize);
                    for (int k = 0; k < ArraySize; k++)
                    {
                        if ((Labels as Array).GetValue(k).ToString().Contains("Mono"))
                        {
                            ms.mz = Convert.ToDouble((Values as Array).GetValue(k).ToString());
                        }
                        if ((Labels as Array).GetValue(k).ToString().Contains("Charge State"))
                        {
                            ms.Charge = Convert.ToInt32((Values as Array).GetValue(k).ToString());
                        }
                    }
                    //Если не нашли в labels - берем из фильтра
                    if (ms.mz == 0.0)
                    {
                        string part = Filter.Substring(0, Filter.IndexOf('@'));
                        ms.mz = Convert.ToDouble(part.Substring(part.LastIndexOf(' ')));
                    }
                    Labels    = null;
                    Values    = null;
                    ArraySize = 0;
                    double RT = 0;
                    RawFile.GetStatusLogForScanNum(j, ref RT, ref Labels, ref Values, ref ArraySize);

                    RawFile.RTFromScanNum(j, ref ms.RT);
                    ms.ScanNumber = j;
                    //Фильтры
                    if (ms.Charge < MinCharge)
                    {
                        continue;
                    }
                    if (ms.Charge > MaxCharge)
                    {
                        continue;
                    }
                    if (ms.RT < MinRT)
                    {
                        continue;
                    }
                    if (MaxRT != 0.0 && ms.RT > MaxRT)
                    {
                        continue;
                    }
                    if (ms.mz < MinMZ)
                    {
                        continue;
                    }
                    if (MaxMZ != 0.0 && ms.mz > MaxMZ)
                    {
                        continue;
                    }
                    //забираем сам спектр
                    MassList  = null;
                    EmptyRef  = null;
                    ArraySize = 0;
                    temp      = 0.0;
                    try{
                        if (Filter.IndexOf("FTMS") != -1)
                        {
                            //извлекаем FTMS данные
                            (RawFile as IXRawfile2).GetLabelData(ref MassList, ref EmptyRef, ref j);
                            ArraySize = (MassList as Array).GetLength(1);
                            for (int k = 0; k < ArraySize; k++)
                            {
                                ch           = new Childs();
                                ch.Mass      = (double)(MassList as Array).GetValue(0, k);
                                ch.Intensity = (double)(MassList as Array).GetValue(1, k);
                                ms.Data.Add(ch);
                            }
                        }
                        else
                        {
                            //извлекаем ITMS данные
                            RawFile.GetMassListFromScanNum(ref j, null, 0, 0, 0, 1, ref temp, ref MassList, ref EmptyRef, ref ArraySize);
                            ArraySize = (MassList as Array).GetLength(1);
                            for (int k = 0; k < ArraySize; k++)
                            {
                                ch           = new Childs();
                                ch.Mass      = (double)(MassList as Array).GetValue(0, k);
                                ch.Intensity = (double)(MassList as Array).GetValue(1, k);
                                ms.Data.Add(ch);
                            }
                        }
                    }catch {
                        Exception e = new Exception(string.Format("Scan #{0} cannot be loaded, probably RAW file is corrupted!", j));
                        throw e;
                    }
                    if (RTApex)
                    {
                        ms.RTApex = CheckRTApex(ms.RT, ms.mz);
                    }

                    if (MarkInstrtument)
                    {
                        if (Filter.Contains("ecd") || Filter.Contains("etd"))
                        {
                            ms.Instrument = "ETD-TRAP";
                        }
                        else
                        {
                            ms.Instrument = "ESI-FTICR";
                        }
                    }
                    //очистить ETD если был такой запрос
                    if (CleanETD)
                    {
                        if (Filter.Contains("ecd") || Filter.Contains("etd"))
                        {
                            FilterETD(ref ms, Filter.Contains("FTMS"));
                        }
                    }
                    //сбросить лишние сигналы
                    if (MaxPeakNumber > 0 && ms.Data.Count > MaxPeakNumber)
                    {
                        ms.Data.Sort(ci);
                        ms.Data.RemoveRange(0, ms.Data.Count - MaxPeakNumber);
                        ms.Data.Sort(cm);
                    }
                    //сформировать TITLE
                    if (RTApex)
                    {
                        ms.Title = String.Format("Elution from: {0:f4} to {0:f4} RT Apex: {1:f2} FinneganScanNumber: {2}",
                                                 ms.RT, ms.RTApex, ms.ScanNumber);
                    }
                    else
                    {
                        ms.Title = String.Format("Elution from: {0:f4} to {0:f4} FinneganScanNumber: {1}",
                                                 ms.RT, ms.ScanNumber);
                    }
                    MGF.Spectra.Add(ms);
                    // рапортовать прогресс
                    GC.Collect(2);

                    if ((int)(((double)j / (double)Spectra) * 100.0) > Progress)
                    {
                        Progress = (int)(((double)j / (double)Spectra) * 100.0);
                        Console.WriteLine("{0}%... {1} {2}", Progress, MSCount, MSMSCount);
                    }

                    //backgroundWorker1.ReportProgress((int)(((double)LocalCount/(double)SpCount)*100.0));
                }
                MGF.MGFComments.Add(String.Format("Created by RawToMGF 2.1.3; Spectra obtained from {0}", InFileName));
                MGF.MGFComments.Add(String.Format("Filters: Parent m/z from {0} Th to {1} Th;", MinMZ, MaxMZ));
                MGF.MGFComments.Add(String.Format("         RT from {0} min. to {1} min.;", MinRT, MaxRT));
                MGF.MGFComments.Add(String.Format("         Charge state of parent ions minimum {0}, maximum {1};", MinCharge, MaxCharge));
                MGF.MGFComments.Add(String.Format("         Max number of peaks in MS/MS spectra - {0}", MaxPeakNumber));
                if (CleanETD)
                {
                    MGF.MGFComments.Add("         ETD spectra cleaned from precursors and neutral losses");
                }
                MGF.MGFWrite(OutFileName, true);
            }catch (Exception e) {
                Console.Write("Error:");
                Console.Write(e.Message);
                Console.WriteLine("STACKINFO:" + e.StackTrace);
                //Console.ReadKey();
                return(1);
            }
            Console.WriteLine("Completed");
            Console.ReadLine();
            return(0);
        }
예제 #26
0
 public static void Contruct(string typeName, RawFile reader)
 {
     var t = Type.GetType(typeName);
 }
예제 #27
0
 public void addFile(int index, RawFile toAdd)
 {
     throw new NotImplementedException();
 }
예제 #28
0
        /// <summary>
        /// Add imports to the PE file.
        /// </summary>
        /// <param name="additionalImports">List with additional imports.</param>
        public void AddImports(List <AdditionalImport> additionalImports)
        {
            if (ImageNtHeaders is null || ImageSectionHeaders is null || _dataDirectoryParsers is null)
            {
                throw new Exception("NT Headers, Section Headers and Data Directory must not be null.");
            }

            const int sizeOfImpDesc   = 0x14;
            var       sizeOfThunkData = Is32Bit ? 4 : 8;
            var       numAddImpDescs  = additionalImports.Count;
            var       importRva       = ImageNtHeaders.OptionalHeader.DataDirectory[(int)DataDirectoryType.Import].VirtualAddress;
            var       importSize      = ImageNtHeaders.OptionalHeader.DataDirectory[(int)DataDirectoryType.Import].Size;

            ImageSectionHeader GetImportSection()
            => ImageSectionHeaders.First(sh => sh.VirtualAddress + sh.VirtualSize >= importRva);

            int EstimateAdditionalNeededSpace()
            => additionalImports.Select(ai => ai.Functions).Count() * 64;

            var impSection             = GetImportSection();
            var newUnalignedRawSecSize = EstimateAdditionalNeededSpace();

            // First copy the current import descriptor array to the start of the new section to have enough space to
            // add additional import descriptors.
            AddSection(".addImp", (int)(impSection !.SizeOfRawData + newUnalignedRawSecSize), (ScnCharacteristicsType)0xC0000000);
            var newImpSec       = ImageSectionHeaders.First(sh => sh.Name == ".addImp");
            var oldImpDescBytes = RawFile.AsSpan(importRva.RvaToOffset(ImageSectionHeaders), importSize);

            RawFile.WriteBytes(newImpSec.PointerToRawData, oldImpDescBytes);

            // Set the import data directory to the new import section and adjust the size
            ImageNtHeaders.OptionalHeader.DataDirectory[(int)DataDirectoryType.Import].VirtualAddress = newImpSec.VirtualAddress;
            ImageNtHeaders.OptionalHeader.DataDirectory[(int)DataDirectoryType.Import].Size           = (uint)(importSize + (sizeOfImpDesc * numAddImpDescs));
            var newImportRva  = ImageNtHeaders.OptionalHeader.DataDirectory[(int)DataDirectoryType.Import].VirtualAddress;
            var newImportSize = ImageNtHeaders.OptionalHeader.DataDirectory[(int)DataDirectoryType.Import].Size;

            var paAdditionalSpace = newImpSec.PointerToRawData + newImportSize;

            // Update import descriptors and imported functions to reflect the new
            // position in the new section.
            _dataDirectoryParsers.ReparseImportDescriptors(ImageSectionHeaders);
            _dataDirectoryParsers.ReparseImportedFunctions();

            uint AddModName(ref uint offset, string module)
            {
                var tmp   = Encoding.ASCII.GetBytes(module);
                var mName = new byte[tmp.Length + 1];

                Array.Copy(tmp, mName, tmp.Length);

                var paName = offset;

                RawFile.WriteBytes(offset, mName);

                offset = (uint)(offset + mName.Length);
                return(paName);
            }

            List <uint> AddImpByNames(ref uint offset, List <string> funcs)
            {
                var adrList = new List <uint>();

                foreach (var f in funcs)
                {
                    var ibn = new ImageImportByName(RawFile, offset)
                    {
                        Hint = 0,
                        Name = f
                    };

                    adrList.Add(offset);

                    offset += (uint)ibn.Name.Length + 2;
                }

                // Add zero DWORD to end array
                RawFile.WriteUInt(offset + 1, 0);
                offset += 5;

                return(adrList);
            }

            uint AddThunkDatas(ref uint offset, List <uint> adrList)
            {
                var paThunkStart = offset;

                foreach (var adr in adrList)
                {
                    _ = new ImageThunkData(RawFile, offset, Is64Bit)
                    {
                        AddressOfData = adr.OffsetToRva(ImageSectionHeaders !)
                    };

                    offset += (uint)sizeOfThunkData;
                }

                // End array with empty thunk data
                _ = new ImageThunkData(RawFile, offset, Is64Bit)
                {
                    AddressOfData = 0
                };

                offset += (uint)sizeOfThunkData;

                return(paThunkStart);
            }

            void AddImportWithNewImpDesc(ref uint tmpOffset, ref long paIdesc, AdditionalImport ai)
            {
                var paName    = AddModName(ref tmpOffset, ai.Module);
                var funcAdrs  = AddImpByNames(ref tmpOffset, ai.Functions);
                var thunkAdrs = AddThunkDatas(ref tmpOffset, funcAdrs);

                _ = new ImageImportDescriptor(RawFile, paIdesc)
                {
                    Name = paName.OffsetToRva(ImageSectionHeaders),
                    OriginalFirstThunk = 0,
                    FirstThunk         = thunkAdrs.OffsetToRva(ImageSectionHeaders),
                    ForwarderChain     = 0,
                    TimeDateStamp      = 0
                };
                paIdesc += (uint)sizeOfImpDesc;
            }

            var paIdesc   = newImportRva.RvaToOffset(ImageSectionHeaders) + ImageImportDescriptors !.Length * sizeOfImpDesc;
            var tmpOffset = paAdditionalSpace;

            // Add new imports
            foreach (var ai in additionalImports)
            {
                AddImportWithNewImpDesc(ref tmpOffset, ref paIdesc, ai);
            }

            // End with zero filled idesc
            _ = new ImageImportDescriptor(RawFile, paIdesc)
            {
                Name = 0,
                OriginalFirstThunk = 0,
                FirstThunk         = 0,
                ForwarderChain     = 0,
                TimeDateStamp      = 0
            };


            // Reparse imports
            _dataDirectoryParsers.ReparseImportDescriptors(ImageSectionHeaders);
            _dataDirectoryParsers.ReparseImportedFunctions();
        }
    }
예제 #29
0
        public RawFile SaveAsZipArchive(string fileName)
        {
            var lstCleanUp = new List <string>(_attachments.Count + 1);

            //If the filename does not include the zip extension add it
            if (!fileName.EndsWith(".zip"))
            {
                fileName += ".zip";
            }

            var obj = new RawFile
            {
                FileName = fileName
            };

            //Get a temporary path to work with
            var path = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            //Get a separate directory so that the zip file can be created to avoid IO access exceptions
            var pathZip = path + "zip";

            //Create base working directory
            Directory.CreateDirectory(path);

            //Create directory for zip file
            Directory.CreateDirectory(pathZip);

            //Save to / Save as zip file
            var zipFilePath = Path.Combine(pathZip, fileName);

            lstCleanUp.Add(zipFilePath);

            //For each attachment
            foreach (var a in _attachments)
            {
                //Decode the base 64 string into a byte array
                var bytes = Base64Decode(a.AttachmentBase64);

                //Save as
                var filePath = Path.Combine(path, a.Name);

                //Write byte array to temp path with original name
                File.WriteAllBytes(filePath, bytes);

                lstCleanUp.Add(filePath);
            }

            //Archive everything into a zip file in the temp path
            ZipFile.CreateFromDirectory(
                path,
                zipFilePath,
                CompressionLevel.Optimal,
                false);

            //Create a byte array from the zip file
            obj.Contents = File.ReadAllBytes(zipFilePath);

            //Delete all files created
            lstCleanUp.ForEach(File.Delete);

            //Delete the working directories created
            Directory.Delete(path);

            Directory.Delete(pathZip);

            return(obj);
        }
예제 #30
0
        public void replaceFile(string filename, RawFile toReplace)
        {
            int index = afsList.FindIndex(file => file.fileName == filename);

            replaceFile(index, new MemoryStream(toReplace.fileContents));
        }