-1

I have some reads:

ATCAAAGTCCCGTAGGTACGGGAAATGCAAAAAAA GGGCTAGGTAGGGATTGCCTAGTCAACTGGGGGGG TAGCTAGGTAGGGATTGCCTAGTCAACTGGCCCGG

...

...

now ,I want to Cut the 12 bases to the left of each reads and write to a file:

f2 = open("./read1.csv","w")
with open('../001.fastq') as reader:
     for index, line in enumerate(reader):
         if index % 4 == 1:
             f2.write(line[:12]+'\n')
f2.close()

I want to know how to write a xlsx file

elaine_lq
  • 1
  • 2

2 Answers2

0

every reads have 4 rows

@

ATCAAAGTCCCGTAGGTACGGGAAATGCAAAAAAA

+

JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

@

GGGCTAGGTAGGGATTGCCTAGTCAACTGGGGGGG

+

JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

@

TAGCTAGGTAGGGATTGCCTAGTCAACTGGCCCGG

+

JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ

for this example ,the output is:

ATCAAAGTCCCG

GGGCTAGGTAGG

TAGCTAGGTAGG

elaine_lq
  • 1
  • 2
  • Please stick to the guidelines and edit your original question (and delete this answer). These additional 'posts' are reserved for answers only (comments are to ask for clarification, add some supplementary or nonessential info and similar things) while the original question is the one and only place where anything related to it should be. I know it can be confusing a bit first. By the way welcome on stackoverflow. – user2986898 Apr 09 '18 at 03:15
0

Assuming you are on Windows and have Excel installed. There are multiple libraries to make it possible to use Excel from python. Out of the I only have used win32com.client but I was pretty content with it. I think it comes with anaconda by default but if not then you can download it from here: https://github.com/mhammond/pywin32/releases (don't forget to select the appropriate version/architecture). Here is a mini reference to the most important functionalities:

from win32com.client import Dispatch  # import the necessary library

xlApp = Dispatch("Excel.Application") # starts excel
xlApp.Visible = False                 # hides window (this makes things a bit faster)
xlBook = xlApp.Workbooks.Open( fullPathToXlsx ) # opens an existing workbook

xlSheet = xlBook.Sheets(3)            # which sheet you want to use, indexing starts from 1!
print [sheet.Name for sheet in xlBook.Sheets] # if it is not the one you want you can see their order this way
xlSheet = xlBook.Sheets(sheetName)    # or you can use its name

xlSheet.Cells(row, col).Value = newValue # newValue is what you want to assign to the cell, row and col indexing starts from 1

xlApp.DisplayAlerts = False           # turns off any affirmation popup
xlBook.Save()                         # saving workbook
xlBook.SaveAs(newFullPath)            # saving as...

xlBook.Close()                        # close workbook
xlApp.Quit()                          # exit application

You might want to convert between column/row index and the 'letter representation (i mean 'A1' for top left cell and so on). Here is what I used for it:

def cellStringToNum(aStr):
    """
    returns an excel sheet cell reference as numbers in a list as [col, row]
    """
    import string

    colS = ""
    for i in xrange(len(aStr)):
        if aStr[i] in string.ascii_uppercase:
            colS += aStr[i]
        else:
            row = int(aStr[i:])
            break
    col = 0
    for i in xrange(len(colS)):
        col += (string.ascii_uppercase.find(colS[len(colS)-1-i])+1)*(26**i)
    return [col, row]

def cellNumToString(col, row):
    import string

    colS = string.ascii_uppercase[col % 26 - 1]
    if col % 26 == 0:
        col -= 26
    else:
        col -= col % 26
    while col != 0:
        col /= 26
        colS = string.ascii_uppercase[col % 26 - 1] + colS
        if col % 26 == 0:
            col -= 26
        else:
            col -= col % 26
    return colS+str(row)

Edit

But this question is already answered here: Python - Write to Excel Spreadsheet

user2986898
  • 413
  • 3
  • 10