I do experiments where I grow strains of yeast in multiple flasks and take samples I obtain data from for different chemicals.
I've set up a SQLite table with the fields StrainId, FlaskNum, DateTime, HarvestIndex, Species, NumLabeledCarbons, and MDV.
I'm interested in obtaining the MDV data for the earliest sample harvested from each flask for all metabolites in the latest experiment corresponding to that particular strain.
I've written a Python script using the sqlite3 package to retrieve the data I need using a series of for-loops and it works but I suspect there might be a more SQL-centric way to obtain and group the individual data.
Here's what the fetching code basically looks like:
import sqlite3 as lite
con = lite.connect('fluxData.db')
with con:
cur = con.cursor()
for strainId in cur.execute("SELECT DISTINCT StrainId FROM LCMS WHERE StrainId IN ('U','S','UG','SG');").fetchall():
for dateVal in cur.execute("SELECT max(date(DateTime)) FROM LCMS WHERE StrainId=?",[strainId[0]]).fetchall():
for flaskNum in cur.execute("SELECT DISTINCT FlaskNum FROM LCMS WHERE StrainId=? AND date(DateTime)=?",[strainId[0],dateVal[0]]).fetchall():
for harvestIndex in cur.execute("SELECT min(HarvestIndex) FROM LCMS").fetchall():
for species in cur.execute("SELECT DISTINCT Species FROM LCMS WHERE StrainId=? AND date(DateTime)=? AND FlaskNum=? AND HarvestIndex=?",[strainId[0],dateVal[0],flaskNum[0],harvestIndex[0]]).fetchall():
print '\n'
for row in cur.execute("SELECT NumLabeledCarbons,MDV FROM LCMS WHERE StrainId=? AND date(DateTime)=? AND FlaskNum=? AND HarvestIndex=? AND Species=? ORDER BY NumLabeledCarbons",[strainId[0],dateVal[0],flaskNum[0],harvestIndex[0],species[0]]).fetchall():
print row
After I obtain the data I group data with the same species and then average them within each group with the same strain.
I'm wondering if this is the best way to do this? Is there a way fetch all of the grouped data using just SQL statements?
Aucun commentaire:
Enregistrer un commentaire