1

What I have is a long list of codes that involves reading different files and in the end putting everything into different .csv

This is all my codes

import csv
import os.path
#open files + readlines
with open("C:/Users/Ivan Wong/Desktop/Placement/Lists of targets/Mouse/UCSC to Ensembl.csv", "r") as f:
    reader = csv.reader(f, delimiter = ',')
    #find files with the name in 1st row
    for row in reader:
        graph_filename = os.path.join("C:/Python27/Scripts/My scripts/Selenoprotein/NMD targets",row[0]+"_nt_counts.txt.png")
        if os.path.exists(graph_filename):
            y = row[0]+'_nt_counts.txt'  
            r = open('C:/Users/Ivan Wong/Desktop/Placement/fp_mesc_nochx/'+y, 'r')
            k = r.readlines()
            r.close
            del k[:1]
            k = map(lambda s: s.strip(), k)
            interger = map(int, k)   
            import itertools
            #adding the numbers for every 3 rows
            def grouper(n, iterable, fillvalue=None):
                "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
                args = [iter(iterable)] * n
                return itertools.izip_longest(*args, fillvalue=fillvalue)
            result = map(sum, grouper(3, interger, 0))       
            e = row[1]
            cDNA = open('C:/Users/Ivan Wong/Desktop/Placement/Downloaded seq/Mouse/cDNA.txt', 'r')
            seq = cDNA.readlines()
            # get all lines that have a gene name
            lineNum = 0;
            lineGenes = []
            for line in seq:
                lineNum = lineNum +1
                if '>' in line:
                    lineGenes.append(str(lineNum))
                if '>'+e in line:
                    lineBegin = lineNum

            cDNA.close

            # which gene is this
            index1 = lineGenes.index(str(lineBegin))
            lineEnd = lineGenes[index1+1]           
# linebegin and lineEnd now give you, where to look for your sequence, all that 
# you have to do is to read the lines between lineBegin and lineEnd in the file
# and make it into a single string.            
            lineEnd = lineGenes[index1+1]
            Lastline = int(lineEnd) -1

# in your code you have already made a list with all the lines (q), first delete
# \n and other symbols, then combine all lines into a big string of nucleotides (like this)     
            qq = seq[lineBegin:Lastline]
            qq = map(lambda s: s.strip(), qq)
            string  = ''
            for i in range(len(qq)):
                string = string + qq[i]
# now you want to get a list of triplets, again you can use the for loop:
# first get the length of the string
            lenString = len(string);
# this is your list codons
            listCodon = []
            for i in range(0,lenString/3): 
                listCodon.append(string[0+i*3:3+i*3])
            with open(e+'.csv','wb') as outfile:
                outfile.writelines(str(result)+'\n'+str(listCodon))

My problem here is the file produced looks like this:

 0      0      0        
'GCA'  'CTT'   'GGT'

I want to make it like this:

0  GCA    
0  CTT    
0  GGT

What can I do in my code to achieve this?

print result:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 1, 2, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 3, 3, 0, 3, 1, 2, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 5, 0, 0, 0, 0, 6, 0, 1, 0, 0, 2, 0, 1, 0, 0, 1, 1, 0, 1, 6, 34, 35, 32, 1, 1, 0, 4, 1, 0, 1, 0, 0, 0, 0, 1, 6, 0, 0, 0, 0, 1, 3, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

print listCodon:

['gtt', 'gaa', 'aca', 'gag', 'aca', 'tgt', 'tct', 'gga', 'gat', 'gag', 'ctg', 'tgg', 'gca', 'gaa', 'gga', 'cag', 'gcc', 'taa', 'gca', 'cag', 'gca', 'gca', 'gag', 'ctt', 'tga', 'tct', 'ctt', 'ggt', 'gat', 'cgg', 'tgg', 'ggg', 'atc', 'cgg', 'tgg', 'cct', 'agc', 'ttg', 'tgc', 'caa', 'gga', 'agc', 'tgc', 'tca', 'gct', 'ggg', 'aaa', 'gaa', 'ggt', 'ggc', 'tgt', 'ggc', 'tga', 'cta', 'tgt', 'gga', 'acc', 'ttc', 'tcc', 'ccg', 'agg', 'cac', 'caa', 'gtg', 'ggg', 'cct', 'tgg', 'tgg', 'cac', 'ctg', 'tgt', 'caa', 'cgt', 'ggg', 'ttg', 'cat', 'acc', 'caa', 'gaa', 'gct', 'gat', 'gca', 'tca', 'ggc', 'tgc', 'act', 'gct', 'ggg', 'ggg', 'cat', 'gat', 'cag', 'aga', 'tgc', 'tca', 'cca', 'cta', 'tgg', 'ctg', 'gga', 'ggt', 'ggc', 'cca', 'gcc', 'tgt', 'cca', 'aca', 'caa', 'ctg', 'gtg', 'aga', 'gag', 'aag', 'ccc', 'ttg', 'ccc', 'tct', 'gca', 'ggt', 'ccc', 'att', 'gaa', 'agg', 'aga', 'ggt', 'ttg', 'ctc', 'tct', 'gcc', 'act', 'cat', 'ctg', 'taa', 'ccg', 'tga', 'gct', 'ttt', 'cca', 'ccc', 'ggc', 'ctc', 'ctc', 'ttt', 'gat', 'ccc', 'aga', 'ata', 'atg', 'act', 'ctg', 'aga', 'ctt', 'ctt', 'atg', 'tat', 'gaa', 'taa', 'atg', 'cct', 'ggg', 'cca', 'aaa', 'acc']

This is what Marek's code helped me to achieve This is what I hope for

picture on the left is what Marek's code helped me to achieve, I want to make an improvement so it arrange like the picture on the right

9
  • What exactly is contained in result and listCodon? I'm asking because right now your code snippet implies that both are simple strings already, and the str() call is useless. Otherwise there would be no way to achieve that output, unless they are custom classes that overload __str__(), and if that was the case, we'd definitely need to see these definitions. Commented Aug 2, 2012 at 9:47
  • @Tim Pietzcker result and listCodon are in the codes that I didn't put up. They are data that has been processed with python and basically result is the numbers (example I put up will be 0) and listCodon will be the 3 letters (GCA, CTT etc.) Commented Aug 2, 2012 at 9:50
  • So you've also left out a loop in your sample? You need to post more code - we can't guess what your program is doing. Please post as much as needed for us to reproduce the result (and preferably not more than that). Then we can look at fixing it. Commented Aug 2, 2012 at 9:52
  • @TimPietzcker Right, I have done that. Do you need those raw data as well? Commented Aug 2, 2012 at 9:53
  • One tip: You're using close wrong, the method needs to be called: cDNA.close() is necessary (but unrelated to your problem). Commented Aug 2, 2012 at 9:56

2 Answers 2

4

You can use zip() to zip together two iterators. So if you have

result = [0, 0, 0, 0, 0]
listCodons = ['gtt', 'gaa', 'aca', 'gag', 'aca']

then you can do

>>> list(zip(result, listCodons))
[(0, 'gtt'), (0, 'gaa'), (0, 'aca'), (0, 'gag'), (0, 'aca')]

or, for your example:

with open(e+'.csv','w') as outfile:
    out = csv.writer(outfile)
    out.writerows(zip(result, listCodons))
Sign up to request clarification or add additional context in comments.

2 Comments

zip(result, listCodons) actually returns list so there's no need to wrapping it into "list" constructor.
@MarekWawrzyczek: Not on Python 3.
1

try this:

proper_result = '\n'.join([ '%s %s' % (nr, codon)  for nr, codon  in zip(result, listCodon) ] )

Edit (codons split into separate columns):

proper_result = '\n'.join(' '.join([str(nr),] + list(codon))  for nr, codon  in zip(nrs, cdns)) 

Edit (comma separated values):

proper_result = '\n'.join('%s, %s' % (nr, codon) for nr, codon in zip(result, listCodon))

6 Comments

SyntaxError: invalid syntax...why?
Thanks! It now its all in a column now. Although instead of 2 columns they are squeezed into 1 column. I think this is good but do you think you can separate them?
In one column? What is squeezed into 1 columny - codons?
As you know, the outfile is .csv, and when I open it, it is like an excel file with columns A, B, C etc. and row 1, 2, 3 etc. After using your codes, the outfiles do go from top to down but the only problem is both result(the number) and listCodon(the letters) are squeezed into column A instead of what I hope for (column A for result and column B for listCodon)
Please provide sample output file which you would like to generate.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.