0

I have a list of PDB files in a text file appearing like this:

3O8BA 2WHXA 3Q3YA 1D4M1 4F49A 1JQ6A 2FYQA 3W95A 2FMBA 2BBVA 4M0WA 1AT3A 2CXVA 2WV4A 4EKFA 2SNWA 4GUAA 1JEW2 3MMGA 4IZKA

I would like to create a new file with the PDB files ordered by PDBID, something like this

1AT3A 1D4M1 1JEW2 1JQ6A ...

I had started reading the text in the file and using the split method in order to create a temporary list of PDBs to be ordered, but then I got stuck on that and I do not how to order them and proceed.

Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
Silvia
  • 11
  • 1

1 Answers1

2

You can use sorted()

>>> s = '''3O8BA 2WHXA 3Q3YA 1D4M1 4F49A 1JQ6A 2FYQA 3W95A 2FMBA 2BBVA 4M0WA 1AT3A 2CXVA 2WV4A 4EKFA 2SNWA 4GUAA 1JEW2 3MMGA 4IZKA'''
>>> sorted(s.split())
['1AT3A', '1D4M1', '1JEW2', '1JQ6A', '2BBVA', '2CXVA', '2FMBA', '2FYQA', '2SNWA', '2WHXA', '2WV4A', '3MMGA', '3O8BA', '3Q3YA', '3W95A', '4EKFA', '4F49A', '4GUAA', '4IZKA', '4M0WA']
>>> ' '.join(sorted(s.split()))
'1AT3A 1D4M1 1JEW2 1JQ6A 2BBVA 2CXVA 2FMBA 2FYQA 2SNWA 2WHXA 2WV4A 3MMGA 3O8BA 3Q3YA 3W95A 4EKFA 4F49A 4GUAA 4IZKA 4M0WA'
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42
  • Thank you very much. I tried to do it in the command line and it works.Can I ask how to do if I want this list sorted in a new file from the old file containing the list not sorted? – Silvia Jan 29 '15 at 10:37