コード例 #1
0
ファイル: Newick.cs プロジェクト: aaroncaffrey/Bioinformatics
        /// <summary>
        /// Saves a Tree already generated by the NewickTreeFormat method.
        /// </summary>
        /// <param name="outputTreeFilename"></param>
        /// <param name="treeList"></param>
        /// <param name="fileExistsOptions"></param>
        /// <returns></returns>
        public static string SaveNewickTree(string outputTreeFilename, List <string> treeList, FileExistsHandler.FileExistsOptions fileExistsOptions = FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
        {
            var filename = new FileInfo(FileAndPathMethods.RemoveFileExtension(outputTreeFilename) + ".tree");

            if (filename.Exists)
            {
                if (fileExistsOptions == FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
                {
                    filename = new FileInfo(FileExistsHandler.FindNextFreeOutputFilename(filename.FullName));
                }
                else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.OverwriteFile)
                {
                }
                else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.SkipFile)
                {
                    return(null);
                }
            }

            filename.Directory?.Create();

            File.WriteAllLines(filename.FullName, treeList);

            return(filename.FullName);
        }
コード例 #2
0
        public static string[] SaveSpreadsheetTsv(string saveFilename, string[] sheetNames, List <List <SpreadsheetCell[]> > spreadsheet, ProgressActionSet progressActionSet = null, FileExistsHandler.FileExistsOptions fileExistsOptions = FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
        {
            var resultList = new List <string>();

            for (int sheetIndex = 0; sheetIndex < spreadsheet.Count; sheetIndex++)
            {
                var padLen = ("" + spreadsheet.Count).Length;
                if (padLen < 2)
                {
                    padLen = 2;
                }
                var paddedSheetIndex = ("" + (sheetIndex + 1)).PadLeft(padLen, '0');

                var sheetName = "";
                if (sheetNames != null && sheetNames.Length > sheetIndex)
                {
                    sheetName = sheetNames[sheetIndex];
                }

                if (string.IsNullOrWhiteSpace(sheetName))
                {
                    sheetName = Path.GetFileNameWithoutExtension(saveFilename) + " [sheet " + paddedSheetIndex + "]";
                }

                var saveSheetFilename = FileAndPathMethods.MergePathAndFilename(Path.GetDirectoryName(saveFilename), sheetName + Path.GetExtension(saveFilename));
                var result            = SaveSpreadsheetTsv(saveSheetFilename, spreadsheet[sheetIndex], progressActionSet, fileExistsOptions);
                resultList.AddRange(result);
            }
            return(resultList.ToArray());
        }
コード例 #3
0
        public static string[] SaveSpreadsheetTsv(string saveFilename, List <SpreadsheetCell[]> spreadsheet, ProgressActionSet progressActionSet = null, FileExistsHandler.FileExistsOptions fileExistsOptions = FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
        {
            var result      = new List <string>();
            var tsvFilename = new FileInfo(FileAndPathMethods.RemoveFileExtension(saveFilename) + ".tsv");

            if (tsvFilename.Exists)
            {
                if (fileExistsOptions == FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
                {
                    tsvFilename = new FileInfo(FileExistsHandler.FindNextFreeOutputFilename(tsvFilename.FullName));
                }
                else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.OverwriteFile)
                {
                }
                else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.SkipFile)
                {
                    return(result.ToArray());
                }
            }

            var stringBuilder = new StringBuilder();

            for (int rowIndex = 0; rowIndex < spreadsheet.Count; rowIndex++)
            {
                var rowDataArray = spreadsheet[rowIndex];

                if (rowDataArray == null)
                {
                    continue;
                }

                stringBuilder.AppendLine(string.Join("\t", rowDataArray.Select(row => row.CellData)));
            }

            if (tsvFilename.Directory != null)
            {
                tsvFilename.Directory.Create();
            }

            File.WriteAllText(tsvFilename.FullName, stringBuilder.ToString());
            result.Add(tsvFilename.FullName);

            return(result.ToArray());
        }
コード例 #4
0
        /// <summary>
        /// Outputs distance matrix in cluto dense/sparse matrix format (*.mat), row headings in cluto row label file format (*.mat.rlabel) and column headings in cluto column label file format (*.mat.clabel)
        /// </summary>
        /// <param name="distanceMatrix"></param>
        /// <param name="matrixOutputFilename"></param>
        /// <param name="rowLabels"></param>
        /// <param name="rowLabelsOutputFilename"></param>
        /// <param name="columnLabels"></param>
        /// <param name="columnLabelsOutputFilename"></param>
        /// <param name="zeroHalf"></param>
        /// <param name="sprase"></param>
        /// <param name="clutoMatrixFormatTypes"></param>
        /// <param name="fileExistsOptions"></param>
        /// <returns></returns>
        public static string[] ConvertToMatrixFile(decimal[,] distanceMatrix, string matrixOutputFilename, string[] rowLabels = null, string rowLabelsOutputFilename = null, string[] columnLabels = null, string columnLabelsOutputFilename = null, ClutoMatrixFormatTypes clutoMatrixFormatTypes = ClutoMatrixFormatTypes.SparseMatrixTopHalf, FileExistsHandler.FileExistsOptions fileExistsOptions = FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
        {
            if (distanceMatrix == null || distanceMatrix.Length == 0)
            {
                throw new ArgumentNullException(nameof(distanceMatrix), "null or empty");
            }

            if (string.IsNullOrWhiteSpace(matrixOutputFilename))
            {
                throw new ArgumentNullException(nameof(matrixOutputFilename), "null or empty");
            }

            if (rowLabels != null && rowLabels.Length != distanceMatrix.GetLength(1))
            {
                throw new ArgumentOutOfRangeException(nameof(rowLabels), "length not equal to matrix rows");
            }

            if (columnLabels != null && columnLabels.Length != distanceMatrix.GetLength(0))
            {
                throw new ArgumentOutOfRangeException(nameof(columnLabels), "length not equal to matrix columns");
            }

            if (columnLabels != null && string.IsNullOrWhiteSpace(columnLabelsOutputFilename))
            {
                throw new ArgumentNullException(nameof(columnLabelsOutputFilename), "null or empty");
            }

            if (rowLabels != null && string.IsNullOrWhiteSpace(rowLabelsOutputFilename))
            {
                throw new ArgumentNullException(nameof(rowLabelsOutputFilename), "null or empty");
            }

            if (clutoMatrixFormatTypes == ClutoMatrixFormatTypes.DenseMatrixTopHalf || clutoMatrixFormatTypes == ClutoMatrixFormatTypes.SparseMatrixTopHalf)
            {
                distanceMatrix = DiagonalZeroHalfMatrix(distanceMatrix, true);
            }
            else if (clutoMatrixFormatTypes == ClutoMatrixFormatTypes.DenseMatrixBottomHalf || clutoMatrixFormatTypes == ClutoMatrixFormatTypes.SparseMatrixBottomHalf)
            {
                distanceMatrix = DiagonalZeroHalfMatrix(distanceMatrix, false);
            }

            bool sparse = clutoMatrixFormatTypes == ClutoMatrixFormatTypes.SparseMatrixBottomHalf || clutoMatrixFormatTypes == ClutoMatrixFormatTypes.SparseMatrixTopHalf || clutoMatrixFormatTypes == ClutoMatrixFormatTypes.SparseMatrixComplete;

            var result = new List <List <string> >();

            result.Add(new List <string>());

            var rowTotal     = distanceMatrix.GetLength(1);
            var columnTotal  = distanceMatrix.GetLength(0);
            var nonZeroTotal = distanceMatrix.Cast <decimal>().Count(a => a != 0);

            result[0].Add(rowTotal.ToString());
            result[0].Add(columnTotal.ToString());

            if (sparse)
            {
                result[0].Add(nonZeroTotal.ToString());
            }

            for (var y = 0; y < rowTotal; y++)
            {
                result.Add(new List <string>());

                for (var x = 0; x < columnTotal; x++)
                {
                    var value = distanceMatrix[x, y];

                    if (sparse)
                    {
                        if (value == 0)
                        {
                            continue;
                        }
                        result[result.Count - 1].Add(x.ToString());
                        result[result.Count - 1].Add(value.ToString());
                    }
                    else
                    {
                        result[result.Count - 1].Add(value.ToString());
                    }
                }
            }

            var lines = result.Select(a => string.Join(" ", a)).ToList();

            var savedFiles = new List <string>();

            if (File.Exists(matrixOutputFilename))
            {
                if (fileExistsOptions == FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
                {
                    matrixOutputFilename = FileExistsHandler.FindNextFreeOutputFilename(matrixOutputFilename);
                }
                else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.OverwriteFile)
                {
                }
                else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.SkipFile)
                {
                    return(savedFiles.ToArray());
                }
            }


            FileAndPathMethods.CreateDirectory(matrixOutputFilename);
            File.WriteAllLines(matrixOutputFilename, lines);
            savedFiles.Add(matrixOutputFilename);

            if (rowLabels != null && !string.IsNullOrWhiteSpace(rowLabelsOutputFilename))
            {
                var saveRowLabels = true;

                if (File.Exists(rowLabelsOutputFilename))
                {
                    if (fileExistsOptions == FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
                    {
                        rowLabelsOutputFilename = FileExistsHandler.FindNextFreeOutputFilename(rowLabelsOutputFilename);
                    }
                    else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.OverwriteFile)
                    {
                    }
                    else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.SkipFile)
                    {
                        saveRowLabels = false;
                    }
                }

                if (saveRowLabels)
                {
                    FileAndPathMethods.CreateDirectory(rowLabelsOutputFilename);
                    File.WriteAllLines(rowLabelsOutputFilename, rowLabels);
                    savedFiles.Add(rowLabelsOutputFilename);
                }
            }

            if (columnLabels != null && !string.IsNullOrWhiteSpace(columnLabelsOutputFilename))
            {
                var saveColumnLabels = true;

                if (File.Exists(columnLabelsOutputFilename))
                {
                    if (fileExistsOptions == FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
                    {
                        columnLabelsOutputFilename = FileExistsHandler.FindNextFreeOutputFilename(columnLabelsOutputFilename);
                    }
                    else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.OverwriteFile)
                    {
                    }
                    else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.SkipFile)
                    {
                        saveColumnLabels = false;
                    }
                }

                if (saveColumnLabels)
                {
                    FileAndPathMethods.CreateDirectory(columnLabelsOutputFilename);
                    File.WriteAllLines(columnLabelsOutputFilename, columnLabels);
                    savedFiles.Add(columnLabelsOutputFilename);
                }
            }

            return(savedFiles.ToArray());
        }
コード例 #5
0
        public static string[] SaveSpreadsheetXl(string saveFilename, string[] sheetNames, List <List <SpreadsheetCell[]> > spreadsheet, ProgressActionSet progressActionSet = null, FileExistsHandler.FileExistsOptions fileExistsOptions = FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
        {
            var result = new List <string>();

            var xlFilename = new FileInfo(FileAndPathMethods.RemoveFileExtension(saveFilename) + ".xlsx");

            if (xlFilename.Exists)
            {
                if (fileExistsOptions == FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
                {
                    xlFilename = new FileInfo(FileExistsHandler.FindNextFreeOutputFilename(xlFilename.FullName));
                }
                else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.OverwriteFile)
                {
                }
                else if (fileExistsOptions == FileExistsHandler.FileExistsOptions.SkipFile)
                {
                    return(result.ToArray());
                }
            }

            if (xlFilename.Directory != null)
            {
                xlFilename.Directory.Create();
            }

            var totalSheets = spreadsheet.Count;


            string sheetName = XlSheetNameVerification(Path.GetFileNameWithoutExtension(xlFilename.Name));

            if (sheetNames == null || sheetNames.Length < spreadsheet.Count)
            {
                var extraSheetNames = new string[totalSheets];

                if (sheetNames != null && sheetNames.Length > 0)
                {
                    Array.Copy(sheetNames, extraSheetNames, sheetNames.Length);
                }

                for (var sheetIndex = sheetNames != null ? sheetNames.Length : 0; sheetIndex < extraSheetNames.Length; sheetIndex++)
                {
                    extraSheetNames[sheetIndex] = sheetName + (sheetIndex + 1);
                }

                sheetNames = extraSheetNames;
            }

            for (int index = 0; index < sheetNames.Length; index++)
            {
                if (string.IsNullOrWhiteSpace(sheetNames[index]))
                {
                    sheetNames[index] = sheetName + (index + 1);
                }

                sheetNames[index] = XlSheetNameVerification(sheetNames[index]);
            }


            XlSpreadsheetDocumentContainer xlSpreadsheet = ExcelCreateSpreadsheet.CreateSpreadsheetWorkbook(xlFilename.FullName, sheetNames);


            //Worksheet worksheet1 = xlSpreadsheet.WorksheetPart.Worksheet;//new Worksheet();

            var runPropertiesArray = GetAminoAcidRunProperties();

            for (uint sheetIndex = 0; sheetIndex < spreadsheet.Count; sheetIndex++)
            {
                var worksheet1 = new Worksheet();

                var sheetData1 = new SheetData();


                for (uint rowIndex = 0; rowIndex < spreadsheet[(int)sheetIndex].Count; rowIndex++)
                {
                    var rowDataArray = spreadsheet[(int)sheetIndex][(int)rowIndex];

                    if (rowDataArray == null)
                    {
                        continue;
                    }

                    var row1 = new Row {
                        RowIndex = (UInt32Value)rowIndex + 1                  /*, Spans = new ListValue<StringValue>() { InnerText = "1:3" }, DyDescent = 0.25D*/
                    };
                    //var row4 = new Row(){ RowIndex = (UInt32Value)4U, Spans = new ListValue<StringValue>() { InnerText = "1:2" }, DyDescent = 0.25D };

                    for (uint columnIndex = 0; columnIndex < rowDataArray.Length; columnIndex++)
                    {
                        string columnValue = rowDataArray[columnIndex].CellData;

                        if (string.IsNullOrWhiteSpace(columnValue))
                        {
                            continue;
                        }

                        string columnName = AlphabetLetterRollOver((int)columnIndex);
                        string cellRef    = columnName + (rowIndex + 1);

                        var cell1 = new Cell {
                            CellReference = cellRef, StyleIndex = 1U
                        };

                        switch (rowDataArray[columnIndex].SpreadsheetDataType)
                        {
                        case SpreadsheetDataTypes.String:
                            cell1.DataType = CellValues.String;
                            break;

                        case SpreadsheetDataTypes.Integer:
                            cell1.DataType = CellValues.Number;
                            break;

                        case SpreadsheetDataTypes.Double:
                            cell1.DataType = CellValues.Number;
                            break;

                        case SpreadsheetDataTypes.Decimal:
                            cell1.DataType = CellValues.Number;
                            break;
                        }

                        //InlineString inlineString1 = new InlineString();
                        //Text text1 = new Text();
                        //text1.Text = columnValue;

                        //inlineString1.Append(text1);

                        //cell1.Append(inlineString1);


                        //if (rowDataArray[columnIndex].CellColourScheme == SpreadsheetCellColourScheme.Default)
                        //{
                        var cellValue1 = new CellValue();
                        cellValue1.Text = columnValue;
                        cell1.Append(cellValue1);
                        row1.Append(cell1);
                        //}
                        //else if (rowDataArray[columnIndex].CellColourScheme == SpreadsheetCellColourScheme.AminoAcidsUniProtKb)
                        //{

                        //    foreach (var ch in rowDataArray[columnIndex].CellData)
                        //    {
                        //        var subgroups = AminoAcidGroups.AminoAcidGroups.ConvertAminoAcidNameCodeToGroupNumbers(AminoAcidGroups.AminoAcidGroups.EnumAminoAcidGroups.UniProtKb, ch);
                        //        var firstGroup = subgroups[0];
                        //        //var groupColours = AminoAcidGroups.AminoAcidGroups.GetGroupColors(AminoAcidGroups.AminoAcidGroups.EnumAminoAcidGroups.UniProtKb);
                        //        //var firstGroupColour = groupColours[firstGroup];

                        //        var runProperties = runPropertiesArray[(int) AminoAcidGroups.AminoAcidGroups.EnumAminoAcidGroups.UniProtKb][firstGroup];

                        //        Run run = new Run();
                        //        Text text = new Text();
                        //        text.Text = ""+ch;

                        //        run.Append(runProperties);
                        //        run.Append(text);

                        //        cell1.Append(run);
                        //        row1.Append(cell1);
                        //    }
                        //}

                        //worksheetPart1.Worksheet = worksheet1;


                        //var cell = ExcelSheets.InsertCellInWorksheet(ProteinInterfaceDetection.AlphabetLetterRollOver(columnIndex), rowIndex + 1, xlSpreadsheet.WorksheetPart);
                        //ExcelSheets.InsertText(xlSpreadsheet.SpreadsheetDocument, xlSpreadsheet.WorksheetPart, cell, "Test");
                        //cell.DataType = new EnumValue<CellValues>(CellValues.String);//.SharedString);
                        //cell.CellValue = new CellValue("test");

                        //xlSpreadsheet.WorksheetPart.Worksheet.Save();
                    }
                    sheetData1.Append(row1);
                }

                worksheet1.Append(sheetData1);

                //xlSpreadsheet.WorksheetPart.Worksheet = worksheet1;
                xlSpreadsheet.WorkbookPartObject.WorksheetParts.ToList()[(int)sheetIndex].Worksheet = worksheet1;
            }

            //xlSpreadsheet.WorksheetPart.Worksheet.Save();
            //xlSpreadsheet.WorkbookPart.Workbook.Save();
            //xlSpreadsheet.SpreadsheetDocument.WorkbookPart.Workbook.Save();

            xlSpreadsheet.SpreadsheetDocumentObject.Close();

            result.Add(xlFilename.FullName);
            return(result.ToArray());
        }