Exemplo n.º 1
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;
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
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;
            }
        }