diff --git a/psmfragmentation/psmfragmentation.py b/psmfragmentation/psmfragmentation.py index b0a882c..81d171f 100644 --- a/psmfragmentation/psmfragmentation.py +++ b/psmfragmentation/psmfragmentation.py @@ -113,9 +113,7 @@ def __init__(self, obj): super(TabularResults, self).__init__() self._results = {} self._tolerance = obj['tolerance'] - self._keys = ('scan_number', 'scan_file', 'max_cont_b', 'max_cont_y', 'pct_pks_matched', - 'num_peaks_unmatched_10pct', 'tic', 'precursor_mz', 'retention_time') - + def _unmatched_peaks(self): for _, entry in self._results.items(): max_int_threshold = max(entry['scan']['intensity'], default=0) * .10 @@ -139,10 +137,12 @@ def _fragment_percent(self): self._results[psmid]['pct_b_peaks_matched'] = round((num_match_b/num_b) * 100.0, 2) self._results[psmid]['pct_y_peaks_matched'] = round((num_match_y/num_y) * 100.0, 2) - def add_psm(self, psm, scan_file_name): - obj = {k:v for k,v in psm.items()} - self._results[psm['ID']] = obj - self._results[psm['ID']]['scanFile'] = scan_file_name + #def add_psm(self, psm, scan_file_name): + def add_psm(self, conf_obj): + obj = {k:v for k,v in conf_obj['psm'].items()} + self._results[conf_obj['psm']['ID']] = obj + self._results[conf_obj['psm']['ID']]['scanFile'] = conf_obj['scan_filename'] + self._results[conf_obj['psm']['ID']]['peptideSequence'] = conf_obj['pepseq'] def add_ionruns(self, psm_id, matches): self._results[psm_id]['matched_peaks'] = matches @@ -158,9 +158,9 @@ def add_frags(self, psm_id, fragments): def generate_output(self): self._fragment_percent() self._unmatched_peaks() - r_keys = ('spectrumID', 'spectrumTitle', 'scanFile','PSPSMScore', 'PSPSMConfidence','TIC','max_b_run','max_y_run','pct_b_peaks_matched', 'pct_y_peaks_matched','num_peaks_unmatched_10pct') - with open('results.txt', 'w') as f: - f.write(f"spectrumID,spectrumTitle,scanFile,PSScore,PSConfidence,TIC,Max_B_Run, Max_Y_Run,PCT_B_Matched, PCT_Y_Matched,Num_10Pct_Peaks_Unmatched") + r_keys = ('peptideSequence','spectrumID', 'spectrumTitle', 'scanFile','PSPSMScore', 'PSPSMConfidence','TIC','max_b_run','max_y_run','pct_b_peaks_matched', 'pct_y_peaks_matched','num_peaks_unmatched_10pct') + with open('results.csv', 'w') as f: + f.write(f"sequence,spectrumID,spectrumTitle,scanFile,PSScore,PSConfidence,TIC,Max_B_Run, Max_Y_Run,PCT_B_Matched, PCT_Y_Matched,Num_10Pct_Peaks_Unmatched") f.write(os.linesep) for _, entry in self._results.items(): line = '' @@ -618,7 +618,7 @@ def score_psms(db_path, sequence_file, ion_types=('b', 'b-H2O', 'b-NH3','y', 'y- logger.info(f"No consecutive ion run for {pep_seq}") else: #add psm to tabular results - tabular_results.add_psm(tp, scan['scanFile']) + tabular_results.add_psm({'psm':tp, 'scan_filename':scan['scanFile'], 'pepseq':pep_seq}) tabular_results.add_ionruns(tp['ID'], matched_peaks) tabular_results.add_scan(tp['ID'], {'mz': _to_float(scan['mzValues']), 'intensity': _to_float(scan["intensities"])}) tabular_results.add_frags(tp['ID'], frags)