Beispiel #1
0
 public static bool SaveToFile(string filename, PeptideArray PA)
 {
     try
     {
         PA.Version = typeof(Analyzer).Assembly.GetName().Version.ToString();
         string json = JsonConvert.SerializeObject(PA);
         File.WriteAllText(filename, json);
         return(true);
     }
     catch { return(false); }
 }
Beispiel #2
0
 public static PeptideArray ReadFromFile(string filename)
 {
     try
     {
         PeptideArray PA = null;
         if (File.Exists(filename))
         {
             PA = JsonConvert.DeserializeObject <PeptideArray>(File.ReadAllText(filename));
             if (PA.Version == "")
             {
                 PA.Version = "Old version";
                 PA.Upgrade("PositiveThreshold");
             }
         }
         return(PA);
     }
     catch { return(null); }
 }
Beispiel #3
0
        public static bool ReadQuantificationData(string fileName, PeptideArray PA, bool headersExist)
        {
            try
            {
                FileInfo existingFile = new FileInfo(fileName);
                string[,] data = null;
                if (existingFile.Extension.StartsWith(".xls"))
                {
                    data = ReadArrayFromExcel(existingFile);
                }
                else if (existingFile.Extension.StartsWith(".csv"))
                {
                    data = ReadArrayFromCSV(existingFile);
                }

                PA.SetQuantificationMatrix(data, headersExist);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Beispiel #4
0
        public static bool ExportPeptideArrayToExcel(string fileName, PeptideArray PA, bool overwrite, out string errormsg)
        {
            try
            {
                errormsg = "";
                FileInfo existingFile = new FileInfo(fileName);
                if (existingFile.Exists && !overwrite)
                {
                    return(false);
                }
                using (ExcelPackage package = new ExcelPackage(existingFile))
                {
                    if (existingFile.Exists)
                    {
                        while (package.Workbook.Worksheets.Count > 0)
                        {
                            package.Workbook.Worksheets.Delete(1);
                        }
                    }
                    ExcelWorksheet worksheet = GetWorksheetBlank(package, "Arrays");

                    int lastrow = MatrixToExcel(worksheet, 1, "Peptide Matrix", PA.PeptideMatrix);
                    if (lastrow < 0)
                    {
                        return(false);
                    }
                    lastrow = MatrixToExcel(worksheet, lastrow + 3, "Binary Matrix", PA.BinaryMatrix);
                    if (lastrow < 0)
                    {
                        return(false);
                    }
                    lastrow = MatrixToExcel(worksheet, lastrow + 3, "Quantification Matrix", PA.QuantificationMatrix);
                    if (lastrow < 0)
                    {
                        return(false);
                    }

                    worksheet.Cells[++lastrow, 1].Value = "Norm By:";
                    worksheet.Cells[lastrow, 2].Value   = PA.NormalizationValue;
                    lastrow = MatrixToExcel(worksheet, lastrow + 3, "Normalized Matrix", PA.NormalizedMatrix);

                    WriteToSheetWeighedPeptideList(package, PA.NormalizedPeptideWeights, PA.PositiveThreshold, PA.NegativeThreshold);

                    if (PA.KeyPosition != null && PA.KeyAA != ' ')
                    {
                        int           rowind      = 1;
                        List <string> mainList    = PA.ModifiedPeptides.Where(s => s[(int)PA.KeyPosition - 1] == PA.KeyAA).ToList();
                        List <string> shiftedList = Analyzer.ShiftPeptides(PA.ModifiedPeptides.Where(s => s[(int)PA.KeyPosition - 1] != PA.KeyAA).ToList(), PA.KeyAA, PA.PeptideLength, (int)PA.KeyPosition - 1,
                                                                           out List <string> replacements);
                        Motif motif = new Motif(mainList, PA.PeptideLength);
                        motif.FreqThreshold = PA.FrequencyThreshold;

                        WriteToSheetFrequencyMotif(package, motif, "Motif", ref rowind);
                        rowind++;

                        motif = new Motif(shiftedList, PA.PeptideLength);
                        motif.FreqThreshold = PA.FrequencyThreshold;
                        WriteToSheetFrequencyMotif(package, motif, "Shifted Motif", ref rowind, false);
                    }
                    else
                    {
                        Motif motif = new Motif(PA.ModifiedPeptides, PA.PeptideLength);
                        motif.FreqThreshold = PA.FrequencyThreshold;

                        int rowind = 1;
                        WriteToSheetFrequencyMotif(package, motif, "Motif", ref rowind);
                    }

                    WriteToSheetReferenceInfo(package, PA);

                    try
                    {
                        package.Save();
                    }
                    catch
                    {
                        errormsg = "There is a problem with saving the export file. Please make sure the file is not open and you have writing rights to the specific folder.";
                        return(false);
                    }
                }

                return(true);
            }
            catch
            {
                errormsg = "There is a problem with creating the export file. Please try again.";
                return(false);
            }
        }