viz_reader.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """
  2. This is free and unencumbered software released into the public domain.
  3. Anyone is free to copy, modify, publish, use, compile, sell, or
  4. distribute this software, either in source code form or as a compiled
  5. binary, for any purpose, commercial or non-commercial, and by any
  6. means.
  7. In jurisdictions that recognize copyright laws, the author or authors
  8. of this software dedicate any and all copyright interest in the
  9. software to the public domain. We make this dedication for the benefit
  10. of the public at large and to the detriment of our heirs and
  11. successors. We intend this dedication to be an overt act of
  12. relinquishment in perpetuity of all present and future rights to this
  13. software under copyright law.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. For more information, please refer to [http://unlicense.org]
  22. """
  23. import sys
  24. import os
  25. import pandas as pd
  26. MCELL_PATH = os.environ.get('MCELL_PATH', '')
  27. if MCELL_PATH:
  28. sys.path.append(os.path.join(MCELL_PATH, 'lib'))
  29. else:
  30. print("Error: variable MCELL_PATH that is used to find the mcell library was not set.")
  31. sys.exit(1)
  32. import mcell as m
  33. def load_counts_from_dat_file(file_name):
  34. # using pandas read_cvs because it is usually faster
  35. # we could also read the file line by line and count the number of occurences
  36. df = pd.read_csv(
  37. file_name,
  38. sep=' ',
  39. names=['name', 'id', 'x', 'y', 'z', 'nx', 'ny', 'nz'])
  40. # create a dataframe with counts per unique species
  41. counts = df['name'].value_counts().rename_axis('species').reset_index(name='count')
  42. # need to sort the data to get consistent results
  43. counts = counts.sort_values(['count','species'], ascending=(False, True))
  44. return counts
  45. def parse_bngl_strings_to_complex_representations(counts_df):
  46. # returns a list of pairs (mcell.Complex, int), the second item is count
  47. res = []
  48. for index, row in counts_df.iterrows():
  49. # constructor m.Complex parses the BNGL representaion into
  50. # a MCell4 API representation
  51. # (see https://cnl.salk.edu/~ahusar/mcell4_documentation/generated/subsystem.html#complex
  52. cplx = m.Complex(row['species'])
  53. res.append((cplx, row['count']))
  54. return res
  55. def read_dat_file(file_name):
  56. # returns a list of pairs (mcell.Complex, int), the second item is count
  57. # load the .dat file as a pandas dataframe
  58. counts_df = load_counts_from_dat_file(file_name)
  59. # parse the BNGL representations
  60. complex_counts = parse_bngl_strings_to_complex_representations(counts_df)
  61. return complex_counts