Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8328213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:21:55+00:00 2026-06-09T01:21:55+00:00

I have codes that allow me to perform a fairly complicated task (for me

  • 0

I have codes that allow me to perform a fairly complicated task (for me at least):

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/Top 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[0]
            print e
            cDNA = open('C:/Users/Ivan Wong/Desktop/Placement/Downloaded seq/Mouse/MOUSE_mRNAs.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])
            proper_result = '\n'.join('%s, %s' % (nr, codon) for nr, codon in zip(result, listCodon))
            with open(e+'.csv','wb') as outfile:
                outfile.writelines(proper_result)

These codes read a file from a .csv, identifying from a folder with files having the same name, if they exist then it goes on to process some data and write them into a .csv
with them, my outfiles now looks like thisoutfile

It looks completely fine but with one problem, I know from my data (I checked it in different ways) that the 2nd columns should be longer than what I have got. I think it is because the codes are writing the files when BOTH result(the number) and listCodon(the letters) are existed, therefore I am missing something. How can I fix it?

I tried to print listCodon just before the file is written, and found out the all triplets are still there so I am guessing the problem is within here:

proper_result = '\n'.join('%s, %s' % (nr, codon) for nr, codon in zip(result, listCodon))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T01:21:57+00:00Added an answer on June 9, 2026 at 1:21 am

    zip will stop as soon as any of its iterables stop (because otherwise it wouldn’t know what to fill in the blanks with!):

    The returned list is truncated in length to the length of the shortest argument sequence.

    If you want to pad the shorter iterables to the length of the longest, use izip_longest (which takes an optional parameter to use as the fill value).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there any development environments that allow you to have one code base that
I have a need to allow my users to execute .NET code that they
I have heard that MbUnit comes with pretty nice test decorators which allow writing
I have an array of hex codes that translate into assembly instructions and I
I have a bunch of string with special escape codes that I want to
I have a database that uses codes. Each code can be anywhere from two
I have a DropDownList that contains Station codes. @Html.DropDownList(Station, null, new { id =
I have one activity/java file (Browsefile.java) that would obtain the absolute path of the
I have some code that I want to only allow access to by one
I have a GridView control hooked up to a datasource that I allow row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.