0 | module Text.Molfile.Writer
 1 |
 2 | import Chem
 3 | import Data.Linear.Traverse1
 4 | import Data.String.Builder
 5 | import Syntax.T1
 6 | import Text.Molfile.Types
 7 | import Text.Molfile.Writer.Util
 8 | import Text.Molfile.Writer.V2000
 9 | import Text.Molfile.Writer.V3000
10 |
11 | %default total
12 |
13 | --------------------------------------------------------------------------------
14 | -- Writing SD-files
15 | --------------------------------------------------------------------------------
16 |
17 | parameters {default V2000 version : MolVersion}
18 |
19 |   ||| Writes a molecular graph plus header line in mol format to the
20 |   ||| given string builder.
21 |   |||
22 |   ||| The given `version` is used for the format unless the molecul is
23 |   ||| very large (more than 999 atoms or bonds), in which case V3000 *has*
24 |   ||| to be used.
25 |   export
26 |   putMol : Builder q => (n,i,ct : MolLine) -> MolGraph' h t c -> F1' q
27 |   putMol n i c (G 0 _) = pure ()
28 |   putMol n i c g       =
29 |    let es := edges g.graph
30 |        v3 := version == V3000 || g.order >= 1000 || length es >= 1000
31 |     in T1.do
32 |          putTextLn n.value
33 |          putTextLn i.value
34 |          putTextLn c.value
35 |          if v3 then putMol3000 es g else putMol2000 es g
36 |          putTextLn "M  END"
37 |
38 |   export
39 |   putSDF : Builder q => Molfile' h t c -> F1' q
40 |   putSDF (MkMolfile n i c g ds) = T1.do
41 |     putMol n i c g
42 |     traverse1_ writeStructureData ds
43 |     sdfDelimiter
44 |
45 |   ||| Converts a molecular graph to a string in mol format.
46 |   |||
47 |   ||| The given `version` is used for the format unless the molecul is
48 |   ||| very large (more than 999 atoms or bonds), in which case V3000 *has*
49 |   ||| to be used.
50 |   export %inline
51 |   writeMolfile : Molfile' h t c -> String
52 |   writeMolfile (MkMolfile n i c g _) = withBuilder $ putMol n i c g
53 |
54 |   export %inline
55 |   writeSDFile : Molfile' h t c -> String
56 |   writeSDFile m = withBuilder $ putSDF m
57 |
58 |   export %inline
59 |   writeSDF : List (Molfile' h t c) -> String
60 |   writeSDF ms = withBuilder $ traverse1_ putSDF ms
61 |