Returns a dictionary mapping Ensembl transcript IDs to FPKM expression
    values or None if neither Cufflinks tracking file nor StringTie GTF file
    were specified.
167228
def rna_transcript_expression_dict_from_args(args):
    
    if args.rna_transcript_fpkm_tracking_file:
        return load_cufflinks_fpkm_dict(args.rna_transcript_fpkm_tracking_file)
    elif args.rna_transcript_fpkm_gtf_file:
        return load_transcript_fpkm_dict_from_gtf(
            args.rna_transcript_fpkm_gtf_file)
    else:
        return None
720434
def extract_mfd_params(src):
    
    tags = get_taglist(src)
    if "incrementalMFD" in tags:
        mfd_node = src.nodes[tags.index("incrementalMFD")]
    elif "truncGutenbergRichterMFD" in tags:
        mfd_node = src.nodes[tags.index("truncGutenbergRichterMFD")]
    elif "arbitraryMFD" in tags:
        mfd_node = src.nodes[tags.index("arbitraryMFD")]
    elif "YoungsCoppersmithMFD" in tags:
        mfd_node = src.nodes[tags.index("YoungsCoppersmithMFD")]
    else:
        raise ValueError("Source %s contains no supported MFD type!" % src.tag)
    data = []
    rates = []
    for key, param, vtype in MFD_PARAMS:
        if key in mfd_node.attrib and mfd_node.attrib[key] is not None:
            data.append((param, mfd_node.attrib[key]))
        else:
            data.append((param, None))
    if ("incrementalMFD" or "arbitraryMFD") in mfd_node.tag:
        # Extract Rates
        rates = ~mfd_node.occurRates
        n_r = len(rates)
        if n_r > MAX_RATES:
            raise ValueError("Number of rates in source %s too large "
                             "to be placed into shapefile" % src.tag)
        rate_dict = dict([(key, rates[i] if i < n_r else None)
                          for i, (key, _) in enumerate(RATE_PARAMS)])
    elif "YoungsCoppersmithMFD" in mfd_node.tag:
        rate_dict = dict([(key, mfd_node.attrib['characteristicRate'])
                          for i, (key, _) in enumerate(RATE_PARAMS)])
    else:
        rate_dict = dict([(key, None)
                          for i, (key, _) in enumerate(RATE_PARAMS)])
    return dict(data), rate_dict
1310750
def matchImpObjStrs(fdefs,imp_obj_strs,cdefs):
    ''''''
    imp_funcs=dict()
    imp_classes=dict()
    for source in imp_obj_strs:
        if not imp_obj_strs[source]:
            continue
        imp_funcs[source]=[]
        imp_classes[source]=[]
        for (mod,func) in imp_obj_strs[source]:
            if mod not in fdefs:
                #print(mod+" is not part of the project.")
                continue
            if func=='*':
                all_fns = [x for x in fdefs[mod] if x.name!='body']
                imp_funcs[source] += all_fns
                all_cls = [x for x in cdefs[mod]]
                imp_classes[source] += all_cls
            else:
                fn_node = [x for x in fdefs[mod] if x.name==func]
                cls_node = [x for x in cdefs[mod] if x.name==func]
                #assert len(fn_node) in [1,0]
                #assert len(cls_node) in [1,0]
                if cls_node:
                    imp_classes[source] += cls_node
                if fn_node:
                    imp_funcs[source] += fn_node
                if not fn_node and not cls_node:
                    pass
                    #print(func+' not found in function and class definitions.')
    return imp_funcs,imp_classes
241856
def _get_snpeff_transcript(self, transcript_info):
        
        transcript = Transcript(
                hgnc_symbol = transcript_info.get('Gene_Name'),
                transcript_id = transcript_info.get('Feature'),
                ensembl_id = transcript_info.get('Gene_ID'),
                biotype = transcript_info.get('Transcript_BioType'),
                consequence = transcript_info.get('Annotation'),
                exon = transcript_info.get('Rank'),
                HGVSc = transcript_info.get('HGVS.c'),
                HGVSp = transcript_info.get('HGVS.p')
            )
        return transcript
997327
def get_coding_noncoding_transcript_ids(gtf):
    
    coding_gtf = partition_gtf(gtf, coding=True)
    coding_db = get_gtf_db(coding_gtf)
    coding_ids = set([x['transcript_id'][0] for x in coding_db.all_features()
                  if 'transcript_id' in x.attributes])
    noncoding_gtf = partition_gtf(gtf)
    noncoding_db = get_gtf_db(noncoding_gtf)
    noncoding_ids = set([x['transcript_id'][0] for x in noncoding_db.all_features()
                     if 'transcript_id' in x.attributes])
    return coding_ids, noncoding_ids
46355
def fpkm(args):
    
    p = OptionParser(fpkm.__doc__)
    opts, args = p.parse_args(args)

    if len(args) < 2:
        sys.exit(not p.print_help())

    fastafile = args[0]
    bamfiles = args[1:]
    # Create a DUMMY gff file for cuffdiff
    gffile = fastafile.rsplit(".", 1)[0] + ".gff"
    if need_update(fastafile, gffile):
        fw = open(gffile, "w")
        f = Fasta(fastafile, lazy=True)
        for key, size in f.itersizes_ordered():
            print("\t".join(str(x) for x in (key, "dummy", "transcript",\
                1, size, ".", ".", ".", "ID=" + key)), file=fw)
        fw.close()
        logging.debug("Dummy GFF created: {0}".format(gffile))

    cmd = "cuffdiff {0} {1}".format(gffile, " ".join(bamfiles))
    sh(cmd)
477539
def get_rlzs_by_gsim(self, trt_or_grp_id, sm_id=None):
        
        if isinstance(trt_or_grp_id, (int, U16, U32)):  # grp_id
            trt = self.csm_info.trt_by_grp[trt_or_grp_id]
            sm_id = self.csm_info.get_sm_by_grp()[trt_or_grp_id]
        else:  # assume TRT string
            trt = trt_or_grp_id
        acc = collections.defaultdict(list)
        if sm_id is None:  # full dictionary
            for rlz, gsim_by_trt in zip(self.realizations, self.gsim_by_trt):
                acc[gsim_by_trt[trt]].append(rlz.ordinal)
        else:  # dictionary for the selected source model
            for rlz in self.rlzs_by_smodel[sm_id]:
                gsim_by_trt = self.gsim_by_trt[rlz.ordinal]
                try:  # if there is a single TRT
                    [gsim] = gsim_by_trt.values()
                except ValueError:  # there is more than 1 TRT
                    gsim = gsim_by_trt[trt]
                acc[gsim].append(rlz.ordinal)
        return {gsim: numpy.array(acc[gsim], dtype=U16)
                for gsim in sorted(acc)}
659944
def extract_texkeys_from_pdf(pdf_file):
    
    with open(pdf_file, 'rb') as pdf_stream:
        try:
            pdf = PdfFileReader(pdf_stream, strict=False)
            destinations = pdf.getNamedDestinations()
        except Exception:
            LOGGER.debug(u"PDF: Internal PyPDF2 error, no TeXkeys returned.")
            return []
        # not all named destinations point to references
        refs = [dest for dest in destinations.iteritems()
                if re_reference_in_dest.match(dest[0])]
        try:
            if _destinations_in_two_columns(pdf, refs):
                LOGGER.debug(u"PDF: Using two-column layout")

                def sortfunc(dest_couple):
                    return _destination_position(pdf, dest_couple[1])

            else:
                LOGGER.debug(u"PDF: Using single-column layout")

                def sortfunc(dest_couple):
                    (page, _, ypos, xpos) = _destination_position(
                        pdf, dest_couple[1])
                    return (page, ypos, xpos)

            refs.sort(key=sortfunc)
            # extract the TeXkey from the named destination name
            return [re_reference_in_dest.match(destname).group(1)
                    for (destname, _) in refs]
        except Exception:
            LOGGER.debug(u"PDF: Impossible to determine layout, no TeXkeys returned")
            return []
342686
def matchingfiles(self, projectpath):
        """"""
        results = []

        if projectpath[-1] == '/':
            inputpath = projectpath + 'input/'
        else:
            inputpath = projectpath + '/input/'

        for linkf,realf in clam.common.util.globsymlinks(inputpath + '/.*.INPUTTEMPLATE.' + self.id + '.*'):
            seqnr = int(linkf.split('.')[-1])
            results.append( (seqnr, realf[len(inputpath):], self) )
        results = sorted(results)
        if self.unique and len(results) != 1:
            return []
        else:
            return results
941396
def _collect_data(directory, input_ext, transcription_ext):
  """"""
  # Directory from string to tuple pair of strings
  # key: the filepath to a datafile including the datafile's basename. Example,
  #   if the datafile was "/path/to/datafile.wav" then the key would be
  #   "/path/to/datafile"
  # value: a pair of strings (media_filepath, label)
  data_files = {}
  for root, _, filenames in os.walk(directory):
    transcripts = [filename for filename in filenames
                   if transcription_ext in filename]
    for transcript in transcripts:
      transcript_path = os.path.join(root, transcript)
      with open(transcript_path, "r") as transcript_file:
        for transcript_line in transcript_file:
          line_contents = transcript_line.strip().split(" ", 1)
          media_base, label = line_contents
          key = os.path.join(root, media_base)
          assert key not in data_files
          media_name = "%s.%s"%(media_base, input_ext)
          media_path = os.path.join(root, media_name)
          data_files[key] = (media_base, media_path, label)
  return data_files
245435
def get_scenfit_labelings(instance,nm, OS, FP, FC, EP):
  '''
  
  '''
  sem = [sign_cons_prg, bwd_prop_prg]
  if OS : sem.append(one_state_prg)
  if FP : sem.append(fwd_prop_prg)
  if FC : sem.append(founded_prg)
  if EP : sem.append(elem_path_prg)

  inst     = instance.to_file()
  prg      = sem + scenfit + [inst]
  coptions = '--opt-strategy=5'
  solver   = GringoClasp(clasp_options=coptions)
  solution = solver.run(prg,collapseTerms=True,collapseAtoms=False)

  opt      = solution[0].score[0]

  prg      = prg + [show_labels_prg, show_err_prg]
  coptions = str(nm)+' --project --opt-strategy=5 --opt-mode=optN --opt-bound='+str(opt)
  solver2  = GringoClasp(clasp_options=coptions)
  models   = solver2.run(prg,collapseTerms=True,collapseAtoms=False)

  os.unlink(inst)
  return models
1368637
def get_ngrams(inputfile, n=1, use_transcript=False, use_vtt=False):
    '''
    
    '''

    words = []
    if use_transcript:
        for s in audiogrep.convert_timestamps(inputfile):
            for w in s['words']:
                words.append(w[0])
    elif use_vtt:
        vtts = get_vtt_files(inputfile)
        for vtt in vtts:
            with open(vtt['vtt'], 'r') as infile:
                sentences = parse_auto_sub(infile.read())
            for s in sentences:
                for w in s['words']:
                    words.append(w['word'])
    else:
        text = ''
        srts = get_subtitle_files(inputfile)
        for srt in srts:
            lines = clean_srt(srt)
            if lines:
                for timespan in lines.keys():
                    line = lines[timespan].strip()
                    text += line + ' '

        words = re.split(r'[.?!,:\"]+\s*|\s+', text)

    ngrams = zip(*[words[i:] for i in range(n)])
    return ngrams
800525
def get_label_map(opts):
    '''  '''
    results = {}
    try:
        for entry in os.scandir(diskdir):
            target = normpath(join(diskdir, os.readlink(entry.path)))
            decoded_name = entry.name.encode('utf8').decode('unicode_escape')
            results[target] = decoded_name
        if opts.debug:
            print('\n\nlabel_map:', results)
    except FileNotFoundError:
        pass
    return results
142964
def index_transcriptome(gtf_file, ref_file, data):
    
    gtf_fasta = gtf.gtf_to_fasta(gtf_file, ref_file)
    bowtie2_index = os.path.splitext(gtf_fasta)[0]
    bowtie2_build = config_utils.get_program("bowtie2", data["config"]) + "-build"
    cmd = "{bowtie2_build} --offrate 1 {gtf_fasta} {bowtie2_index}".format(**locals())
    message = "Creating transcriptome index of %s with bowtie2." % (gtf_fasta)
    do.run(cmd, message)
    return bowtie2_index
448805
def _get_compositekws(ckw_matches, spires=False):
    
    output = {}
    for composite_keyword, info in ckw_matches:
        output[composite_keyword.output(spires)] = {"number": len(info[0]),
                                                    "details": info[1]}
    output = [{'keyword': key,
               'number': value['number'],
               'details': value['details']}
              for key, value in output.iteritems()]
    return sorted(output, key=lambda x: x['number'], reverse=True)
228358
def _get_seqtype_from_ext(handle):
    ''''''
    if isinstance(handle, basestring):
        name = handle
    elif hasattr(handle, 'filename'):
        name = handle.filename
    elif hasattr(handle, 'name'):
        name = handle.name
    else:
        raise ValueError("Unknown datatype for handle!")

    modifier = ''
    dummy, ext = path.splitext(name.lower())
    if ext == ".gz":
        modifier = 'gz-'
        dummy, ext = path.splitext(dummy)

    if not ext:
        ext = "." + dummy

    if ext in (".gbk", ".gb", ".genbank", ".gbff"):
        return modifier + "genbank"
    elif ext in (".embl", ".emb"):
        return modifier + "embl"
    elif ext in (".fa", ".fasta", ".fna", ".faa", ".fas"):
        return modifier + "fasta"
    else:
        raise ValueError("Unknown file format '%s'." % ext)
1080878
def _get_singlekws(skw_matches, spires=False):
    
    output = {}
    for single_keyword, info in skw_matches:
        output[single_keyword.output(spires)] = len(info[0])
    output = [{'keyword': key, 'number': value}
              for key, value in output.iteritems()]
    return sorted(output, key=lambda x: x['number'], reverse=True)