Function for extracting extra seqeunce data to the query
         alignment if the full reference length are not covered
186795
def get_query_align(hit, contig):
      

      # Getting data needed to extract sequences
      query_seq = hit['query_string']
      homo_seq = hit['homo_string']
      sbjct_start = int(hit['sbjct_start'])
      sbjct_end = int(hit['sbjct_end'])
      query_start = int(hit['query_start'])
      query_end = int(hit['query_end'])
      length = int(hit['sbjct_length'])

      # If the alignment doesn't start at the first position data is
      # added to the begnning
      if sbjct_start != 1:
         missing = sbjct_start - 1

         if(query_start >= missing and hit['strand'] != 1
            or hit['strand'] == 1 and missing <= (len(contig) - query_end)):

            # Getting the query sequence.
            # If the the hit is on the other strand the characters
            # are reversed.
            if hit['strand'] == 1:
               start_pos = query_end
               end_pos = query_end + missing
               chars = contig[start_pos:end_pos]
               chars = Blaster.reversecomplement(chars)
            else:
               start_pos = query_start - missing - 1
               end_pos = query_start - 1
               chars = contig[start_pos:end_pos]

            query_seq = chars + str(query_seq)
         else:
            # Getting the query sequence.
            # If the the hit is on the other strand the characters
            # are reversed.
            if hit['strand'] == 1:
               if query_end == len(contig):
                  query_seq = "-" * missing + str(query_seq)
               else:
                  start_pos = query_end
                  chars = contig[start_pos:]
                  chars = Blaster.reversecomplement(chars)

                  query_seq = ("-" * (missing - len(chars))
                               + chars + str(query_seq))
            elif query_start < 3:
               query_seq = "-" * missing + str(query_seq)
            else:
               end_pos = query_start - 2
               chars = contig[0:end_pos]

               query_seq = ("-" * (missing - len(chars))
                            + chars + str(query_seq))

         # Adding to the homo sequence
         spaces = " " * missing
         homo_seq = str(spaces) + str(homo_seq)

      # If the alignment dosen't end and the last position data is
      # added to the end
      if sbjct_end < length:
         missing = length - sbjct_end

         if(missing <= (len(contig) - query_end) and hit['strand'] != 1
            or hit['strand'] == 1 and query_start >= missing):

            # Getting the query sequence.
            # If the the hit is on the other strand the characters
            # are reversed.
            if hit['strand'] == 1:
               start_pos = query_start - missing - 1
               end_pos = query_start - 1
               chars = contig[start_pos:end_pos]
               chars = Blaster.reversecomplement(chars)
            else:
               start_pos = query_end
               end_pos = query_end + missing
               chars = contig[start_pos:end_pos]

            query_seq = query_seq + chars
         else:
            # If the hit is on the other strand the characters are reversed
            if hit['strand'] == 1:
               if query_start < 3:
                  query_seq = query_seq + "-" * missing
               else:
                  end_pos = query_start - 2
                  chars = contig[0:end_pos]
                  chars = Blaster.reversecomplement(chars)

                  query_seq = (query_seq
                               + chars + "-" * (missing - len(chars)))
            elif query_end == len(contig):
               query_seq = query_seq + "-" * missing
            else:
               start_pos = query_end
               chars = contig[start_pos:]

               query_seq = query_seq + chars + "-" * (missing - len(chars))

         # Adding to the homo sequence
         spaces = " " * int(missing)
         homo_seq = str(homo_seq) + str(spaces)

      return query_seq, homo_seq
694830
def dalignbed2dalignbedqueriesseq(cfg):
    
    datatmpd=cfg['datatmpd']
    dalignbedqueries=del_Unnamed(pd.read_csv(cfg['dalignbedqueriesp'],sep='\t'))
    dalignedfasta=del_Unnamed(pd.read_csv(cfg['dalignedfastap'],sep='\t'))
    dalignbedqueriesseqp=cfg['dalignbedqueriesseqp']
    logging.info(basename(dalignbedqueriesseqp))
    if not exists(dalignbedqueriesseqp) or cfg['force']:        
        dalignbedqueriesseq=pd.merge(dalignbedqueries,dalignedfasta,on='id',suffixes=('', '.2'))
        dalignbedqueriesseq=dalignbedqueriesseq.dropna(subset=['aligned sequence'],axis=0)

        # dalignbed.index.name='id'
        dalignbedqueriesseq=dalignbedqueriesseq.drop_duplicates()
        dalignbedqueriesseq.to_csv(dalignbedqueriesseqp,sep='\t')
    return cfg
1012485
def _fetch_bounded_seq(self, var, start, end, window_size, boundary):
        
        var_len = end - start - window_size

        start = start if start >= boundary[0] else boundary[0]
        end = end if end <= boundary[1] else boundary[1]
        if start >= end:
            return ""

        seq = self.hdp.get_seq(var.ac, start, end)

        if len(seq) < end - start and len(seq) < var_len:
            raise HGVSInvalidVariantError(
                "Variant span is outside sequence bounds ({var})".format(var=var))

        return seq
243698
def qmark(cls, query):
        

        def sub_sequence(m):
            s = m.group(0)
            if s == "??":
                return "?"
            if s == "%":
                return "%%"
            else:
                return "%s"

        return cls.RE_QMARK.sub(sub_sequence, query)
709470
def filter_known_searchspace(elements, seqtype, lookup, ns, ntermwildcards,
                             deamidation):
    """"""
    for element in elements:
        seq_is_known = False
        for seq in get_seqs_from_element(element, seqtype, ns, deamidation):
            if lookup.check_seq_exists(seq, ntermwildcards):
                seq_is_known = True
                break
        if seq_is_known:
            formatting.clear_el(element)
        else:
            yield formatting.string_and_clear(element, ns)
1214313
def ellipsis(source, max_length):
    """"""
    if max_length == 0 or len(source) <= max_length:
        return source
    return source[: max(0, max_length - 3)] + "..."
1326568
private static boolean extractSimpleExtents(Variable var, Extents e, boolean row, int length) {
        int lower;
        int upper;
        if( var.getType() == VariableType.INTEGER_SEQUENCE ) {
            IntegerSequence sequence = ((VariableIntegerSequence)var).sequence;
            if( sequence.getType() == IntegerSequence.Type.FOR ) {
                IntegerSequence.For seqFor = (IntegerSequence.For)sequence;
                seqFor.initialize(length);
                if( seqFor.getStep() == 1 ) {
                    lower = seqFor.getStart();
                    upper = seqFor.getEnd();
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } else if( var.getType() == VariableType.SCALAR ) {
            lower = upper = ((VariableInteger)var).value;
        } else {
            throw new RuntimeException("How did a bad variable get put here?!?!");
        }
        if( row ) {
            e.row0 = lower;
            e.row1 = upper;
        } else {
            e.col0 = lower;
            e.col1 = upper;
        }
        return true;
    }
955232
function encodeQueryAsString(query = {}) {
  const params = Object.keys(query).map((name) =>
    [encodeURIComponent(name), encodeURIComponent(query[name])].join('=')
  );
  return params.length ? `?${params.join('&')}` : '';
}
958031
def alignments(self):
        
        referenceIds = self.referenceIds
        dropUnmapped = self.dropUnmapped
        dropSecondary = self.dropSecondary
        dropSupplementary = self.dropSupplementary
        dropDuplicates = self.dropDuplicates
        keepQCFailures = self.keepQCFailures
        storeQueryIds = self.storeQueryIds
        filterRead = self.filterRead
        minScore = self.minScore
        maxScore = self.maxScore
        scoreTag = self.scoreTag

        if storeQueryIds:
            self.queryIds = queryIds = set()

        lastAlignment = None
        count = 0
        with samfile(self.filename) as samAlignment:
            for count, alignment in enumerate(samAlignment.fetch(), start=1):
                if storeQueryIds:
                    queryIds.add(alignment.query_name)

                if minScore is not None or maxScore is not None:
                    try:
                        score = alignment.get_tag(scoreTag)
                    except KeyError:
                        continue
                    else:
                        if ((minScore is not None and score < minScore) or
                                (maxScore is not None and score > maxScore)):
                            continue

                # Secondary and supplementary alignments may have a '*'
                # (pysam returns this as None) SEQ field, indicating that
                # the previous sequence should be used. This is best
                # practice according to section 2.5.2 of
                # https://samtools.github.io/hts-specs/SAMv1.pdf So we use
                # the last alignment query and quality strings if we get
                # None as a query sequence.
                if alignment.query_sequence is None:
                    if lastAlignment is None:
                        raise InvalidSAM(
                            'pysam produced an alignment (number %d) with no '
                            'query sequence without previously giving an '
                            'alignment with a sequence.' % count)
                    # Use the previous query sequence and quality. I'm not
                    # making the call to _hardClip dependent on
                    # alignment.cigartuples (as in the else clause below)
                    # because I don't think it's possible for
                    # alignment.cigartuples to be None in this case. If we
                    # have a second match on a query, then it must be
                    # aligned to something (i.e., it cannot be unmapped
                    # with no CIGAR string). The assertion will tell us if
                    # this is ever not the case.
                    assert alignment.cigartuples
                    (alignment.query_sequence,
                     alignment.query_qualities, _) = _hardClip(
                         lastAlignment.query_sequence,
                         lastAlignment.query_qualities,
                         alignment.cigartuples)
                else:
                    lastAlignment = alignment
                    if alignment.cigartuples:
                        (alignment.query_sequence,
                         alignment.query_qualities, _) = _hardClip(
                             alignment.query_sequence,
                             alignment.query_qualities,
                             alignment.cigartuples)

                if ((filterRead is None or
                     filterRead(Read(alignment.query_name,
                                     alignment.query_sequence,
                                     alignment.qual))) and
                    not (
                        (referenceIds and
                         alignment.reference_name not in referenceIds) or
                        (alignment.is_unmapped and dropUnmapped) or
                        (alignment.is_secondary and dropSecondary) or
                        (alignment.is_supplementary and dropSupplementary) or
                        (alignment.is_duplicate and dropDuplicates) or
                        (alignment.is_qcfail and not keepQCFailures))):
                    yield alignment

        self.alignmentCount = count
1318225
def get_fixed_long_line(target, previous_line, original,
                        indent_word='    ', max_line_length=79,
                        aggressive=False, experimental=False, verbose=False):
    
    indent = _get_indentation(target)
    source = target[len(indent):]
    assert source.lstrip() == source
    assert not target.lstrip().startswith('#')

    # Check for partial multiline.
    tokens = list(generate_tokens(source))

    candidates = shorten_line(
        tokens, source, indent,
        indent_word,
        max_line_length,
        aggressive=aggressive,
        experimental=experimental,
        previous_line=previous_line)

    # Also sort alphabetically as a tie breaker (for determinism).
    candidates = sorted(
        sorted(set(candidates).union([target, original])),
        key=lambda x: line_shortening_rank(
            x,
            indent_word,
            max_line_length,
            experimental=experimental))

    if verbose >= 4:
        print(('-' * 79 + '\n').join([''] + candidates + ['']),
              file=wrap_output(sys.stderr, 'utf-8'))

    if candidates:
        best_candidate = candidates[0]

        # Don't allow things to get longer.
        if longest_line_length(best_candidate) > longest_line_length(original):
            return None

        return best_candidate
113346
def seqrecord2sequence(record, qlen, index):
    
    # aligned_len = sum(map(str.isupper, str(record.seq)))
    # assert qlen == aligned_len, (
    #         "Aligned sequence length %s, query %s\n%s"
    #         % (aligned_len, qlen, str(record)))

    description = (record.description.split(' ', 1)[1]
            if ' ' in record.description
            else ' ')
    return dict(index=index,
                id=record.id,
                description=description,
                dbxrefs={},
                phylum='',
                taxchar='',
                head_len=None,
                tail_len=None,
                head_seq='',
                tail_seq='',
                length=len(record.seq) - record.seq.count('-'),
                seq=str(record.seq),
                )
1215464
def actual_query_range(self):
    
    a = self.alignment_ranges
    #return GenomicRange(a[0][1].chr,a[0][1].start,a[-1][1].end,self.get_strand())
    if self.get_strand() == '+':
      return GenomicRange(a[0][1].chr,a[0][1].start,a[-1][1].end,self.get_strand())
    #must be - strand
    return GenomicRange(a[0][1].chr,self.query_sequence_length-a[-1][1].end+1,self.query_sequence_length-a[0][1].start+1,dir=self.strand)
302865
function Tokenize(query) {
  var parts = __splitTrim(query, WHERE);
  var pathway = parts[0];
  var where = parts[1];

  synopsis.pathway = __splitTrim(pathway, COMMA);
  for (var i = 0, len = synopsis.pathway.length; i < len; i++) {
    synopsis.pathway[i] = __splitTrim(synopsis.pathway[i], DELIMITER);
    if (synopsis.pathway[i][0] == WILDCARD)
      synopsis.pathway[i].shift();
    if (synopsis.pathway[i].length === 0)
      synopsis.pathway.splice(i, 1);
  }

  var lastLeft = -1,
    lastRight = -1,
    current = 0;
  while (current < where.length) {
    if (where[current] === LEFT) {
      lastLeft = current;
    } else if (where[current] === RIGHT) {
      lastRight = current;
      if (lastRight > lastLeft && lastLeft !== -1) {
        var k = 'gr' + '_' + new Date().getTime();
        synopsis.groups[k] = where.substring(lastLeft + 1, lastRight);
        where = where.replace(LEFT + synopsis.groups[k] + RIGHT, k);
        current = -1;
      }
    }
    current += 1;
  }
  LogicalGrouping(AST, where);
}
163588
def extract(query_dict, prefix=""):
    

    strs = ['order_by']
    ints = ['per_page', 'page']

    extracted = { }

    for key in (strs + ints):
        if (prefix + key) in query_dict:
            val = query_dict.get(prefix + key)

            extracted[key] = (val
                if not key in ints
                else int(val))

    return extracted
1341304
def original_query_sequence_length(self):
    
    if not self.is_aligned() or not self.entries.cigar:
      return self.query_sequence_length # take the naive approach
    # we are here with something aligned so take more intelligent cigar apporach
    return sum([x[0] for x in self.cigar_array if re.match('[HMIS=X]',x[1])])
410789
def actual_original_query_range(self):
    
    l = self.original_query_sequence_length
    a = self.alignment_ranges
    qname = a[0][1].chr
    qstart = a[0][1].start
    qend = a[-1][1].end
    #rng = self.get_query_range()
    start = qstart
    end = qend
    if self.strand == '-':
      end = l-(qstart-1)
      start = 1+l-(qend)
    return GenomicRange(qname,start,end,dir=self.strand)
1353755
def _get_alignment_ranges(self):
    """"""
    if not self.is_aligned(): return None
    alignment_ranges = []
    cig = [x[:] for x in self.cigar_array]
    target_pos = self.entries.pos
    query_pos = 1
    while len(cig) > 0:
      c = cig.pop(0)
      if re.match('[S]$',c[1]): # hard or soft clipping
        query_pos += c[0]
      elif re.match('[ND]$',c[1]): # deleted from reference
        target_pos += c[0]
      elif re.match('[I]$',c[1]): # insertion to the reference
        query_pos += c[0]
      elif re.match('[MI=X]$',c[1]): # keep it
        t_start = target_pos
        q_start = query_pos
        target_pos += c[0]
        query_pos += c[0]
        t_end = target_pos-1
        q_end = query_pos-1
        alignment_ranges.append([GenomicRange(self.entries.rname,t_start,t_end),GenomicRange(self.entries.qname,q_start,q_end)])
    return alignment_ranges