<jupyter_start><jupyter_text>Create cis spliced peptidesSince neo-fusion can only run with cis spliced peptides, we need to compare our results to theirs on those terms  1. Load fasta file<jupyter_code>import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
    sys.path.append(module_path)
module_path = os.path.abspath(os.path.join('../..'))
if module_path not in sys.path:
    sys.path.append(module_path)
    
from src.file_io import fasta

fasta_file = '../../testing framework/data/databases/100prots.fasta'
database = fasta.read(fasta_file, True)

database = {x['name']: x for x in database}<jupyter_output><empty_output><jupyter_text>2.  Generate the peptides, hybrid proteins and peptides<jupyter_code>from collections import namedtuple
from random import randint
hybridprotein = namedtuple('hybridprotein', ['protein', 'section_removed', 'start_index_removed', 'end_index_removed', 'sequence', 'name'])
hybridpeptide = namedtuple('hybridpeptide', ['left_parent_name', 'right_parent_name', 'sequence', 'start_index', 'peptide_name'])
maxlen = 25
maxpeplen = 26
minpeplen = 6

def create_cis_spliced_hybrid_proteins(protdict, n):
    protdictkeys = list(protdict.keys())
    lenprotdict = len(protdictkeys)
    hybrids = []
    for i in range(n):
        prot = protdict[protdictkeys[randint(0, lenprotdict-1)]]
        start_pos = randint(maxpeplen, len(prot['sequence']) - maxlen)
        length = randint(minpeplen, maxlen)
        hp = hybridprotein(prot['name'], prot['sequence'][start_pos: start_pos+length], start_pos, start_pos+length-1, prot['sequence'][:start_pos] + prot['sequence'][start_pos+length:], 'hybrid-{}-protein'.format(i))
        hybrids.append(hp)
    return hybrids
        
def create_cis_spliced_hybrid_peptides(listhybridprots, n):
    hybridpeps = []
    for i in range(n):
        a = randint(0, len(listhybridprots)-1)
        hybprot = listhybridprots[a]
        peplen = randint(minpeplen, maxpeplen)
        half = round(peplen/2)
        hp = hybridpeptide(hybprot.protein, hybprot.protein, hybprot.sequence[hybprot.start_index_removed - half - 1: hybprot.start_index_removed + half], hybprot.start_index_removed - half - 1, f'hybrid_peptide_{i}')
        hybridpeps.append(hp)
    return hybridpeps
                                                                                                                            
from modules.sequence_generation import proteins, peptides
test_directory = '../data/testing_output/cis_spliced/'

num_hybs = 5
min_length= 5
max_length = 20
num_peptides = 100
min_cont = 3 #min contribution for each side of a hybrid

# create peptides
non_hybrid_peps = peptides.gen_peptides([x for _, x in database.items()], num_peptides, min_length=min_length, max_length=max_length, digest='random', dist='beta')

hybridprots = create_cis_spliced_hybrid_proteins(database, num_hybs)
hybridpeps = create_cis_spliced_hybrid_peptides(hybridprots, num_hybs**2)

all_proteins_raw = [x for _,x in database.items()] + [dict(x._asdict()) for x in hybridprots]
all_peptides_raw = non_hybrid_peps + [dict(x._asdict()) for x in hybridpeps]

peps = {}
for i, pep in enumerate(all_peptides_raw):
    peps[i] = pep
    peps[i]['scan_no'] = i<jupyter_output><empty_output><jupyter_text>2.1 Save this info so that I can analyze it later from Neo-Fusion<jupyter_code>import json
experiment_info_file_name = 'experiment_info.json'

exp = {'database': fasta_file, 'peptides': peps}
with open(test_directory + experiment_info_file_name, 'w') as o:
    json.dump(exp, o)<jupyter_output><empty_output><jupyter_text>3. Generate spectra<jupyter_code>from src.spectra import gen_spectra
from src import utils
from modules.sequence_generation import write_spectra, realistic_spectra

utils.make_dir(test_directory)
spectra = []

sequences = [pep['sequence'] for _, pep in peps.items()]

spectra = realistic_spectra.gen_realistic_spectra(sequences)

write_spectra.write_mzml('realisticCisSplicedSpectra', [x._asdict() for x in spectra], output_dir=test_directory)<jupyter_output>Determination of memory status is not supported on this 
 platform, measuring for memoryleaks will never fail<jupyter_text>test and fix any mess ups<jupyter_code>import xml.etree.ElementTree as ET
ET.register_namespace('', "http://psi.hupo.org/ms/mzml")
tree = ET.parse(test_directory + 'testSpectraFile.mzML')
mzml = tree.find('{http://psi.hupo.org/ms/mzml}mzML')
# add an id to the mzml
mzml.set('id', 'testSpectraFileFixed')
mzml.set('xmlns', "http://psi.hupo.org/ms/mzml")
mzml.set('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')
# remove some info from the mzml header [accession, version]
toDelMzml = ['accession']
for todel in toDelMzml:
    del mzml.attrib[todel]
    
# add file description stuff to the fileDescription tag
fileDescription = mzml.find('{http://psi.hupo.org/ms/mzml}fileDescription')


sourceFileElement = ET.fromstring('<sourceFileList count="1"> <sourceFile id="NOD2_E3.mzXML" name="NOD2_E3.mzXML" location="file:///C:/Users/zachmcgrath/Downloads"> <cvParam cvRef="MS" accession="MS:1000776" name="scan number only nativeID format" value=""/> <cvParam cvRef="MS" accession="MS:1000566" name="ISB mzXML format" value=""/> <cvParam cvRef="MS" accession="MS:1000569" name="SHA-1" value="89ecb0dd31ca3a2fdf5ef2c4f5341f6e5e9f06f0"/> </sourceFile> </sourceFileList>')
fileDescription.append(sourceFileElement)

instrumentConfigurationList = mzml.find('{http://psi.hupo.org/ms/mzml}instrumentConfigurationList')

instrumentConfiguration = ET.fromstring(' <instrumentConfiguration id="IC1"> <componentList count="3"> <source order="1"> <userParam name="msIonisation" value="HPLC-Chip/MS"/>  </source> <analyzer order="1"> <userParam name="msMassAnalyzer" value="Q-TOF"/>  </analyzer> <detector order="1"> <userParam name="msDetector" value="ADC"/> </detector> </componentList> </instrumentConfiguration>')
for child in instrumentConfigurationList:
    del child

instrumentConfigurationList.append(instrumentConfiguration)

run = mzml.find('{http://psi.hupo.org/ms/mzml}run')
spectrumList = run.find('{http://psi.hupo.org/ms/mzml}spectrumList')
print(len(spectrumList))
for spectrumElement in spectrumList:
    # need to add this
    # <cvParam cvRef="MS" accession="MS:1000127" name="centroid spectrum" value=""/>
    centroidElement = ET.Element('{http://psi.hupo.org/ms/mzml}cvParam', attrib={'name': 'centroid spectrum', 'accession':'MS:1000127', 'value': ''})
    pl = spectrumElement.find('{http://psi.hupo.org/ms/mzml}precursorList')
    bdal = spectrumElement.find('{http://psi.hupo.org/ms/mzml}binaryDataArrayList')
    if bdal is None or pl is None:
        print(spectrumElement)
        spectrumList.remove(spectrumElement)
        
    # add the range 
    scanListEl = spectrumElement.find('{http://psi.hupo.org/ms/mzml}scanList')
    scanEl = scanListEl.find('{http://psi.hupo.org/ms/mzml}scan')[0]
    scanWindowListElement = ET.fromstring('<scanWindowList count="1"> <scanWindow> <cvParam cvRef="MS" accession="MS:1000501" value="0" name="scan window lower limit" unitAccession="MS:1000040" unitName="m/z" unitCvRef="MS" /> <cvParam cvRef="MS" accession="MS:1000500" value="10000" name="scan window upper limit" unitAccession="MS:1000040" unitName="m/z" unitCvRef="MS" /> </scanWindow></scanWindowList>')
    scanEl.append(scanWindowListElement)
    
    spectrumElement.append(centroidElement)

tree.write(test_directory + 'testSpectraFileFixed.mzML', encoding='utf-8', xml_declaration=True)
    
'''
inside <instrumentConfiguration>
<componentList count="3">
          <source order="1">
            <userParam name="msIonisation" value="HPLC-Chip/MS"/>
          </source>
          <analyzer order="1">
            <userParam name="msMassAnalyzer" value="Q-TOF"/>
          </analyzer>
          <detector order="1">
            <userParam name="msDetector" value="ADC"/>
          </detector>
        </componentList>
        
inside <fileDescription>     
<sourceFileList count="2">
        <sourceFile id="NOD2_E3.mzXML" name="NOD2_E3.mzXML" location="file:///C:\Users\zachmcgrath\Downloads">
          <cvParam cvRef="MS" accession="MS:1000776" name="scan number only nativeID format" value=""/>
          <cvParam cvRef="MS" accession="MS:1000566" name="ISB mzXML format" value=""/>
          <cvParam cvRef="MS" accession="MS:1000569" name="SHA-1" value="89ecb0dd31ca3a2fdf5ef2c4f5341f6e5e9f06f0"/>
        </sourceFile>
      </sourceFileList>
      
inside run -> spectrumList -> spectrum -> scanList -> scan
<scanWindowList count="1">
                <scanWindow>
                  <cvParam cvRef="MS" accession="MS:1000501" value="0" name="scan window lower limit" unitAccession="MS:1000040" unitName="m/z" unitCvRef="MS" />
                  <cvParam cvRef="MS" accession="MS:1000500" value="4000" name="scan window upper limit" unitAccession="MS:1000040" unitName="m/z" unitCvRef="MS" />
                </scanWindow>
              </scanWindowList>
'''
import base64
import struct

base64.encodebytes(struct.pack(f'<{1025}f', *[i+1 for i in range(1025)]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13850202873122053, 'fraction_numerical': 0.025880030705121175, 'mean_word_length': 4.62962962962963, 'pattern_counts': {'":': 0, '<': 74, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '266907', 'license': 'MIT', 'n_tokens_mistral': 3312, 'n_tokens_neox': 3061, 'n_words': 608, 'path': 'sandbox/jupyter notebooks/create cis spliced peptides.ipynb', 'repo_name': 'jamessdixon/hypedsearch', 'size': '9119'}