Author: bugman Date: Thu Apr 4 15:14:11 2013 New Revision: 19354 URL: http://svn.gna.org/viewcvs/relax?rev=19354&view=rev Log: Created the lib.software.sparky.write_list() function and associated unit test. This will be used to create simple Sparky .list files. Added: trunk/test_suite/unit_tests/_lib/_software/test_sparky.py Modified: trunk/lib/software/sparky.py Modified: trunk/lib/software/sparky.py URL: http://svn.gna.org/viewcvs/relax/trunk/lib/software/sparky.py?rev=19354&r1=19353&r2=19354&view=diff ============================================================================== --- trunk/lib/software/sparky.py (original) +++ trunk/lib/software/sparky.py Thu Apr 4 15:14:11 2013 @@ -107,3 +107,72 @@ # Return the data. return data + + +def write_list(file_prefix=None, dir=None, res_names=None, res_nums=None, atom1_names=None, atom2_names=None, w1=None, w2=None, data_height=None, force=True): + """Create a Sparky .list file. + + @keyword file_prefix: The base part of the file name without the .list extension. + @type file_prefix: str + @keyword dir: The directory to place the file in. + @type dir: str or None + @keyword res_names: The residue name list for each peak entry. + @type res_names: list of str + @keyword res_nums: The residue number list for each peak entry. + @type res_nums: list of int + @keyword atom1_names: The atom name list for w1 for each peak entry. + @type atom1_names: list of str + @keyword atom2_names: The atom name list for w2 for each peak entry. + @type atom2_names: list of str + @keyword w1: The w1 chemical shift list in ppm for each peak entry. + @type w1: list of float + @keyword w2: The w2 chemical shift list in ppm for each peak entry. + @type w2: list of float + @keyword data_height: The optional data height list for each peak entry. + @type data_height: None or list of float + @keyword force: A flag which if True will cause any pre-existing files to be overwritten. + @type force: bool + """ + + # Checks. + N = len(w1) + if len(res_names) != N: + raise RelaxError("The %s residue names does not match the %s number of entries." % (len(res_names), N)) + if len(res_nums) != N: + raise RelaxError("The %s residue numbers does not match the %s number of entries." % (len(res_nums), N)) + if len(atom1_names) != N: + raise RelaxError("The %s w1 atom names does not match the %s number of entries." % (len(atom1_names), N)) + if len(atom2_names) != N: + raise RelaxError("The %s w2 atom names does not match the %s number of entries." % (len(atom2_names), N)) + if len(w1) != N: + raise RelaxError("The %s w1 chemical shifts does not match the %s number of entries." % (len(w1), N)) + if len(w2) != N: + raise RelaxError("The %s w2 chemical shifts does not match the %s number of entries." % (len(w2), N)) + if data_height and len(data_height) != N: + raise RelaxError("The %s data heights does not match the %s number of entries." % (len(data_height), N)) + + # Printout. + print("Creating the Sparky list file.") + + # Open the file. + if isinstance(file_prefix, str): + file = open_write_file(file_name=file_prefix+".list", dir=dir, force=force) + else: + file = file_prefix + + # The header. + file.write("%17s %10s %10s" % ("Assignment ", "w1 ", "w2 ")) + if data_height != None: + file.write(" %12s" % "Data Height") + file.write("\n\n") + + # The data. + for i in range(N): + # Generate the assignment. + assign = "%s%i%s-%s" % (res_names[i], res_nums[i], atom1_names[i], atom2_names[i]) + + # Write out the line. + file.write("%17s %10.3f %10.3f" % (assign, w1[i], w2[i])) + if data_height != None: + file.write(" %12i" % data_height[i]) + file.write("\n") Added: trunk/test_suite/unit_tests/_lib/_software/test_sparky.py URL: http://svn.gna.org/viewcvs/relax/trunk/test_suite/unit_tests/_lib/_software/test_sparky.py?rev=19354&view=auto ============================================================================== --- trunk/test_suite/unit_tests/_lib/_software/test_sparky.py (added) +++ trunk/test_suite/unit_tests/_lib/_software/test_sparky.py Thu Apr 4 15:14:11 2013 @@ -1,0 +1,69 @@ +############################################################################### +# # +# Copyright (C) 2013 Edward d'Auvergne # +# # +# This file is part of the program relax (http://www.nmr-relax.com). # +# # +# This program 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. # +# # +# This program 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 this program. If not, see <http://www.gnu.org/licenses/>. # +# # +############################################################################### + +# Python module imports. +from numpy import array, eye, float64, zeros +from sys import stdout +from unittest import TestCase + +# relax module imports. +from lib.io import DummyFileObject +from lib.software.sparky import write_list + + +class Test_sparky(TestCase): + """Unit tests for the lib.software.sparky relax module.""" + + def test_write_list(self): + """Test the lib.software.sparky.write_list() function.""" + + # The data. + res_names = ['LEU', 'GLY', 'SER', 'MET', 'TRP', 'TRP', 'ASN'] + res_nums = [3, 4, 5, 6, 40, 40, 55] + atom1_names = ['N', 'N', 'N', 'N', 'N', 'NE1', 'N'] + atom2_names = ['HN', 'HN', 'HN', 'HN', 'HN', 'HE1', 'HN'] + w1 = [122.454, 111.978, 115.069, 120.910, 123.335, 130.204, 116.896] + w2 = [8.397, 8.720, 8.177, 8.813, 8.005, 10.294, 7.468] + heights = [2535, 5050, 51643, 53663, -65111, -181131, -105322] + + # The result. + file_data = [ + ' Assignment w1 w2 Data Height\n', + '\n', + ' LEU3N-HN 122.454 8.397 2535\n', + ' GLY4N-HN 111.978 8.720 5050\n', + ' SER5N-HN 115.069 8.177 51643\n', + ' MET6N-HN 120.910 8.813 53663\n', + ' TRP40N-HN 123.335 8.005 -65111\n', + ' TRP40NE1-HE1 130.204 10.294 -181131\n', + ' ASN55N-HN 116.896 7.468 -105322\n' + ] + + # Write the data out. + file = DummyFileObject() + write_list(file_prefix=file, res_names=res_names, res_nums=res_nums, atom1_names=atom1_names, atom2_names=atom2_names, w1=w1, w2=w2, data_height=heights) + + # Check the file data. + lines = file.readlines() + print lines + self.assertEqual(len(lines), len(file_data)) + for i in range(len(lines)): + self.assertEqual(lines[i], file_data[i])