Ejemplo n.º 1
0
        private static string GetLocalizedString(LocalizedHit hit)
        {
            StringBuilder sb = new StringBuilder();

            IMass[] mods  = hit.LocalizedIsoform.GetModifications();
            bool    first = false;

            for (int resNumber = 1; resNumber < mods.Length - 1; resNumber++)
            {
                IMass mod = mods[resNumber];
                if (mod == null)
                {
                    continue;
                }

                ModificationCollection modCollection = mod as ModificationCollection;
                if (modCollection != null)
                {
                    IMass trueMod = mod;
                    bool  passes  = false;
                    foreach (IMass mod2 in modCollection)
                    {
                        if (Lotor.QuantifiedModifications.Contains(mod2))
                        {
                            trueMod = mod2;
                            passes  = true;
                            break;
                        }
                    }
                    if (!passes)
                    {
                        continue;
                    }
                    mod = trueMod;
                }
                else
                {
                    if (Lotor.FixedModifications.Contains(mod) || !Lotor.QuantifiedModifications.Contains(mod))
                    {
                        continue;
                    }
                }

                int  fullResidueNumber = hit.PSM.StartResidue + resNumber - 1;
                char res = hit.LocalizedIsoform.Sequence[resNumber - 1];
                if (first)
                {
                    sb.Append(" & ");
                }
                sb.Append(res);
                sb.Append(fullResidueNumber);
                sb.Append("[" + mod + "]");
                first = true;
            }
            return(sb.ToString());
        }
Ejemplo n.º 2
0
 public void AddHit(LocalizedHit hit)
 {
     string localizedString = GetLocalizedString(hit);
     List<LocalizedHit> hits = null;
     if (Hits.TryGetValue(localizedString, out hits))
     {
         hits.Add(hit);
     }
     else
     {
         hits = new List<LocalizedHit>();
         hits.Add(hit);
         Hits.Add(localizedString, hits);
     }
 }
Ejemplo n.º 3
0
        public void AddHit(LocalizedHit hit)
        {
            string localizedString   = GetLocalizedString(hit);
            List <LocalizedHit> hits = null;

            if (Hits.TryGetValue(localizedString, out hits))
            {
                hits.Add(hit);
            }
            else
            {
                hits = new List <LocalizedHit>();
                hits.Add(hit);
                Hits.Add(localizedString, hits);
            }
        }
Ejemplo n.º 4
0
        private static string GetLocalizedString(LocalizedHit hit)
        {
            StringBuilder sb = new StringBuilder();
            IMass[] mods = hit.LocalizedIsoform.GetModifications();
            bool first = false;
            for(int resNumber = 1; resNumber < mods.Length-1; resNumber++)
            {
                IMass mod = mods[resNumber];
                if (mod == null)
                    continue;

                ModificationCollection modCollection = mod as ModificationCollection;
                if (modCollection != null)
                {
                    IMass trueMod = mod;
                    bool passes = false;
                    foreach (IMass mod2 in modCollection)
                    {

                        if (Lotor.QuantifiedModifications.Contains(mod2))
                        {
                            trueMod = mod2;
                            passes = true;
                            break;
                        }
                    }
                    if (!passes)
                        continue;
                    mod = trueMod;
                }
                else
                {
                    if (Lotor.FixedModifications.Contains(mod) || !Lotor.QuantifiedModifications.Contains(mod))
                        continue;
                }

                int fullResidueNumber = hit.PSM.StartResidue + resNumber - 1;
                char res = hit.LocalizedIsoform.Sequence[resNumber-1];
                if (first)
                {
                    sb.Append(" & ");
                }
                sb.Append(res);
                sb.Append(fullResidueNumber);
                sb.Append("[" + mod + "]");
                first = true;
            }
            return sb.ToString();
        }
Ejemplo n.º 5
0
        private List<LocalizedHit> CalculateBestIsoforms(List<PSM> psms, FragmentTypes fragType, Tolerance prod_tolerance, double productThreshold, bool phosphoNeutralLosses)
        {
            Log("Localizing Best Isoforms...");
            int totalisofromscount = 0;
            int count = 0;
            int psm_count = 0;
            int localized_psm = 0;
            List<LocalizedHit> hits = new List<LocalizedHit>();
            foreach (PSM psm in psms)
            {
                psm_count++;
                count++;

                // Generate all the isoforms for the PSM
                int isoformCount = psm.GenerateIsoforms(_ignoreCTerminal);

                if(isoformCount == 0)
                    continue;

                totalisofromscount += isoformCount;

                // Calculate the probability of success for random matches
                //double pvalue = GetPValue(psm, prod_tolerance, productThreshold);

                // Match all the isoforms to the spectrum and log the results
                psm.MatchIsofroms(fragType, prod_tolerance, productThreshold, phosphoNeutralLosses, 1);

                // Perform the localization for all combinations of isoforms
                //double[,] res = psm.Calc(pvalue);

                Tuple<int, int, double> scores = LocalizedIsoformSimple(psm);

                // Check if the localization is above some threshold
                //Tuple<int, int, double> scores = LocalizedIsoform(res);
                int bestIsoform = scores.Item1;
                int secondBestIsoform = scores.Item2;
                double ascore = scores.Item3;
                //int numSDFs = psm.NumSiteDeterminingFragments[bestIsoform, secondBestIsoform];

                List<PeptideIsoform> isoforms = psm.PeptideIsoforms.ToList();
                //LocalizedHit hit = new LocalizedHit(psm, isoforms[bestIsoform], isoforms[secondBestIsoform], numSDFs,
                //    psm.BestSiteDeterminingFragments[bestIsoform, secondBestIsoform],
                //    psm.BestSiteDeterminingFragments[secondBestIsoform, bestIsoform], pvalue, ascore);

                LocalizedHit hit = new LocalizedHit(psm, isoforms[bestIsoform], isoforms[secondBestIsoform], 0,
                   0,
                   0, 0, ascore);
                hits.Add(hit);
                if (hit.IsLocalized)
                {
                    localized_psm++;
                }

                psm.PeptideIsoforms.Clear();

                // Progress Bar Stuff
                if (count <= 50)
                    continue;
                count = 0;
                ProgressUpdate((double)psm_count / psms.Count);
            }
            ProgressUpdate(1.0);
            Log(string.Format("Total Number of Possible Isoforms Considered: {0:N0}",totalisofromscount));
            Log(string.Format("Total Number of PSMs Considered: {0:N0}", psm_count));
            Log(string.Format("Total Number of PSMs Localized: {0:N0} ({1:00.00}%)", localized_psm, localized_psm * 100 / psm_count));
            ProgressUpdate(0);
            return hits;
        }
Ejemplo n.º 6
0
        private List <LocalizedHit> CalculateBestIsoforms(List <PSM> psms, FragmentTypes fragType, Tolerance prod_tolerance, double productThreshold, bool phosphoNeutralLosses)
        {
            Log("Localizing Best Isoforms...");
            int totalisofromscount = 0;
            int count                = 0;
            int psm_count            = 0;
            int localized_psm        = 0;
            List <LocalizedHit> hits = new List <LocalizedHit>();

            foreach (PSM psm in psms)
            {
                psm_count++;
                count++;

                // Generate all the isoforms for the PSM
                int isoformCount = psm.GenerateIsoforms(_ignoreCTerminal);

                if (isoformCount == 0)
                {
                    continue;
                }

                totalisofromscount += isoformCount;

                // Calculate the probability of success for random matches
                //double pvalue = GetPValue(psm, prod_tolerance, productThreshold);

                // Match all the isoforms to the spectrum and log the results
                psm.MatchIsofroms(fragType, prod_tolerance, productThreshold, phosphoNeutralLosses, 1);

                // Perform the localization for all combinations of isoforms
                //double[,] res = psm.Calc(pvalue);

                Tuple <int, int, double> scores = LocalizedIsoformSimple(psm);

                // Check if the localization is above some threshold
                //Tuple<int, int, double> scores = LocalizedIsoform(res);
                int    bestIsoform       = scores.Item1;
                int    secondBestIsoform = scores.Item2;
                double ascore            = scores.Item3;
                //int numSDFs = psm.NumSiteDeterminingFragments[bestIsoform, secondBestIsoform];

                List <PeptideIsoform> isoforms = psm.PeptideIsoforms.ToList();
                //LocalizedHit hit = new LocalizedHit(psm, isoforms[bestIsoform], isoforms[secondBestIsoform], numSDFs,
                //    psm.BestSiteDeterminingFragments[bestIsoform, secondBestIsoform],
                //    psm.BestSiteDeterminingFragments[secondBestIsoform, bestIsoform], pvalue, ascore);

                LocalizedHit hit = new LocalizedHit(psm, isoforms[bestIsoform], isoforms[secondBestIsoform], 0,
                                                    0,
                                                    0, 0, ascore);
                hits.Add(hit);
                if (hit.IsLocalized)
                {
                    localized_psm++;
                }

                psm.PeptideIsoforms.Clear();

                // Progress Bar Stuff
                if (count <= 50)
                {
                    continue;
                }
                count = 0;
                ProgressUpdate((double)psm_count / psms.Count);
            }
            ProgressUpdate(1.0);
            Log(string.Format("Total Number of Possible Isoforms Considered: {0:N0}", totalisofromscount));
            Log(string.Format("Total Number of PSMs Considered: {0:N0}", psm_count));
            Log(string.Format("Total Number of PSMs Localized: {0:N0} ({1:00.00}%)", localized_psm, localized_psm * 100 / psm_count));
            ProgressUpdate(0);
            return(hits);
        }
Ejemplo n.º 7
0
        private List <Protein> CompileResults(List <LocalizedHit> hits, string csvFile, string outputDirectory, bool breakProteinsApart = false)
        {
            Dictionary <string, LocalizedHit> hitsdict = new Dictionary <string, LocalizedHit>();

            // Group all the localized Hits into proteins
            Dictionary <string, Protein> proteins = new Dictionary <string, Protein>();

            foreach (LocalizedHit hit in hits)
            {
                hitsdict.Add(hit.PSM.Filename, hit);


                string defline = hit.PSM.Defline;

                if (breakProteinsApart)
                {
                    string[] groups = hit.PSM.ProteinGroup.Split('|');
                    foreach (string group in groups)
                    {
                        Protein prot;
                        if (!proteins.TryGetValue(group, out prot))
                        {
                            prot = new Protein(group, defline);
                            proteins.Add(group, prot);
                        }
                        prot.AddHit(hit);
                    }
                }
                else
                {
                    Protein prot;
                    if (!proteins.TryGetValue(hit.PSM.ProteinGroup, out prot))
                    {
                        prot = new Protein(hit.PSM.ProteinGroup, defline);
                        proteins.Add(hit.PSM.ProteinGroup, prot);
                    }
                    prot.AddHit(hit);
                }
            }
            using (StreamWriter writer = new StreamWriter(Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(csvFile) + "_all.csv")),
                   localizedWriter = new StreamWriter(Path.Combine(outputDirectory, Path.GetFileNameWithoutExtension(csvFile) + "_localized.csv")))
            {
                using (CsvReader reader = new CsvReader(new StreamReader(csvFile), true))
                {
                    LocalizedHit hit = null;
                    headerInfo = reader.GetFieldHeaders();
                    bool tqFound = false;
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        if (headerInfo[i].EndsWith("NL)"))
                        {
                            if (!tqFound)
                            {
                                FirstQuantColumn = i;
                                tqFound          = true;
                            }
                        }
                        if (headerInfo[i] == "Channels Detected")
                        {
                            LastQuantColumn = i - 1;
                        }
                    }
                    string header = string.Join(",", headerInfo) + ",# Isoforms,# of Considered Fragments,Localized?,Delta Score,Best Isoform,Spectral Matches,% TIC,Second Best Isoform,Second Spectral Matches,Second % TIC";
                    writer.WriteLine(header);
                    localizedWriter.WriteLine(header);
                    while (reader.ReadNextRecord())
                    {
                        string mods = reader["Mods"];
                        if (string.IsNullOrEmpty(mods))
                        {
                            continue;
                        }

                        List <Modification> variableMods = OmssaModification.ParseModificationLine(mods).Select(item => item.Item1).OfType <Modification>().ToList();

                        // Only keep things with quantified Modifications
                        if (!variableMods.Any(mod => QuantifiedModifications.Contains(mod)))
                        {
                            continue;
                        }

                        string filename = reader["Filename/id"];
                        if (!hitsdict.TryGetValue(filename, out hit))
                        {
                            continue;
                        }

                        string[] data = new string[reader.FieldCount];
                        reader.CopyCurrentRecordTo(data);

                        hit.omssapsm = data;

                        StringBuilder sb = new StringBuilder();

                        foreach (string datum in data)
                        {
                            if (datum.Contains(','))
                            {
                                sb.Append("\"");
                                sb.Append(datum);
                                sb.Append("\"");
                            }
                            else
                            {
                                sb.Append(datum);
                            }
                            sb.Append(',');
                        }
                        sb.Append(hit.PSM.Isoforms);
                        sb.Append(',');
                        sb.Append(hit.LocalizedIsoform.Fragments.Count);
                        sb.Append(',');
                        sb.Append(hit.IsLocalized);
                        sb.Append(',');
                        sb.Append(hit.MatchDifference);
                        sb.Append(',');
                        sb.Append(hit.LocalizedIsoform.SequenceWithModifications);
                        sb.Append(',');
                        sb.Append(hit.LocalizedIsoform.SpectralMatch.Matches);
                        sb.Append(',');
                        sb.Append(hit.LocalizedIsoform.SpectralMatch.PercentTIC);
                        if (hit.PSM.Isoforms > 1)
                        {
                            //sb.Append(',');
                            //sb.Append(hit.BestPeptideSDFCount);
                            sb.Append(',');
                            sb.Append(hit.SecondBestPeptideIsoform.SequenceWithModifications);
                            sb.Append(',');
                            sb.Append(hit.SecondBestPeptideIsoform.SpectralMatch.Matches);
                            sb.Append(',');
                            sb.Append(hit.SecondBestPeptideIsoform.SpectralMatch.PercentTIC);
                            //sb.Append(',');
                            //sb.Append(hit.SecondBestPeptideSDFCount);
                        }

                        if (hit.IsLocalized)
                        {
                            localizedWriter.WriteLine(sb.ToString());
                        }
                        writer.WriteLine(sb.ToString());
                    }
                }
            }

            return(proteins.Values.ToList());
        }