How to find the full strings in List2 by searching with the substrings in List1?

Viewed 63

I have two lists composed of amino acid sequences (this is not a biology question, but stated for context) where List1 contains substrings (partial sequences) of full strings (complete sequences). List2 contains full strings where some contain the substrings from List1 and some of the strings do not.

These lists are large which is why they were made, but now I do not know how to trace the substrings to the full strings.

Below is a modified example of what the two lists look like using real data from my own data set. For the two substrings contained within List1 there should be two matches in List2. I have confirmed that the matches are in List2.

List1 = ['QSLNQNVVSRTCPAVVSHRARRAVRVMATGSPLTFSKYQGLGNDFILIDNRHTSEPVVTPEQAVKICDRNFGVGGDGVIFALPPVGETDLTMRIFNSDGSEPEMCGNGIRCLAKFVADIDKSSPRKYKIHTLAGLIQPELLADGQVRVDMGAPILDGSKVPTTLTPTEGNTVVQQDLVVDGKTYKVTCVSMGNPHAVIYTCNGKTIKIDDLESDLAALGPKFERNTVFPARTNTEFVEVISPSHVRMVVWERGAGRTLACGTGACALVVAGILEGRVDRSKTCRVDLPGGPLQIEWSTVDNHIYMTGPAELVFGGSLRV', 'DMRISYERGGLEEAAFRGRDPMQVFDEWFKAAVAGKVCEEPNAISLASSNPSGQPSVRVVLLKGYDERGFVFYTNYSSRKGTELESGSAAFSIYWEKLQRQIRVEGTVEHVSEEESTAYFHSRPRGSQIGAWVSAQSQPCRNRGEMEARNAELQQRFSDESVPVPKPPHWGGYLIRPTRIEFWQGRPSRLHDRIRFRRPSPNESWVMERLQP']
List2 = [Seq('SSLPSNSVWASGKSYLGHLY*CVHPAHTVTFTLPLVAA*YRALSYDVRRSKFLT...LHL', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('PLYHLILSGPLENPT*DTYTDAFILLTRSLSPSLS*PRNTALCHMTFAVQNFLL...CIF', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('LSTI*FCLGLWKILPRTPILMRSSCSHGHFHPPSRSRVIPRFVI*RSPFKISYS...TAS', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('EDAVIESKCGQSHMPGCCQPPGTQGCARNGYGIAPDVLQVSGPW*RFHLD*QSP...VER', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('KMQSLNQNVVSRTCPAVVSHRARRAVRVMATGSPLTFSKYQGLGNDFILIDNRH...*RG', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('RCSH*IKMWSVAHARLLSATGHAGLCA*WLRDRP*RSPSIRALVTISS*LTIAT...GRE', HasStopCodon(ExtendedIUPACProtein(), '*')) Seq('VLTHVVASDKELLARAVRWEALPSRKNLSGLHHPSAPKPLSNSQYYSKKKPIRL...DFV', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('FLHTWLLPTRSCSRVQSAGKHCQAEKTSQVCITHRRLSH*ATLNITVKKNQSVS...QTS', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('SYTRGCFRQGVARACSPLGSIAKQKKPLRSASPIGA*AIKQLSILQ*KKTNPSH...RLR', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('HEVCVSVT*QHYVLP*RTNLWGHPSSELLSRVRINC*LQLLSVLNQCSIAHHRA...CKN', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('TKSAFQ*HNNIMFFPNAQIYGDTPAPSCYHVCA*IANCNYYLCSINAV*HIIAP...CVR', HasStopCodon(ExtendedIUPACProtein(), '*')), Seq('RSLRFSNITTLCSSLTHKFMGTPQLRVAITCAHKLLTATIICAQSMQYSTSSRQ...V*E', HasStopCodon(ExtendedIUPACProtein(), '*'))]

Here is a highly condensed version of my script for more context:

import os
import xml.etree.ElementTree as ET
from Bio.Seq import Seq
from Bio.Alphabet import generic_dna

path_to_allxmlfiles = "path/to/xml/file/dir/"  # Path to a directory where a bunch of XML files are found.
xml_dir = os.listdir(path_to_allxmlfiles)

path_to_transcriptome = "path/to/transcriptome/file.fasta" #This is just a giant fasta file.
transcriptomefile = open(path_to_transcriptome, 'r')

List1=[]
for file in xml_dir:
    if file.endswith(".xml"):
        xml_file_path = os.path.join(path_to_allxmlfiles, file)
        xml_files = open(xml_file_path, 'r')
            for lines in xml_files:
                tree = ET.parse(xml_files)
                root = tree.getroot()

                for substring in root.findall("./BlastOutput_iterations/Iteration/Iteration_hits/Hit[1]/Hit_hsps/Hsp[1]/substring"):
                    SUBSTRING = substring.text
                    List1.append(SUBSTRING)
fullstrlist1 = []
fullstrlist2 = []
fullstrlist3 = []
fullstrlist4 = []
fullstrlist5 = []
fullstrlist6 = []
for line in transcriptomefile:
    if (stuff_was_done_here):

      A_lot_of_stuff_done_here_where_I_appended_full_strings_to_six_lists. # I am translating in 6 reading frames so this is necessary because each reading frame is unique.

        List2 = [fullstrlist1, fullstrlist2, fullstrlist3, fullstrlist4, fullstrlist5, fullstrlist6] #List2 is a combination of the six lists above.

for item in List2:
    if any(x in item for x in List1):
        print(item)

What is given back in print(item) are not the items that contain the substrings from List1 from what I can tell.

This is my first question on StackOverflow. Please let me know if any more detail is needed. I appreciate the help in advance.

1 Answers

I'm not quite sure what your asking, but here is a sample of 2 search methods...

#Sub Strings
List1 = ["Apple", "Mulberry"]

# List of lists
List2 = [
    ["Apple", "Grapefruit", "Guava"],
    ["Banana", "Blueberry", "Grape"],
    ["Lemon", "Lime"],
    ["Loquat", "Lychee", "Mango"],
    ["Mulberry", "Nectarine", "Strawberry"],
    ["Pomegranate", "Raspberry"]
]

#a substring to search for.
List3 = ["berry", "ime"]

print("Search for whole matching strings", List1)
for item in List2:
    if any(x in item for x in List1):
        print(item)

print('\n Substring search for substrings', List3)
for group in List2:
    for item in group:
        if any(sbs in item for sbs in List3):
            print(group)
            break # no need to keep searching the group

and the output

Search for whole matching strings ['Apple', 'Mulberry']
['Apple', 'Grapefruit', 'Guava']
['Mulberry', 'Nectarine', 'Strawberry']

 Substring search for substrings ['berry', 'ime']
['Banana', 'Blueberry', 'Grape']
['Lemon', 'Lime']
['Mulberry', 'Nectarine', 'Strawberry']
['Pomegranate', 'Raspberry']
Related