Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                ShowMessage(Resources.Program_Main_Missing_report_file_path_argument_from_Skyline__try_re_installing_TFExport_);
                return;
            }
            var reportFilePath = args[0];

            Dictionary <string, List <TransitionRecord> > peptideTransitions;

            try
            {
                peptideTransitions = GetTransitions(reportFilePath);
            }
            catch (Exception e)
            {
                ShowMessage(string.Format(Resources.Program_Main_Failure_reading_the_report_file__0__, reportFilePath) +
                            Environment.NewLine + e.Message);
                return;
            }

            var filePaths = new List <string>(peptideTransitions
                                              .SelectMany(trans => trans.Value).OrderBy(t => t.FileName)
                                              .Select(t => t.FileName).Distinct());

            using (var tf = new TFExportDlg(filePaths))
            {
                if (tf.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                _isAutoFill           = tf.IsAutoFill();           // if false don't auto fill anything
                _isAutoFillAll        = tf.IsAutoFillAll();        // auto fill all as 'TargetPeak'
                _isAutoFillConfirming = tf.IsAutoFillConfirming(); // auto fill one as 'TargetPeak' and rest as 'Confirming' or 'Fragment'
                _internalStandardType = (InternalStandardType)tf.GetStandardType();
                _rtFile        = tf.GetSelectedRTFile();
                _intensityFile = tf.GetSelectedIntensityFile();
                _rtWindow      = tf.GetRtWindow();
                if (_rtFile == null)
                {
                    _useAvgRt = true;
                    _rtFile   = filePaths[0];
                }
                if (_intensityFile == null)
                {
                    _useAvgIntensity = true;
                }
                // Strip peptideTransitions to either file or first one
                var updatedPeptideTransitions = new Dictionary <string, List <TransitionRecord> >();
                foreach (var k in peptideTransitions.Keys)
                {
                    IList <TransitionRecord> values = peptideTransitions[k];
                    var transitionRecords           = new List <TransitionRecord>();
                    foreach (var v in values)
                    {
                        if (v.FileName.Equals(_rtFile))
                        {
                            transitionRecords.Add(v);
                        }
                    }

                    var fileAreas = new Dictionary <string, List <double> >();
                    AddTransitionFileAreas(_useAvgIntensity
                        ? values
                        : values.Where(tr => Equals(tr.FileName, _intensityFile)), fileAreas);
                    // even if area from a single file is used will still funnel through this averaging code
                    // but instead will just have an int array with one value so the average will be that value(value in selected file)
                    var avgAreas = new Dictionary <string, double>();
                    foreach (var fileArea in fileAreas)
                    {
                        var avg = fileArea.Value.Average();
                        avgAreas.Add(fileArea.Key, avg);
                    }
                    foreach (var avgArea in avgAreas)
                    {
                        var keyInfo     = GetKeyParts(avgArea.Key);
                        var seq         = keyInfo[0];
                        var precursorMz = keyInfo[1];
                        var fragmentIon = keyInfo[2];
                        var label       = keyInfo[3];
                        var prodMz      = keyInfo[4];
                        foreach (var transitionRecord in transitionRecords)
                        {
                            if (transitionRecord.ModifiedSequence.Equals(seq) &&
                                transitionRecord.PrecursorMz.ToString(CultureInfo.InvariantCulture).Equals(precursorMz) &&
                                transitionRecord.FragmentIon.Equals(fragmentIon) &&
                                transitionRecord.IsotopeLabelType.Equals(label) &&
                                transitionRecord.ProductMz.ToString(CultureInfo.InvariantCulture).Equals(prodMz))
                            {
                                transitionRecord.Area = avgArea.Value;
                            }
                        }
                    }
                    updatedPeptideTransitions.Add(k, transitionRecords); // add the transitions for this peptide
                    var standards    = new List <TransitionRecord>();
                    var nonStandards = new List <TransitionRecord>();
                    foreach (var v in updatedPeptideTransitions[k])
                    {
                        if (v.IsotopeLabelType.Equals(_internalStandardType.ToString()))
                        {
                            standards.Add(v);
                        }
                        else
                        {
                            nonStandards.Add(v);
                        }
                    }
                    // make non-standards sort same as standards by giving them the same area
                    // area will not be seen by user but is only used to sort in this case
                    foreach (var v in nonStandards)
                    {
                        if (!v.IsotopeLabelType.Equals(_internalStandardType.ToString()))
                        {
                            foreach (var transitionRecord in standards)
                            {
                                if (transitionRecord.PeptideModifiedSequence.Equals(v.PeptideModifiedSequence) &&
                                    transitionRecord.ProductCharge == v.ProductCharge &&
                                    transitionRecord.PrecursorCharge == v.PrecursorCharge &&
                                    transitionRecord.FragmentIon.Equals(v.FragmentIon))
                                {
                                    v.Area = transitionRecord.Area;
                                }
                            }
                        }
                    }
                    var all = standards.Concat(nonStandards).ToList();
                    // Sort final list of transitions by peptide, then label type, then peak area
                    all.Sort((x, y) =>
                    {
                        if (x.IsotopeLabelType != y.IsotopeLabelType)
                        {
                            if (_internalStandardType.ToString().Equals(x.IsotopeLabelType))
                            {
                                return(-1);
                            }
                            if (_internalStandardType.ToString().Equals(y.IsotopeLabelType))
                            {
                                return(1);
                            }
                        }
                        return(y.Area.CompareTo(x.Area));
                    });
                    updatedPeptideTransitions[k] = all;
                }
                // EXPORT
                using (var saveFileDialog = new SaveFileDialog
                {
                    FileName = @"TFExport.csv", // Not L10N
                    Filter = Resources.Program_Main_CSV__Comma_delimited_____csv____csv
                })
                {
                    if (saveFileDialog.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    var saveFileLocation = saveFileDialog.FileName;
                    try
                    {
                        WriteToCsv(saveFileLocation, updatedPeptideTransitions);
                        ShowMessage(string.Format(Resources.Program_Main_File_saved_to__0__, saveFileLocation));
                    }
                    catch (Exception e)
                    {
                        ShowMessage(string.Format(Resources.Program_Main_Error___0_, e.Message));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("Error opening report file from Skyline, try re-installing TFExport."); // Not L10N
                return;
            }
            var reportFilePath     = args[0];
            var peptideTransitions = GetTransitions(reportFilePath);
            var filePaths          = new List <string>();

            foreach (var transitions in peptideTransitions.Values)
            {
                foreach (var t in transitions)
                {
                    if (!filePaths.Contains(t.FileName))
                    {
                        filePaths.Add(t.FileName);
                    }
                }
            }
            filePaths.Sort();
            using (var tf = new TFExportDlg(filePaths))
            {
                if (tf.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                _isAutoFill           = tf.IsAutoFill();           // if false don't auto fill anything
                _isAutoFillAll        = tf.IsAutoFillAll();        // auto fill all as 'TargetPeak'
                _isAutoFillConfirming = tf.IsAutoFillConfirming(); // auto fill one as 'TargetPeak' and rest as 'Confirming' or 'Fragment'
                _internalStandardType = (InternalStandardType)tf.GetStandardType();
                _rtFile        = tf.GetSelectedRTFile();
                _intensityFile = tf.GetSelectedIntensityFile();
                _rtWindow      = tf.GetRtWindow();
                if (_rtFile == null)
                {
                    _useAvgRt = true;
                    _rtFile   = filePaths[0];
                }
                if (_intensityFile == null)
                {
                    _useAvgIntensity = true;
                }
                // Strip peptideTransitions to either file or first one
                var updatedPeptideTransitions = new Dictionary <string, List <TransitionRecord> >();
                foreach (var k in peptideTransitions.Keys)
                {
                    var values            = peptideTransitions[k];
                    var transitionRecords = new List <TransitionRecord>();
                    foreach (var v in values)
                    {
                        if (v.FileName.Equals(_rtFile))
                        {
                            transitionRecords.Add(v);
                        }
                    }

                    var fileAreas = new Dictionary <string, List <double> >();
                    if (_useAvgIntensity) // if average intensity then calculate values
                    {
                        foreach (var v in values)
                        {
                            var key = v.ModifiedSequence + "," + v.PrecursorMz + "," + v.FragmentIon + "," +
                                      v.IsotopeLabelType + "," + v.ProductMz;
                            if (!fileAreas.ContainsKey(key))
                            {
                                fileAreas.Add(key, new List <double>());
                            }
                            fileAreas[key].Add(v.Area);
                        }
                    } // if use intensity from specific file
                    else
                    {
                        foreach (var v in values)
                        {
                            if (!v.FileName.Equals(_intensityFile))
                            {
                                continue;
                            }
                            var key = v.ModifiedSequence + "," + v.PrecursorMz + "," + v.FragmentIon + "," +
                                      v.IsotopeLabelType + "," + v.ProductMz;
                            if (!fileAreas.ContainsKey(key))
                            {
                                fileAreas.Add(key, new List <double>());
                            }
                            fileAreas[key].Add(v.Area);
                        }
                    }
                    // even if area from a single file is used will still funnel through this averageing code
                    // but instead will just have an int array with one value so the average will be that value(value in selected file)
                    var avgAreas = new Dictionary <string, double>();
                    foreach (var fileArea in fileAreas)
                    {
                        var avg = fileArea.Value.Average();
                        avgAreas.Add(fileArea.Key, avg);
                    }
                    foreach (var avgArea in avgAreas)
                    {
                        var keyInfo     = avgArea.Key.Split(',');
                        var seq         = keyInfo[0];
                        var precursorMz = keyInfo[1];
                        var fragmentIon = keyInfo[2];
                        var label       = keyInfo[3];
                        var prodMz      = keyInfo[4];
                        foreach (var transitionRecord in transitionRecords)
                        {
                            if (transitionRecord.ModifiedSequence.Equals(seq) &&
                                transitionRecord.PrecursorMz.ToString(CultureInfo.InvariantCulture).Equals(precursorMz) &&
                                transitionRecord.FragmentIon.Equals(fragmentIon) &&
                                transitionRecord.IsotopeLabelType.Equals(label) &&
                                transitionRecord.ProductMz.ToString(CultureInfo.InvariantCulture).Equals(prodMz))
                            {
                                transitionRecord.Area = avgArea.Value;
                            }
                        }
                    }
                    updatedPeptideTransitions.Add(k, transitionRecords); // add the transitions for this peptide
                    var standards    = new List <TransitionRecord>();
                    var nonStandards = new List <TransitionRecord>();
                    foreach (var v in updatedPeptideTransitions[k])
                    {
                        if (v.IsotopeLabelType.Equals(_internalStandardType.ToString()))
                        {
                            standards.Add(v);
                        }
                        else
                        {
                            nonStandards.Add(v);
                        }
                    }
                    // make non-standards sort same as standards by giving them the same area
                    // area will not be seen by user but is only used to sort in this case
                    foreach (var v in nonStandards)
                    {
                        if (!v.IsotopeLabelType.Equals(_internalStandardType.ToString()))
                        {
                            foreach (var transitionRecord in standards)
                            {
                                if (transitionRecord.PeptideModifiedSequence.Equals(v.PeptideModifiedSequence) &&
                                    transitionRecord.ProductCharge == v.ProductCharge &&
                                    transitionRecord.PrecursorCharge == v.PrecursorCharge &&
                                    transitionRecord.FragmentIon.Equals(v.FragmentIon))
                                {
                                    v.Area = transitionRecord.Area;
                                }
                            }
                        }
                    }
                    var all = standards.Concat(nonStandards).ToList();
                    // Sort final list of transitions by peptide, then label type, then peak area
                    all.Sort((x, y) =>
                    {
                        if (x.IsotopeLabelType == y.IsotopeLabelType)
                        {
                            return(y.Area.CompareTo(x.Area));
                        }
                        if (_internalStandardType.ToString().Equals(x.IsotopeLabelType))
                        {
                            return(-1);
                        }
                        if (_internalStandardType.ToString().Equals(y.IsotopeLabelType))
                        {
                            return(1);
                        }
                        return(y.Area.CompareTo(x.Area));
                    });
                    updatedPeptideTransitions[k] = all;
                }
                // EXPORT
                using (var saveFileDialog = new SaveFileDialog
                {
                    FileName = "TFExport.csv",         // Not L10N
                    Filter = "csv files (*.csv)|*.csv" // Not L10N
                })
                {
                    if (saveFileDialog.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    var saveFileLocation = saveFileDialog.FileName;
                    var res = WriteToCsv(saveFileLocation, updatedPeptideTransitions);
                    if (string.IsNullOrEmpty(res))
                    {
                        Console.WriteLine("File saved to {0}.", saveFileLocation);
                    }
                    else // if not success
                    {
                        Console.WriteLine("Error: {0}", res);
                    }
                }
            }
        }