dinopy.sam_writer module

class dinopy.sam_writer.SamWriter(fp, append=False, force_overwrite=False)

Class for writing AlignmentRecord to a SAM-file.

Examples

Writing records from a list:

from dinopy.sambam import AlignmentRecord
from dinopy import SamWriter
records = [AlignmentRecord.fromvalues('r003', 0, 'ref', 9, 30, '5S6M', '*', 0, 0, 'GCCTAAGCTAA', '*', {'SA': 'ref,29,-,6H5M,17,0;'}),
           AlignmentRecord.fromvalues('r004', 0, 'ref', 16, 30, '6M14N5M', '*', 0, 0, 'ATAGCTTCAGC', '*', None) ]
with dinopy.SamWriter("somefile.sam") as saw:
    saw.write_records(records)

Writing a SAM file header generated from reference names and lengths (similar to pysam):

from dinopy import SamWriter
with dinopy.SamWriter("somefile.sam") as saw:
    saw.write_sq_header(reference_names, reference_lengths)

Writing a custom SAM file header:

from dinopy import SamWriter lines = [(‘HD’, {‘VN’: 1.5, ‘SO’: ‘coordinate’}), (@SQ’, {‘SN’: ‘ref’, ‘LN’: 45})] with dinopy.SamWriter(“somefile.sam”) as saw:

saw.write_header([(‘HD’, {‘VN’: 1.5, ‘SO’: ‘coordinate’}),

(‘SQ’, {‘SN’: ‘ref1’, ‘LN’: 45}), (‘SQ’, {‘SN’: ‘ref2’, ‘LN’: 42})

]

)

close(self)

Close the file (after writing).

Note

This should only be used if the exact number of files is not known at development time. Otherwise the use of the environment is encouraged, as it is much harder to ‘forget’ closing an opened file.

create_header_sq_lines(self, list reference_names, list reference_lengths) list
Parameters:
  • reference_names – A list of reference names

  • reference_lengths – A list of reference lengths

Returns:

a list of ('SQ', {'SN': refname, 'LN': reflen}) tuples to be used in conjunction with write_header

write_header(self, list lines) void

Writes the given header lines to the SAM file. Does not perform any validation.

Parameters:

lines (list) – a list of (str, dict) tuples

Examples

  1. Write some header lines:

    from dinopy import SamWriter
    lines = [('HD', {'VN': 1.5, 'SO': 'coordinate'}), ('@SQ', {'SN': 'ref', 'LN': 45})]
    with dinopy.SamWriter("somefile.sam") as saw:
        saw.write_header(lines)
    
write_header_line(self, unicode tag, dict items) void

Writes a single header line to the SAM file. Does not perform any validation.

Parameters:
  • tag (str) – the two character tag, with or without leading ‘@’.

  • items (dict) – a dictionary containing the actual information. For example, for a tag of @HD’, items could be {'VN': 1.5, 'SO': 'coordinate'}.

Examples

  1. Write a single header line:

    from dinopy import SamWriter
    with dinopy.SamWriter("somefile.sam") as saw:
        saw.write_header_line('HD', {'VN': 1.5, 'SO': 'coordinate'})
    
  2. Write a single header line using tuple unpacking:

    from dinopy import SamWriter
    with dinopy.SamWriter("somefile.sam") as saw:
        saw.write_header_line(*('@SQ', {'SN': 'ref', 'LN': 45}))
    
write_record(self, AlignmentRecord ar) void

Writes a single AlignmentRecord to the output sink.

Parameters:

ar (AlignmentRecord) – AlignmentRecord that is to be written to the output.

Examples

  1. Write a single record:

    from dinopy import SamWriter
    ar = AlignmentRecord.fromstr("r003      2064    ref     29      17      6H5M    *       0       0       TAGGC   *       SA:Z:ref,9,+,5S6M,30,1;")
    with dinopy.SamWriter("somefile.sam") as saw:
        saw.write_record(ar)
    
write_records(self, c) void

Writes a collection of AlignmentRecord to the output sink.

Parameters:

c – a collection or iterable of AlignmentRecord.

Examples

  1. Write some records:

    from dinopy import SamWriter
    records = [AlignmentRecord.fromvalues('r004', 0, 'ref', 16, 30, '6M14N5M', '*', 0, 0, 'ATAGCTTCAGC', '*', None),
               AlignmentRecord.fromstr("r003        2064    ref     29      17      6H5M    *       0       0       TAGGC   *       SA:Z:ref,9,+,5S6M,30,1;"),
               AlignmentRecord.fromstr("r001        147     ref     37      30      9M      =       7       -39     CAGCGGCAT       *       NM:i:1"), ]
    with dinopy.SamWriter("somefile.sam") as saw:
        saw.write_records(records)
    
write_sq_header(self, list reference_names, list reference_lengths) void

Same as write_header(create_header_sq_lines(reference_names, reference_lengths)).

Parameters:
  • reference_names – A list of reference names

  • reference_lengths – A list of reference lengths