public XicData(XicData copy)
        {
            OutputMeta = new XicOutputMeta(copy.OutputMeta);

            Content = new List <XicUnit>();
            foreach (XicUnit unit in copy.Content)
            {
                Content.Add(new XicUnit(unit));
            }
        }
        public static void run(XicParameters parameters)
        {
            var jsonString       = File.ReadAllText(parameters.jsonFilePath, Encoding.UTF8);
            var validationErrors = JSONParser.ValidateJson(jsonString);

            if (!validationErrors.IsNullOrEmpty())
            {
                var validationMessage = new StringBuilder("JSON validation error(s):\n");
                foreach (var validationError in validationErrors)
                {
                    if (validationError.ToString().Contains("ExcludedSchemaValidates"))
                    {
                        validationMessage.Append(
                            "Use M/Z and tolerance, M/Z start and M/Z end or sequence and tolerance, not a combination (with optional RT start and/or end).\n");
                    }

                    validationMessage.Append(
                        $"element start line number: {validationError.LineNumber}\n{validationError.ToString()}");
                }

                throw new RawFileParserException(validationMessage.ToString());
            }

            var xicData = JSONParser.ParseJSON(jsonString);

            foreach (string rawFile in parameters.rawFileList)
            {
                var dataInstance = new XicData(xicData);
                XicReader.ReadXic(rawFile, parameters.base64, dataInstance);

                if (parameters.stdout)
                {
                    StdOutputXicData(dataInstance);
                }
                else
                {
                    // if outputDirectory has been defined, put output there.
                    string directory;
                    if (parameters.outputDirectory != null)
                    {
                        directory = parameters.outputDirectory;
                    }
                    // otherwise put output files into the same directory as the raw file input
                    else
                    {
                        directory = Path.GetDirectoryName(rawFile);
                    }

                    var outputFileName = Path.Combine(directory ?? throw new NoNullAllowedException(),
                                                      Path.GetFileNameWithoutExtension(rawFile) + ".json");

                    OutputXicData(dataInstance, outputFileName);
                }
            }
        }
Beispiel #3
0
        public static void ReadXic(string rawFilePath, bool base64, XicData xicData)
        {
            IRawDataPlus rawFile;

            using (rawFile = RawFileReaderFactory.ReadFile(rawFilePath))
            {
                if (!rawFile.IsOpen)
                {
                    throw new RawFileParserException("Unable to access the RAW file using the native Thermo library.");
                }

                // Check for any errors in the RAW file
                if (rawFile.IsError)
                {
                    throw new RawFileParserException(
                              $"Error opening ({rawFile.FileError}) - {rawFilePath}");
                }

                // Check if the RAW file is being acquired
                if (rawFile.InAcquisition)
                {
                    throw new RawFileParserException("RAW file still being acquired - " + rawFilePath);
                }

                // Get the number of instruments (controllers) present in the RAW file and set the
                // selected instrument to the MS instrument, first instance of it
                rawFile.SelectInstrument(Device.MS, 1);

                // Get the first and last scan from the RAW file
                var firstScanNumber = rawFile.RunHeaderEx.FirstSpectrum;
                var lastScanNumber  = rawFile.RunHeaderEx.LastSpectrum;

                // Get the start and end time from the RAW file
                var startTime = rawFile.RunHeaderEx.StartTime;
                var endTime   = rawFile.RunHeaderEx.EndTime;

                // Get the mass range from the RAW file
                var minMass = rawFile.RunHeaderEx.LowMass;
                var maxMass = rawFile.RunHeaderEx.HighMass;

                // Update global metadata
                xicData.OutputMeta.base64   = base64;
                xicData.OutputMeta.timeunit = "minutes";

                foreach (var xicUnit in xicData.Content)
                {
                    IChromatogramSettings settings = null;
                    if (!xicUnit.Meta.MzStart.HasValue && !xicUnit.Meta.MzEnd.HasValue)
                    {
                        settings = new ChromatogramTraceSettings()
                        {
                            Filter = xicUnit.Meta.Filter ?? "ms"
                        };
                    }

                    if (!xicUnit.Meta.MzStart.HasValue)
                    {
                        xicUnit.Meta.MzStart = minMass;
                    }

                    if (!xicUnit.Meta.MzEnd.HasValue)
                    {
                        xicUnit.Meta.MzEnd = maxMass;
                    }

                    if (settings == null)
                    {
                        settings = new ChromatogramTraceSettings(TraceType.MassRange)
                        {
                            Filter     = xicUnit.Meta.Filter ?? "ms",
                            MassRanges = new[]
                            {
                                new Range(xicUnit.Meta.MzStart.Value,
                                          xicUnit.Meta.MzEnd.Value)
                            }
                        };
                    }

                    List <int> rtFilteredScans = null;
                    if (!xicUnit.Meta.RtStart.HasValue && !xicUnit.Meta.RtEnd.HasValue)
                    {
                        rtFilteredScans = new List <int>();
                    }

                    if (!xicUnit.Meta.RtStart.HasValue)
                    {
                        xicUnit.Meta.RtStart = startTime;
                    }

                    if (!xicUnit.Meta.RtEnd.HasValue)
                    {
                        xicUnit.Meta.RtEnd = endTime;
                    }

                    IChromatogramData data = null;
                    if (rtFilteredScans == null)
                    {
                        rtFilteredScans = rawFile.GetFilteredScansListByTimeRange(MsFilter,
                                                                                  xicUnit.Meta.RtStart.Value, xicUnit.Meta.RtEnd.Value);

                        if (rtFilteredScans.Count != 0)
                        {
                            data = GetChromatogramData(rawFile, settings, rtFilteredScans[0],
                                                       rtFilteredScans[rtFilteredScans.Count - 1]);
                            if (data != null && data.PositionsArray.Length == 1 && data.PositionsArray[0].Length == 1 &&
                                (Math.Abs(data.PositionsArray[0][0] - startTime) < 0.001 ||
                                 Math.Abs(data.PositionsArray[0][0] - endTime) < 0.001))
                            {
                                Log.Warn(
                                    $"Only the minimum or maximum retention time was returned. This is an indication that the provided retention time range [{xicUnit.Meta.RtStart}-{xicUnit.Meta.RtEnd}] lies outside the max. window [{startTime}-{endTime}]");
                            }
                        }
                        else
                        {
                            Log.Warn(
                                $"No scans found in retention time range [{xicUnit.Meta.RtStart}-{xicUnit.Meta.RtEnd}]. This is an indication that the provided retention time window lies outside the max. window [{startTime}-{endTime}]");
                        }
                    }
                    else
                    {
                        data = GetChromatogramData(rawFile, settings, firstScanNumber, lastScanNumber);
                    }

                    if (data != null)
                    {
                        var chromatogramTrace = ChromatogramSignal.FromChromatogramData(data);
                        if (chromatogramTrace[0].Scans.Count != 0)
                        {
                            if (!base64)
                            {
                                xicUnit.RetentionTimes = chromatogramTrace[0].Times;
                                xicUnit.Intensities    = chromatogramTrace[0].Intensities;
                            }
                            else
                            {
                                xicUnit.RetentionTimes = GetBase64String(chromatogramTrace[0].Times);
                                xicUnit.Intensities    = GetBase64String(chromatogramTrace[0].Intensities);
                            }
                        }
                    }
                }
            }
        }
        public static XicData ParseJSON(string jsonString)
        {
            List <JSONInputUnit> jsonIn;
            XicData data = new XicData();

            jsonIn = JsonConvert.DeserializeObject <List <JSONInputUnit> >(jsonString);
            foreach (JSONInputUnit xic in jsonIn)
            {
                XicUnit xicUnit = null;
                if (xic.HasSequence())
                {
                    Peptide p = new Peptide(xic.Sequence);
                    xic.Mz = p.GetMz(xic.Charge);
                }

                if (xic.HasMz())
                {
                    double delta;
                    switch (xic.ToleranceUnit.ToLower())
                    {
                    case "ppm":
                        delta = xic.Mz.Value * xic.Tolerance.Value * 1e-6;
                        break;

                    case "amu":
                        delta = xic.Tolerance.Value;
                        break;

                    case "mmu":
                        delta = xic.Tolerance.Value * 1e-3;
                        break;

                    case "da":
                        delta = xic.Tolerance.Value;
                        break;

                    case "":
                        delta = xic.Mz.Value * xic.Tolerance.Value * 1e-6;
                        break;

                    default:
                        delta = 10;
                        break;
                    }

                    xicUnit = new XicUnit(xic.Mz.Value - delta, xic.Mz.Value + delta, xic.RtStart,
                                          xic.RtEnd, xic.Filter);
                }
                else if (xic.HasMzRange())
                {
                    xicUnit = new XicUnit(xic.MzStart.Value, xic.MzEnd.Value, xic.RtStart, xic.RtEnd, xic.Filter);
                }

                if (xicUnit == null || !xicUnit.HasValidRanges())
                {
                    throw new RawFileParserException(
                              $"Invalid M/Z and/or retention time range:\n{JsonConvert.SerializeObject(xic, Formatting.Indented)}");
                }

                data.Content.Add(xicUnit);
            }

            return(data);
        }
        private static void OutputXicData(XicData outputData, string outputFileName)
        {
            var outputString = JsonConvert.SerializeObject(outputData, Formatting.Indented);

            File.WriteAllText(outputFileName, outputString);
        }
        private static void StdOutputXicData(XicData outputData)
        {
            var outputString = JsonConvert.SerializeObject(outputData, Formatting.Indented);

            Console.WriteLine(outputString);
        }