1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#[cfg(feature = "compression")]
use flate2::{write::GzEncoder, Compression};

use crate::error::*;
use crate::structs::*;
use crate::validate;
use crate::StrictnessLevel;

use std::fs::File;
use std::io::prelude::*;
use std::io::BufWriter;

/// Save the given PDB struct to the given file as mmCIF or PDBx.
/// # Errors
/// It validates the PDB. It fails if the validation fails with the given `level`, or if the file could not be opened.
/// If validation gives rise to problems, use the `save_raw` function.
pub fn save_mmcif(
    pdb: &PDB,
    filename: impl AsRef<str>,
    level: StrictnessLevel,
) -> Result<(), Vec<PDBError>> {
    save_mmcif_(pdb, filename, level, BufWriter::new)
}

/// Save the given PDB struct to the given file as mmCIF or PDBx and compresses to .gz
/// # Errors
/// It validates the PDB. It fails if the validation fails with the given `level`, or if the file could not be opened.
/// If validation gives rise to problems, use the `save_raw` function.
#[cfg(feature = "compression")]
pub fn save_mmcif_gz(
    pdb: &PDB,
    filename: impl AsRef<str>,
    level: StrictnessLevel,
    compression_level: Option<Compression>,
) -> Result<(), Vec<PDBError>> {
    save_mmcif_(pdb, filename, level, |file| {
        BufWriter::new(GzEncoder::new(
            file,
            compression_level.unwrap_or(Compression::default()),
        ))
    })
}

/// Generic function to save the given PDB struct to the given file as mmCIF or PDBx,
/// to some writer function, e.g. a GzEncoder or BufWriter.
fn save_mmcif_<T, W>(
    pdb: &PDB,
    filename: impl AsRef<str>,
    level: StrictnessLevel,
    writer: W,
) -> Result<(), Vec<PDBError>>
where
    T: Write,
    W: FnOnce(File) -> BufWriter<T>,
{
    // Validates the PDB, and returns early if any errors are found
    let filename = filename.as_ref();

    let mut errors = validate(pdb);
    for error in &errors {
        if error.fails(level) {
            return Err(errors);
        }
    }

    // Creates a writer for the file
    let file = match File::create(filename) {
        Ok(f) => f,
        Err(_e) => {
            errors.push(PDBError::new(
                ErrorLevel::BreakingError,
                "Could not open file",
                "Could not open the file for writing, make sure you have permission for this file and no other program is currently using it.",
                Context::show(filename)
            ));
            return Err(errors);
        }
    };

    let writer = writer(file);

    // Now call the writer function
    save_mmcif_raw(pdb, writer);

    Ok(())
}

/// Save the given PDB struct to the given BufWriter.
/// It does not validate or renumber the PDB, so if that is needed that needs to be done in preparation.
/// It does change the output format based on the StrictnessLevel given.
#[allow(clippy::unwrap_used)]
pub fn save_mmcif_raw<T: Write>(pdb: &PDB, mut sink: BufWriter<T>) {
    /// Write a piece of text to the file, has the same structure as format!
    macro_rules! write {
        ($($arg:tt)*) => {
            sink.write_fmt(format_args!($($arg)*)).unwrap();
            sink.write_all(b"\n").unwrap();
        }
    }

    let empty = "?".to_string();
    let name = pdb.identifier.as_ref().unwrap_or(&empty);

    // Header
    write!(
        "data_{}
#
_entry.id   {}
#
_audit_conform.dict_name       mmcif_pdbx.dic
_audit_conform.dict_version    5.338
_audit_conform.dict_location   http://mmcif.pdb.org/dictionaries/ascii/mmcif_pdbx.dic",
        name,
        name
    );

    // Cryst
    if let Some(unit_cell) = &pdb.unit_cell {
        write!(
            "# Unit cell definition
_cell.entry_id           {}
_cell.length_a           {}
_cell.length_b           {}
_cell.length_c           {}
_cell.angle_alpha        {}
_cell.angle_beta         {}
_cell.angle_gamma        {}
_cell.Z_PDB              {}",
            name,
            unit_cell.a(),
            unit_cell.b(),
            unit_cell.c(),
            unit_cell.alpha(),
            unit_cell.beta(),
            unit_cell.gamma(),
            if let Some(symmetry) = &pdb.symmetry {
                symmetry.z().to_string()
            } else {
                "?".to_owned()
            }
        );
    }

    // Scale
    if let Some(scale) = &pdb.scale {
        let ma = scale.matrix();
        write!(
            "# Scale definition
            _atom_sites.entry_id                   '{}'
_atom_sites.Cartn_transf_matrix[1][1]  {}
_atom_sites.Cartn_transf_matrix[1][2]  {}
_atom_sites.Cartn_transf_matrix[1][3]  {}
_atom_sites.Cartn_transf_matrix[2][1]  {}
_atom_sites.Cartn_transf_matrix[2][2]  {}
_atom_sites.Cartn_transf_matrix[2][3]  {}
_atom_sites.Cartn_transf_matrix[3][1]  {}
_atom_sites.Cartn_transf_matrix[3][2]  {}
_atom_sites.Cartn_transf_matrix[3][3]  {}
_atom_sites.Cartn_transf_vector[1]     {}
_atom_sites.Cartn_transf_vector[2]     {}
_atom_sites.Cartn_transf_vector[3]     {}",
            name,
            ma[0][0],
            ma[0][1],
            ma[0][2],
            ma[1][0],
            ma[1][1],
            ma[1][2],
            ma[2][0],
            ma[2][1],
            ma[2][2],
            ma[0][3],
            ma[1][3],
            ma[2][3],
        );
    }

    // OrigX
    if let Some(origx) = &pdb.origx {
        let ma = origx.matrix();
        write!(
            "# OrigX definition
_database_PDB_matrix.entry_id                   '{}'
_database_PDB_matrix.origx[1][1]  {}
_database_PDB_matrix.origx[1][2]  {}
_database_PDB_matrix.origx[1][3]  {}
_database_PDB_matrix.origx[2][1]  {}
_database_PDB_matrix.origx[2][2]  {}
_database_PDB_matrix.origx[2][3]  {}
_database_PDB_matrix.origx[3][1]  {}
_database_PDB_matrix.origx[3][2]  {}
_database_PDB_matrix.origx[3][3]  {}
_database_PDB_matrix.origx_vector[1]     {}
_database_PDB_matrix.origx_vector[2]     {}
_database_PDB_matrix.origx_vector[3]     {}",
            name,
            ma[0][0],
            ma[0][1],
            ma[0][2],
            ma[1][0],
            ma[1][1],
            ma[1][2],
            ma[2][0],
            ma[2][1],
            ma[2][2],
            ma[0][3],
            ma[1][3],
            ma[2][3],
        );
    }

    // MtriX
    for mtrix in pdb.mtrix() {
        let ma = mtrix.transformation.matrix();
        write!(
            r#"# OrigX definition
_struct_ncs_oper.id            '{}'
_struct_ncs_oper.code          {}
_struct_ncs_oper.matrix[1][1]  {}
_struct_ncs_oper.matrix[1][2]  {}
_struct_ncs_oper.matrix[1][3]  {}
_struct_ncs_oper.matrix[2][1]  {}
_struct_ncs_oper.matrix[2][2]  {}
_struct_ncs_oper.matrix[2][3]  {}
_struct_ncs_oper.matrix[3][1]  {}
_struct_ncs_oper.matrix[3][2]  {}
_struct_ncs_oper.matrix[3][3]  {}
_struct_ncs_oper.vector[1]     {}
_struct_ncs_oper.vector[2]     {}
_struct_ncs_oper.vector[3]     {}"#,
            mtrix.serial_number,
            if mtrix.contained {
                "given"
            } else {
                "generated"
            },
            ma[0][0],
            ma[0][1],
            ma[0][2],
            ma[1][0],
            ma[1][1],
            ma[1][2],
            ma[2][0],
            ma[2][1],
            ma[2][2],
            ma[0][3],
            ma[1][3],
            ma[2][3],
        );
    }

    if let Some(symmetry) = &pdb.symmetry {
        write!(
            "# Space group definition
_symmetry.entry_id                         {}
_symmetry.space_group_name_H-M             '{}'
_symmetry.pdbx_full_space_group_name_H-M   '{}'
_symmetry.Int_Tables_number                {}",
            name,
            symmetry.herman_mauguin_symbol(),
            symmetry.herman_mauguin_symbol(),
            symmetry.index()
        );
    }

    let anisou = pdb
        .atoms()
        .any(|a| a.anisotropic_temperature_factors().is_some());
    write!(
        "loop_
_atom_site.group_PDB
_atom_site.id
_atom_site.type_symbol
_atom_site.label_atom_id
_atom_site.label_alt_id
_atom_site.label_comp_id
_atom_site.label_asym_id
_atom_site.auth_asym_id
_atom_site.label_entity_id
_atom_site.label_seq_id
_atom_site.auth_seq_id
_atom_site.pdbx_PDB_ins_code
_atom_site.Cartn_x
_atom_site.Cartn_y
_atom_site.Cartn_z
_atom_site.occupancy
_atom_site.B_iso_or_equiv
_atom_site.pdbx_formal_charge
_atom_site.pdbx_PDB_model_num{}",
        if anisou {
            "
_atom_site.aniso_U[1][1]
_atom_site.aniso_U[1][2]
_atom_site.aniso_U[1][3]
_atom_site.aniso_U[2][1]
_atom_site.aniso_U[2][2]
_atom_site.aniso_U[2][3]
_atom_site.aniso_U[3][1]
_atom_site.aniso_U[3][2]
_atom_site.aniso_U[3][3]"
        } else {
            ""
        }
    );

    let mut lines = Vec::new();

    for model in pdb.models() {
        let mut chain_index = 0;
        for chain in model.chains() {
            chain_index += 1;
            for (residue_index, residue) in chain.residues().enumerate() {
                for conformer in residue.conformers() {
                    for atom in conformer.atoms() {
                        let mut data = vec![
                            (if atom.hetero() { "HETATM" } else { "ATOM" }).to_string(), // ATOM or HETATM
                            atom.serial_number().to_string(), // Serial number
                            atom.element()
                                .map_or_else(|| "", Element::symbol)
                                .to_string(), // Element
                            atom.name().to_string(),          // Name
                            conformer.alternative_location().unwrap_or(".").to_string(), // Alternative location
                            conformer.name().to_string(), // Residue name
                            number_to_base26(chain_index), // Label Chain name, defined to be without gaps
                            chain.id().to_string(),        // Auth Chain name
                            chain_index.to_string(),       // Entity ID, using chain serial number
                            (residue_index + 1).to_string(), // `label_seq_id` defined to be [1-N] where N is the index
                            residue.serial_number().to_string(), // Residue serial number
                            residue.insertion_code().unwrap_or(".").to_string(), // Insertion code
                            print_float(atom.x()),           // X
                            print_float(atom.y()),           // Y
                            print_float(atom.z()),           // Z
                            print_float(atom.occupancy()),   // OCC/Q
                            print_float(atom.b_factor()),    // B
                            atom.charge().to_string(),       // Charge
                            model.serial_number().to_string(), // Model serial number
                        ];
                        if anisou {
                            if let Some(matrix) = atom.anisotropic_temperature_factors() {
                                data.extend(vec![
                                    print_float(matrix[0][0]),
                                    print_float(matrix[0][1]),
                                    print_float(matrix[0][2]),
                                    print_float(matrix[1][0]),
                                    print_float(matrix[1][1]),
                                    print_float(matrix[1][2]),
                                    print_float(matrix[2][0]),
                                    print_float(matrix[2][1]),
                                    print_float(matrix[2][2]),
                                ]);
                            } else {
                                data.extend(vec![
                                    ".".to_string(),
                                    ".".to_string(),
                                    ".".to_string(),
                                    ".".to_string(),
                                    ".".to_string(),
                                    ".".to_string(),
                                    ".".to_string(),
                                    ".".to_string(),
                                    ".".to_string(),
                                ]);
                            }
                        }

                        lines.push(data);
                    }
                }
            }
        }
    }
    if !lines.is_empty() {
        // Now align the table
        let mut sizes = vec![1; lines[0].len()];
        for line in &lines {
            for index in 0..line.len() {
                sizes[index] = std::cmp::max(sizes[index], line[index].len());
            }
        }

        // Now write the table
        for line in lines {
            let mut output = String::new();
            output.push_str(&line[0]);
            output.push_str(&" ".repeat(sizes[0] - line[0].len()));
            for index in 1..line.len() {
                output.push(' ');
                if line[index].trim() != "" {
                    output.push_str(&line[index]);
                    output.push_str(&" ".repeat(sizes[index] - line[index].len()));
                } else {
                    output.push('?');
                    output.push_str(&" ".repeat(sizes[index] - 1));
                }
            }
            output.push('\n');
            sink.write_all(output.as_bytes()).unwrap();
        }
    }

    write!("#");

    sink.flush().unwrap();
}

/// Print a floating point with at least 1 decimal place and at max 5 decimals
#[allow(clippy::cast_possible_truncation)]
fn print_float(num: f64) -> String {
    let rounded = (num * 100000.).round() / 100000.;
    if (rounded.round() - rounded).abs() < std::f64::EPSILON {
        format!("{}.0", rounded.trunc() as isize)
    } else {
        format!("{rounded}")
    }
}

#[cfg(test)]
#[allow(clippy::print_stdout)]
mod tests {
    use super::*;

    #[test]
    #[allow(clippy::excessive_precision, clippy::print_literal)]
    fn test_print_float() {
        assert_eq!(print_float(1.), "1.0".to_string());
        assert_eq!(print_float(128734.), "128734.0".to_string());
        assert_eq!(print_float(0.1), "0.1".to_string());
        assert_eq!(print_float(1.015), "1.015".to_string());
        assert_eq!(print_float(2.015), "2.015".to_string());
        assert_eq!(print_float(1.4235263), "1.42353".to_string());
        println!("{}", 235617341053.235611341053); // Already printed as 235617341053.23563
        assert_eq!(
            print_float(235617341053.235611341053),
            "235617341053.23563".to_string()
        );
        println!("{}", 23561753.235617341053); // Printed as 23561753.23561734
        assert_eq!(
            print_float(23561753.235617341053),
            "23561753.23562".to_string()
        );
    }
}