csv_slope_calc.py 1.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
'''
Created on 29.05.2018

@author: mirandaa
'''
import sys
import pandas as pd

def dataframe_slope(peaksDF):
    max = peaksDF.Intensity.max()
    tenPC = max / 10
    selected_peaks = peaksDF.loc[peaksDF.Intensity > tenPC]
    selected_peaks.sort_values('mz', inplace=True)
#     selected_peaks.plot.scatter('mz', 'resolution')
    idx_lowMZ = selected_peaks.head(10)['Resolution'].max() == selected_peaks['Resolution']
    idx_highMZ = selected_peaks.tail(10)['Resolution'].min() == selected_peaks['Resolution']
    print 'selected points are :'
    print selected_peaks[idx_lowMZ]
    print selected_peaks[idx_highMZ]
    slope = (selected_peaks[idx_highMZ].Resolution.values[-1] - selected_peaks[idx_lowMZ].Resolution.values[0]) / (selected_peaks[idx_highMZ].mz.values[-1] - selected_peaks[idx_lowMZ].mz.values[0])
    print ('slope of (res.highMZ - res.lowMZ ) /  (highMZ - lowMZ ) is {}'.format(slope))
    return slope

def rcc(filename):
    print ('clean csv')
    df = pd.read_csv(filename)
    df.dropna(inplace=True)
    df.head()
    df.columns = df.iloc[0]
    df.rename(index=str, columns={"m/z": "mz"},inplace =True)   
    df = df.iloc[1:]
    df.reset_index(inplace=True)
    df = df.apply(pd.to_numeric)
    
    slope = dataframe_slope(df)
    print('----')
    print('slope: {}'.format(slope))
    
if __name__ == '__main__':
    print ('provide *.csv file, copy/paste from excalibur with masses in the range expected for the slope')
    
    if len(sys.argv) <= 1:
        print ('provide *.csv file, copy/paste from excalibur')
        sys.exit(1)
    filename = ' '.join(sys.argv[1:])
    
    if str(filename[-4:]).lower() != '.csv':
        print ('provideded {}r'.format(filename))
        print ('provide *.csv file, copy/paste from excalibur')
        sys.exit(1)
    
    try:
        rcc(filename)
    except Exception as e:
        print(e)