original_docstring: Write a complete alignment to a MAF block.
docstring: Write a complete alignment to a MAF block.
docstring_tokens: Write a complete alignment to a MAF block .
code: def write_alignment(self, alignment):
          """Write a complete alignment to a MAF block."""
          if not isinstance(alignment, Alignment):
              raise TypeError("Expected an Alignment object")
          try:
              alignment_annotations = alignment.annotations
          except AttributeError:
              alignment_annotations = None
          self._write_score_line(alignment, alignment_annotations)
          name_width = 0
          start_width = 0
          size_width = 0
          length_width = 0
          stream = self.stream
          n = len(alignment.sequences)
          for i in range(n):
              record = alignment.sequences[i]
              coordinates = alignment.coordinates[i]
              name = record.id
              start = coordinates[0]
              end = coordinates[-1]
              length = len(record.seq)
              if start < end:
                  size = end - start
              else:
                  size = start - end
                  start = length - start
              name_width = max(name_width, len(name))
              start_width = max(start_width, len(str(start)))
              size_width = max(size_width, len(str(size)))
              length_width = max(length_width, len(str(length)))
          for empty in alignment_annotations.get("empty", []):
              record, segment, status = empty
              name = record.id
              name_width = max(name_width, len(name))
              start, end = segment
              length = len(record.seq)
              if start <= end:
                  size = end - start
              else:
                  size = start - end
                  start = length - start
              start_width = max(start_width, len(str(start)))
              size_width = max(size_width, len(str(size)))
              length_width = max(length_width, len(str(length)))
          quality_width = name_width + start_width + size_width + length_width + 5
          for i in range(n):
              record = alignment.sequences[i]
              coordinates = alignment.coordinates[i]
              name = record.id
              start = coordinates[0]
              end = coordinates[-1]
              length = len(record.seq)
              if start < end:
                  size = end - start
                  strand = "+"
              else:
                  size = start - end
                  start = length - start
                  strand = "-"
              text = alignment[i]
              name = record.id.ljust(name_width)
              start = str(start).rjust(start_width)
              size = str(size).rjust(size_width)
              length = str(length).rjust(length_width)
              line = f"s {name} {start} {size} {strand} {length} {text}\n"
              stream.write(line)
              try:
                  annotations = record.annotations
              except AttributeError:
                  annotations = None
              if annotations is not None:
                  quality = annotations.get("quality")
                  if quality is not None:
                      gapped_quality = ""
                      i = 0
                      for letter in text:
                          if letter == "-":
                              gapped_quality += "-"
                          else:
                              gapped_quality += quality[i]
                              i += 1
                      name = record.id.ljust(quality_width)
                      line = f"q {name} {gapped_quality}\n"
                      stream.write(line)
                  try:
                      leftStatus = annotations["leftStatus"]
                      leftCount = annotations["leftCount"]
                      rightStatus = annotations["rightStatus"]
                      rightCount = annotations["rightCount"]
                  except KeyError:
                      pass
                  else:
                      name = record.id.ljust(name_width)
                      line = f"i {name} {leftStatus} {leftCount} {rightStatus} {rightCount}\n"
                      stream.write(line)
          for empty in alignment_annotations.get("empty", []):
              record, segment, status = empty
              name = record.id.ljust(name_width)
              start, end = segment
              length = len(record.seq)
              if start <= end:
                  size = end - start
                  strand = "+"
              else:
                  size = start - end
                  start = length - start
                  strand = "-"
              start = str(start).rjust(start_width)
              size = str(size).rjust(size_width)
              length = str(length).rjust(length_width)
              line = f"e {name} {start} {size} {strand} {length} {status}\n"
              stream.write(line)
          stream.write("\n")
code_tokens: def write_alignment ( self , alignment ) : if not isinstance ( alignment , Alignment ) : raise TypeError ( "Expected an Alignment object" ) try : alignment_annotations = alignment . annotations except AttributeError : alignment_annotations = None self . _write_score_line ( alignment , alignment_annotations ) name_width = 0 start_width = 0 size_width = 0 length_width = 0 stream = self . stream n = len ( alignment . sequences ) for i in range ( n ) : record = alignment . sequences [ i ] coordinates = alignment . coordinates [ i ] name = record . id start = coordinates [ 0 ] end = coordinates [ - 1 ] length = len ( record . seq ) if start < end : size = end - start else : size = start - end start = length - start name_width = max ( name_width , len ( name ) ) start_width = max ( start_width , len ( str ( start ) ) ) size_width = max ( size_width , len ( str ( size ) ) ) length_width = max ( length_width , len ( str ( length ) ) ) for empty in alignment_annotations . get ( "empty" , [ ] ) : record , segment , status = empty name = record . id name_width = max ( name_width , len ( name ) ) start , end = segment length = len ( record . seq ) if start <= end : size = end - start else : size = start - end start = length - start start_width = max ( start_width , len ( str ( start ) ) ) size_width = max ( size_width , len ( str ( size ) ) ) length_width = max ( length_width , len ( str ( length ) ) ) quality_width = name_width + start_width + size_width + length_width + 5 for i in range ( n ) : record = alignment . sequences [ i ] coordinates = alignment . coordinates [ i ] name = record . id start = coordinates [ 0 ] end = coordinates [ - 1 ] length = len ( record . seq ) if start < end : size = end - start strand = "+" else : size = start - end start = length - start strand = "-" text = alignment [ i ] name = record . id . ljust ( name_width ) start = str ( start ) . rjust ( start_width ) size = str ( size ) . rjust ( size_width ) length = str ( length ) . rjust ( length_width ) line = f"s {name} {start} {size} {strand} {length} {text}\n" stream . write ( line ) try : annotations = record . annotations except AttributeError : annotations = None if annotations is not None : quality = annotations . get ( "quality" ) if quality is not None : gapped_quality = "" i = 0 for letter in text : if letter == "-" : gapped_quality += "-" else : gapped_quality += quality [ i ] i += 1 name = record . id . ljust ( quality_width ) line = f"q {name} {gapped_quality}\n" stream . write ( line ) try : leftStatus = annotations [ "leftStatus" ] leftCount = annotations [ "leftCount" ] rightStatus = annotations [ "rightStatus" ] rightCount = annotations [ "rightCount" ] except KeyError : pass else : name = record . id . ljust ( name_width ) line = f"i {name} {leftStatus} {leftCount} {rightStatus} {rightCount}\n" stream . write ( line ) for empty in alignment_annotations . get ( "empty" , [ ] ) : record , segment , status = empty name = record . id . ljust ( name_width ) start , end = segment length = len ( record . seq ) if start <= end : size = end - start strand = "+" else : size = start - end start = length - start strand = "-" start = str ( start ) . rjust ( start_width ) size = str ( size ) . rjust ( size_width ) length = str ( length ) . rjust ( length_width ) line = f"e {name} {start} {size} {strand} {length} {status}\n" stream . write ( line ) stream . write ( "\n" )
short_docstring: Write a complete alignment to a MAF block.
short_docstring_tokens: Write a complete alignment to a MAF block .
comment: """Write a complete alignment to a MAF block."""