|
| 1 | +module DPPOP.CLI.Program |
| 2 | + |
| 3 | +open System |
| 4 | +open System.IO |
| 5 | +open Argu |
| 6 | +open BioFSharp |
| 7 | +open BioFSharp.IO |
| 8 | +open BioFSharp.ML.DPPOP |
| 9 | +open DPPOP.CLI |
| 10 | + |
| 11 | +module private App = |
| 12 | + |
| 13 | + type ModelProfile = |
| 14 | + | PlantProfile |
| 15 | + | NonPlantProfile |
| 16 | + |
| 17 | + type AppConfig = { |
| 18 | + ProteomePath : string |
| 19 | + ProteinsOfInterestPath: string |
| 20 | + OutputPath : string option |
| 21 | + ModelProfile : ModelProfile |
| 22 | + CustomModelPath : string option |
| 23 | + } |
| 24 | + |
| 25 | + let private parseModelProfile = function |
| 26 | + | None -> NonPlantProfile |
| 27 | + | Some value when String.Equals(value, "plant", StringComparison.OrdinalIgnoreCase) -> PlantProfile |
| 28 | + | Some value when String.Equals(value, "nonplant", StringComparison.OrdinalIgnoreCase) -> NonPlantProfile |
| 29 | + | Some value when String.Equals(value, "non-plant", StringComparison.OrdinalIgnoreCase) -> NonPlantProfile |
| 30 | + | Some value -> invalidArg "model" $"Unsupported model profile '{value}'. Use 'plant' or 'nonplant'." |
| 31 | + |
| 32 | + let private ensureFileExists label path = |
| 33 | + if not (File.Exists(path)) then |
| 34 | + invalidArg label $"Could not find {label} file at '{path}'." |
| 35 | + |
| 36 | + let private ensureOutputDirectory path = |
| 37 | + let fullPath = Path.GetFullPath(path) |
| 38 | + let directory = Path.GetDirectoryName(fullPath) |
| 39 | + if not (String.IsNullOrWhiteSpace(directory)) then |
| 40 | + Directory.CreateDirectory(directory) |> ignore |
| 41 | + |
| 42 | + let private sanitizeTsvValue (value:string) = |
| 43 | + value.Replace("\t", " ").Replace("\r", " ").Replace("\n", " ") |
| 44 | + |
| 45 | + let private getPredictionModel config = |
| 46 | + match config.CustomModelPath with |
| 47 | + | Some modelPath -> Prediction.Model.Custom modelPath |
| 48 | + | None -> |
| 49 | + match config.ModelProfile with |
| 50 | + | PlantProfile -> Prediction.Model.Plant |
| 51 | + | NonPlantProfile -> Prediction.Model.NonPlant |
| 52 | + |
| 53 | + let private getNormalization config = |
| 54 | + match config.ModelProfile with |
| 55 | + | PlantProfile -> Classification.zNormalizePlantFeatureVector |
| 56 | + | NonPlantProfile -> Classification.zNormalizeNonPlantFeatureVector |
| 57 | + |
| 58 | + let private toTsv (results: PredictionOutput array) = |
| 59 | + let header = "ProteinId\tSequence\tPredictionScore\tDistinct" |
| 60 | + let rows = |
| 61 | + results |
| 62 | + |> Array.map (fun result -> |
| 63 | + $"{sanitizeTsvValue result.ProteinId}\t{result.Sequence}\t{result.PredictionScore:G17}\t{result.Distinct}" |
| 64 | + ) |
| 65 | + String.concat Environment.NewLine (Array.append [| header |] rows) |
| 66 | + |
| 67 | + let buildConfig (parseResults: ParseResults<CLIArguments>) = |
| 68 | + let customModelPath = parseResults.TryGetResult CLIArguments.Custom_Model |
| 69 | + customModelPath |> Option.iter (ensureFileExists "custom model") |
| 70 | + |
| 71 | + { |
| 72 | + ProteomePath = parseResults.GetResult CLIArguments.Proteome |
| 73 | + ProteinsOfInterestPath = parseResults.GetResult CLIArguments.Proteins_Of_Interest |
| 74 | + OutputPath = parseResults.TryGetResult CLIArguments.Output |
| 75 | + ModelProfile = parseResults.TryGetResult CLIArguments.Model |> parseModelProfile |
| 76 | + CustomModelPath = customModelPath |
| 77 | + } |
| 78 | + |
| 79 | + let run config = |
| 80 | + ensureFileExists "proteome" config.ProteomePath |
| 81 | + ensureFileExists "proteins-of-interest" config.ProteinsOfInterestPath |
| 82 | + |
| 83 | + let proteome = Input.readFastaWithSanitation config.ProteomePath |
| 84 | + let proteinsOfInterest = Input.readFastaWithSanitation config.ProteinsOfInterestPath |
| 85 | + |
| 86 | + if proteome.EncounteredIllegalCharacters || proteinsOfInterest.EncounteredIllegalCharacters then |
| 87 | + eprintfn "Warning: input FASTA contained one or more illegal sequence characters ('*' or '-') which were removed before prediction." |
| 88 | + |
| 89 | + let predictionModel = getPredictionModel config |
| 90 | + let normalization = getNormalization config |
| 91 | + |
| 92 | + let results = |
| 93 | + Prediction.scoreProteinsAgainstProteome predictionModel normalization proteome.Entries proteinsOfInterest.Entries |
| 94 | + |> Seq.collect id |
| 95 | + |> Array.ofSeq |
| 96 | + |
| 97 | + let tsv = toTsv results |
| 98 | + |
| 99 | + match config.OutputPath with |
| 100 | + | Some outputPath -> |
| 101 | + ensureOutputDirectory outputPath |
| 102 | + File.WriteAllText(outputPath, tsv) |
| 103 | + | None -> |
| 104 | + printfn "%s" tsv |
| 105 | + |
| 106 | +[<EntryPoint>] |
| 107 | +let main argv = |
| 108 | + let parser = ArgumentParser.Create<CLIArguments>(programName = "dppop") |
| 109 | + |
| 110 | + try |
| 111 | + let parseResults = parser.ParseCommandLine(inputs = argv, raiseOnUsage = true) |
| 112 | + parseResults |
| 113 | + |> App.buildConfig |
| 114 | + |> App.run |
| 115 | + 0 |
| 116 | + with |
| 117 | + | :? ArguParseException as ex -> |
| 118 | + match ex.ErrorCode with |
| 119 | + | ErrorCode.HelpText -> |
| 120 | + printfn "%s" ex.Message |
| 121 | + 0 |
| 122 | + | _ -> |
| 123 | + eprintfn "%s" ex.Message |
| 124 | + eprintfn "%s" (parser.PrintUsage()) |
| 125 | + 1 |
| 126 | + | ex -> |
| 127 | + eprintfn "%s" ex.Message |
| 128 | + 1 |
0 commit comments