<jupyter_start><jupyter_text>Next two cells taken froma Ava's notebook to generate test df<jupyter_code># import libraries and define associated email for queries
from Bio import Entrez
import time
import pandas as pd

Entrez.email ="<EMAIL>"
# define gis and use Biopython.Entrez to pull protein sequences from NCBI

gis = [15595384, 15595400] # gi = gi number, input is PROTEIN_GI
                         # eventually replace with list
request = Entrez.epost("protein",id=",".join(map(str,gis)))
result = Entrez.read(request)
webEnv = result["WebEnv"]
queryKey = result["QueryKey"]
handle = Entrez.efetch(db="protein",retmode="xml", webenv=webEnv, query_key=queryKey)

d = [] # empty dataframe 

for r in Entrez.parse(handle):
    try:
        gi=int([x for x in r['GBSeq_other-seqids'] if "gi" in x][0].split("|")[1])
    except ValueError:
        gi=None
    #print (">GI ",gi," " + r["GBSeq_primary-accession"] + " " + r["GBSeq_definition"] + "\n" + r["GBSeq_sequence"])
    #print (">GI ",gi," " +  r["GBSeq_sequence"])
    d.append((gi, r['GBSeq_sequence']))
    
seq_data = pd.DataFrame(d, columns=('GI', 'PROTEIN_SEQ'))
seq_data
seq_list = list(seq_data['PROTEIN_SEQ'].to_list())
seq_list
pair_list = []

for a, b in itertools.combinations(seq_list, 2): 
    pair_list.append([a, b])
    
print(pair_list)


'''Optimal alignments too large!'''

aligner = Align.PairwiseAligner()
aligner.substitution_matrix = substitution_matrices.load('BLOSUM62')
alignments = aligner.align(str.upper((seq_list[0])), str.upper((seq_list[1])))
#alignments = aligner.align(str.upper("KEVLa"), str.upper("EVL"))
alignments = list(alignments)
print("Number of alignment %d" % len(alignments))<jupyter_output><empty_output><jupyter_text>Psuedo Code:- read csv column containing sequences as a list of strings- use single for loop with `itertools.combinations(, 2)` and `Align.PairwiseAligner()` to get new list of alignement scores.- Issues to solve:    - Limit each sequence pair to 1 alignment (sometimes it likes to give 2)    - Ensure scoring params are same as blastn (https://www.ncbi.nlm.nih.gov/books/NBK279684/_appendices_BLAST_Substitution_Matrices_)<jupyter_code>with open('Alignment_RM_Test.csv', 'r') as csv_file:
    lines = csv_file.readlines()
    
headers = lines[0].rstrip().split(',') # rstrip removes end-of-line chars
numLines = len(lines)

# create lineList to include only numLines elements
linelist = [x.rstrip().split(',')
           for x in lines[1:numLines+1]] 

# list comprehension within dictionary comprehension to split each element by its headers and create dictionary of lists
outputDict = {keyVal:[x[idx] 
                       for x in linelist 
                       if len(x) == len(headers)] 
              for idx, keyVal in enumerate (headers)} 


outputDict # print dict to make sure everything worked
# convert our desired column(s) from dict values into list
seq_list = list(outputDict.get('y'))
print(seq_list)
pair_list = []

for a, b in itertools.combinations(seq_list, 2): 
    pair_list.append([a, b])
    
print(pair_list)

aligner = Align.PairwiseAligner()
aligner.substitution_matrix = substitution_matrices.load('BLOSUM62')
#aligner.mode = 'local'

alignments_list = []

for pair in pair_list:
    alignments_list.append(aligner.align(pair[0], pair[1]))
    
alignments_list # check to make sure correct number of alignments generated
'''play code (not necessary for running entire notebook)'''

def avg_score(alignments_list):
    
    # Finding length of the list
    length = len(alignments_list) 
    i = 0
    y = []
    
    # While Loop to iterate through list
    while i < length:
        x = []
        for alignment in test_alignment:
            x.append(alignment.score)
        y.append(np.mean(x))
        i += 1
    return y

'''this returns the correct number of scores, but the values are wrong.
  averaged values should not all be four.  this is obvious when you print the scores
  for the first alignment only, which returns two alignment scores, both of 7.'''
avg_score(alignments_list)  
'''Need to figure out how to get one score value back for every alignment'''




score_list = []
avg_score_list = []

for alignments in alignments_list:
    for alignment in alignments:
        score_list.append(alignment.score)
        avg_score_list = np.mean(score_list)
    #avg_score_list.append(np.mean(score_list))
    

print(score_list)
#print(avg_score_list)
pair_list
print(len(pair_list))
'''waaaaaaaaay too big.  we need to get this value down to 21'''


score_list
print(len(score_list))
alignment_df = pd.DataFrame(pair_list)
alignment_df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11726665229575339, 'fraction_numerical': 0.009700366458288425, 'mean_word_length': 4.0989010989010985, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '197951', 'license': 'MIT', 'n_tokens_mistral': 1559, 'n_tokens_neox': 1473, 'n_words': 432, 'path': 'notebooks/Alignment_RM.ipynb', 'repo_name': 'UWInSpace/InSpace', 'size': '4645'}