Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        public static string SaveInteractionsOutput(string saveFilename, string[] interactionOutputStrings, FileExistsHandler.FileExistsOptions fileExistsOptions = FileExistsHandler.FileExistsOptions.AppendNumberToFilename)
        {
            var fileInfo = new FileInfo(saveFilename);

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

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

            File.WriteAllLines(fileInfo.FullName, interactionOutputStrings);

            return(fileInfo.FullName);
        }
Beispiel #3
0
 /// <summary>
 /// Provides emojis from Emoji One (http://emojione.com/) stored as local files.
 /// </summary>
 /// <param name="SourceFileType">Type of files to use.</param>
 /// <param name="Width">Desired width of emojis.</param>
 /// <param name="Height">Desired height of emojis.</param>
 /// <param name="ImageURL">URL for remote clients to fetch the image. If not provided, images are embedded into generated pages.
 /// Include the string %FILENAME% where the name of the emoji image file is to be inserted.</param>
 /// <param name="FileExists">Delegate to method used to check if a file exists.</param>
 /// <param name="ReadAllBytes">Method to read all bytes of a file.</param>
 public Emoji1LocalFiles(Emoji1SourceFileType SourceFileType, int Width, int Height, string ImageURL,
                         FileExistsHandler FileExists, ReadAllBytesHandler ReadAllBytes)
 {
     this.sourceFileType = SourceFileType;
     this.width          = Width;
     this.height         = Height;
     this.imageUrl       = ImageURL;
     this.fileexists     = FileExists;
     this.readAllBytes   = ReadAllBytes;
 }
Beispiel #4
0
        public bool Exists(string path)
        {
            try
            {
                AbsolutePath absolutePath = string.IsNullOrWhiteSpace(path) ? null : owner.ToAbsolutePath(path);

                var handler   = new FileExistsHandler(root);
                var arguments = new DirectoryOrFileExistsArguments(absolutePath);

                return(handler.Handle(arguments));
            }
            catch (Exception ex) when(ShouldSuppress(ex))
            {
                return(false);
            }
        }
Beispiel #5
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());
        }
        /// <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());
        }
Beispiel #7
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());
        }
Beispiel #8
0
 /// <summary>
 /// Provides emojis from Emoji One (http://emojione.com/) stored as local files.
 /// </summary>
 /// <param name="SourceFileType">Type of files to use.</param>
 /// <param name="Width">Desired width of emojis.</param>
 /// <param name="Height">Desired height of emojis.</param>
 /// <param name="FileExists">Delegate to method used to check if a file exists.</param>
 /// <param name="ReadAllBytes">Method to read all bytes of a file.</param>
 public Emoji1LocalFiles(Emoji1SourceFileType SourceFileType, int Width, int Height,
                         FileExistsHandler FileExists, ReadAllBytesHandler ReadAllBytes)
     : this(SourceFileType, Width, Height, string.Empty, FileExists, ReadAllBytes)
 {
 }