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
use itertools::Itertools;
use crate::seq::{Date, QualifierKey, Seq};
use std::convert::AsRef;
use std::io::{self, Write};

// Following constants taken from Biopython's InsdcIO.py
const MAX_WIDTH: usize = 79;
const QUALIFIER_INDENT: &str = "                     ";
const FIELD_INDENT: &str = "            ";
// Commented out some of these, need to check if they're still in
// use
const FTQUAL_NO_QUOTE: &[QualifierKey] = &[
    qualifier_key!("anticodon"),
    //qualifier_key!("citation"),
    qualifier_key!("codon_start"),
    //qualifier_key!("compare"),
    //qualifier_key!("direction"),
    qualifier_key!("estimated_length"),
    //qualifier_key!("mod_base"),
    qualifier_key!("number"),
    qualifier_key!("rpt_type"),
    //qualifier_key!("rpt_unit_range"),
    //qualifier_key!("tag_peptide"),
    qualifier_key!("transl_except"),
    qualifier_key!("transl_table"),
];

const POS_QUAL: &[QualifierKey] = &[
    // should be formatted like Locations
    qualifier_key!("transl_except"),
    qualifier_key!("anticodon"),
];



#[derive(Debug)]
pub struct SeqWriter<W: Write> {
    stream: W,
    truncate_locus: bool,
    escape_locus: bool,
}

impl<W: Write> SeqWriter<W> {
    /// Create a new `SeqWriter` to write GenBank files to the given stream.
    pub fn new(stream: W) -> Self {
        Self {
            stream,
            truncate_locus: false,
            escape_locus: true,
        }
    }

    /// Set the behaviour regarding locus name truncation.
    ///
    /// If `true` (the default when creating a `SeqWriter`, for backwards
    /// compatibility), the locus fields (`name` and `molecule_type`) will be
    /// truncated if they are too long so that the LOCUS line is no longer
    /// than 79 characters.
    ///
    /// By setting `truncate` to `false`, the full strings are written to the
    /// LOCUS line, resulting in fields that may not in usual positions if
    /// the name or molecule types are too long. Most parsers (including `gb-io`
    /// and Biopython) should however be able to process these.
    pub fn truncate_locus(&mut self, truncate: bool) -> &mut Self {
        self.truncate_locus = truncate;
        self
    }

    /// Set the behaviour regarding locus name escaping.
    ///
    /// If `true` (the default when creating a `SeqWriter`, for backwards
    /// compatibility), any whitespace in the locus will be escaped with
    /// and underscore (_).
    ///
    /// Set to `false` to write the locus as-is, which could result in an
    /// invalid LOCUS line that some parsers may not be able to process.
    pub fn escape_locus(&mut self, escape: bool) -> &mut Self {
        self.escape_locus = escape;
        self
    }

    /// Generate the locus line for the record.
    ///
    /// Ported from Biopython (InsdcIO.py).
    fn locus_line(&self, record: &Seq) -> String {
        let mut locus = record.name.clone().unwrap_or_else(|| {
            record
                .accession
                .clone()
                .unwrap_or_else(|| "UNTITLED".into())
        });
        let length = format!("{}", record.len());
        if self.truncate_locus && locus.len() + 1 + length.len() > 28 {
            locus = locus[..27 - length.len()].into();
        }

        if self.escape_locus && locus.split_whitespace().count() > 1 {
            locus = locus.split_whitespace().join("_");
        }

        let units = "bp"; // TODO: 'aa' support

        let mol_type = match record.molecule_type {
            Some(ref m) if m.len() > 7 && self.truncate_locus => "",
            Some(ref m) => m,
            None => ""
        };

        if locus.len() + 1 + length.len() >= 28 {
            let length = format!(" {}", length);
            locus.push_str(&length);
        } else {
            let length = format!("{:>28}", length);
            let rest = &length[locus.len()..];
            locus.push_str(rest);
        }

        format!(
            "LOCUS       {} {}    {:<7} {:<8} {} {}\n",
            locus,
            units,
            mol_type,
            record.topology,
            record.division,
            record
                .date
                .as_ref()
                .unwrap_or(&Date::from_ymd(1970, 1, 1).unwrap())
        )
    }

    /// Write the sequence to the stream.
    pub fn write(&mut self, record: &Seq) -> io::Result<()> {
        // LOCUS

        let locus_line = self.locus_line(record);
        write!(&mut self.stream, "{}", locus_line)?;

        // Fields

        write_field_maybe(&mut self.stream, &record.definition, "DEFINITION")?;
        write_field_maybe(&mut self.stream, &record.accession, "ACCESSION")?;
        write_field_maybe(&mut self.stream, &record.version, "VERSION")?;
        write_field_maybe(&mut self.stream, &record.dblink, "DBLINK")?;
        write_field_maybe(&mut self.stream, &record.keywords, "KEYWORDS")?;
        if let Some(ref source) = record.source {
            write_field(&mut self.stream, &source.source, "SOURCE")?;
            write_field_maybe(&mut self.stream, &source.organism, "  ORGANISM")?;
        }
        for r in &record.references {
            write_field(&mut self.stream, &r.description, "REFERENCE")?;
            for a in &r.authors {
                write_field(&mut self.stream, a, "  AUTHORS")?;
            }
            write_field_maybe(&mut self.stream, &r.consortium, "  CONSRTM")?;
            write_field(&mut self.stream, &r.title, "  TITLE")?;
            write_field_maybe(&mut self.stream, &r.journal, "  JOURNAL")?;
            write_field_maybe(&mut self.stream, &r.pubmed, "   PUBMED")?; // TODO: this should be a subfield of Journal
            write_field_maybe(&mut self.stream, &r.remark, "  REMARK")?;
        }
        for comment in &record.comments {
            write_field(&mut self.stream, comment, "COMMENT")?;
        }

        // Features

        if !record.features.is_empty() {
            self.stream.write_all(b"FEATURES             Location/Qualifiers\n")?;
            for f in &record.features {
                let first_indent = format!("     {:<15} ", f.kind);
                let location = f.location.to_gb_format();
                wrap_location(
                    &mut self.stream,
                    &location,
                    MAX_WIDTH,
                    first_indent.as_str(),
                    QUALIFIER_INDENT,
                )?;
                for &(ref key, ref val) in &f.qualifiers {
                    match *val {
                        None => writeln!(&mut self.stream, "{}/{}", QUALIFIER_INDENT, key)?,
                        Some(ref val) => {
                            let quote = !FTQUAL_NO_QUOTE.iter().any(|x| x == key);
                            let first_indent = format!("{}/{}=", QUALIFIER_INDENT, key);
                            if POS_QUAL.iter().any(|x| x == key) {
                                wrap_location(
                                    &mut self.stream,
                                    val,
                                    MAX_WIDTH,
                                    first_indent.as_str(),
                                    QUALIFIER_INDENT,
                                )?;
                            } else {
                                wrap_text(
                                    &mut self.stream,
                                    val,
                                    MAX_WIDTH,
                                    first_indent.as_str(),
                                    QUALIFIER_INDENT,
                                    quote,
                                )?;
                            }
                        }
                    }
                }
            }
        }

        // CONTIG, maybe

        if let Some(ref contig) = record.contig {
            wrap_location(
                &mut self.stream,
                &contig.to_gb_format(),
                MAX_WIDTH,
                "CONTIG      ",
                FIELD_INDENT,
            )?;
        }

        // ORIGIN

        if !record.seq.is_empty() {
            let mut line = Vec::with_capacity(79);
            write!(&mut line, "ORIGIN      ")?;
            for (i, &b) in record.seq.iter().enumerate() {
                if i % 60 == 0 {
                    line.push(b'\n');
                    self.stream.write_all(&line)?;
                    line.clear();
                    write!(&mut line, "{:>9}", i + 1)?;
                }
                if i % 10 == 0 {
                    line.push(b' ');
                }
                line.push(b);
            }
            line.push(b'\n');
            self.stream.write_all(&line)?;
        }
        writeln!(&mut self.stream, "//")?;
        Ok(())
    }
}

pub fn write<T: Write>(file: T, record: &Seq) -> io::Result<()> {
    SeqWriter::new(file).write(record)
}

fn write_field<T>(mut file: T, field: &str, keyword: &str) -> io::Result<()>
where
    T: Write,
{
    let keyword = format!("{:<12}", keyword);
    wrap_text(
        &mut file,
        field,
        MAX_WIDTH,
        keyword.as_str(),
        FIELD_INDENT,
        false,
    )
}

fn write_field_maybe<T, U>(mut file: T, field: &Option<U>, keyword: &str) -> io::Result<()>
where
    T: Write,
    U: AsRef<str>,
{
    if let Some(ref field) = *field {
        write_field(&mut file, field.as_ref(), keyword)?;
    };
    Ok(())
}

/// Wrap a genbank location, splitting on commas if possible
fn wrap_location<T: Write>(
    mut file: T,
    mut text: &str,
    max_width: usize,
    first_indent: &str,
    subsequent_ident: &str,
) -> io::Result<()> {
    let mut indent = first_indent;
    while indent.len() + text.len() > max_width {
        let split_at = if let Some(location) = text[..max_width - indent.len()].rfind(',') {
            location + 1
        } else {
            warn!("Couldn't split location appropriately!");
            max_width - indent.len()
        };
        writeln!(file, "{}{}", indent, &text[..split_at])?;
        text = &text[split_at..];
        indent = subsequent_ident;
    }
    if !text.is_empty() {
        writeln!(file, "{}{}", indent, text)?;
    }
    Ok(())
}

/// Wrap a line of text in a Genbank file. The line is appended to `line`, which
/// may already contain up to `max_len` - 1 bytes. Wrapping occurs on a space if
/// possible. Outputs valid UTF-8, however lines are wrapped to their length in
/// bytes, not characters, and UTF-8 characters that consist of multiple `char`s
/// are likely to get mangled. Returns the rest of `input`. If `quote` is set,
/// `"` will be escaped with `""`
fn wrap_get_line<'a>(line: &mut String, input: &'a str, max_len: usize, quote: bool) -> &'a str {
    assert!(line.len() < max_len);
    let mut consumed = 0;
    let mut last_space_in = None;
    let mut last_space_out = 0; // need to keep track of this separately because
                                // of escape sequences
    let mut i = input.char_indices();
    while line.len() < max_len {
        if let Some((idx, ch)) = i.next() {
            match ch {
                ' ' => {
                    last_space_in = Some(idx);
                    last_space_out = line.len();
                    line.push(' ');
                }
                '\n' => {
                    return &input[idx + 1..];
                }
                '"' if quote => {
                    // if we're one character away from the end of the line,
                    // wrap now to avoid splitting the escape sequence
                    if line.len() >= max_len - 1 {
                        break;
                    } else {
                        line.push_str("\"\"");
                    }
                }
                _ => {
                    line.push(ch);
                }
            }
            consumed = idx + ch.len_utf8();
        } else {
            // end of input
            return &input[consumed..];
        }
    }
    // So it's time to split the line. First check if it's really necessary,
    // maybe the next character is a newline or space or the end
    if let Some((idx_next, next)) = i.next() {
        if next == ' ' || next == '\n' {
            return &input[idx_next + 1..];
        }
    } else {
        // it's the end, no need to wrap
        assert!(consumed == input.len());
        return &input[consumed..];
    }
    // try to wrap at last space
    if let Some(last_space_in) = last_space_in {
        line.truncate(last_space_out);
        &input[last_space_in + 1..]
    } else {
        // there's no way to split the line nicely
        &input[consumed..]
    }
}

fn wrap_text<T: Write>(
    mut file: T,
    mut text: &str,
    max_width: usize,
    first_indent: &str,
    subsequent_indent: &str,
    quote: bool,
) -> io::Result<()> {
    let mut line = String::with_capacity(max_width);
    let mut indent = first_indent;
    if quote {
        line.push('"');
    }
    text = wrap_get_line(&mut line, text, max_width - indent.len(), quote);
    write!(file, "{}{}", indent, line)?;
    while !text.is_empty() {
        indent = subsequent_indent;
        line.clear();
        text = wrap_get_line(&mut line, text, max_width - subsequent_indent.len(), quote);
        write!(file, "\n{}{}", subsequent_indent, line)?;
    }
    if quote {
        if line.len() + indent.len() >= max_width {
            writeln!(file, "\n{}\"", subsequent_indent)?;
        } else {
            writeln!(file, "\"")?;
        }
    } else {
        writeln!(file)?;
    }
    Ok(())
}

#[cfg(test)]
pub mod tests {

    use super::*;
    use std::io::BufRead;

    #[test]
    fn truncate_locus() {
        let mut seq = Seq::empty();
        seq.name = Some(String::from("1122217.SAMN02441331.KB899611_14"));

        let mut out = Vec::new();
        SeqWriter::new(&mut out).truncate_locus(true).write(&seq).unwrap();

        let mut line = String::new();
        std::io::Cursor::new(&out).read_line(&mut line).unwrap();
        assert_eq!(
            line,
            "LOCUS       1122217.SAMN02441331.KB899 0 bp            linear UNK 01-JAN-1970\n"
        );
    }

    #[test]
    fn no_truncate_locus() {
        let mut seq = Seq::empty();
        seq.name = Some(String::from("1122217.SAMN02441331.KB899611_14"));

        let mut out = Vec::new();
        SeqWriter::new(&mut out).truncate_locus(false).write(&seq).unwrap();

        let mut line = String::new();
        std::io::Cursor::new(&out).read_line(&mut line).unwrap();
        assert_eq!(
            line,
            "LOCUS       1122217.SAMN02441331.KB899611_14 0 bp            linear UNK 01-JAN-1970\n"
        );
    }
}