Skip to content

Commit 1483031

Browse files
committed
Add DPPOP CLI and FASTA sanitation regression tests
1 parent a52b433 commit 1483031

10 files changed

Lines changed: 353 additions & 0 deletions

File tree

‎BioFSharp.ML.sln‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".ci", ".ci", "{812698C9-F1C
3737
EndProject
3838
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BioFSharp.ML", "src\BioFSharp.ML\BioFSharp.ML.fsproj", "{E2E3AA2A-BEEA-4965-A58F-2A3BCDD658D6}"
3939
EndProject
40+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "DPPOP.CLI", "src\DPPOP.CLI\DPPOP.CLI.fsproj", "{44776F6D-39F7-47F5-94D1-EAB557B7AA74}"
41+
EndProject
4042
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "BioFSharp.ML.Tests", "tests\BioFSharp.ML.Tests\BioFSharp.ML.Tests.fsproj", "{9A66D71D-8A9B-4376-85B4-3D5DB8BDDC22}"
4143
EndProject
44+
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "DPPOP.Tests", "tests\DPPOP.Tests\DPPOP.Tests.fsproj", "{C411B682-CBA2-4756-8903-6CE56F4C6DB9}"
45+
EndProject
4246
Global
4347
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4448
Debug|Any CPU = Debug|Any CPU
@@ -53,17 +57,27 @@ Global
5357
{E2E3AA2A-BEEA-4965-A58F-2A3BCDD658D6}.Debug|Any CPU.Build.0 = Debug|Any CPU
5458
{E2E3AA2A-BEEA-4965-A58F-2A3BCDD658D6}.Release|Any CPU.ActiveCfg = Release|Any CPU
5559
{E2E3AA2A-BEEA-4965-A58F-2A3BCDD658D6}.Release|Any CPU.Build.0 = Release|Any CPU
60+
{44776F6D-39F7-47F5-94D1-EAB557B7AA74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
61+
{44776F6D-39F7-47F5-94D1-EAB557B7AA74}.Debug|Any CPU.Build.0 = Debug|Any CPU
62+
{44776F6D-39F7-47F5-94D1-EAB557B7AA74}.Release|Any CPU.ActiveCfg = Release|Any CPU
63+
{44776F6D-39F7-47F5-94D1-EAB557B7AA74}.Release|Any CPU.Build.0 = Release|Any CPU
5664
{9A66D71D-8A9B-4376-85B4-3D5DB8BDDC22}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
5765
{9A66D71D-8A9B-4376-85B4-3D5DB8BDDC22}.Debug|Any CPU.Build.0 = Debug|Any CPU
5866
{9A66D71D-8A9B-4376-85B4-3D5DB8BDDC22}.Release|Any CPU.ActiveCfg = Release|Any CPU
5967
{9A66D71D-8A9B-4376-85B4-3D5DB8BDDC22}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{C411B682-CBA2-4756-8903-6CE56F4C6DB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69+
{C411B682-CBA2-4756-8903-6CE56F4C6DB9}.Debug|Any CPU.Build.0 = Debug|Any CPU
70+
{C411B682-CBA2-4756-8903-6CE56F4C6DB9}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{C411B682-CBA2-4756-8903-6CE56F4C6DB9}.Release|Any CPU.Build.0 = Release|Any CPU
6072
EndGlobalSection
6173
GlobalSection(SolutionProperties) = preSolution
6274
HideSolutionNode = FALSE
6375
EndGlobalSection
6476
GlobalSection(NestedProjects) = preSolution
6577
{8DCCA688-871C-49DD-BC06-4877F714497C} = {78399A41-F06A-4B35-AA24-E2E20209D8DE}
6678
{E2E3AA2A-BEEA-4965-A58F-2A3BCDD658D6} = {7501C4DF-A844-40A0-8742-3343E48E55F1}
79+
{44776F6D-39F7-47F5-94D1-EAB557B7AA74} = {7501C4DF-A844-40A0-8742-3343E48E55F1}
6780
{9A66D71D-8A9B-4376-85B4-3D5DB8BDDC22} = {D152DA0B-8F60-4611-B613-642379E2B17C}
81+
{C411B682-CBA2-4756-8903-6CE56F4C6DB9} = {D152DA0B-8F60-4611-B613-642379E2B17C}
6882
EndGlobalSection
6983
EndGlobal

‎build/ProjectInfo.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ let project = "BioFSharp.ML"
88
let testProjects =
99
[
1010
"tests/BioFSharp.ML.Tests/BioFSharp.ML.Tests.fsproj"
11+
"tests/DPPOP.Tests/DPPOP.Tests.fsproj"
1112
]
1213

1314
let solutionFile = $"{project}.sln"

‎src/DPPOP.CLI/CLIArgs.fs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace DPPOP.CLI
2+
3+
open Argu
4+
5+
type CLIArguments =
6+
| [<Unique; Mandatory; AltCommandLine("-p")>] Proteome of path:string
7+
| [<Unique; Mandatory; AltCommandLine("-i")>] Proteins_Of_Interest of path:string
8+
| [<Unique; AltCommandLine("-o")>] Output of path:string
9+
| [<Unique; AltCommandLine("-m")>] Model of value:string
10+
| [<Unique; AltCommandLine("-c")>] Custom_Model of path:string
11+
interface IArgParserTemplate with
12+
member this.Usage =
13+
match this with
14+
| Proteome _ -> "Path to the FASTA proteome used to determine distinct tryptic peptides."
15+
| Proteins_Of_Interest _ -> "Path to the FASTA file containing proteins to score."
16+
| Output _ -> "Optional output path for TSV results. Defaults to stdout."
17+
| Model _ -> "Prediction profile to use for normalization: plant or nonplant. Defaults to nonplant."
18+
| Custom_Model _ -> "Optional path to a custom CNTK model. The selected model profile still controls normalization."

‎src/DPPOP.CLI/DPPOP.CLI.fsproj‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<PackAsTool>true</PackAsTool>
7+
<ToolCommandName>dppop</ToolCommandName>
8+
<AssemblyName>DPPOP.CLI</AssemblyName>
9+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
10+
</PropertyGroup>
11+
12+
<PropertyGroup>
13+
<Authors>Timo Muehlhaus, David Zimmer, Kevin Schneider, F# open source contributors</Authors>
14+
<Description>CLI wrapper for DPPOP peptide observability prediction based on BioFSharp.ML.</Description>
15+
<PackageLicenseExpression>MIT</PackageLicenseExpression>
16+
<PackageProjectUrl>https://github.com/BioFSharp/BioFSharp.ML/</PackageProjectUrl>
17+
<PackageReadmeFile>README.md</PackageReadmeFile>
18+
<PackageTags>bioinformatics proteomics peptide-observability cntk cli fsharp</PackageTags>
19+
<RepositoryUrl>https://github.com/BioFSharp/BioFSharp.ML</RepositoryUrl>
20+
<RepositoryType>git</RepositoryType>
21+
</PropertyGroup>
22+
23+
<ItemGroup>
24+
<None Include="README.md" Pack="true" PackagePath="" />
25+
<Compile Include="CLIArgs.fs" />
26+
<Compile Include="Input.fs" />
27+
<Compile Include="Program.fs" />
28+
</ItemGroup>
29+
30+
<ItemGroup>
31+
<ProjectReference Include="..\BioFSharp.ML\BioFSharp.ML.fsproj" />
32+
<PackageReference Include="Argu" Version="6.2.5" />
33+
</ItemGroup>
34+
35+
</Project>

‎src/DPPOP.CLI/Input.fs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
namespace DPPOP.CLI
2+
3+
open System
4+
open System.IO
5+
open System.Text
6+
open BioFSharp
7+
open BioFSharp.IO
8+
9+
module Input =
10+
11+
type ReadFastaResult<'T> = {
12+
Entries: 'T array
13+
EncounteredIllegalCharacters: bool
14+
}
15+
16+
let private isIllegalSequenceCharacter (c: char) =
17+
c = '*' || c = '-'
18+
19+
let readFastaWithSanitation path =
20+
let sanitizedBuilder = StringBuilder()
21+
let mutable encounteredIllegalCharacters = false
22+
23+
for rawLine in File.ReadLines(path) do
24+
let line = rawLine.Trim()
25+
if line.StartsWith(">", StringComparison.Ordinal) then
26+
sanitizedBuilder.AppendLine(line) |> ignore
27+
elif String.IsNullOrWhiteSpace(line) then
28+
()
29+
else
30+
let sanitizedLine =
31+
line
32+
|> Seq.filter (fun c ->
33+
let isIllegal = isIllegalSequenceCharacter c
34+
if isIllegal then
35+
encounteredIllegalCharacters <- true
36+
not isIllegal
37+
)
38+
|> Array.ofSeq
39+
|> String
40+
41+
sanitizedBuilder.AppendLine(sanitizedLine) |> ignore
42+
43+
let tempPath = Path.GetTempFileName()
44+
45+
try
46+
File.WriteAllText(tempPath, sanitizedBuilder.ToString())
47+
48+
{
49+
Entries =
50+
tempPath
51+
|> Fasta.read BioArray.ofAminoAcidString
52+
|> Array.ofSeq
53+
EncounteredIllegalCharacters = encounteredIllegalCharacters
54+
}
55+
finally
56+
if File.Exists(tempPath) then
57+
File.Delete(tempPath)

‎src/DPPOP.CLI/Program.fs‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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

‎src/DPPOP.CLI/README.md‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# DPPOP.CLI
2+
3+
`DPPOP.CLI` packages the peptide observability workflow from `BioFSharp.ML.DPPOP` as a command-line tool.
4+
5+
## Usage
6+
7+
```powershell
8+
dppop --proteome proteome.fasta --proteins-of-interest targets.fasta --model nonplant --output results.tsv
9+
```
10+
11+
## Container
12+
13+
Build the container from the repository root after exporting the legacy runtime archive:
14+
15+
```powershell
16+
docker build -t dppop .
17+
```
18+
19+
Run it with mounted input data under `/data`:
20+
21+
```powershell
22+
docker run --rm --mount type=bind,source="C:/my-data",target=/data dppop --proteome /data/proteome.fasta --proteins-of-interest /data/targets.fasta --output /data/results.tsv
23+
```
24+
25+
### Notes
26+
27+
- `--model` controls the normalization profile and accepts `plant` or `nonplant`.
28+
- `--custom-model` can point to a custom CNTK model file while keeping the selected normalization profile.
29+
- CNTK native dependencies are still required at runtime.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<IsPackable>false</IsPackable>
6+
<GenerateProgramFile>false</GenerateProgramFile>
7+
<IsTestProject>true</IsTestProject>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Compile Include="FastaCompatibilityTests.fs" />
12+
<Compile Include="Program.fs" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="coverlet.collector" Version="10.0.0" />
17+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.4.0" />
18+
<PackageReference Include="xunit" Version="2.9.3" />
19+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5">
20+
<PrivateAssets>all</PrivateAssets>
21+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
22+
</PackageReference>
23+
</ItemGroup>
24+
25+
<ItemGroup>
26+
<ProjectReference Include="..\..\src\BioFSharp.ML\BioFSharp.ML.fsproj" />
27+
<ProjectReference Include="..\..\src\DPPOP.CLI\DPPOP.CLI.fsproj" />
28+
</ItemGroup>
29+
30+
</Project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
namespace DPPOP.Tests
2+
3+
open System
4+
open System.IO
5+
open Xunit
6+
open BioFSharp
7+
open DPPOP.CLI
8+
9+
module private TestPaths =
10+
11+
let repoRoot =
12+
Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..", "..", "..", "..", ".."))
13+
14+
let testFasta =
15+
Path.Combine(repoRoot, "src", "BioFSharp.ML", "Resources", "test.fasta")
16+
17+
type FastaCompatibilityTests() =
18+
19+
[<Fact>]
20+
member _.``CLI input sanitation removes illegal characters and reports them`` () =
21+
Assert.True(
22+
File.Exists(TestPaths.testFasta),
23+
$"Expected test FASTA at '{TestPaths.testFasta}'."
24+
)
25+
26+
let result = Input.readFastaWithSanitation TestPaths.testFasta
27+
28+
Assert.True(result.EncounteredIllegalCharacters)
29+
Assert.Single(result.Entries) |> ignore
30+
31+
let sanitizedSequence =
32+
result.Entries
33+
|> Array.exactlyOne
34+
|> fun entry -> entry.Sequence
35+
|> Array.ofSeq
36+
|> BioArray.toString
37+
38+
Assert.DoesNotContain("*", sanitizedSequence)
39+
Assert.DoesNotContain("-", sanitizedSequence)
40+
Assert.EndsWith("TTLS", sanitizedSequence)

‎tests/DPPOP.Tests/Program.fs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module Program = let [<EntryPoint>] main _ = 0

0 commit comments

Comments
 (0)