I am trying to write a script that will take 10 data files, read them line by line making a dictionary of the first(gene) and third(value) item in the file, and then combine all of these into an output file with a 12th column showing the average among the 10 replicates. (I will be doing this for several different samples, each with 10 replicates.) I am trying to use a for loop and the range function to automatically generate the file names, dictionary names, etc. as I am pretty sure this is a cleaner way to do the job than writing out the the code to make the dictionary 10 times. However, I am new to coding and I keep getting a "SyntaxError: can't assign to function call" on line 11 of my code ('gene{0}key'.format(i) = []) and I am sure it will do the same for several other lines of code that are structured the same way. If someone knows what I am doing wrong I would love to know what it is! Thank you in advance!
#!/usr/bin/env ipython
import re
import itertools
import numpy
sample = raw_input('sample: ')
for i in range(10):
filename = sample+'_rand{0}.genes.tajD'.format(i)
'gene{0}key'.format(i) = []
'taj{0}value'.format(i) = []
with open(filename, 'r') as data:
for line in data:
line = line.strip().split('\t')
gene, taj = line[0], line[3]
'gene{0}key'.format(i).append(gene)
'taj{0}value'.format(i).append(taj)
'dic{0}'.format(i) = dict(itertools.izip('gene{0}key'.format(i),'taj{0}value'.format(i)))
outfilename = sample+'_tajD.genes.all'
with open(sample+'_rand0.genes.tajD', 'r') as genes, open(outfilename, 'w') as outfile :
for line in genes :
line = line.strip().split('\t')
mastergene = line[0]
for i in range(10):
'value{0}'.format(i) = 'dic{0}'.format(i)[mastergene]
allrand = [value0, value1, value2, value3, value4, value5, value6, value7, value8, value9]
avg = numpy.mean(allrand)
outfile.write(mastergene + '\t' + value0 + '\t' + value1 + '\t' + value2 +'\t' + value3 + '\t' + value4 + '\t' + value5 + '\t' + value6 + '\t' + value7 + '\t' + value8 + '\t' + value9 + '\t' + avg + '\n')
'dic{0}'.format(i) = dict(itertools.izip('gene{0}key'.format(i),'taj{0}value'.format(i)))?