Python Forum
extracting sublist from a large multiple molecular file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extracting sublist from a large multiple molecular file
#1
I am using Phyton 3.7 in last version of Spider

I have a list of about 500 chemical molecule IDs (ten number digits each) which I selected for significant recognition of a protein among 300000 molecule-drug possibilities in one large sdf file of about 2G.

Now, I need to extract the complete 500 sdf records (each one contains about 400 lines between their ID to the final $$$$ line) from the large sdf 2G file and then to rejoin all 500 in one new "best" sdf file, required to fine tune further studies with them.

After googling and consulting during days, I came out to my best unsufficient code.

The code was tested with a txt file containing only 3 IDs (3bestSN.txt) to be selected among an sdf file of only 5 molecules (SNII.5mol.sdf).
However, it did not work as it was expected, and it was very VERY slow !!!!.

Could anybody suggest an alternative solution?
I will really appreciatted it!!!

Thanks, julio

Here is the final code of my best-lousy aproximation:


import sys
#----------------------------------------------------------------DECLARACION DE VARIABLES 
input_f1 = "3bestSN.txt"
input_f2 = "SNII.5mol.sdf"
output_f3 = "best_SN.sdf"
i = 0 # nº SN readed
j = 0  # nº Molecules $$$$ readed
lines1 = [] # mimic of the bestSN ~500 IDs SNxxxxxxxx
lines2 = [] # mimic of a Druglibrary of 300000 molecule IDs SNxxxxxxxx
past_mol = []
found = False
chunk = []

#·----------------------------------------------------------------
f1 = open(input_f1, "r", errors='ignore')    # best SN file to open
#print("Input file1: ", input_f1) 
lines1 = f1.read().split("\n")          # Create a list containing each SN in one line
#print(lines1)
for i in lines1:
    linea_actual = i.rstrip()
    print(linea_actual)
    f1.close() 
    f2 = open(input_f2, "r", errors='ignore')
    f3 = open(output_f3 + '.sdf', 'w')
    #print("Input file2: ", input_f2)    
    for line in f2:
        sline = line.rstrip()
        if sline == linea_actual:
            found = True
            past_mol.append(sline)
        elif sline == '$$$$':
            chunk.append(line)                
            found = False
        if found:
            chunk.append(line)
            f3.write(line)
            print("este es el chunk  ", chunk)
            print("este es el past_mol", past_mol)
f2.close()
Reply
#2
Please repost the code using python tags, rather then phyton. The language is named python, after Monty Python, not phyton after part of a plant.
Using the tags preserves indentation which is very important in Python.
Reply
#3
sorry for the h mistake and thank you for the correction........

I was wondering why the code did not look as in the Spyder

IT LOOKS MUCH BETTER NOW!

in case it may help
I copy only part of the response I got in printing the chunk.
The whole response is too long and repetitive.
A similar response but no writing into the f3 file I also got for the past_mol

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading large crapy text file in anaconda to profile data syamatunuguntla 0 831 Nov-18-2022, 06:15 PM
Last Post: syamatunuguntla
  Chunking and Sorting a large file Robotguy 1 3,574 Jul-29-2020, 12:48 AM
Last Post: Larz60+
  XLSX file with multiple sheets to josn file ovidius 2 2,225 Apr-05-2020, 09:22 AM
Last Post: ovidius
  Sorting a large CVS file DavidTheGrockle 1 2,053 Oct-31-2019, 12:32 PM
Last Post: ichabod801
  large csv to many xlsx containing multiple tabs thatIsTheCase 3 4,401 Nov-27-2018, 02:34 PM
Last Post: thatIsTheCase
  Searching a .txt file for a specific number and extracting the corresponding data nrozman 3 3,223 Jul-27-2018, 02:07 PM
Last Post: nrozman
  How to filter specific rows from large data file Ariane 7 8,197 Jun-29-2018, 02:43 PM
Last Post: gontajones
  access a very large file? As an array or as a dataframe? Angelika 5 4,908 May-18-2017, 08:15 AM
Last Post: Angelika

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020