Note: an alternative is to use the RCSB API e.g. http://www.rcsb.org/pdb/rest/hmmer?structureId=1cdg.
179983
def get_pfam_accession_numbers_from_pdb_id(self, pdb_id):
        ''''''
        pdb_id = pdb_id.lower()
        if self.pdb_chain_to_pfam_mapping.get(pdb_id):
            return self.pdb_chain_to_pfam_mapping[pdb_id].copy()
453839
def rmd160(msg_bytes):
    '''
    
    '''
    h = hashlib.new('ripemd160')
    h.update(msg_bytes)
    return h.digest()
623565
public final void getEntitiesDescription( StringBuilder sb )
	{
		sb.append(toString());
		sb.append("{");
		synchronized (consumersMap)
		{
			if (!consumersMap.isEmpty())
			{
				int pos = 0;
				Iterator<AbstractMessageConsumer> consumers = consumersMap.values().iterator();
				while (consumers.hasNext())
				{
					AbstractMessageHandler handler = consumers.next();
					if (pos++ > 0)
						sb.append(",");					
					handler.getEntitiesDescription(sb);
				}
			}
		}
		synchronized (producersMap)
		{
			if (!producersMap.isEmpty())
			{
				int pos = 0;
				Iterator<AbstractMessageProducer> producers = producersMap.values().iterator();
				while (producers.hasNext())
				{
					AbstractMessageHandler handler = producers.next();
					if (pos++ > 0)
						sb.append(",");					
					handler.getEntitiesDescription(sb);
				}
			}
		}
		sb.append("}");
	}
1017464
def get_on_tmdb(uri, **kwargs):
    
    kwargs['api_key'] = app.config['TMDB_API_KEY']
    response = requests_session.get((TMDB_API_URL + uri).encode('utf8'), params=kwargs)
    response.raise_for_status()
    return json.loads(response.text)
812271
def from_httplib(cls, message, duplicates=('set-cookie',)): # Python 2
        """"""
        ret = cls(message.items())
        # ret now contains only the last header line for each duplicate.
        # Importing with all duplicates would be nice, but this would
        # mean to repeat most of the raw parsing already done, when the
        # message object was created. Extracting only the headers of interest 
        # separately, the cookies, should be faster and requires less
        # extra code.
        for key in duplicates:
            ret.discard(key)
            for val in message.getheaders(key):
                ret.add(key, val)
            return ret
553483
def parse_uniprot_xml_metadata(sr):
    
    # TODO: What about "reviewed" status? and EC number
    xref_dbs_to_keep = ['GO', 'KEGG', 'PDB', 'PROSITE', 'Pfam', 'RefSeq']

    infodict = {}

    infodict['alt_uniprots'] = list(set(sr.annotations['accessions']).difference([sr.id]))
    infodict['gene_name'] = None
    if 'gene_name_primary' in sr.annotations:
        infodict['gene_name'] = sr.annotations['gene_name_primary']
    infodict['description'] = sr.description
    infodict['taxonomy'] = None
    if 'organism' in sr.annotations:
        infodict['taxonomy'] = sr.annotations['organism']
    infodict['seq_version'] = sr.annotations['sequence_version']
    infodict['seq_date'] = sr.annotations['sequence_modified']
    infodict['entry_version'] = sr.annotations['version']
    infodict['entry_date'] = sr.annotations['modified']

    tmp = defaultdict(list)
    for xref in sr.dbxrefs:
        database = xref.split(':', 1)[0]
        xrefs = xref.split(':', 1)[-1]

        if database in xref_dbs_to_keep:
            if database == 'PDB':
                tmp['pdbs'].append(xrefs)
            else:
                tmp[database.lower()].append(xrefs)

    infodict.update(tmp)

    return infodict
664244
def dump_to_rsb(params: dict, times: np.ndarray, data: np.ndarray) -> bytes:
    
    assert isinstance(times, np.ndarray)
    assert times.ndim == 1
    assert isinstance(data, np.ndarray)
    assert data.ndim == 2
    assert len(data) == len(times)

    params['b_size'] = data.shape[1]
    params['events_num'] = data.shape[0]

    start = int(times.min() * 1e-9)
    end = int(times.max() * 1e-9)

    if 'start_time' not in params:
        params['start_time'] = datetime.fromtimestamp(start).isoformat()

    if 'end_time' not in params:
        params['end_time'] = datetime.fromtimestamp(end).isoformat()

    text_header = bytearray(5120)
    text = serialise_to_rsh(params).encode('cp1251')
    text_header[:len(text)] = text

    params['text_header_size'] = len(text)

    binary_header = serialize_to_rsb(params)

    bin_data = b''

    ch_num = params['channel_number']
    ev_size = params['b_size']
    for i, event_data in enumerate(data):
        event = bytearray(
            np.zeros(96 + 2 * ch_num * ev_size, np.byte).tostring())
        text_hdr = datetime.fromtimestamp(int(times[i] * 10e-9)).isoformat()

        event[:len(text_hdr)] = text_hdr.encode('cp1251')
        event[64:68] = struct.pack('I', i)
        event[72:80] = struct.pack('Q', int(times[i] * 10e-9))
        event[80:88] = struct.pack('Q', int(times[i]))

        event[96:] = event_data.astype(np.int16).tostring()
        bin_data += event

    return bytes(text_header + binary_header + bin_data)
262274
public static HMMPOSTagger learn(String[][] sentences, PennTreebankPOS[][] labels) {
        int index = 1;
        int suffixIndex = 0;
        Map<String, Integer> symbol = new HashMap<>();
        Map<String, Integer> suffix = new HashMap<>();
        for (int i = 0; i < sentences.length; i++) {
            for (int j = 0; j < sentences[i].length; j++) {
                Integer sym = symbol.get(sentences[i][j]);
                if (sym == null) {
                    symbol.put(sentences[i][j], index++);
                }
                
                if (sentences[i][j].length() > 2) {
                    String s = sentences[i][j].substring(sentences[i][j].length() - 2);
                    sym = suffix.get(s);
                    if (sym == null) {
                        suffix.put(s, suffixIndex++);
                    }
                }
            }
        }

        int numStates = PennTreebankPOS.values().length;

        double[] pi = new double[numStates];
        double[][] a = new double[numStates][numStates];
        double[][] b = new double[numStates][symbol.size() + 1];
        double[][] c = new double[numStates][suffix.size()];
        
        PennTreebankPOS[] tags = PennTreebankPOS.values();
        for (int i = 0; i < numStates; i++) {
            if (tags[i].open) {
                b[i][0] = 1.0;
            }
        }

        for (int i = 0; i < sentences.length; i++) {
            int[] tag = translate(labels[i]);
            int[][] obs = translate(symbol, suffix, sentences[i]);

            pi[tag[0]]++;
            b[tag[0]][obs[0][0]]++;            
            if (obs[0][1] >= 0) {
                c[tag[0]][obs[0][1]]++;
            }
            
            for (int j = 1; j < obs.length; j++) {
                a[tag[j-1]][tag[j]]++;
                b[tag[j]][obs[j][0]]++;
                if (obs[j][1] >= 0) {
                    c[tag[j]][obs[j][1]]++;
                }
            }                
        }

        Math.unitize1(pi);
        for (int i = 0; i < numStates; i++) {
            Math.unitize1(a[i]);
            Math.unitize1(b[i]);
            Math.unitize1(c[i]);
        }
        
        return new HMMPOSTagger(symbol, suffix, pi, a, b, c);
    }
981626
String
rrToString() {
	StringBuffer sb = new StringBuffer();
	if (options != null) {
		sb.append(options);
		sb.append(" ");
	}
	sb.append(" ; payload ");
	sb.append(getPayloadSize());
	sb.append(", xrcode ");
	sb.append(getExtendedRcode());
	sb.append(", version ");
	sb.append(getVersion());
	sb.append(", flags ");
	sb.append(getFlags());
	return sb.toString();
}
714063
static public int getBlockCrc(DataNode datanode, ReplicaToRead ri,
      int namespaceId, Block block) throws IOException {

    InputStream rawStreamIn = null;
    DataInputStream streamIn = null;
    int blockCrc = 0;

    try {
      int bytesPerCRC;
      int checksumSize;

      bytesPerCRC = ri.getBytesPerChecksum();
      int checksumType = ri.getChecksumType();
      if (checksumType != DataChecksum.CHECKSUM_CRC32) {
        throw new IOException("File Checksum now is only supported for CRC32");
      }
      DataChecksum dataChecksum = DataChecksum.newDataChecksum(checksumType,
          bytesPerCRC);
      checksumSize = dataChecksum.getChecksumSize();
      
      rawStreamIn = ri.getBlockInputStream(datanode, 0);
      streamIn = new DataInputStream(new BufferedInputStream(rawStreamIn,
          FSConstants.BUFFER_SIZE));
      IOUtils.skipFully(streamIn, BlockInlineChecksumReader.getHeaderSize());

      long lengthLeft = ((FileInputStream) rawStreamIn).getChannel().size()
          - BlockInlineChecksumReader.getHeaderSize();
      if (lengthLeft == 0) {
        blockCrc = (int) dataChecksum.getValue();
      } else {
        byte[] buffer = new byte[checksumSize];
        boolean firstChecksum = true;

        while (lengthLeft > 0) {
          long dataByteLengh;

          if (lengthLeft >= bytesPerCRC + checksumSize) {
            lengthLeft -= bytesPerCRC + checksumSize;
            dataByteLengh = bytesPerCRC;
          } else if (lengthLeft > checksumSize) {
            dataByteLengh = lengthLeft - checksumSize;
            lengthLeft = 0;
          } else {
            // report to name node the corruption.
            DataBlockScanner.reportBadBlocks(block, namespaceId, datanode);
            throw new IOException("File for namespace " + namespaceId
                + " block " + block + " seems to be corrupted");
          }

          IOUtils.skipFully(streamIn, dataByteLengh);
          IOUtils.readFully(streamIn, buffer, 0, buffer.length);
          int intChecksum = DataChecksum.getIntFromBytes(buffer, 0);
          if (firstChecksum) {
            blockCrc = intChecksum;
            firstChecksum = false;
          } else {
            blockCrc = CrcConcat.concatCrc(blockCrc, intChecksum,
                (int) dataByteLengh);
          }
        }
      }

      if (LOG.isDebugEnabled()) {
        LOG.debug("block=" + block + ", bytesPerCRC=" + bytesPerCRC
            + ", crc=" + blockCrc);
      }
      return blockCrc;
    } finally {
      IOUtils.closeStream(streamIn);
      IOUtils.closeStream(rawStreamIn);
    }

  }
1145331
public SortedSet<HmmerResult> scan(ProteinSequence sequence, URL serviceLocation) throws IOException{

		StringBuffer postContent = new StringBuffer();

		postContent.append("hmmdb=pfam");


		// by default hmmscan runs with the HMMER3 cut_ga parameter enabled, the "gathering threshold", which depends on
		// the cutoffs defined in the underlying HMM files.
		// to request a different cutoff by e-value this could be enabled:
		//postContent.append("&E=1");


		postContent.append("&seq=");
		postContent.append(sequence.getSequenceAsString());


		HttpURLConnection connection = (HttpURLConnection) serviceLocation.openConnection();
		connection.setDoOutput(true);
		connection.setDoInput(true);
		connection.setConnectTimeout(15000); // 15 sec
		connection.setInstanceFollowRedirects(false);
		connection.setRequestMethod("POST");
		connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

		connection.setRequestProperty("Accept","application/json");

		connection.setRequestProperty("Content-Length", "" +
				Integer.toString(postContent.toString().getBytes().length));

		//Send request
		DataOutputStream wr = new DataOutputStream (
				connection.getOutputStream ());
		wr.write(postContent.toString().getBytes());
		wr.flush ();
		wr.close ();


		//Now get the redirect URL
		URL respUrl = new URL( connection.getHeaderField( "Location" ));

		int responseCode = connection.getResponseCode();
		if ( responseCode == 500){
			LOGGER.warn("Got 500 response code for URL {}. Response message: {}.", serviceLocation, connection.getResponseMessage());
		}

		HttpURLConnection connection2 = (HttpURLConnection) respUrl.openConnection();
		connection2.setRequestMethod("GET");
		connection2.setRequestProperty("Accept", "application/json");
		connection2.setConnectTimeout(60000); // 1 minute

		//Get the response
		BufferedReader in = new BufferedReader(
				new InputStreamReader(
						connection2.getInputStream()));

		String inputLine;

		StringBuffer result = new StringBuffer();
		while ((inputLine = in.readLine()) != null) {
			result.append(inputLine);
		}

		in.close();

		// process the response and build up a container for the data.

		SortedSet<HmmerResult> results = new TreeSet<HmmerResult>();
		try {
			JSONObject json =  JSONObject.fromObject(result.toString());

			JSONObject hmresults = json.getJSONObject("results");


			JSONArray hits = hmresults.getJSONArray("hits");

			for(int i =0 ; i < hits.size() ; i++){
				JSONObject hit = hits.getJSONObject(i);

				HmmerResult hmmResult = new HmmerResult();

				Object dclO = hit.get("dcl");
				Integer dcl = -1;
				if ( dclO instanceof Long){
					Long dclL = (Long) dclO;
					dcl = dclL.intValue();
				} else if ( dclO instanceof Integer){
					dcl = (Integer) dclO;
				}


				hmmResult.setAcc((String)hit.get("acc"));
				hmmResult.setDcl(dcl);
				hmmResult.setDesc((String)hit.get("desc"));
				hmmResult.setEvalue(Float.parseFloat((String)hit.get("evalue")));
				hmmResult.setName((String)hit.get("name"));
				hmmResult.setNdom((Integer)hit.get("ndom"));
				hmmResult.setNreported((Integer)hit.get("nreported"));
				hmmResult.setPvalue((Double)hit.get("pvalue"));
				hmmResult.setScore(Float.parseFloat((String)hit.get("score")));

				JSONArray hmmdomains = hit.getJSONArray("domains");

				SortedSet<HmmerDomain> domains = new TreeSet<HmmerDomain>();
				for ( int j= 0 ; j < hmmdomains.size() ; j++){
					JSONObject d = hmmdomains.getJSONObject(j);
					Integer is_included = getInteger(d.get("is_included"));
					if ( is_included == 0) {
						continue;
					}


					// this filters out multiple hits to the same clan
					Integer outcompeted = getInteger(d.get("outcompeted"));
					if ( outcompeted != null && outcompeted == 1) {
						continue;
					}

					Integer significant = getInteger(d.get("significant"));

					if (  significant != 1) {
						continue;
					}

					HmmerDomain dom = new HmmerDomain();
					dom.setAliLenth((Integer)d.get("aliL"));
					dom.setHmmAcc((String)d.get("alihmmacc"));
					dom.setHmmDesc((String)d.get("alihmmdesc"));

					dom.setHmmFrom(getInteger(d.get("alihmmfrom")));
					dom.setHmmTo(getInteger(d.get("alihmmto")));
					dom.setSimCount((Integer) d.get("aliSimCount"));
					dom.setSqFrom(getInteger(d.get("alisqfrom")));
					dom.setSqTo(getInteger(d.get("alisqto")));
					dom.setHmmName((String)d.get("alihmmname"));
					dom.setEvalue(Float.parseFloat((String)d.get("ievalue")));

					domains.add(dom);


				}

				hmmResult.setDomains(domains);

				results.add(hmmResult);
			}
		} catch (NumberFormatException e){
			LOGGER.warn("Could not parse number in Hmmer web service json response: {}", e.getMessage());
		}

		return results;

	}
181295
public static HMMPOSTagger getDefault() {
        if (DEFAULT_TAGGER == null) {
            try {
                ObjectInputStream ois = new ObjectInputStream(HMMPOSTagger.class.getResourceAsStream("/smile/nlp/pos/hmmpostagger.model"));
                DEFAULT_TAGGER = (HMMPOSTagger) ois.readObject();
                ois.close();
            } catch (Exception ex) {
                logger.error("Failed to load /smile/nlp/pos/hmmpostagger.model", ex);
            }
        }
        return DEFAULT_TAGGER;
    }
1112840
def to_api_repr(self):
        
        info = {}
        if self.log_name is not None:
            info["logName"] = self.log_name
        if self.resource is not None:
            info["resource"] = self.resource._to_dict()
        if self.labels is not None:
            info["labels"] = self.labels
        if self.insert_id is not None:
            info["insertId"] = self.insert_id
        if self.severity is not None:
            info["severity"] = self.severity
        if self.http_request is not None:
            info["httpRequest"] = self.http_request
        if self.timestamp is not None:
            info["timestamp"] = _datetime_to_rfc3339(self.timestamp)
        if self.trace is not None:
            info["trace"] = self.trace
        if self.span_id is not None:
            info["spanId"] = self.span_id
        if self.trace_sampled is not None:
            info["traceSampled"] = self.trace_sampled
        if self.source_location is not None:
            source_location = self.source_location.copy()
            source_location["line"] = str(source_location.pop("line", 0))
            info["sourceLocation"] = source_location
        if self.operation is not None:
            info["operation"] = self.operation
        return info
864168
def rest_xml
      doc = REXML::Document.new APIConnection::XML_HEADER
      periph = doc.add_element RSRC_OBJECT_KEY.to_s

      general = periph.add_element('general')
      general.add_element('type').text = @type
      general.add_element('site').add_element('name').text = @site
      general.add_element('bar_code_1').text = @bar_code_1
      general.add_element('bar_code_2').text = @bar_code_2
      general.add_element('computer_id').text = @computer_id

      fields = general.add_element('fields')
      @fields.each do |n,v|
        fld =  fields.add_element('field')
        fld.add_element('name').text = n
        fld.add_element('value').text = v
      end

      if has_location?
        periph << location_xml
      end
      if has_purchasing?
        periph << purchasing_xml
      end
      add_site_to_xml doc

      return doc.to_s
    end
125799
def match_RCSB_pdb_chains(pdb_id1, pdb_id2, cut_off = 60.0, allow_multiple_matches = False, multiple_match_error_margin = 3.0, use_seqres_sequences_if_possible = True, strict = True):
    ''''''
    try:
        stage = pdb_id1
        pdb_1 = PDB(retrieve_pdb(pdb_id1), strict = strict)
        stage = pdb_id2
        pdb_2 = PDB(retrieve_pdb(pdb_id2), strict = strict)
    except (PDBParsingException, NonCanonicalResidueException, PDBValidationException), e:
        raise PDBParsingException("An error occurred while loading %s: '%s'" % (stage, str(e)))

    return match_pdb_chains(pdb_1, pdb_id1, pdb_2, pdb_id2, cut_off = cut_off, allow_multiple_matches = allow_multiple_matches, multiple_match_error_margin = multiple_match_error_margin, use_seqres_sequences_if_possible = use_seqres_sequences_if_possible)
1160362
private CmsResource getResource(CmsUUID structureId) {

        CmsResource resource = m_resources.get(structureId);
        if (resource == null) {
            try {
                resource = m_cms.readResource(structureId, CmsResourceFilter.ALL);
                m_resources.put(structureId, resource);
            } catch (CmsException e) {
                LOG.info(e.getLocalizedMessage(), e);
            }
        }
        return resource;
    }
164922
def static_get_pdb_object(pdb_id, bio_cache = None, cache_dir = None):
        ''''''
        pdb_id = pdb_id.upper()

        if bio_cache:
            return bio_cache.get_pdb_object(pdb_id)

        if cache_dir:
            # Check to see whether we have a cached copy of the PDB file
            filepath = os.path.join(cache_dir, '{0}.pdb'.format(pdb_id))
            if os.path.exists(filepath):
                return PDB.from_filepath(filepath)

        # Get any missing files from the RCSB and create cached copies if appropriate
        pdb_contents = retrieve_pdb(pdb_id)
        if cache_dir:
            write_file(os.path.join(cache_dir, "%s.pdb" % pdb_id), pdb_contents)
        return PDB(pdb_contents)