text
stringlengths
0
267k
class EMBLConverter(CustomVisitor):
def __init__(self, locus_tag=None, translation_table=11):
CustomVisitor.__init__(self)
self.contigs = {}
self.locus_tag = locus_tag
self.translation_table = translation_table
def visit_feature_node(self, feature_node):
sequence_id = feature_node.get_seqid()
contig = self.contigs.get(sequence_id)
if contig: # contig already exists, just try and update it
contig.add_feature(sequence_id = sequence_id, feature_type = feature_node.get_type(), start = feature_node.get_start(),
end = feature_node.get_end(), strand = feature_node.get_strand(),
feature_attributes = feature_node.attribs,
locus_tag = self.locus_tag, translation_table = self.translation_table)
else:
contig = EMBLContig()
successfully_added_feature = contig.add_feature(sequence_id = sequence_id, feature_type = feature_node.get_type(), start = feature_node.get_start(),
end = feature_node.get_end(), strand = feature_node.get_strand(),
feature_attributes = feature_node.attribs,
locus_tag = self.locus_tag, translation_table = self.translation_table)
if successfully_added_feature:
self.contigs[sequence_id] = contig
else:
pass # discard the contig because we didn't add a feature so it is empty
def visit_region_node(self, region_node):
pass # for now
def visit_comment_node(self, comment_node):
pass # for now
def visit_sequence_node(self, sequence_node):
sequence_id = sequence_node.get_description()
contig = self.contigs.setdefault(sequence_id, EMBLContig())
contig.add_sequence(sequence_node.get_sequence())
# This file is part of GridCal.
#
# GridCal is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GridCal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GridCal. If not, see <http://www.gnu.org/licenses/>.
import numpy as np
def slice_to_range(sl: slice, n):
"""
Turn a slice into a range
:param sl: slice object
:param n: total number of items
:return: range object, if the slice is not supported an exception is raised
"""
if sl.start is None and sl.step is None and sl.start is None: # (:)
return range(n)
elif sl.start is not None and sl.step is None and sl.start is None: # (a:)
return range(sl.start, n)
elif sl.start is not None and sl.step is not None and sl.start is None: # (?)
raise Exception('Invalid slice')
elif sl.start is not None and sl.step is None and sl.start is not None: # (a:b)
return range(sl.start, sl.stop)
elif sl.start is not None and sl.step is not None and sl.start is not None: # (a:s:b)
return range(sl.start, sl.stop, sl.step)
elif sl.start is None and sl.step is None and sl.start is not None: # (:b)
return range(sl.stop)
else:
raise Exception('Invalid slice')
def dense_to_str(mat: np.ndarray):
"""
Turn dense 2D numpy array into a string
:param mat: 2D numpy array
:return: string
"""
rows, cols = mat.shape
val = "Matrix (" + ("%d" % rows) + " x " + ("%d" % cols) + ")\n"
val += str(mat).replace('. ', ' ').replace('[', ' ').replace(']', '').replace('0 ', '_ ').replace('0.', '_ ')
# for i in range(0, rows):
# for j in range(0, cols):
# x = mat[i, j]
# if x is not None:
# if x == 0:
# val += '{:<4}'.format(0)
# else: