简体   繁体   中英

Using a python script which reads a 77gb file into memory to scan for keywords, the code works but my computer doesn't have enough ram

The problem:

I have a python script which reads in, what is essentially a text file, scanning for certain keywords - this then prints to the screen, unfortunately, one of the text files I'm reading into the program is 77GB, so the process is automatically terminated when my computer runs out of swap space

The code:

import re

listofspecies = open("bacreduced.txt").readlines()

listofseqs = open("Bac16s.faa").readlines()

for x in listofspecies:
    for i in range(0,len(listofseqs)):
        #print (i)
        names = x.split("_")
        if re.findall(names[0].strip(), listofseqs[i]):
            if re.findall(names[1].strip(), listofseqs[i]):
                print (">" + x + listofseqs[i+1].strip())
                break

Input:

The .txt file is just a list of species names separated by new lines like this (but bigger):

Metallosphaera_hakonensis
Bacillus_clausii
Acidiphilium_multivorum
Tolumonas_auensis
Mycoplasma_yeatsii
Cardiobacterium_valvarum
Bacillus_gibsonii
Actinomadura_formosensis
Vibrio_gigantis

The large file, is in fasta format, and looks like this:

>Methanothermobacter_thermautotrophicus
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------G--G-C---G--G----------------A--G-G-C----T-----A---C---T---G---C----T------------A-----T----T-----G----G---G------G---T-----T----C----G-------A-----T----------------T----------A-----------A--G--C---C-------A---T---G-C----A--A--G-T-C-Ga--A-CGagcacctcggtgct----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CG-T---G--G--C----G---A--A---C-----G--G--C--T--C---A------G----T---A---A-----C--A--C--G----T---G--G--A--T-----A---------A----C-C-----T-------------G-------C--C--C---T---T--G--G----G-------A------C---C--G---G--------G----A-----T--------A------A--C---C----C--C---G-G---G-----A---A------------------------------------------A-------------------------------------------------C-T----G-G----G-----G---A--T--A-----A----A-C-------------C--C-G-G--A-------T----A--G--G--T---G---A-A--G-A-T----GCC---T-G--G----A-AT------G-G-T----------G---C--T-T----C--A---C--C------C----A--AAc--ACCtccg----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------GG-T-G--C--C--C----A--A----G-G--A----T------G-G--G--T----C-T-G--C--G--G---C--C--G---A----T-T---A----G-G---T-----A---G--T-T-G--G--T--A--G--G-----G-T---A--A------------C---G-------------------------------------------------G---------------------------------------C-C-----T--A--C--C----A--A-----G-C--C---C---A--T--C-A------T-----C-G--G--T--A-----C-G-G-----G---T--T--G--T----G------A---G--A----G--C--A-A------G----A---G---C-C--C-----G--------G-A-G---A---T----G---G--A---A-C-------C------T-----G-------A---G--A-----C-------A------------------A--G----------G---T---T--C--C--A-----------G----G---C--Cc---T-------------A---C-G-----G-------G-G--C---G---C---A---------G-C---A--G--G-C---G-----------------C---G--------A----A--------A----C--C-------T--C------C--G------C-----------------------------------------------------------------------------------------------A------A-----T--G--C----A-C---G----C--A-------A----------------------------G-T--G---C--G---A----C-G------G----G--G----G-A----A---C---C-----C--C--A----A----G-------T--G--C-C-----A-C-TCTTaacgg------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------GG-T-----G--G--C-----T--T--T---T----C--A------G-A--A--G-T-----G---T--A---A-A------------------------------------A-------------------------A------G-C----T--T-C-------------T-G----------------G----AA------TA---A----G-G-------G------C---T--G-----G------------G------C----A-----------------------------------------A--------------------------------------------G------------------------------------A----C----C--G---G-------T-------------------------------------------------G----C--C--------------------------------------------------A----G----C-------C-----------------------------------------------------G--C--C--------------------------------------G--C---G----G----T--------A-A--C--A----C--C--G---G------C--A--G--C--T--C--A-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------A-G--T--G--G--T--A--G---C--C-----G-----C---------T---T--T--T--A--T--T-----G--G---G---C----C---T----A-----A-A----G----C--G----T-C------C-----G--T-----A---G--C--C------G-----G--T--C---T--G---A--T-------A--A--G--T--C---T--C--T----G-G---T-G----A-----------------------A--A---T--C-C---C---A---C--A--G-----C---------------------------------T-T------------------------A-A--------------C--T--G---T--G---G--GAAt------T-G-C--T-------G-G-A----G----A--T---A-----C--T--------A-T--C--At--G--A--C----------T------C------G------A--G---G---T--C--G--G--G--A-----G--A----G--G--C--T--G-G--A----------------------------------------------------------------------------------------------------------------------------------------------------G-G---T---A--------C--T---C--C--C---A---G--G-----G----T-------A---G--G-G-----G---T--G-----------------A----A-A-------------------------------------------------------------T-C---C---T----G--T--A--A--T-----C--C---T------G---G--G---A---G----G-A---------------------------------C--C-A----C-----C----------------------------------T--G---------T------------------------------------G--G--C-----G--------------------------------------------A---------------------------------------------------A---------------------------------------------G----------------------------------------------------G--C-----------G---T-C-C--A-----G-C----T---G------------------G--A--A--C---G--A--A--C--C--T-------G--A--C------G-G----T-----G--A--G---------------------G--G--A--C----G----A---A-----A---G---C---C---A--G-G----G----G-------------------------------------C--G--C----G---------------------------------------------------------------------A---------------------------------------------------------------------------------------------A----C---C--G-----G----A------------------------------T--T--A--G-------------------------------------------A---------------------------------------------------------------------------------------------------------------T----------------------------------------------------------------------------------------------------A--------------------------------C-----------------------------------------------------------------------------------------------------------------------------------C---C--G--G--G--T----A----------------------------------------------------------------------------------G-T----------------------------------C---C---T--G--G---C--------C-------G---T----A---A------------------------A--------C---------G--A----T------G-T---G------G------A--C----------T------------------------T-----G---G---T---G-------T----T----G---G-----G---A---T-----G----G--C------T--T---C--G--A-----------------G--C---T---G-----C--C--C--C---A----G---T-----G----C-C----------G----------A------A--G--G--G---------A--A----------------------------------G-C--T---G---T--T----A--A---------G----T--C--C----A---C-----C-----------G-----C----C----T---G--G---G----A------A---G--T-----A----C--G-G--C---C-----G---C--A---A---------------------------------------G--G---C--T---G----A--A--A-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------C--T---T----A--A----A----G---G---A---------A----T---T---G---G---C--G----G---G---G--G--A--G--C--A-------C----C--A--C------A--------------A----C--G-C--G--T---G---G-A---G---C--C--T--G----C--G-G--T--------T------------T-----A------A---T--T--G--G----A------T---T---C-----A-A--C----G--------------------------------------C--C---G---G--A-------C------A----------------------------------------T------------C--T------------------------------------------------------------------C-----A----C-C---A--G----G--G--G-----C-----------G-----A--C--A-G-C------AG---T---------------A------T--G---A---T----G--G---C------C---A--G-G-----T-----T--G--------A-----T----G-------A-----------C--C--T--T--G----C--T-----TG--A---C-A-A--------G-C---T-G-------A----G-----A-----G---G-------A--G-G-T-G----C----A-T-G-G-C--C---G-C--C--G---T------------------------------------C---------------------------------------------------------------------------------------------------------------------------------------------A-G--C---T-C-G---T-----A--C--C------G-T----------G----------------------------------------------------A--------------------------------------------G--G--C--G-----T--C-C-T-G---T----T------A---------------------------------------------A------------------------------------------------G----T----------C-A---G--G-C---A-----A-C-G--A--G---C---G--A-------G----A------C-C--C--A--C------------G-C-C--C--T-T--A--A------T----T--------A-C-C---AG--C-G--------G------A--A-C-CC--------T---------Tatg--------------GGTTg---C----------------------------C-G----------------G-G--C------A-C-A----------------C---T---A--A--G--G--G--G------------------------------------------A---C---C-G---C----C--A-G-T-Ga-----TAA--------------------A-C--T-G---G--A--G-G---A-A-G-G-A---------------------------------------------G--T--G--G--A--C--G-A------------------------------------------C-G-G--T---A----G---G-T--C-C-G-T-A-------------------------------------------------------------------------------------T---G-C-C--------C-C---G-----A-A--T--------C-C-C--C--T--G-G-G---C-A--------A--C---A---C---G-C--G-G-G--C---T-A-C-A-A--T--G-G-C-C--T--G-G----A-C--A--A-T-G-G-G----T---------TCC---G-----A--C--A-C--T-----G--A-A--A----G-G-T---G---G---A---------------G-G--T-A-A--T--C--CC-Ct--AAA---C-C--A---G---G---T-C------G---T-A---G--T--T-C--G-G-----A--T--C----G---A--G--G-G---C-----T-G-----T-----A--A--C-T--C-------G--C-C-C--T---C-G-T--GA-A---G-C-T--G---G-A--A--T--G--C-G--T-----A---G----T--A----A-T--C--G--C--G--T--G--------T-C-------A-------------------------T----T-A-T------------------------------------C--G--C--G--C-----G--G--T-----G-----A--A--T--A-C------G--T--C--C---C----T-----G---C--T---C------C--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>Metallosphaera_hakonensis
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------C-C---G--G----------------A--C-C-C----G-----A---T---C---G---C----T------------A-----T----A-----G----G---G------G---T-----A----G----G-------G-----A----------------T----------A-----------A--G--C---C-------A---T---G-G----G--A--G-T-C-Gt--A-CGctctcgggaagaggg---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------CG-T---G--G--C----G---G--A---C-----G--G--C--T--G---A------G----T---A---A-----C--A--C--G----T---G--G--C--T-----A---------A----C-C-----T-------------G-------C--C--C---T---T--G--G----G-------A------C---C--C---G--------G----A-----T--------A------A--C---C----C--C---G-G---G-----A---A------------------------------------------A-------------------------------------------------C-T----G-G----G-----G---C--T--A-----A----T-C-------------C--G-G-G--G-------C----A--G--A--T---A---A-G--G-G-G----ATC---T-G--G----A-AC------G-A-C----------C---T--C-T----T--A---T--C------T----A--AAt--GCCtcccggctgatcccgtcggga-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------GG-T-G--C--C--C----A--A----G-G--A----T------G-G--G--G----C-T-G--C--G--G---C--C--C---A----T-C---A----G-G---C-----T---G--T-T-G--G--G--G--G--A-----G-T---A--A------------A---G-------------------------------------------------G---------------------------------------T-C-----C--C--C--C----A--A-----A-C--C---G---A--T--A-A------C-----G-G--G--T--A-----G-G-G-----G---C--C--G--T----G------G---G--A----G--C--G-G------G----A---G---C-C--C-----C--------C-A-G---T---T----G---G--G---C-A-------C------T-----G-------A---G--A-----C-------A------------------A--G----------G---G---C--C--C--A-----------G----G---C--Cc---T-------------A---C-G-----G-------G-G--C---G---C---A---------C-C---A--G--G-C---G-----------------C---G--------G----A--------A----C--G-------T--C------C--C------C-----------------------------------------------------------------------------------------------A------A-----T--G--C----G-G---G----A--A-------A----------------------------C-C--G---T--G---A----G-G------G----C--G----T-T----A---C---C-----C--C--T----A----G-------T--G--C-C-----C-T-CGCAa----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------GA-G-----G--G--C-----T--T--T---T----C--T------C-C--A--C-T-----T---T--A---G-A------------------------------------A-------------------------C------G-G----T--G-G-------------A-G----------------G----AA------TA---A----G-C-------G------G---G--G-----G------------G------C----A-----------------------------------------A--------------------------------------------G------------------------------------A----C----T--G---G-------T-------------------------------------------------G----T--C--------------------------------------------------A----G----C-------C-----------------------------------------------------G--C--C--------------------------------------G--C---G----G----T--------A-A--T--A----C--C--A---G------C--C--C--C--G--C--G-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------A-G--T--G--A--T--C--G---G--G-----A-----C---------G---T--T--T--A--T--T-----G--G---G---C----T---T----A-----A-A----G----C--G----C-C------C-----G--T-----A---G--C--C------G-----G--C--C---T--G---T--A-------A--A--G--T--C---A--C--C----G-T---T-T----A-----------------------A--A---G--A-C---C---C---G--G--G-----C---------------------------------T-C------------------------A-A--------------C--T--C---G--G---G--GAA-------C-G-G--C-------G-G-T----G----A--T---A-----C--T--------T-A--C--A---G--G--C----------T------A------G------G--G---G---G--C--G--G--G--A-----G--A----G--G--T--C--G-G--A----------------------------------------------------------------------------------------------------------------------------------------------------G-G---T---A--------C--T---C--C--C---G---G--A-----G----T-------A---G--G-G-----G---C--G-----------------A----A-A-------------------------------------------------------------T-C---C---T----C--A--G--A--T-----C--C---C------G---G--G---A---G----G-A---------------------------------C--C-A----C-----C----------------------------------A--G---------T------------------------------------G--G--C-----G--------------------------------------------A---------------------------------------------------A---------------------------------------------A----------------------------------------------------G--C-----------G---T-C-C--G-----G-C----T---A------------------G--A--A--C---G--C--G--C--C--C-------G--A--C------G-G----T-----G--A--G---------------------G--G--G--C----G----A---A-----A---G---C---C---G--G-G----G----T-------------------------------------A--G--C----A---------------------------------------------------------------------A---------------------------------------------------------------------------------------------A----T---A--G-----G----A------------------------------T--T--A--G-------------------------------------------A---------------------------------------------------------------------------------------------------------------T----------------------------------------------------------------------------------------------------A--------------------------------C-----------------------------------------------------------------------------------------------------------------------------------C---C--T--A--G--T----A----------------------------------------------------------------------------------G-T----------------------------------C---C---C--G--G---C--------T-------G---T----A---A------------------------A--------C---------G--A----T------G-C---A------G------G--C----------T------------------------A-----G---G---T---G-------T----C----G---C-----G---T---A-----G----G--C------T--T---T--G--G-----------------G--C---C---T-----A--C--G--C---G----G---T-----G----C-C----------G----------C------A--G--G--A---------A--A----------------------------------A-C--T---G---G--T----A--A---------G----C--C--C----G---C-----C-----------G-----C----C----T---G--G---G----G------A---G--T-----A----C--G-G--C---C-----G---C--A---A---------------------------------------G--G---C--T---G----A--A--A-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------C--T---T----A--A----A----G---G---A---------A----T---T---G---G---C--G----G---G---G--G--A--G--C--A-------C----C--A--C------A--------------A----G--G-G--G--T---G---G-A---A---C--C--T--G----C--G-G--C--------T------------C-----A------A---T--T--G--G----A------G---T---C-----A-A--C----G--------------------------------------C--C---T---G--G-------A------A----------------------------------------T------------C--T------------------------------------------------------------------C-----A----C-C---G--G----G--G--G-----A-----------G-----A--C--C-G-C------AG---G---------------A------T--G---A---C----G--G---C------C---A--G-G-----C-----T--A--------A-----C----G-------A-----------C--C--T--T--G----C--C-----AG--A---C-T-C--------G-C---G-G-------A----G-----A-----G---G-------A--G-G-T-G----C----A-T-G-G-C--C---G-T--C--G---C------------------------------------C---------------------------------------------------------------------------------------------------------------------------------------------A-G--C---T-C-G---T-----G--T--T------G-T----------G----------------------------------------------------A--------------------------------------------A--A--T--G-----T--C-C-G-G---T----T------A---------------------------------------------A------------------------------------------------G----T----------C-C---G--G-C---A-----A-C-G--A--G---C---G--A-------G----A------C-C--C--C--C------------A-C-T--T--C-T--A--G------T----T--------G-G-T---AA--T-T--------G------T--C-T-CT--------C---------Tg----------------GAGA----C----------------------------G-G----------------T-C--C------A-C-A----------------C---T---A--G--A--A--G--G------------------------------------------A---C---T-G---C----C--G-G-T-Gt-----TAA--------------------A-C--C-G---G--A--G-G---A-A-G-G-A---------------------------------------------G--G--G--G--G--C--C-A------------------------------------------C-G-G--C---A----G---G-T--C-A-G-C-A-------------------------------------------------------------------------------------T---G-C-C--------C-C---G-----A-A--A--------C-T-T--C--C--G-G-G---C-C--------G--C---A---C---G-C--G-G-G--T---T-A-C-A-A--T--G-G-C-A--G--G-G----A-C--A--G-C-G-G-G----A---------TCT---G-----A--C--C-C--C-----G--A-A--A----G-G-G---G---A---A---------------G-G--C-A-A--T--C--CC-Ac--AAA---C-C--C---T---G---C-C------T---C-A---G--T--T-G--G-G-----A--T--C----G---A--G--G-G---C-----T-G-----A-----A--A--C-T--C-------G--C-C-C--T---C-G-T--GA-A---C-G-A--G---G-A--A--T--C--C-C--T-----A---G----T--A----A-C--C--G--C--G--G--G--------T-C-------A-------------------------A----C-A-A------------------------------------C--C--C--G--C-----G--G--T-----G-----A--A--T--A-C------G--T--C--C---C----T-----G---C--T---C------C--T--------T--G--C---A--C-A--C----A-C-----------C---G-C---------C---C-G--------T-C---------G-----C---T--C--C---A-C-C-C--G-A---G-T-G--G-AGg---GGA-A-G--T--G--A-G-G-C-C----T-C-T-TGCCcttcgg-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------GGT-G-G---G-A-G-G--T--C-G-A---G--C--T--T----C--Tc-----C---T----C-C--G--C--G-A-------G--G---G----------G-----G-G--AG--A-----A-------G--T------------C-G-------T-A-----A--C--A---A---G---G---T--A--G-C--C-G-----T-AG-G-----G-------G--AA-------------C----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Desired output:

A list of the files with identified with the keyword in the sequence, then printed instead of the description field, but the code already does this. How do I make this code workable when my computer only has 16GB of RAM?

Instead of loading the file as a whole into memory, you could load it in one line at a time. Python supplies a handy iterator over the file, which does exactly that:

import re

listofspecies = open("bacreduced.txt").readlines()
do_print = False
with open("Bac16s.faa") as f:
    for line in f:
        if do_print:
            print (do_print + line.strip())
            do_print = False
            continue
        for x in listofspecies:    
            names = x.split("_")
            if re.findall(names[0].strip(), line):
                if re.findall(names[1].strip(), line):
                    do_print = ">" + x
                    break

On a 77GB file, it's probably worth doing a bit of preparation ahead of time to avoid the repeated search within strings. I think this gives you the same answer, but is likely much more efficient as it uses sets to determine the presence of the token rather than searching in strings. Note also that your original algorithm assumes that the cases match - it's trivial to make the comparisons case insensitive by forcing the contents of the sets to be lowercase, and forcing the tokens that are tested to be lower case too. Also frozenset is python 3.

The behaviour of this is I think what you intended which is that if both one of the genus and species is present a line should be printed:

listofspecies = open("bacreduced.txt").readlines()
species_1 = []
species_2 = []
for line in listofspecies:
    species = line.strip().split('_')
    species_1.append(species[0])
    species_2.append(species[1])

species_1 = frozenset(species_1)
species_2 = frozenset(species_2)

with open("Bac16s.faa") as f:
    for line in f:
        if not line[0] == '>':
            continue
        line_tokens = line[1:].strip().split('_')
        if (line_tokens[0] in species_1) and (line_tokens[1] in species_2):
            print(line_tokens[0] + '_' + line_tokens[1])

but if what you wanted was to print a line if only the exact combination of genus and species is present, it's actually much easier:

listofspecies = open("bacreduced.txt").readlines()
species_list = []
for line in listofspecies:
    species = line.strip()
    species_list.append(species)

species_set = frozenset(species_list)

with open("Bac16s.faa") as f:
    for line in f:
        if not line[0] == '>':
            continue
        stripped_line = line[1:].strip()
        if stripped_line in species_set:
            print(stripped_line)

Following discussion over on this question I asked, I've decided I will delete my earlier answer, since there's a better way of doing this. The problem is the same one I identified earlier: you are using readlines which means you are having to read the whole file into memory when you want to be parsing it a line at a time, but the solution I suggested using a simple for loop becomes unwieldy because of the need to access the line after the one you are searching for, instead you want to use a generator to iterate over your file with knowledge of the current and next lines all the time:

def pairwiseLowMemory(iterable) :
    current = next(iterable, None)
    if not current :
        return
    while (True) :
        last = current
        current = next(iterable, None)
        if not current :
            break
        yield (last, current)

and then use a loop over pairwise to have both the current and next lines valid in your loop:

with open('Bac16s.faa', 'r') as fastaFile:
  for currentLine, nextLine in pairwiseLowMemory(fastaFile) :
    # do your check on currentLine
    # and then to print using the next line you can do:
    print ('>' + currentLine + nextLine)

This approach allows you to cleanly and elegantly process one line while knowing about the next, and the pairwise implementation allows this to be done efficiently and without unreasonable memory demands.

You could look into using online VMs(virtual machines). For example, microsofts azure :- https://azure.microsoft.com/en-gb/pricing/details/virtual-machines/linux/ (You can scroll down and see the server locations & ram sizes, maximum they provide is 256GB RAM). There are other providers too and maybe some free ones for lower ram sizes. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM