Skip to content

Commit d4fa2af

Browse files
committed
Prettier mmCIF output with all columns adjusted to their sizes
1 parent fe7396c commit d4fa2af

File tree

3 files changed

+300
-227
lines changed

3 files changed

+300
-227
lines changed

biojava-structure/src/main/java/org/biojava/nbio/structure/io/FileConvert.java

Lines changed: 25 additions & 182 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ public class FileConvert {
4747

4848
private static final Logger logger = LoggerFactory.getLogger(FileConvert.class);
4949

50-
/**
51-
* The character to be printed out in cases where a value is not assigned in mmCIF files
52-
*/
53-
public static final String MMCIF_MISSING_VALUE = "?";
50+
5451

5552
private Structure structure ;
5653

@@ -687,206 +684,52 @@ else if (name.length()==1)
687684
}
688685

689686

690-
public String toMmCif() {
691-
StringBuilder str = new StringBuilder();
692-
693-
try {
694-
String header = MMCIFFileTools.toLoopMmCifHeaderString("_atom_site", AtomSite.class.getName());
695-
str.append(header);
696-
} catch (ClassNotFoundException e) {
697-
logger.error("Class not found, will not have a header for this MMCIF category: "+e.getMessage());
698-
}
687+
public String toMMCIF() {
699688

689+
StringBuilder str = new StringBuilder();
700690

691+
str.append(MMCIFFileTools.MMCIF_TOP_HEADER+"BioJava_mmCIF_file"+newline);
701692

702-
int nrModels = structure.nrModels();
693+
str.append(getAtomSiteHeader());
703694

704-
for (int m = 0 ; m < nrModels ; m++) {
705-
List<Chain> model = structure.getModel(m);
706-
707-
708-
int nrChains = model.size();
709-
for ( int c =0; c<nrChains;c++) {
710-
Chain chain = model.get(c);
711-
712-
if (chain.getCompound()==null) {
713-
logger.warn("No Compound (entity) found for chain {}: entity_id will be set to 0, label_seq_id will be the same as auth_seq_id", chain.getChainID());
714-
}
715-
716-
int nrGroups = chain.getAtomLength();
717-
718-
for ( int h=0; h<nrGroups;h++){
719-
720-
Group g= chain.getAtomGroup(h);
721-
722-
toMmCif(g,str,m+1);
723-
724-
}
725-
726-
}
695+
@SuppressWarnings("unchecked")
696+
List<Object> list =
697+
(List<Object>) (List<?>) MMCIFFileTools.structureToAtomSites(structure);
727698

728699

729-
}
700+
str.append(MMCIFFileTools.toMMCIF(list));
730701

731702
return str.toString();
732703
}
733704

734-
public static String toMmCif(Chain chain, String chainId, String internalChainId) {
705+
public static String toMMCIF(Chain chain, String chainId, String internalChainId) {
735706
StringBuilder str = new StringBuilder();
736707

737-
try {
738-
String header = MMCIFFileTools.toLoopMmCifHeaderString("_atom_site", AtomSite.class.getName());
739-
str.append(header);
740-
} catch (ClassNotFoundException e) {
741-
logger.error("Class not found, will not have a header for this MMCIF category: "+e.getMessage());
742-
}
743-
744-
if (chain.getCompound()==null) {
745-
logger.warn("No Compound (entity) found for chain {}: entity_id will be set to 0, label_seq_id will be the same as auth_seq_id", chain.getChainID());
746-
}
708+
str.append(getAtomSiteHeader());
747709

748710

749-
int nrGroups = chain.getAtomLength();
750-
751-
for ( int h=0; h<nrGroups;h++){
752-
753-
Group g= chain.getAtomGroup(h);
754-
755-
toMmCif(g,str, 1, chainId, internalChainId);
756-
757-
}
711+
@SuppressWarnings("unchecked")
712+
List<Object> list =
713+
(List<Object>) (List<?>) MMCIFFileTools.chainToAtomSites(chain, 1, chainId, internalChainId);
758714

715+
str.append(MMCIFFileTools.toMMCIF(list));
759716
return str.toString();
760717
}
761718

762-
public static String toMmCif(Chain chain) {
763-
StringBuilder str = new StringBuilder();
764-
765-
try {
766-
String header = MMCIFFileTools.toLoopMmCifHeaderString("_atom_site", AtomSite.class.getName());
767-
str.append(header);
768-
} catch (ClassNotFoundException e) {
769-
logger.error("Class not found, will not have a header for this MMCIF category: "+e.getMessage());
770-
}
771-
772-
int nrGroups = chain.getAtomLength();
773-
774-
for ( int h=0; h<nrGroups;h++){
775-
776-
Group g= chain.getAtomGroup(h);
777-
778-
toMmCif(g, str, 1);
779-
780-
}
781-
782-
return str.toString();
783-
}
784-
785-
public static void toMmCif(Group g, StringBuilder str, int model) {
786-
toMmCif(g, str, model, g.getChainId(), g.getChain().getInternalChainID());
719+
public static String toMMCIF(Chain chain) {
720+
return toMMCIF(chain, chain.getChainID(),chain.getInternalChainID());
787721
}
788722

789-
public static void toMmCif(Group g, StringBuilder str, int model, String chainId, String internalChainId) {
790-
int groupsize = g.size();
791-
792-
for ( int atompos = 0 ; atompos < groupsize; atompos++) {
793-
Atom a = null ;
794-
795-
a = g.getAtom(atompos);
796-
if ( a == null)
797-
continue ;
798-
799-
toMmCif(a, str, model, chainId, internalChainId);
723+
private static String getAtomSiteHeader() {
724+
String header;
725+
try {
726+
header = MMCIFFileTools.toLoopMmCifHeaderString("_atom_site", AtomSite.class.getName());
800727

728+
} catch (ClassNotFoundException e) {
729+
logger.error("Class not found, will not have a header for this MMCIF category: "+e.getMessage());
730+
header = "";
801731
}
802-
if ( g.hasAltLoc()){
803-
for (Group alt : g.getAltLocs() ) {
804-
toMmCif(alt,str,model,chainId,internalChainId);
805-
}
806-
}
807-
}
808-
809-
/**
810-
* Write the atom as a mmCIF atom_site record.
811-
* @param a
812-
* @param str
813-
* @param chainId
814-
* @param internalChainId
815-
*/
816-
private static void toMmCif(Atom a, StringBuilder str, int model, String chainId, String internalChainId) {
817-
818-
/*
819-
ATOM 7 C CD . GLU A 1 24 ? -10.109 15.374 38.853 1.00 50.05 ? ? ? ? ? ? 24 GLU A CD 1
820-
ATOM 8 O OE1 . GLU A 1 24 ? -9.659 14.764 37.849 1.00 49.80 ? ? ? ? ? ? 24 GLU A OE1 1
821-
ATOM 9 O OE2 . GLU A 1 24 ? -11.259 15.171 39.310 1.00 50.51 ? ? ? ? ? ? 24 GLU A OE2 1
822-
ATOM 10 N N . LEU A 1 25 ? -5.907 18.743 37.412 1.00 41.55 ? ? ? ? ? ? 25 LEU A N 1
823-
ATOM 11 C CA . LEU A 1 25 ? -5.168 19.939 37.026 1.00 37.55 ? ? ? ? ? ? 25 LEU A CA 1
824-
*/
825-
826-
Group g = a.getGroup();
827-
828-
String record ;
829-
if ( g.getType().equals(GroupType.HETATM) ) {
830-
record = "HETATM";
831-
} else {
832-
record = "ATOM";
833-
}
834-
835-
String entityId = "0";
836-
String labelSeqId = Integer.toString(g.getResidueNumber().getSeqNum());
837-
if (g.getChain()!=null && g.getChain().getCompound()!=null) {
838-
entityId = Integer.toString(g.getChain().getCompound().getMolId());
839-
labelSeqId = Integer.toString(g.getChain().getCompound().getAlignedResIndex(g, g.getChain()));
840-
}
841-
842-
Character altLoc = a.getAltLoc() ;
843-
String altLocStr = altLoc.toString();
844-
if (altLoc==null || altLoc == ' ') {
845-
altLocStr = ".";
846-
}
847-
848-
Element e = a.getElement();
849-
String eString = e.toString().toUpperCase();
850-
if ( e.equals(Element.R)) {
851-
eString = "X";
852-
}
853-
854-
String insCode = MMCIF_MISSING_VALUE;
855-
if (g.getResidueNumber().getInsCode()!=null ) {
856-
insCode = Integer.toString(g.getResidueNumber().getInsCode());
857-
}
858-
859-
AtomSite atomSite = new AtomSite();
860-
atomSite.setGroup_PDB(record);
861-
atomSite.setId(Integer.toString(a.getPDBserial()));
862-
atomSite.setType_symbol(eString);
863-
atomSite.setLabel_atom_id(a.getName());
864-
atomSite.setLabel_alt_id(altLocStr);
865-
atomSite.setLabel_comp_id(g.getPDBName());
866-
atomSite.setLabel_asym_id(internalChainId);
867-
atomSite.setLabel_entity_id(entityId);
868-
atomSite.setLabel_seq_id(labelSeqId);
869-
atomSite.setPdbx_PDB_ins_code(insCode);
870-
atomSite.setCartn_x(d3.format(a.getX()));
871-
atomSite.setCartn_y(d3.format(a.getY()));
872-
atomSite.setCartn_z(d3.format(a.getZ()));
873-
atomSite.setOccupancy(d2.format(a.getOccupancy()));
874-
atomSite.setB_iso_or_equiv(d2.format(a.getTempFactor()));
875-
atomSite.setCartn_x_esd(MMCIF_MISSING_VALUE);
876-
atomSite.setCartn_y_esd(MMCIF_MISSING_VALUE);
877-
atomSite.setCartn_z_esd(MMCIF_MISSING_VALUE);
878-
atomSite.setOccupancy_esd(MMCIF_MISSING_VALUE);
879-
atomSite.setB_iso_or_equiv_esd(MMCIF_MISSING_VALUE);
880-
atomSite.setPdbx_formal_charge(MMCIF_MISSING_VALUE);
881-
atomSite.setAuth_seq_id(Integer.toString(g.getResidueNumber().getSeqNum()));
882-
atomSite.setAuth_comp_id(g.getPDBName());
883-
atomSite.setAuth_asym_id(chainId);
884-
atomSite.setAuth_atom_id(a.getName());
885-
atomSite.setPdbx_PDB_model_num(Integer.toString(model));
886-
887-
str.append(MMCIFFileTools.toSingleLineMmCifString(atomSite));
888732

733+
return header;
889734
}
890-
891-
892735
}

0 commit comments

Comments
 (0)