2桁で1000個ぐらいのものを扱いたいという場合、34進数を使ってみるのは如何という話
def dec_to_base33(num):
characters = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ" # IとOを除外
if num < 0 or num >= 33**2:
raise ValueError("Number out of range (must be between 0 and 1088).")
return characters[num // 33] + characters[num % 33]
def base33_to_dec(s):
characters = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"
if len(s) != 2 or any(c not in characters for c in s):
raise ValueError("Invalid base33 number.")
return characters.index(s[0]) * 33 + characters.index(s[1])
def sort_base33(lst):
return sorted(lst, key=base33_to_dec)
# 使用例
number = 123 # 10進数の数値
base33_result = dec_to_base33(number)
print(f"{number} in decimal is {base33_result} in base 33.")
# 33進数を10進数に戻す
converted_back = base33_to_dec(base33_result)
print(f"{base33_result} in base 33 is {converted_back} in decimal.")
# 33進数のリストをソート
base33_numbers = ["02", "10", "ZZ", "AB", "01"]
sorted_base33 = sort_base33(base33_numbers)
print(f"Sorted: {sorted_base33}")
def base33_to_dec(s):
characters = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ" # IとOを除外
return characters.index(s[0]) * 33 + characters.index(s[1])
def sort_and_renumber_hetatm_lines(pdb_file_path):
hetatm_lines = []
with open(pdb_file_path, 'r') as file:
for line in file:
if line.startswith("HETATM"):
hetatm_lines.append(line)
# 14-15列目に基づいてソート
hetatm_lines.sort(key=lambda x: base33_to_dec(x[13:15]))
# 新しい原子番号を割り当て
renumbered_lines = []
for i, line in enumerate(hetatm_lines, start=1):
# 新しい原子番号を設定(7-11列目)
new_line = line[:6] + f"{i:5d}" + line[11:]
renumbered_lines.append(new_line)
return renumbered_lines
# PDBファイルのパスを指定してください
pdb_file_path = 'path/to/your/pdb_file.pdb'
# ソートされ、原子番号が再割り当てされたHETATM行を取得
renumbered_hetatm_lines = sort_and_renumber_hetatm_lines(pdb_file_path)
# 結果を表示(または必要に応じてファイルに保存)
for line in renumbered_hetatm_lines:
print(line, end='')
コメント