<s> import string <EOL> from io import StringIO <EOL> from unittest import TestCase , main <EOL> from skbio import TabularMSA <EOL> from skbio . sequence . _grammared_sequence import GrammaredSequence <EOL> from skbio . util . _decorator import classproperty , overrides <EOL> from skbio . io . format . clustal import ( <EOL> _clustal_to_tabular_msa , _tabular_msa_to_clustal , _clustal_sniffer , <EOL> _is_clustal_seq_line , _delete_trailing_number , _check_length , <EOL> _label_line_parser ) <EOL> from skbio . io import ClustalFormatError <EOL> class CustomSequence ( GrammaredSequence ) : <EOL> @ classproperty <EOL> @ overrides ( GrammaredSequence ) <EOL> def gap_chars ( cls ) : <EOL> return set ( '<STR_LIT>' ) <EOL> @ classproperty <EOL> @ overrides ( GrammaredSequence ) <EOL> def default_gap_char ( cls ) : <EOL> return '<STR_LIT:->' <EOL> @ classproperty <EOL> @ overrides ( GrammaredSequence ) <EOL> def definite_chars ( cls ) : <EOL> return set ( string . ascii_letters ) <EOL> @ classproperty <EOL> @ overrides ( GrammaredSequence ) <EOL> def degenerate_map ( cls ) : <EOL> return { } <EOL> class ClustalHelperTests ( TestCase ) : <EOL> def test_label_line_parser ( self ) : <EOL> self . assertEqual ( _label_line_parser ( StringIO ( '<STR_LIT>' ) ) , <EOL> ( { "<STR_LIT:abc>" : [ "<STR_LIT>" ] } , [ '<STR_LIT:abc>' ] ) ) <EOL> with self . assertRaises ( ClustalFormatError ) : <EOL> _label_line_parser ( StringIO ( '<STR_LIT>' ) ) <EOL> def test_is_clustal_seq_line ( self ) : <EOL> ic = _is_clustal_seq_line <EOL> self . assertTrue ( ic ( '<STR_LIT:abc>' ) ) <EOL> self . assertTrue ( ic ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( ic ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( ic ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( ic ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( ic ( '<STR_LIT>' ) ) <EOL> self . assertFalse ( ic ( '<STR_LIT>' ) ) <EOL> def test_delete_trailing_number ( self ) : <EOL> dtn = _delete_trailing_number <EOL> self . assertEqual ( dtn ( '<STR_LIT:abc>' ) , '<STR_LIT:abc>' ) <EOL> self . assertEqual ( dtn ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( dtn ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( dtn ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( dtn ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_check_lengh ( self ) : <EOL> self . assertEqual ( False , <EOL> _check_length ( { '<STR_LIT:abc>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> [ '<STR_LIT:abc>' , '<STR_LIT>' ] ) ) , <EOL> self . assertEqual ( True , <EOL> _check_length ( { '<STR_LIT:abc>' : [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] } , <EOL> [ '<STR_LIT:abc>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( True , <EOL> _check_length ( { '<STR_LIT:abc>' : [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] } , <EOL> [ '<STR_LIT:abc>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( False , <EOL> _check_length ( { '<STR_LIT:abc>' : [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] } , <EOL> [ '<STR_LIT:abc>' , '<STR_LIT>' ] ) ) <EOL> self . assertEqual ( False , <EOL> _check_length ( { '<STR_LIT:abc>' : [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] } , <EOL> [ '<STR_LIT:abc>' , '<STR_LIT>' ] ) ) <EOL> class ClustalIOTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . valid_clustal_out = [ <EOL> StringIO ( '<STR_LIT>' ) , <EOL> StringIO ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> StringIO ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) , <EOL> StringIO ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) ) , <EOL> StringIO ( """<STR_LIT>""" ) , <EOL> StringIO ( """<STR_LIT>""" ) , <EOL> StringIO ( """<STR_LIT>""" ) , <EOL> StringIO ( """<STR_LIT>""" ) <EOL> ] <EOL> self . invalid_clustal_out = [ StringIO ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) , <EOL> StringIO ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' ] ) ) , <EOL> StringIO ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) , <EOL> StringIO ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT:\t>' , <EOL> '<STR_LIT>' ] ) ) , <EOL> StringIO ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) , <EOL> StringIO ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) ) , <EOL> StringIO ( """<STR_LIT>""" ) , <EOL> StringIO ( """<STR_LIT>""" ) , <EOL> StringIO ( """<STR_LIT>""" ) , <EOL> StringIO ( """<STR_LIT>""" ) , <EOL> StringIO ( """<STR_LIT>""" ) ] <EOL> def test_tabular_msa_to_clustal_with_empty_input ( self ) : <EOL> result = _clustal_to_tabular_msa ( StringIO ( ) , <EOL> constructor = CustomSequence ) <EOL> self . assertEqual ( dict ( result ) , { } ) <EOL> def test_tabular_msa_to_clustal_with_bad_input ( self ) : <EOL> BAD = StringIO ( '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , '<STR_LIT>' ] ) ) <EOL> with self . assertRaises ( ClustalFormatError ) : <EOL> dict ( _clustal_to_tabular_msa ( BAD , constructor = CustomSequence ) ) <EOL> def test_valid_tabular_msa_to_clustal_and_clustal_to_tabular_msa ( self ) : <EOL> for valid_out in self . valid_clustal_out : <EOL> result_before = _clustal_to_tabular_msa ( <EOL> valid_out , constructor = CustomSequence ) <EOL> with StringIO ( ) as fh : <EOL> _tabular_msa_to_clustal ( result_before , fh ) <EOL> fh . seek ( <NUM_LIT:0> ) <EOL> result_after = _clustal_to_tabular_msa ( <EOL> fh , constructor = CustomSequence ) <EOL> self . assertEqual ( result_before , result_after ) <EOL> def test_invalid_tabular_msa_to_clustal_and_clustal_to_tabular_msa ( self ) : <EOL> for invalid_out in self . invalid_clustal_out : <EOL> with self . assertRaises ( ClustalFormatError ) : <EOL> dict ( _clustal_to_tabular_msa ( invalid_out , <EOL> constructor = CustomSequence ) ) <EOL> def test_clustal_sniffer_valid_files ( self ) : <EOL> for valid_out in self . valid_clustal_out : <EOL> self . assertEqual ( _clustal_sniffer ( valid_out ) , ( True , { } ) ) <EOL> def test_clustal_sniffer_invalid_files ( self ) : <EOL> for invalid_out in self . invalid_clustal_out : <EOL> self . assertEqual ( _clustal_sniffer ( invalid_out ) , ( False , { } ) ) <EOL> self . assertEqual ( _clustal_sniffer ( StringIO ( ) ) , ( False , { } ) ) <EOL> def test_no_constructor ( self ) : <EOL> with self . assertRaisesRegex ( ValueError , "<STR_LIT>" ) : <EOL> _clustal_to_tabular_msa ( self . valid_clustal_out [ <NUM_LIT:0> ] ) <EOL> def test_duplicate_labels ( self ) : <EOL> msa = TabularMSA ( [ CustomSequence ( '<STR_LIT:foo>' ) , <EOL> CustomSequence ( '<STR_LIT:bar>' ) ] , index = [ '<STR_LIT:a>' , '<STR_LIT:a>' ] ) <EOL> with self . assertRaisesRegex ( ClustalFormatError , "<STR_LIT>" ) : <EOL> with StringIO ( ) as fh : <EOL> _tabular_msa_to_clustal ( msa , fh ) <EOL> def test_invalid_lengths ( self ) : <EOL> fh = StringIO ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> with self . assertRaisesRegex ( ClustalFormatError , "<STR_LIT>" ) : <EOL> _clustal_to_tabular_msa ( fh , constructor = CustomSequence ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
