def find_control_file(self,rep,default=None):
     '''Attempts to find an appropriate control file.'''
     # TODO Make more generic and move to dxencode.py when needed.
     
     (AUTHID,AUTHPW,SERVER) = dxencode.processkey(self.server_key)
     if 'controls' not in rep:
         return None
     for file_key in rep['controls']:
         if isinstance(file_key,list):
             file_key = file_key[0]
         file_obj = dxencode.enc_lookup_json(file_key,self.server_key,frame='embedded')
         rep_id = file_obj["replicate"]['@id']
         rep_obj = dxencode.enc_lookup_json(rep_id,self.server_key,frame='embedded')
         exp_id = rep_obj['experiment']['@id'].split('/')[2]
         rep_tech = "rep%s_%s" % \
                 (rep_obj['biological_replicate_number'], rep_obj['technical_replicate_number'])
         control_root = self.psv['control_path']
         # Cheating:
         if self.proj_name == "scratchPad" and self.psv['control_path'] == self.CONTROL_ROOT_FOLDER:
             control_root = "/lrna"
         path_n_glob = control_root + exp_id + '/' + rep_tech + '/' + self.CONTROL_FILE_GLOB
         target_folder = dxencode.find_folder(exp_id + '/' + rep_tech,self.project,control_root)
         #print "Target found [%s]" % target_folder
         if target_folder != None:
             path_n_glob = target_folder + '/' + self.CONTROL_FILE_GLOB
         fid = dxencode.find_file(path_n_glob,self.proj_id,multiple=False,recurse=False)
         if fid != None:
             return dxencode.file_path_from_fid(fid)
             
     if default != None:
         return default
     #print json.dumps(rep,indent=4)
     print "Unable to find control in search of %s" % rep['controls']
     sys.exit(1)
 def find_control_file(self,rep,default=None):
     '''Attempts to find an appropriate control file.'''
     # TODO Make more generic and move to dxencode.py when needed.
     
     (AUTHID,AUTHPW,SERVER) = dxencode.processkey(self.server_key)
     for file_key in rep['controls']:
         url = '%s%s/?format=json&frame=embedded' % (SERVER,file_key)
         #print '-- ' + AUTHID + " " + AUTHPW + " " + SERVER + " " + url
         try:
             response = dxencode.encoded_get(url, AUTHID, AUTHPW)
             file_obj = response.json()
         except:
             print "URL to control [%s] returned ?" % url
             print response
             sys.exit(1)
         #print json.dumps(response,indent=4)
         rep_id = file_obj["replicate"]['@id']
         url = '%s%s/?format=json&frame=embedded' % (SERVER,rep_id)
         try:
             response = dxencode.encoded_get(url, AUTHID, AUTHPW)
             rep_obj = response.json()
         except:
             print "URL to replicate [%s] returned ?" % url
             print response
             sys.exit(1)
         exp_id = rep_obj['experiment'].split('/')[2]
         rep_tech = "rep%s_%s" % \
                 (rep_obj['biological_replicate_number'], rep_obj['technical_replicate_number'])
         # default by cheating
         if self.proj_name == dxencode.PRODUCTION_PROJECT:
             control_root = "/long-RNA-seq/runs/"
         else:
             control_root = self.CONTROL_ROOT_FOLDER
         path_n_glob = control_root + exp_id + '/' + rep_tech + '/' + self.CONTROL_FILE_GLOB
         target_folder = dxencode.find_folder(exp_id + '/' + rep_tech,self.project,control_root)
         #print "Target found [%s]" % target_folder
         if target_folder != None:
             path_n_glob = target_folder + '/' + self.CONTROL_FILE_GLOB
         fid = dxencode.find_file(path_n_glob,self.proj_id,multiple=False,recurse=False)
         if fid != None:
             return dxencode.file_path_from_fid(fid)
             
     if default != None:
         return default
     print "Unable to find control in search of %s" % rep['controls']
     sys.exit(1)