| 1 |
module Main |
| 2 |
|
| 3 |
open Command |
| 4 |
open Download |
| 5 |
open System |
| 6 |
open System.IO |
| 7 |
|
| 8 |
open FSharpPlus |
| 9 |
|
| 10 |
type PdbCode = PdbCode of string |
| 11 |
|
| 12 |
type FileFormat = |
| 13 |
| PDB |
| 14 |
| CIF |
| 15 |
|
| 16 |
type PdbFile = |
| 17 |
{ Path: string |
| 18 |
Format: FileFormat |
| 19 |
Code: PdbCode } |
| 20 |
|
| 21 |
type Output = |
| 22 |
{ Output: String |
| 23 |
Warnings: String |
| 24 |
ExitCode: int } |
| 25 |
|
| 26 |
type CalculationError = |
| 27 |
| CalculationFailed of Output |
| 28 |
| DownloadFailed of String |
| 29 |
|
| 30 |
type FreesasaResult = Result<Output, CalculationError> |
| 31 |
|
| 32 |
type ComparisonError = |
| 33 |
| CalculationError of CalculationError list |
| 34 |
| CalculationDifference |
| 35 |
|
| 36 |
type Comparison = (PdbCode * ComparisonError) option |
| 37 |
type ResultComparer = PdbCode -> FreesasaResult -> FreesasaResult -> Comparison |
| 38 |
|
| 39 |
let archiveUrl = Uri("https://files.rcsb.org/download/") |
| 40 |
let dataDirectory = "./data/" |
| 41 |
let freesasaProgram = "../../src/freesasa" |
| 42 |
|
| 43 |
let getExtension format = |
| 44 |
match format with |
| 45 |
| PDB -> ".pdb" |
| 46 |
| CIF -> ".cif" |
| 47 |
|
| 48 |
let downloadPdb code format = |
| 49 |
let getUrl (PdbCode code) = |
| 50 |
Uri(archiveUrl, code + (getExtension format)) |
| 51 |
|
| 52 |
let getPath (PdbCode code) = |
| 53 |
dataDirectory + code + (getExtension format) |
| 54 |
|
| 55 |
async { |
| 56 |
let path = getPath code |
| 57 |
let url = getUrl code |
| 58 |
let! result = downloadFile url path |
| 59 |
|
| 60 |
return |
| 61 |
match result with |
| 62 |
| Ok () -> |
| 63 |
Ok |
| 64 |
{ Path = path |
| 65 |
Format = format |
| 66 |
Code = code } |
| 67 |
| Error e -> Error(DownloadFailed e) |
| 68 |
} |
| 69 |
|
| 70 |
let normalizeCommandResult (commandResult: CommandResult) : Output = |
| 71 |
let stripSource output = |
| 72 |
String.split [ "\n" ] output |
| 73 |
|> Seq.filter (fun line -> not (line.StartsWith "source")) |
| 74 |
|> Seq.sort |
| 75 |
|> String.intercalate "\n" |
| 76 |
|
| 77 |
{ Output = stripSource commandResult.StandardOutput |
| 78 |
Warnings = |
| 79 |
commandResult |
| 80 |
.StandardError |
| 81 |
.Replace(" ", "") |
| 82 |
.Split("\n") |
| 83 |
|> Seq.sort |
| 84 |
|> String.intercalate "\n" |
| 85 |
ExitCode = commandResult.ExitCode } |
| 86 |
|
| 87 |
let runPdbCalc args file = |
| 88 |
let log output = |
| 89 |
printf "\rCalculated %s (exit code %d)" file.Path output.ExitCode |
| 90 |
stdout.Flush() |
| 91 |
|
| 92 |
let allArgs = |
| 93 |
if file.Format = CIF then |
| 94 |
"--cif" :: args |
| 95 |
else |
| 96 |
args |
| 97 |
|
| 98 |
async { |
| 99 |
let! output = |
| 100 |
executeCommand freesasaProgram (file.Path :: allArgs) |
| 101 |
|> Async.map normalizeCommandResult |
| 102 |
|
| 103 |
do log output |
| 104 |
|
| 105 |
return |
| 106 |
match output with |
| 107 |
| { ExitCode = 0 } -> Ok output |
| 108 |
| _ -> Error(CalculationFailed output) |
| 109 |
} |
| 110 |
|
| 111 |
let calcStructure code fileType args = |
| 112 |
downloadPdb code fileType |
| 113 |
|> AsyncResult.bind (runPdbCalc args) |
| 114 |
|
| 115 |
let compareFormats args (compare: ResultComparer) code : Async<Comparison> = |
| 116 |
async { |
| 117 |
let! cifResult = calcStructure code CIF args |
| 118 |
let! pdbResult = calcStructure code PDB args |
| 119 |
return compare code cifResult pdbResult |
| 120 |
} |
| 121 |
|
| 122 |
let compareResults : ResultComparer = |
| 123 |
fun code result1 result2 -> |
| 124 |
let err = CalculationDifference |
| 125 |
|
| 126 |
match (result1, result2) with |
| 127 |
| Ok v1, Ok v2 -> |
| 128 |
if (v1 = v2) then |
| 129 |
None |
| 130 |
else |
| 131 |
Some(code, err) |
| 132 |
| Error e1, Error e2 -> |
| 133 |
if (e1 = e2) then |
| 134 |
None |
| 135 |
else |
| 136 |
Some(code, err) |
| 137 |
| _ -> Some(code, err) |
| 138 |
|
| 139 |
let checkArgs codes args = |
| 140 |
printf "Checking with args %A\n" args |
| 141 |
|
| 142 |
let computations = |
| 143 |
codes |
| 144 |
|> List.map (compareFormats args compareResults) |
| 145 |
|
| 146 |
Async.Parallel(computations, 8) |
| 147 |
|> Async.RunSynchronously |
| 148 |
|> Array.toList |
| 149 |
|> List.choose id |
| 150 |
|> fun errors -> |
| 151 |
printf |
| 152 |
"%s" |
| 153 |
("\r" |
| 154 |
+ (new string (' ', Console.WindowWidth - 1)) |
| 155 |
+ "\r") |
| 156 |
|
| 157 |
if List.isEmpty errors then |
| 158 |
printfn " - No errors" |
| 159 |
None |
| 160 |
else |
| 161 |
printfn " - Comparison failed:\n %A\n" errors |
| 162 |
Some errors |
| 163 |
|
| 164 |
|
| 165 |
let getCodes fileName = |
| 166 |
System.IO.File.ReadAllLines(fileName) |
| 167 |
|> Seq.map (String.trimEnd " " >> PdbCode) |
| 168 |
|> Seq.toList |
| 169 |
|
| 170 |
[<EntryPoint>] |
| 171 |
let main argv = |
| 172 |
if argv.Length < 1 then |
| 173 |
printfn "usage: pass input file as argument" |
| 174 |
1 |
| 175 |
else |
| 176 |
if not (Directory.Exists dataDirectory) then |
| 177 |
do Directory.CreateDirectory(dataDirectory) |> ignore |
| 178 |
// [ PdbCode "1ubq"; PdbCode "1d3z" ] // for testing script itself |
| 179 |
let codes = getCodes argv.[0] |
| 180 |
|
| 181 |
// [ [ "--hetatm" ] |
| 182 |
// [ "--hydrogen" ] |
| 183 |
// [ "--separate-chains" |
| 184 |
// "--separate-models" ] |
| 185 |
// [] ] |
| 186 |
[ [ "--separate-chains" ] ] |
| 187 |
|> List.map (checkArgs codes) |
| 188 |
|> List.choose id |
| 189 |
|> fun errList -> if List.isEmpty errList then 0 else 1 |