Search in sources :

Example 1 with ComputationException

use of de.bioforscher.jstructure.feature.ComputationException in project jstructure by JonStargaryen.

the class OrientationsOfProteinsInMembranesAnnotator method processInternally.

@Override
protected void processInternally(Protein protein) {
    try {
        Document document = getDocument(SEARCH_URL + protein.getPdbId().getPdbId());
        if (document.text().contains("No matches")) {
            throw new ComputationException("did not find OPM entry for " + protein.getIdentifier() + " - possibly it is no membrane protein");
        }
        // create global membrane object - 3rd link points to download
        String downloadLink = document.getElementById("caption").getElementsByTag("a").get(2).attr("href");
        try (InputStreamReader inputStreamReader = new InputStreamReader(new URL(BASE_URL + downloadLink).openStream())) {
            try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
                byte[] bytes = bufferedReader.lines().collect(Collectors.joining(System.lineSeparator())).getBytes();
                // parse protein
                Protein opmProtein = ProteinParser.source(new ByteArrayInputStream(bytes)).forceProteinName(ProteinIdentifier.createFromPdbId(downloadLink.split("=")[0].split("/")[1].substring(0, 4))).parse();
                Membrane membrane = new Membrane(this);
                // superimpose opm protein onto instance of the original protein
                //TODO this alignment is by no means perfect, but works for a first glance
                SVDSuperimposer.ALPHA_CARBON_SVD_INSTANCE.align(protein.select().aminoAcids().asGroupContainer(), opmProtein.select().aminoAcids().asGroupContainer()).transform(opmProtein);
                // extract dummy atoms and move them to membrane object
                List<double[]> membraneAtoms = opmProtein.atoms().map(Atom::getParentGroup).filter(group -> group.getThreeLetterCode().equals("DUM")).flatMap(Group::atoms).map(Atom::getCoordinates).collect(Collectors.toList());
                membrane.setMembraneAtoms(membraneAtoms);
                // extract general information - that is the first table
                Element generalDataTable = document.getElementsByClass("data").get(0);
                Element thicknessTr = generalDataTable.getElementsByTag("tr").get(1);
                membrane.setHydrophobicThickness(thicknessTr.getElementsByTag("td").get(1).text());
                Element tiltTr = generalDataTable.getElementsByTag("tr").get(2);
                membrane.setTiltAngle(tiltTr.getElementsByTag("td").get(1).text());
                Element transferTr = generalDataTable.getElementsByTag("tr").get(3);
                membrane.setDeltaGTransfer(transferTr.getElementsByTag("td").get(1).text());
                Element topologyTr = generalDataTable.getElementsByTag("tr").get(5);
                membrane.setTopology(topologyTr.getElementsByTag("td").get(1).text());
                // extract trans-membrane helices - second table
                Element transMembraneSubunitsTable = document.getElementsByClass("data").get(1);
                List<TransMembraneHelix> helices = transMembraneSubunitsTable.getElementsByTag("tr").stream().skip(1).map(element -> element.getElementsByTag("td").get(0)).map(Element::text).map(TransMembraneHelix::new).collect(Collectors.toList());
                membrane.setTransMembraneHelices(helices);
                protein.getFeatureContainer().addFeature(membrane);
            //                    //TODO remove, used to evaluate alignment manually
            //                    Files.write(Paths.get(System.getProperty("user.home") + "/ori.pdb"), protein.getPdbRepresentation().getBytes());
            //                    Files.write(Paths.get(System.getProperty("user.home") + "/opm.pdb"), opmProtein.getPdbRepresentation().getBytes());
            //                    //TODO remove, used to evaluate segment positions manually
            //                    Files.write(Paths.get(System.getProperty("user.home") + "/tm.pdb"), protein.select()
            //                            .residueNumber(helices.stream()
            //                                    .map(TransMembraneHelix::getSegments)
            //                                    .flatMap(Collection::stream)
            //                                    .collect(Collectors.toList())
            //                                    .toArray(new IntegerRange[0]))
            //                            .asGroupContainer()
            //                            .getPdbRepresentation()
            //                            .getBytes());
            }
        }
    } catch (IOException e) {
        throw new ComputationException("failed to fetch OPM file", e);
    }
}
Also used : AbstractFeatureProvider(de.bioforscher.jstructure.model.feature.AbstractFeatureProvider) ProteinIdentifier(de.bioforscher.jstructure.model.structure.identifier.ProteinIdentifier) ProteinParser(de.bioforscher.jstructure.parser.ProteinParser) Logger(org.slf4j.Logger) ComputationException(de.bioforscher.jstructure.feature.ComputationException) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) SVDSuperimposer(de.bioforscher.jstructure.alignment.SVDSuperimposer) List(java.util.List) ByteArrayInputStream(java.io.ByteArrayInputStream) FeatureProvider(de.bioforscher.jstructure.model.feature.FeatureProvider) Group(de.bioforscher.jstructure.model.structure.Group) Atom(de.bioforscher.jstructure.model.structure.Atom) Document(org.jsoup.nodes.Document) Element(org.jsoup.nodes.Element) Jsoup(org.jsoup.Jsoup) BufferedReader(java.io.BufferedReader) Protein(de.bioforscher.jstructure.model.structure.Protein) Group(de.bioforscher.jstructure.model.structure.Group) InputStreamReader(java.io.InputStreamReader) Element(org.jsoup.nodes.Element) IOException(java.io.IOException) Document(org.jsoup.nodes.Document) URL(java.net.URL) Protein(de.bioforscher.jstructure.model.structure.Protein) Atom(de.bioforscher.jstructure.model.structure.Atom) ByteArrayInputStream(java.io.ByteArrayInputStream) ComputationException(de.bioforscher.jstructure.feature.ComputationException) BufferedReader(java.io.BufferedReader)

Example 2 with ComputationException

use of de.bioforscher.jstructure.feature.ComputationException in project jstructure by JonStargaryen.

the class PLIPParser method parse.

public static List<PLIPInteraction> parse(Chain chain, Document document) {
    List<PLIPInteraction> plipInteractions = new ArrayList<>();
    for (PLIPInteractionType plipInteractionType : PLIPInteractionType.values()) {
        logger.debug("parsing interactions of type {}", plipInteractionType);
        Elements interactionElements = document.getElementsByTag(plipInteractionType.getInteractionTag());
        for (Element interactionElement : interactionElements) {
            Optional<Group> currentGroup = interactionElement.getElementsByTag("resnr").stream().map(Element::text).filter(string -> string.length() < 10).mapToInt(Integer::valueOf).mapToObj(residueNumber -> chain.select().residueNumber(residueNumber).asOptionalGroup()).findFirst().orElse(Optional.empty());
            if (!currentGroup.isPresent()) {
                //TODO does a partner in another chain actually make sense?
                logger.trace("reference to group in different chain or failed to parse line:{}{}", System.lineSeparator(), interactionElement.text());
                continue;
            }
            try {
                Constructor<? extends PLIPInteraction> constructor = plipInteractionType.getDescribingClass().getDeclaredConstructor(Group.class, Element.class);
                constructor.setAccessible(true);
                PLIPInteraction plipInteraction = constructor.newInstance(currentGroup.get(), interactionElement);
                plipInteractions.add(plipInteraction);
            } catch (Exception e) {
                // move to root cause
                Throwable cause = e;
                while (cause.getCause() != null) {
                    cause = cause.getCause();
                }
                logger.warn("encountered exception during plip parsing: {}", cause);
                throw new ComputationException(e);
            }
        }
    }
    // merge entries which need merging and remove the merged entries
    List<PLIPInteraction> plipInteractionsToRemove = new ArrayList<>();
    for (PLIPInteraction plipInteraction : plipInteractions) {
        Group partner1 = plipInteraction.getPartner1();
        Group partner2 = plipInteraction.getPartner2();
        if (plipInteraction instanceof PiCationInteraction || plipInteraction instanceof PiStacking || plipInteraction instanceof SaltBridge) {
            // keep only those interactions where the first resNum is smaller
            if (partner1.getResidueNumber().getResidueNumber() > partner2.getResidueNumber().getResidueNumber()) {
                plipInteractionsToRemove.add(plipInteraction);
                continue;
            }
            try {
                if (plipInteraction instanceof PiCationInteraction) {
                    PiCationInteraction otherHalfOfInteraction = plipInteractions.stream().filter(PiCationInteraction.class::isInstance).map(PiCationInteraction.class::cast).filter(piCationInteraction -> piCationInteraction.getPartner1().equals(partner2) && piCationInteraction.getPartner2().equals(partner1)).findFirst().orElseThrow(NoSuchElementException::new);
                    ((PiCationInteraction) plipInteraction).getAtoms1().addAll(otherHalfOfInteraction.getAtoms2());
                } else if (plipInteraction instanceof PiStacking) {
                    PiStacking otherHalfOfInteraction = plipInteractions.stream().filter(PiStacking.class::isInstance).map(PiStacking.class::cast).filter(piStacking -> piStacking.getPartner1().equals(partner2) && piStacking.getPartner2().equals(partner1)).findFirst().orElseThrow(NoSuchElementException::new);
                    ((PiStacking) plipInteraction).getAtoms1().addAll(otherHalfOfInteraction.getAtoms2());
                } else {
                    SaltBridge otherHalfOfInteraction = plipInteractions.stream().filter(SaltBridge.class::isInstance).map(SaltBridge.class::cast).filter(saltBridge -> saltBridge.getPartner1().equals(partner2) && saltBridge.getPartner2().equals(partner1)).findFirst().orElseThrow(NoSuchElementException::new);
                    ((SaltBridge) plipInteraction).getAtoms1().addAll(otherHalfOfInteraction.getAtoms2());
                }
            } catch (NoSuchElementException e) {
                logger.debug("could not find other half of {} {} {}", plipInteraction.getClass().getSimpleName(), plipInteraction.getPartner1().getIdentifier(), plipInteraction.getPartner2().getIdentifier());
            }
        }
    }
    plipInteractions.removeAll(plipInteractionsToRemove);
    return plipInteractions;
}
Also used : Logger(org.slf4j.Logger) ComputationException(de.bioforscher.jstructure.feature.ComputationException) LoggerFactory(org.slf4j.LoggerFactory) Constructor(java.lang.reflect.Constructor) ArrayList(java.util.ArrayList) List(java.util.List) Group(de.bioforscher.jstructure.model.structure.Group) Document(org.jsoup.nodes.Document) Element(org.jsoup.nodes.Element) Chain(de.bioforscher.jstructure.model.structure.Chain) Optional(java.util.Optional) Elements(org.jsoup.select.Elements) NoSuchElementException(java.util.NoSuchElementException) Group(de.bioforscher.jstructure.model.structure.Group) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Elements(org.jsoup.select.Elements) ComputationException(de.bioforscher.jstructure.feature.ComputationException) NoSuchElementException(java.util.NoSuchElementException) ComputationException(de.bioforscher.jstructure.feature.ComputationException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ComputationException (de.bioforscher.jstructure.feature.ComputationException)2 Group (de.bioforscher.jstructure.model.structure.Group)2 List (java.util.List)2 Document (org.jsoup.nodes.Document)2 Element (org.jsoup.nodes.Element)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 SVDSuperimposer (de.bioforscher.jstructure.alignment.SVDSuperimposer)1 AbstractFeatureProvider (de.bioforscher.jstructure.model.feature.AbstractFeatureProvider)1 FeatureProvider (de.bioforscher.jstructure.model.feature.FeatureProvider)1 Atom (de.bioforscher.jstructure.model.structure.Atom)1 Chain (de.bioforscher.jstructure.model.structure.Chain)1 Protein (de.bioforscher.jstructure.model.structure.Protein)1 ProteinIdentifier (de.bioforscher.jstructure.model.structure.identifier.ProteinIdentifier)1 ProteinParser (de.bioforscher.jstructure.parser.ProteinParser)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Constructor (java.lang.reflect.Constructor)1