import sys
import numpy



class CSVNormalizePlugin:
   def input(self, filename):
      self.myfile = filename

   def run(self):
      filestuff = open(self.myfile, 'r')
      self.firstline = filestuff.readline()
      lines = []
      for line in filestuff:
         lines.append(line)

      self.m = len(lines)
      self.samples = []
      self.bacteria = self.firstline.split(',')
      self.bacteria.remove('\"\"')
      self.n = len(self.bacteria)
      self.ADJ = numpy.zeros([self.m, self.n])
      i = 0
      for i in range(self.m):
            contents = lines[i].split(',')
            self.samples.append(contents[0])
            for j in range(self.n):
               value = float(contents[j+1])
               #print self.ADJ[i][j]
               #print "VALUE: ", value
               self.ADJ[i][j] = value
            i += 1

   def output(self, filename):
      filestuff2 = open(filename, 'w')
      
      filestuff2.write(self.firstline+'\n')
      for i in range(self.m):
         sum = float(0.0)
         for j in range(self.n):
            sum += self.ADJ[i][j]
         #print "SUM: ", sum
         for j in range(self.n):
            self.ADJ[i][j] /= sum
            
      for i in range(self.m):
         #filestuff2.write(self.bacteria[i]+',')
         filestuff2.write(self.samples[i]+',')
         for j in range(self.n):
            filestuff2.write(str(self.ADJ[i][j]))
            if (j < self.n-1):
               filestuff2.write(",")
            else:
               filestuff2.write("\n")



