def calc_quantity(self, file_num, user_id, live_purchase): """ Calculates quantity of product needed to purchase from analyzing the provided file If live_purchase is True, free trials are decremented from the db """ db=current.db self.file_num = file_num self.free_trial = False if(self.prod_name == current.product_names['AlignmentQC']): user_row = db(db.auth_user.id==user_id).select().first() app = db(db.app_data.id > 0).select().first() bs_api = BaseSpaceAPI(app.client_id, app.client_secret, app.baseSpaceUrl, app.version, "", user_row.access_token) input_file = bs_api.getFileById(file_num) # BAM files less than 100 MB are free if input_file.Size < 100*(2**20): self.prod_quantity = 0 else: # determine if free trial trial_row = db((db.free_trial.user_id==user_row.id) & (db.free_trial.product_id==self.prod_id) & (db.free_trial.trials > 0)).select().first() if trial_row: # decrement trials in db if live_purchase: trial_row.update_record(trials=int(trial_row.trials) - 1) self.prod_quantity = 0 self.free_trial = True else: self.prod_quantity = 1 else: raise UnrecognizedProductException(self.prod_name)
def get_file_url(self, file_num, app_session_id): """ Returns the S3 link to the provided file """ db = current.db # get access token for app session's user (can't use current user since may be accessing from scheduler worker) ssn_row = db(db.app_session.id==app_session_id).select().first() user_row = db(db.auth_user.id==ssn_row.user_id).select().first() app = db(db.app_data.id > 0).select().first() bs_api = BaseSpaceAPI(app.client_id, app.client_secret, app.baseSpaceUrl, app.version, ssn_row.app_session_num, user_row.access_token) f = bs_api.getFileById(file_num) return f.getFileUrl(bs_api)
def download_file(self, file_num, local_dir, app_session_id): """ Download a file from BaseSpace into the provided directory (created if doesn't exist) """ db = current.db # get access token for app session's user (can't use current user since may be accessing from scheduler worker) ssn_row = db(db.app_session.id==app_session_id).select().first() user_row = db(db.auth_user.id==ssn_row.user_id).select().first() # get file info from BaseSpace app = db(db.app_data.id > 0).select().first() bs_api = BaseSpaceAPI(app.client_id, app.client_secret, app.baseSpaceUrl, app.version, ssn_row.app_session_num, user_row.access_token) f = bs_api.getFileById(file_num) # create local_path dir if it doesn't exist if not os.path.exists(local_dir): os.makedirs(local_dir) # write downloaded data to new file f.downloadFile(bs_api,local_dir) self.local_path = os.path.join(local_dir, f.Name)
samples = singleProject.getSamples(myAPI) print " The samples for project " + str(singleProject) + " are \n\t" + str(samples) # ## we'll take a further look at the files belonging to the sample and ##analyses from the last project in the loop above for a in appResults: print "# " + a.Id ff = a.getFiles(myAPI) print ff for s in samples: print "Sample " + str(s) ff = s.getFiles(myAPI) print ff ## Now let's do some work with files ## we'll grab a BAM by id and get the coverage for an interval + accompanying meta-data myBam = myAPI.getFileById('2150156') print myBam cov = myBam.getIntervalCoverage(myAPI,'chr2','1','20000') print cov covMeta = myBam.getCoverageMeta(myAPI,'chr2') print covMeta # ## and a vcf file myVCF = myAPI.getFileById('2150158') ##Let's get the variant meta info varMeta = myVCF.getVariantMeta(myAPI) print varMeta var = myVCF.filterVariant(myAPI,'2','1', '11000') print var
myAppSession = appResults.AppSession print myAppSession # we can change the status of our AppSession and add a status-summary as follows myAppSession.setStatus(myBaseSpaceAPI,'needsattention',"We worked hard, but encountered some trouble.") print "\nAfter a change of status of the app sessions we get\n" + str(myAppSession) # we'll set our appSession back to running so we can do some more work myAppSession.setStatus(myBaseSpaceAPI,'running',"Back on track") ### Let's list all AppResults again and see if our new object shows up appRes = p.getAppResults(myBaseSpaceAPI,statuses=['Running']) print "\nThe updated app results are \n" + str(appRes) appResult2 = myBaseSpaceAPI.getAppResultById(appResults.Id) print appResult2 ## Now we will make another AppResult ## and try to upload a file to it appResults2 = p.createAppResult(myBaseSpaceAPI,"My second AppResult","This one I will upload to") appResults2.uploadFile(myBaseSpaceAPI, '/home/mkallberg/Desktop/testFile2.txt', 'BaseSpaceTestFile.txt', '/mydir/', 'text/plain') print "\nMy AppResult number 2 \n" + str(appResults2) ## let's see if our new file made it appResultFiles = appResults2.getFiles(myBaseSpaceAPI) print "\nThese are the files in the appResult" print appResultFiles f = appResultFiles[-1] # we can even download our newly uploaded file f = myBaseSpaceAPI.getFileById(f.Id) f.downloadFile(myBaseSpaceAPI,'/home/mkallberg/Desktop/')
singleProject) + " are \n\t" + str(samples) # ## we'll take a further look at the files belonging to the sample and ##analyses from the last project in the loop above for a in appResults: print "# " + a.Id ff = a.getFiles(myAPI) print ff for s in samples: print "Sample " + str(s) ff = s.getFiles(myAPI) print ff ## Now let's do some work with files ## we'll grab a BAM by id and get the coverage for an interval + accompanying meta-data myBam = myAPI.getFileById('9895890') print myBam cov = myBam.getIntervalCoverage(myAPI, 'chr', '1', '100') print cov try: covMeta = myBam.getCoverageMeta(myAPI, 'chr') except Exception as e: print "Coverage metadata may not be available for this BAM file: %s" % str( e) else: print covMeta # ## and a vcf file myVCF = myAPI.getFileById('9895892') varMeta = myVCF.getVariantMeta(myAPI) print varMeta
# ## we'll take a further look at the files belonging to the sample and ##analyses from the last project in the loop above for a in appResults: print "# " + a.Id ff = a.getFiles(myAPI) print ff for s in samples: print "Sample " + str(s) ff = s.getFiles(myAPI) print ff ## Now let's do some work with files ## we'll grab a BAM by id and get the coverage for an interval + accompanying meta-data myBam = myAPI.getFileById('9895890') print myBam cov = myBam.getIntervalCoverage(myAPI,'chr','1','100') print cov try: covMeta = myBam.getCoverageMeta(myAPI,'chr') except Exception as e: print "Coverage metadata may not be available for this BAM file: %s" % str(e) else: print covMeta # ## and a vcf file myVCF = myAPI.getFileById('9895892') varMeta = myVCF.getVariantMeta(myAPI) print varMeta var = myVCF.filterVariant(myAPI,'chr','1', '25000')
print myAppSession # we can change the status of our AppSession and add a status-summary as follows myAppSession.setStatus(myAPI, "needsattention", "We worked hard, but encountered some trouble.") print "\nAfter a change of status of the app sessions we get\n" + str(myAppSession) # we'll set our appSession back to running so we can do some more work myAppSession.setStatus(myAPI, "running", "Back on track") ### Let's list all AppResults again and see if our new object shows up appRes = p.getAppResults(myAPI, statuses=["Running"]) print "\nThe updated app results are \n" + str(appRes) appResult2 = myAPI.getAppResultById(appResults.Id) print appResult2 ## Now we will make another AppResult ## and try to upload a file to it appResults2 = p.createAppResult(myAPI, "My second AppResult", "This one I will upload to") appResults2.uploadFile(myAPI, "/home/mkallberg/Desktop/testFile2.txt", "BaseSpaceTestFile.txt", "/mydir/", "text/plain") print "\nMy AppResult number 2 \n" + str(appResults2) ## let's see if our new file made it appResultFiles = appResults2.getFiles(myAPI) print "\nThese are the files in the appResult" print appResultFiles f = appResultFiles[-1] # we can even download our newly uploaded file f = myAPI.getFileById(f.Id) f.downloadFile(myAPI, "/home/mkallberg/Desktop/")
myAppSession.setStatus(myAPI, 'needsattention', "We worked hard, but encountered some trouble.") print("\nAfter a change of status of the app sessions we get\n" + str(myAppSession)) # we'll set our appSession back to running so we can do some more work myAppSession.setStatus(myAPI, 'running', "Back on track") ### Let's list all AppResults again and see if our new object shows up appRes = p.getAppResults(myAPI, statuses=['Running']) print("\nThe updated app results are \n" + str(appRes)) appResult2 = myAPI.getAppResultById(appResults.Id) print(appResult2) ## Now we will make another AppResult ## and try to upload a file to it appResults2 = p.createAppResult(myAPI, "My second AppResult", "This one I will upload to") appResults2.uploadFile(myAPI, '/home/mkallberg/Desktop/testFile2.txt', 'BaseSpaceTestFile.txt', '/mydir/', 'text/plain') print("\nMy AppResult number 2 \n" + str(appResults2)) ## let's see if our new file made it appResultFiles = appResults2.getFiles(myAPI) print("\nThese are the files in the appResult") print(appResultFiles) f = appResultFiles[-1] # we can even download our newly uploaded file f = myAPI.getFileById(f.Id) f.downloadFile(myAPI, '/home/mkallberg/Desktop/')