Skip to content

Commit

Permalink
Support float input and output.
Browse files Browse the repository at this point in the history
  • Loading branch information
gioelelm committed Apr 15, 2015
1 parent 13d19d3 commit d612528
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
81 changes: 50 additions & 31 deletions Cef_tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
# Copyright (c) 2015 Gioele La Manno and Sten Linnarsson
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# This code contains a simple parser reader for Cef files
# typical usage to write a cef_file:
#
# cef = CEF_obj()
# cef.add_header('Mynote', 'This is my message')
# cef.set_matrix(your_array)
# cef.add_col_attr(attribute_name, attribute_values_list)
# cef.add_row_attr(attribute_name, attribute_values_list)
# cef.writeCEF('path/to/your/file.cef')
#
# to read:
# cef = CEF_obj()
# cef.readCEF('path/to/your/file.cef')
# your_array = cef.matrix
# attribute_values_list2 = cef.col_attr_values[2]
# ...

class CEF_obj(object):
def __init__(self):
self.headers = 0
Expand All @@ -19,34 +61,6 @@ def __init__(self):

self.matrix = []

# def __add__(self, other):
# assert type(other) == CEF_obj, 'Cannot perform addition between CEF_obj and a different data type'
# result = CEF_obj()

# result.row_attr_names = self.row_attr_names +\
# [i for i in other.row_attr_names if i not in self.row_attr_names]
# result.row_attr_values = self.row_attr_values +\
# [other.row_attr_values[i] for i, v in enumerate(other.row_attr_names) if n not in self.row_attr_names]
# result.row_attr = len(result.row_attr_names)


# self.col_attr = 0
# self.rows = 0
# self.cols = 0
# self.flags = 0
# self.tab_fields = 7

# self.header_names = []
# self.header_values = []

# self.row_attr_names = []
# self.row_attr_values = []

# self.col_attr_names = []
# self.col_attr_values = []

# def __radd__(self,other):
# self.__add__(self, other)

def update(self):
self.headers = len( self.header_names)
Expand All @@ -73,7 +87,7 @@ def set_matrix(self, matrix):
for row in matrix:
self.matrix.append(list(row))

def readCEF(self, filepath, matrix_dtype = int):
def readCEF(self, filepath, matrix_dtype = 'auto'):
#Delete all the stored information
self.__init__()
#Start parsing
Expand Down Expand Up @@ -104,10 +118,15 @@ def readCEF(self, filepath, matrix_dtype = int):
linelist = fin.readline().rstrip('\n').split('\t')
for n, entry in enumerate( linelist[:self.row_attr] ):
self.row_attr_values[n].append( entry )
if matrix_dtype == 'auto':
if sum('.' in i for i in linelist[self.row_attr+1:]) != 0:
matrix_dtype = float
else:
matrix_dtype = int
self.matrix.append( [matrix_dtype(el) for el in linelist[self.row_attr+1:] ])


def writeCEF(self, filepath):
def writeCEF(self, filepath, matrix_str_fmt = '%i'):
self.update()
with open(filepath, 'wb') as fout:
#Write cef file first line
Expand All @@ -127,7 +146,7 @@ def writeCEF(self, filepath):
for j in range(self.row_attr):
fout.write( unicode(self.row_attr_values[j][i]) + u'\t')
fout.write(u'\t')
fout.write(u'\t'.join( [unicode(el) for el in self.matrix[i]] ) )
fout.write(u'\t'.join( [unicode(matrix_str_fmt % el) for el in self.matrix[i]] ) )
fout.write(u'\n')


Expand Down
8 changes: 5 additions & 3 deletions backSPIN.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,11 @@ def usage():
output_cef.add_col_attr('Level_%i_group' % level, [int(el) for el in groups])

output_cef.set_matrix(array(input_cef.matrix)[results.genes_order,:][:,results.cells_order])

output_cef.writeCEF( outfiles_path )

if sum(type(i)==float for i in input_cef.matrix[0]) + sum(type(i)==float for i in input_cef.matrix[-1]) == 0:
fmt = '%i'
else:
fmt ='%.6g'
output_cef.writeCEF( outfiles_path, matrix_str_fmt=fmt )
else:

print 'normal SPIN started\n----------------\n'
Expand Down

0 comments on commit d612528

Please sign in to comment.