示例#1
0
 public static AminoAcid GetAminoAcid(char c)
 {
     if (!AminoAcids.IsStandardAminoAcid(c))
     {
         return(null);
     }
     return(AminoAcidList[c]);
 }
示例#2
0
        static public bool CheckPeptideList(List <string> peptides, int length, out List <string> warnings, out List <string> errors)
        {
            warnings = new List <string>();
            errors   = new List <string>();
            bool ret = true;

            try
            {
                foreach (string p in peptides)
                {
                    if (Regex.Matches(p, @"[a-zA-Z]").Count < p.Length)
                    {
                        errors.Add("Invalid characters in " + p);
                    }
                    else
                    {
                        if (p.Length < length)
                        {
                            warnings.Add("Length inconsistency in " + p);
                        }
                        foreach (char c in p.Substring(0, Math.Min(p.Length, length)))
                        {
                            if (!AminoAcids.IsStandardAminoAcid(c))
                            {
                                warnings.Add("Non-standard amino acid in " + p);
                                break;
                            }
                        }
                    }
                }
                return(ret);
            }
            catch (Exception exc)
            {
                errors.Add("Unhandled exception: " + exc.Message);
                return(false);
            }
        }
示例#3
0
        static private List <string> GenerateCombinations(string input)
        {
            int start = input.IndexOf('[');

            if (start < 0)
            {
                return new List <string>()
                       {
                           input
                       }
            }
            ;

            int end = input.IndexOf(']', start + 1);

            if (end < 0)
            {
                throw new Exception();
            }
            List <string> comb = new List <string>();

            string chars     = input.Substring(start + 1, end - start - 1);
            string firstpart = start > 0 ? input.Substring(0, start) : "";
            string lastpart  = end < input.Length - 1 ? input.Substring(end + 1) : "";

            if (chars.Substring(0, 1) == "-" || chars == "X") //use all aminoacids except the ones listed - X: use all
            {
                string charstouse = "";
                foreach (AminoAcid aa in AminoAcids.GetAminoAcidList())
                {
                    if (!chars.Contains(aa.Abbrev1))
                    {
                        charstouse += aa.Abbrev1;
                    }
                }
                chars = charstouse;
            }
            List <string> withinbrackets = new List <string>();

            while (chars != "")
            {
                char c = chars.First();
                if (c == '{')
                {
                    end = chars.IndexOf('}', 1);
                    if (end < 0)
                    {
                        throw new Exception();
                    }
                    withinbrackets.Add(chars.Substring(1, end - 1));
                    chars = chars.Substring(end + 1);
                    continue;
                }
                withinbrackets.Add(c.ToString());
                chars = chars.Substring(1);
            }
            foreach (string s in withinbrackets)
            {
                string subpart = firstpart + s + lastpart;
                comb.AddRange(GenerateCombinations(subpart));
            }

            return(comb);
        }
    }
示例#4
0
文件: Motif.cs 项目: EmineTopcu/PeSA
        private Bitmap GetLetterImage(Char c, int width, int height, Color color)
        {
            if (width <= 0 || height <= 0)
            {
                return(null);
            }

            var scaledBitmap = new Bitmap(width, height);

            Bitmap bmp = Settings.GetAminoAcidImage(c, color != Common.ColorNegative);

            if (bmp != null)
            {
                try
                {
                    var        sourceRectangle      = SelectFilledPixels(bmp);
                    RectangleF destinationRectangle = new RectangleF(0, 0, width, height);

                    using (var destination = Graphics.FromImage(scaledBitmap))
                    {
                        destination.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        destination.DrawImage(bmp, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel);
                    }
                    return(scaledBitmap);
                }
                catch (Exception exc) { }
            }
            var initialFontSize = (int)(Math.Max(width, height) / 1.5);

            var fullSizeBitmap = new Bitmap(initialFontSize * 2, initialFontSize * 2);

            using (var source = Graphics.FromImage(fullSizeBitmap))
                using (var destination = Graphics.FromImage(scaledBitmap))
                    using (var font = new Font("Arial", initialFontSize, FontStyle.Bold))
                        using (var brush = new SolidBrush(color))
                        {
                            source.FillRectangle(Brushes.White, 0, 0, fullSizeBitmap.Width, fullSizeBitmap.Height);
                            AminoAcid aa = AminoAcids.GetAminoAcid(c); //aa?.MolecularWeight.ToString() ??
                            //source.DrawString(c=='X'?"X":aa?.pI.ToString(), font, brush, 0, 0);
                            source.DrawString(c.ToString(), font, brush, 0, 0);

                            var        sourceRectangle = SelectFilledPixels(fullSizeBitmap);
                            RectangleF destinationRectangle;
                            if (c == 'I')
                            {
                                double newwidth = 5 * sourceRectangle.Width * height / sourceRectangle.Height;
                                if (newwidth > width / 3)
                                {
                                    newwidth = width / 3;
                                }
                                destinationRectangle = new RectangleF((int)(width - newwidth) / 2, 0, (int)newwidth, height);
                            }
                            else
                            {
                                destinationRectangle = new RectangleF(0, 0, width, height);
                            }

                            destination.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                            destination.DrawImage(fullSizeBitmap, destinationRectangle, sourceRectangle, GraphicsUnit.Pixel);
                        }

            return(scaledBitmap);
        }
示例#5
0
        private void GenerateMatrices(string[,] values, out string error)
        {
            error = "";
            try
            {
                if (values == null)
                {
                    return;
                }
                RowCount             = values.GetLength(0) - 1;
                ColCount             = values.GetLength(1) - 1;
                QuantificationMatrix = new double[RowCount, ColCount];
                NormalizedMatrix     = new double[RowCount, ColCount];

                WildTypePeptide = new string('X', PermutationXAxis ? RowCount : ColCount);

                #region Generate permutation, position, and NormBy arrays
                if (PermutationXAxis)
                {
                    NormBy = new double[RowCount];
                    for (int iRow = 1; iRow <= RowCount; iRow++)
                    {
                        NormBy[iRow - 1] = 0;
                    }
                    Permutation      = new char[ColCount];
                    PositionCaptions = new string[RowCount];
                    for (int iCol = 1; iCol <= ColCount; iCol++)
                    {
                        string s = values[0, iCol].Trim();
                        if (AminoAcids.GetAminoAcid(s[0]) == null)
                        {
                            error = "Not a valid permutation string";
                            return;
                        }
                        Permutation[iCol - 1] = s[0];
                    }
                    //PositionYAxisTopToBottom is not used in setting the position captions. What user sees should not change. Use it only during the motif generation
                    for (int iRow = 1; iRow <= RowCount; iRow++)
                    {
                        string s = values[iRow, 0].Trim();
                        PositionCaptions[iRow - 1] = s;
                    }
                }
                else //if(PermutationYAxis)
                {
                    NormBy = new double[ColCount];
                    for (int iCol = 1; iCol <= ColCount; iCol++)
                    {
                        NormBy[iCol - 1] = 0;
                    }
                    Permutation      = new char[RowCount];
                    PositionCaptions = new string[ColCount];
                    for (int iRow = 1; iRow <= RowCount; iRow++)
                    {
                        string s = values[iRow, 0].Trim();
                        if (AminoAcids.GetAminoAcid(s[0]) == null)
                        {
                            error = "Not a valid permutation string";
                            return;
                        }
                        Permutation[iRow - 1] = s[0];
                    }
                    for (int iCol = 1; iCol <= ColCount; iCol++)
                    {
                        string s = values[0, iCol].Trim();
                        PositionCaptions[iCol - 1] = s;
                    }
                }
                #endregion

                //Generate Matrices and Normalization values
                for (int iRow = 0; iRow < RowCount; iRow++)
                {
                    for (int iCol = 0; iCol < ColCount; iCol++)
                    {
                        double d = 0;
                        if (double.TryParse(values[iRow + 1, iCol + 1], out d))
                        {
                            QuantificationMatrix[iRow, iCol] = d;
                        }
                        else if (string.IsNullOrEmpty(values[iRow + 1, iCol + 1]))
                        {
                            d = 0;
                        }
                        else
                        {
                            error = "Wrongly formatted data.";
                            return;
                        }

                        if (PermutationXAxis)
                        {
                            NormBy[iRow] = Math.Max(d, NormBy[iRow]);
                        }
                        else
                        {
                            NormBy[iCol] = Math.Max(d, NormBy[iCol]);
                        }
                    }
                }

                for (int iRow = 0; iRow < RowCount; iRow++)
                {
                    for (int iCol = 0; iCol < ColCount; iCol++)
                    {
                        double normby = PermutationXAxis ? NormBy[iRow] : NormBy[iCol];
                        if (normby != 0)
                        {
                            NormalizedMatrix[iRow, iCol] = QuantificationMatrix[iRow, iCol] / normby;
                        }
                    }
                }
                NormalizationValue = NormBy.Max();
                GenerateNormalizedPeptideWeights();
            }
            catch (Exception exc)
            {
                error = "Unhandled exception: " + exc.Message;
            }
        }
示例#6
0
        private void GenerateMatrices(string[,] values, out List <string> warnings, out string error)
        {
            error    = "";
            warnings = new List <string>();
            if (values == null)
            {
                return;
            }
            try
            {
                RowCount             = values.GetLength(0) - 1;
                ColCount             = values.GetLength(1) - 1;
                PeptideMatrix        = new string[RowCount, ColCount];
                QuantificationMatrix = new double[RowCount, ColCount];
                NormalizedMatrix     = new double[RowCount, ColCount];

                bool nswwarning = false;
                bool nspwarning = false;
                //Generate Permutation and Wildtype arrays
                if (PermutationXAxis)
                {
                    NormBy = new double[RowCount];
                    for (int iRow = 1; iRow <= RowCount; iRow++)
                    {
                        string s = values[iRow, 0].Trim();
                        if (AminoAcids.GetAminoAcid(s[0]) == null)
                        {
                            if (s.Length == 1)
                            {
                                if (!nswwarning)
                                {
                                    warnings.Add("Non-standard amino acid in wildtype peptide.");
                                }
                                nswwarning = true;
                            }
                            else
                            {
                                error = "Not a valid wildtype peptide";
                                return;
                            }
                        }
                        if (WildTypeYAxisTopToBottom)
                        {
                            WildTypePeptide += s[0];
                        }
                        else
                        {
                            WildTypePeptide = s[0] + WildTypePeptide; //read from bottom to top
                        }
                    }
                    Permutation = new char[ColCount];
                    for (int iCol = 1; iCol <= ColCount; iCol++)
                    {
                        string s = values[0, iCol].Trim();
                        if (AminoAcids.GetAminoAcid(s[0]) == null)
                        {
                            if (s.Length == 1)
                            {
                                if (!nspwarning)
                                {
                                    warnings.Add("Non-standard amino acid in permutation string.");
                                }
                                nspwarning = true;
                            }
                            else
                            {
                                error = "Not a valid permutation string";
                                return;
                            }
                        }
                        Permutation[iCol - 1] = s[0];
                    }
                }
                else //if(PermutationYAxis)
                {
                    NormBy = new double[ColCount];
                    for (int iCol = 1; iCol <= ColCount; iCol++)
                    {
                        string s = values[0, iCol].Trim();
                        if (AminoAcids.GetAminoAcid(s[0]) == null)
                        {
                            if (s.Length == 1)
                            {
                                if (!nswwarning)
                                {
                                    warnings.Add("Non-standard amino acid in wildtype peptide.");
                                }
                                nswwarning = true;
                            }
                            else
                            {
                                error = "Not a valid wildtype peptide";
                                return;
                            }
                        }
                        WildTypePeptide += s[0];
                    }

                    Permutation = new char[RowCount];
                    for (int iRow = 1; iRow <= RowCount; iRow++)
                    {
                        string s = values[iRow, 0].Trim();
                        if (AminoAcids.GetAminoAcid(s[0]) == null)
                        {
                            if (s.Length == 1)
                            {
                                if (!nspwarning)
                                {
                                    warnings.Add("Non-standard amino acid in permutation string.");
                                }
                                nspwarning = true;
                            }
                            else
                            {
                                error = "Not a valid permutation string";
                                return;
                            }
                        }
                        Permutation[iRow - 1] = s[0];
                    }
                }

                //Generate Matrices and Normalization values
                int wtl = WildTypePeptide.Length;

                int    counter   = 0;
                double totalNorm = 0;
                for (int iRow = 0; iRow < RowCount; iRow++)
                {
                    for (int iCol = 0; iCol < ColCount; iCol++)
                    {
                        if (double.TryParse(values[iRow + 1, iCol + 1], out double d))
                        {
                            QuantificationMatrix[iRow, iCol] = d;
                        }
                        else if (string.IsNullOrEmpty(values[iRow + 1, iCol + 1]))
                        {
                            d = 0;
                        }
                        else
                        {
                            error = "Wrongly formatted data.";
                            return;
                        }

                        if (PermutationXAxis) //wildTypeYAxis
                        {
                            if (WildTypeYAxisTopToBottom)
                            {
                                PeptideMatrix[iRow, iCol] = (iRow >= 1 ? WildTypePeptide.Substring(0, iRow) : "") + Permutation[iCol] + (iRow < WildTypePeptide.Length - 1 ? WildTypePeptide.Substring(iRow + 1) : "");
                            }
                            else  //from bottom to top
                            {
                                PeptideMatrix[iRow, iCol] = (iRow < wtl - 1 ? WildTypePeptide.Substring(0, wtl - iRow - 1) : "") + Permutation[iCol] + (iRow > 0 ? WildTypePeptide.Substring(wtl - iRow) : "");
                            }
                        }
                        else
                        {
                            PeptideMatrix[iRow, iCol] = (iCol >= 1 ? WildTypePeptide.Substring(0, iCol) : "") + Permutation[iRow] + (iCol < WildTypePeptide.Length - 1 ? WildTypePeptide.Substring(iCol + 1) : "");
                        }

                        if (PeptideMatrix[iRow, iCol] == WildTypePeptide)
                        {
                            if (d > 0)
                            {
                                counter++;
                                totalNorm += d;
                            }
                            if (PermutationXAxis)
                            {
                                NormBy[iRow] = d;
                            }
                            else
                            {
                                NormBy[iCol] = d;
                            }
                        }
                    }
                }
                if (counter > 0 && counter < NormBy.GetLength(0)) //fill the blanks with average
                {
                    for (int i = 0; i < NormBy.GetLength(0); i++)
                    {
                        if (NormBy[i] < 0.0001)
                        {
                            NormBy[i] = totalNorm / counter;
                        }
                    }
                }
                NormalizationValue = totalNorm / counter;
                GenerateNormalizedPeptideWeights();
            }
            catch (Exception exc)
            {
                error = "Unhandled exception: " + exc.Message;
            }
        }