Java Code Examples for org.biojava.nbio.structure.Structure#getChains()

The following examples show how to use org.biojava.nbio.structure.Structure#getChains() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DemoMmcifToPdbConverter.java    From biojava with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void convert(File inFile, File outFile) throws IOException {

	MMcifParser parser = new SimpleMMcifParser();

	SimpleMMcifConsumer consumer = new SimpleMMcifConsumer();
	parser.addMMcifConsumer(consumer);
	parser.parse(new BufferedReader(new InputStreamReader(new FileInputStream(inFile))));

	// now get the protein structure.
	Structure cifStructure = consumer.getStructure();

	// and write it out as PDB format
	PrintWriter pr = new PrintWriter(outFile);
	for (Chain c : cifStructure.getChains()) {
			// we can override the chain name, the mmCIF chain names might have more than 1 character
			c.setName(c.getName().substring(0, 1));
			pr.print(c.toPDB());
			pr.println("TER");
	}

		pr.close();


	}
 
Example 2
Source File: TestDifficultMmCIFFiles.java    From biojava with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Test
public void test4letterChains() throws IOException, StructureException, URISyntaxException {
	String filename = "/1hh0_4char.cif.gz";
	URL url = getClass().getResource(filename);
	assumeNotNull("Can't find resource "+filename,url);

	File file = new File(url.toURI());
	assumeNotNull(file);
	assumeTrue(file.exists());

	MMCIFFileReader reader = new MMCIFFileReader();
	Structure s = reader.getStructure(file);

	assertNotNull("Failed to load structure from jar",s);

	List<Chain> chains = s.getChains();
	assertEquals("Wrong number of chains",chains.size(), 1);

	Chain chain = chains.get(0);
	assertEquals("Wrong chain ID",chain.getId(),"ABCD");

	Chain chain2 = s.getPolyChainByPDB("ABCD");
	assertNotNull(chain2);
	assertEquals(chain2, chain);
}
 
Example 3
Source File: BiologicalAssemblyBuilder.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Returns a list of chain ids in the order they are specified in the ATOM
 * records in the asymmetric unit
 * @param asymUnit
 * @return
 */
private List<String> getChainIds(Structure asymUnit) {
	List<String> chainIds = new ArrayList<String>();
	for ( Chain c : asymUnit.getChains()){
		String intChainID = c.getId();
		chainIds.add(intChainID);
	}
	return chainIds;
}
 
Example 4
Source File: MMCIFFileTools.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Converts a Structure into a List of {@link AtomSite} objects
 * @param s
 * @return
 */
public static List<AtomSite> convertStructureToAtomSites(Structure s) {
	List<AtomSite> list = new ArrayList<AtomSite>();

	for (int m=0;m<s.nrModels();m++) {
		for (Chain c:s.getChains(m)) {
			list.addAll(convertChainToAtomSites(c, m+1, c.getName(), c.getId()));
		}
	}
	return list;
}
 
Example 5
Source File: MmtfUtils.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Count the total number of groups in the structure
 * @param structure the input structure
 * @return the total number of groups
 */
public static int getNumGroups(Structure structure) {
	int count = 0;
	for(int i=0; i<structure.nrModels(); i++) {
		for(Chain chain : structure.getChains(i)){
			count+= chain.getAtomGroups().size();
		}
	}
	return count;
}
 
Example 6
Source File: TestParseOnAsymId.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Test
public void test4cup() throws IOException, StructureException {

	AtomCache cache = new AtomCache();
	cache.setUseMmCif(true);
	FileParsingParameters params = cache.getFileParsingParams();

	cache.setFileParsingParams(params);
	StructureIO.setAtomCache(cache);
	// Set the test lists
	String[] asymChainList = {"A","B","C","D","E","F"};
	String[] authChainList = {"A","A","A","A","A","A"};
	String[] asymChainListTest = new String[6];
	String[] authChainListTest = new String[6];
	// Get the structure
	Structure bioJavaStruct = StructureIO.getStructure("4cup");
	List<Chain> chainList = bioJavaStruct.getChains();
	assertEquals(6,chainList.size());
	for(int i=0; i<chainList.size();i++){
		Chain c = chainList.get(i);
		authChainListTest[i] = c.getName();
		asymChainListTest[i] = c.getId();
	}
	// Now check both lists are the same
	assertArrayEquals(authChainListTest, authChainList);
	assertArrayEquals(asymChainListTest, asymChainList);
}
 
Example 7
Source File: TestNonDepositedFiles.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkChains(Structure s) {
	for (Chain chain:s.getChains()) {
		int seqResLength = chain.getSeqResLength();
		int atomLength = chain.getAtomLength();
		System.out.println("chain "+chain.getId()+", atomLength: "+atomLength+", seqResLength: "+seqResLength);
		//assertTrue("atom length ("+atomLength+") should be smaller than seqResLength ("+seqResLength+")",atomLength<=seqResLength);
		System.out.println("seq res groups size: "+chain.getSeqResGroups().size());
	}
}
 
Example 8
Source File: TestHeaderOnly.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Scan through SeqResGroups, returns true if any have Atoms.
 * @param s
 * @return
 */
public boolean doSeqResHaveAtoms(Structure s) {
	for (int i = 0; i < s.nrModels(); i++) {
		for (Chain c : s.getChains(i)) {
			for (Group g : c.getSeqResGroups()) {
				if (hasAtoms(g)) return true; // Found some Atoms in a Seqres group.
			}
		}
	}

	return false;
}
 
Example 9
Source File: ModifiedCompoundSerializationTest.java    From biojava with GNU Lesser General Public License v2.1 5 votes vote down vote up
public List<ModifiedCompound> testXMLSerialization(String pdbId){
	String xml = null;
	ModifiedCompound currentMC = null;
	List<ModifiedCompound> all = new ArrayList<ModifiedCompound>();
	try {

		Structure struc = TmpAtomCache.cache.getStructure(pdbId);

		ProteinModificationIdentifier parser = new ProteinModificationIdentifier();

		for (Chain c : struc.getChains()) {

			parser.identify(c, ProteinModificationRegistry.allModifications());
			Set<ModifiedCompound> mcs = parser.getIdentifiedModifiedCompound();

			for (ModifiedCompound mc : mcs){
				currentMC = mc;
				xml =  doXMLSerialization(mc) ;
				//logger.info( pdbId + " got XML: " + String.format("%n") + xml);
				ModifiedCompound newMC = getModifiedCompoundFromXML(xml);
				String xml2 = doXMLSerialization(newMC);
				Assert.assertEquals(xml, xml2);
				//logger.info(xml2);
				//assertEquals("The two objects are not equal before and after XML serialization" , mc, newMC);
				//logger.info(mc.getDescription());
				//logger.info(newMC.getDescription());
				all.add(mc);
			}
		}
	} catch (Exception e){
		logger.error(e.getMessage(),e);
		logger.error("Error when serializing {}", pdbId);
		logger.error(currentMC.getDescription());
		logger.error(xml, e);
		Assert.fail(e.getMessage());
	}
	xml = null;
	currentMC =null;
	return all;
}
 
Example 10
Source File: MmtfStructureWriter.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Pass data from Biojava structure  to another generic output type. Loops through the data
 * structure and calls all the set functions.
 * @param structure the input {@link Structure} to write
 * @param dataTransferInterface the generic interface that
 * implements all the set methods.
 */
public MmtfStructureWriter(Structure structure, StructureAdapterInterface dataTransferInterface) {
	this.mmtfDecoderInterface = dataTransferInterface;
	// Reset structure to consider altloc groups with the same residue number but different group names as seperate groups
	MmtfUtils.fixMicroheterogenity(structure);
	// Get the chain name to index map
	MmtfSummaryDataBean mmtfSummaryDataBean = MmtfUtils.getStructureInfo(structure);
	Map<String, Integer> chainIdToIndexMap = mmtfSummaryDataBean.getChainIdToIndexMap();
	List<Atom> allAtoms = mmtfSummaryDataBean.getAllAtoms();
	int numBonds = mmtfSummaryDataBean.getNumBonds();
	List<Chain> allChains = mmtfSummaryDataBean.getAllChains();
	mmtfDecoderInterface.initStructure(numBonds, allAtoms.size(), MmtfUtils.getNumGroups(structure), allChains.size(), structure.nrModels(), structure.getPDBCode());
	// Generate the secondary structure
	MmtfUtils.calculateDsspSecondaryStructure(structure);
	// Get the header and the xtal info.
	PDBHeader pdbHeader = structure.getPDBHeader();
	PDBCrystallographicInfo xtalInfo = pdbHeader.getCrystallographicInfo();
	mmtfDecoderInterface.setHeaderInfo(pdbHeader.getRfree(), pdbHeader.getRwork(), pdbHeader.getResolution(), pdbHeader.getTitle(), MmtfUtils.dateToIsoString(pdbHeader.getDepDate()),
			MmtfUtils.dateToIsoString(pdbHeader.getRelDate()), MmtfUtils.techniquesToStringArray(pdbHeader.getExperimentalTechniques()));
	mmtfDecoderInterface.setXtalInfo(MmtfUtils.getSpaceGroupAsString(xtalInfo.getSpaceGroup()), MmtfUtils.getUnitCellAsArray(xtalInfo), MmtfUtils.getNcsAsArray(xtalInfo.getNcsOperators()));
	// Store the bioassembly data
	storeBioassemblyInformation(chainIdToIndexMap, pdbHeader.getBioAssemblies());
	// Store the entity data
	storeEntityInformation(allChains, structure.getEntityInfos());
	// Now loop through the data structure
	for (int modelIndex=0; modelIndex<structure.nrModels(); modelIndex++) {
		List<Chain> modelChains = structure.getChains(modelIndex);
		// Set this model
		mmtfDecoderInterface.setModelInfo(modelIndex, modelChains.size());
		for(int chainInModelIndex=0; chainInModelIndex<modelChains.size(); chainInModelIndex++) {
			Chain chain = modelChains.get(chainInModelIndex);
			List<Group> groups = chain.getAtomGroups();
			List<Group> sequenceGroups = chain.getSeqResGroups();
			mmtfDecoderInterface.setChainInfo(chain.getId(), chain.getName(), groups.size());
			for(int groupInChainIndex=0; groupInChainIndex<groups.size(); groupInChainIndex++){
				Group group = groups.get(groupInChainIndex);
				List<Atom> atomsInGroup = MmtfUtils.getAtomsForGroup(group);
				ChemComp chemComp = group.getChemComp();
				Character insCode = group.getResidueNumber().getInsCode();
				if(insCode==null || insCode.equals(' ')){
					insCode=MmtfStructure.UNAVAILABLE_CHAR_VALUE;
				}
				char singleLetterCode = 'X';
				if (chemComp.getOne_letter_code().length()==1){
					singleLetterCode = chemComp.getOne_letter_code().charAt(0);
				}
				mmtfDecoderInterface.setGroupInfo(group.getPDBName(), group.getResidueNumber().getSeqNum(), insCode.charValue(),
						chemComp.getType().toUpperCase(), atomsInGroup.size(), MmtfUtils.getNumBondsInGroup(atomsInGroup), singleLetterCode,
						sequenceGroups.indexOf(group), MmtfUtils.getSecStructType(group));
				for (Atom atom : atomsInGroup){
					char altLoc = MmtfStructure.UNAVAILABLE_CHAR_VALUE;
					if(atom.getAltLoc()!=null){
						if(atom.getAltLoc().charValue()!=' '){
							altLoc=atom.getAltLoc().charValue();
						}
					}
					mmtfDecoderInterface.setAtomInfo(atom.getName(), atom.getPDBserial(), altLoc, (float) atom.getX(),
							(float) atom.getY(), (float) atom.getZ(), atom.getOccupancy(),
							atom.getTempFactor(), atom.getElement().toString(), atom.getCharge());
					addBonds(atom, atomsInGroup, allAtoms);
				}
			}
		}
	}
	mmtfDecoderInterface.finalizeStructure();

}
 
Example 11
Source File: ChargeAdder.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Function to add the charges to a given structure.
 */
public static void addCharges(Structure structure) {
	// Loop through the models
	for(int i=0; i<structure.nrModels(); i++){
		for(Chain c: structure.getChains(i)){
			for(Group g: c.getAtomGroups()){
				ChemComp thisChemComp = ChemCompGroupFactory.getChemComp(g.getPDBName());
				List<ChemCompAtom> chemAtoms = thisChemComp.getAtoms();
				for(ChemCompAtom chemCompAtom : chemAtoms) {
					Atom atom = g.getAtom(chemCompAtom.getAtom_id());
					String stringCharge = chemCompAtom.getCharge();
					short shortCharge = 0;
					if (stringCharge!=null){
						if(!stringCharge.equals("?")){
							try{
								shortCharge = Short.parseShort(stringCharge);
							}
							catch(NumberFormatException e){
								logger.warn("Number format exception. Parsing '"+stringCharge+"' to short");
							}
						}
						else{
							logger.warn("? charge on atom "+chemCompAtom.getAtom_id()+" in group "+thisChemComp.getId());
						}
					}
					else{
						logger.warn("Null charge on atom "+chemCompAtom.getAtom_id()+" in group "+thisChemComp.getId());
					}
					if(atom!=null){
						atom.setCharge(shortCharge);
					}
					// Now do the same for alt locs
					for (Group altLoc : g.getAltLocs()) {
						Atom altAtom = altLoc.getAtom(chemCompAtom.getAtom_id());
						if(altAtom!=null){
							altAtom.setCharge(shortCharge);
						}
					}
				}
			}

		}
	}
}
 
Example 12
Source File: TestBondFinding.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Find all of the inter group bonds in a structure.
 *
 * @param pdbId the pdb id of the structure to determine
 * @return the number of inter group bonds (double counted) in a structure
 * @throws IOException
 * @throws StructureException
 */
public int getInterBonds(String pdbId) throws IOException, StructureException {

	// Download parameters
	AtomCache cache = new AtomCache();
	cache.setUseMmCif(true);
	cache.setFetchBehavior(FetchBehavior.FETCH_FILES);
	FileParsingParameters params = cache.getFileParsingParams();
	params.setCreateAtomBonds(true);
	params.setAlignSeqRes(true);
	params.setParseBioAssembly(true);
	DownloadChemCompProvider dcc = new DownloadChemCompProvider();
	ChemCompGroupFactory.setChemCompProvider(dcc);
	dcc.checkDoFirstInstall();
	cache.setFileParsingParams(params);
	StructureIO.setAtomCache(cache);

	// Get the structure
	Structure newStruc = StructureIO.getStructure(pdbId);

	int counter =0;

	// Loop through the atoms and count the bonds
	for(Chain c: newStruc.getChains()){
		for(Group g: c.getAtomGroups()){
			List<Atom> theseAtoms = g.getAtoms();
			for(Atom a: theseAtoms){
				List<Bond> theseBonds = a.getBonds();
				if(theseBonds != null){
					for(Bond b: a.getBonds()){
						Atom other = b.getOther(a);
						int indexOther = theseAtoms.indexOf(other);
						// Check if the index is within the group
						if(indexOther<0 || indexOther >= theseAtoms.size()){
							counter++;
						}
					}
				}
			}
		}
	}
	return counter;
}