@@ -43,7 +43,7 @@ def unAlign(arr):
4343 return (arr )
4444
4545
46- def FastaToArray (infile , outfile_stem = None ):
46+ def FastaToArray (infile , log , outfile_stem = None ):
4747 '''
4848 Convert an alignment into a numpy array.
4949
@@ -63,19 +63,26 @@ def FastaToArray(infile, outfile_stem=None):
6363 List of sequence names in the same order as in the input file
6464 '''
6565
66+ formatErrorMessage = "The MSA file needs to be in FASTA format."
6667 nams = []
6768 seqs = []
6869 nam = ""
6970 seq = ""
7071 with open (infile ) as input :
7172 for line in input :
7273 line = line .strip ()
74+ if len (line ) == 0 :
75+ continue # todo: test!
7376 if line [0 ] == ">" :
7477 seqs .append ([s .upper () for s in seq ])
7578 nams .append (nam )
7679 seq = []
7780 nam = line .replace (">" , "" )
7881 else :
82+ if len (nams ) == 0 :
83+ log .error (formatErrorMessage )
84+ print (formatErrorMessage )
85+ exit ()
7986 seq += list (line )
8087 seqs .append (np .array ([s .upper () for s in seq ]))
8188 nams .append (nam )
@@ -198,6 +205,7 @@ def seqType(arr):
198205 '''
199206 Detects if an alignment is of nucleotides or amino acids using pre-built
200207 dictionarys of amino acid and nucleotide codes.
208+ Checks if arr contains characters that are not in the dictionary (not IUPAC)
201209
202210 Parameters
203211 ----------
@@ -209,27 +217,36 @@ def seqType(arr):
209217 str
210218 'aa' for amino acid and 'nt for nucleotide
211219 '''
212- seq1 = arr [0 ]
213- nucs = set (list (getNtColours ().keys ()))
214- aas = set (list (getAAColours ().keys ()))
215- n = 0
216- a = 0
217- x = 0
218- for s in seq1 :
219- s = s .upper ()
220- if s in nucs :
221- n += 1
222- if s in aas :
223- a += 1
224- if s not in aas and s not in nucs :
225- x += 1
226- counts = n , a , x
227- if n == max (counts ):
220+ nt_count = 0
221+ aa_count = 0
222+ for seq in arr :
223+ nucs = set (list (getNtColours ().keys ()))
224+ aas = set (list (getAAColours ().keys ()))
225+ n = 0
226+ a = 0
227+ x = 0
228+ for s in seq :
229+ s = s .upper ()
230+ if s in nucs :
231+ n += 1
232+ if s in aas :
233+ a += 1
234+ if s not in aas and s not in nucs :
235+ x += 1
236+ counts = n , a , x
237+ if n == len (seq ):
238+ nt_count += 1
239+ elif a == len (seq ):
240+ aa_count += 1
241+ else :
242+ print ("Unknown nucleotides or amino acids detected. Please fix your MSA." )
243+ exit ()
244+ if nt_count == len (arr ):
228245 return "nt"
229- elif a == max ( counts ):
246+ elif aa_count == len ( arr ):
230247 return "aa"
231248 else :
232- print ("Majority of positions are not known nucleotides or amino acids " )
249+ print ("MSA type couldn't be established. Please fix your MSA. " )
233250 exit ()
234251
235252
0 commit comments