public static AFPChain cpFastaToAfpChain(String first, String second, Structure structure, int cpSite) throws StructureException, CompoundNotFoundException { ProteinSequence s1 = new ProteinSequence(first); s1.setUserCollection(getAlignedUserCollection(first)); ProteinSequence s2 = new ProteinSequence(second); s2.setUserCollection(getAlignedUserCollection(second)); return cpFastaToAfpChain(s1, s2, structure, cpSite); }
/** * Prints out the XML representation of an AFPChain from a file containing exactly two FASTA sequences. * * @param args * A String array of fasta-file structure-1-name structure-2-name * @throws StructureException * @throws IOException */ public static void main(String[] args) throws StructureException, IOException { if (args.length != 3) { System.err.println("Usage: FastaAFPChainConverter fasta-file structure-1-name structure-2-name"); return; } File fasta = new File(args[0]); Structure structure1 = StructureTools.getStructure(args[1]); Structure structure2 = StructureTools.getStructure(args[2]); if (structure1 == null) throw new IllegalArgumentException("No structure for " + args[1] + " was found"); if (structure2 == null) throw new IllegalArgumentException("No structure for " + args[2] + " was found"); AFPChain afpChain = fastaFileToAfpChain(fasta, structure1, structure2); String xml = AFPChainXMLConverter.toXML(afpChain); System.out.println(xml); }
/** * TODO Write comment * @param sequence1 * @param sequence2 * @param structure1 * @param structure2 * @return * @throws StructureException * @throws CompoundNotFoundException */ public static AFPChain fastaToAfpChain(String sequence1, String sequence2, Structure structure1, Structure structure2) throws StructureException, CompoundNotFoundException { ProteinSequence s1 = new ProteinSequence(sequence1); s1.setUserCollection(getAlignedUserCollection(sequence1)); ProteinSequence s2 = new ProteinSequence(sequence2); s2.setUserCollection(getAlignedUserCollection(sequence2)); return fastaToAfpChain(s1, s2, structure1, structure2); }
/** * Uses two sequences each with a corresponding structure to create an AFPChain corresponding to the alignment. Provided only for convenience since FastaReaders return such maps. * * @param sequences * A Map containing exactly two entries from sequence names as Strings to gapped ProteinSequences; the name is ignored * @see #fastaToAfpChain(ProteinSequence, ProteinSequence, Structure, Structure) * @throws StructureException */ public static AFPChain fastaToAfpChain(Map<String, ProteinSequence> sequences, Structure structure1, Structure structure2) throws StructureException { if (sequences.size() != 2) { throw new IllegalArgumentException("There must be exactly 2 sequences, but there were " + sequences.size()); } if (structure1 == null || structure2 == null) { throw new IllegalArgumentException("A structure is null"); } List<ProteinSequence> seqs = new ArrayList<ProteinSequence>(); List<String> names = new ArrayList<String>(2); for (Map.Entry<String, ProteinSequence> entry : sequences.entrySet()) { seqs.add(entry.getValue()); names.add(entry.getKey()); } return fastaToAfpChain(seqs.get(0), seqs.get(1), structure1, structure2); }
/** * Returns an AFPChain corresponding to the alignment between {@code structure1} and {@code structure2}, which is given by the gapped protein sequences {@code sequence1} and {@code sequence2}. The * sequences need not correspond to the entire structures, since local alignment is performed to match the sequences to structures. Assumes that a residue is aligned if and only if it is given by * an uppercase letter. * @param sequence1 <em>Must</em> have {@link ProteinSequence#getUserCollection()} set to document upper- and lower-case as aligned and unaligned; see {@link #getAlignedUserCollection(String)} * @throws StructureException */ public static AFPChain fastaToAfpChain(ProteinSequence sequence1, ProteinSequence sequence2, Structure structure1, Structure structure2) throws StructureException { if (structure1 == null || structure2 == null) { throw new IllegalArgumentException("A structure is null"); } if (sequence1 == null || sequence2 == null) { throw new IllegalArgumentException("A sequence is null"); } Atom[] ca1 = StructureTools.getRepresentativeAtomArray(structure1); Atom[] ca2 = StructureTools.getRepresentativeAtomArray(structure2); ResidueNumber[] residues1 = StructureSequenceMatcher.matchSequenceToStructure(sequence1, structure1); ResidueNumber[] residues2 = StructureSequenceMatcher.matchSequenceToStructure(sequence2, structure2); // nullify ResidueNumbers that have a lowercase sequence character if (sequence1.getUserCollection() != null) { CasePreservingProteinSequenceCreator.setLowercaseToNull(sequence1, residues1); } if (sequence2.getUserCollection() != null) { CasePreservingProteinSequenceCreator.setLowercaseToNull(sequence2, residues2); } return buildAlignment(ca1, ca2, residues1, residues2); }
/** * Takes a structure and sequence corresponding to an alignment between a structure or sequence and itself (or even a structure with a sequence), where the result has a circular permutation site * {@link cpSite} residues to the right. * * @param fastaFile A FASTA file containing exactly 2 sequences, the first unpermuted and the second permuted * @param cpSite * The number of residues from the beginning of the sequence at which the circular permutation site occurs; can be positive or negative; values greater than the length of the sequence * are acceptable * @throws IOException * @throws StructureException */ public static AFPChain cpFastaToAfpChain(File fastaFile, Structure structure, int cpSite) throws IOException, StructureException { InputStream inStream = new FileInputStream(fastaFile); SequenceCreatorInterface<AminoAcidCompound> creator = new CasePreservingProteinSequenceCreator(AminoAcidCompoundSet.getAminoAcidCompoundSet()); SequenceHeaderParserInterface<ProteinSequence, AminoAcidCompound> headerParser = new GenericFastaHeaderParser<ProteinSequence, AminoAcidCompound>(); FastaReader<ProteinSequence, AminoAcidCompound> fastaReader = new FastaReader<ProteinSequence, AminoAcidCompound>(inStream, headerParser, creator); LinkedHashMap<String, ProteinSequence> sequences = fastaReader.process(); inStream.close(); Iterator<ProteinSequence> iter = sequences.values().iterator(); ProteinSequence first = iter.next(); ProteinSequence second = iter.next(); return cpFastaToAfpChain(first, second, structure, cpSite); }
/** * Returns an AFPChain corresponding to the alignment between {@code structure1} and {@code structure2}, which is given by the gapped protein sequences {@code sequence1} and {@code sequence2}. The * sequences need not correspond to the entire structures, since local alignment is performed to match the sequences to structures. * @throws StructureException * @throws CompoundNotFoundException */ public static AFPChain fastaStringToAfpChain(String sequence1, String sequence2, Structure structure1, Structure structure2) throws StructureException, CompoundNotFoundException { ProteinSequence seq1 = new ProteinSequence(sequence1); ProteinSequence seq2 = new ProteinSequence(sequence2); return fastaToAfpChain(seq1, seq2, structure1, structure2); }
return buildAlignment(ca1, ca2, residues, nonpermutedResidues);
/** * Provided only for convenience. * * @see #fastaToAfpChain(ProteinSequence, ProteinSequence, Structure, Structure) * @throws StructureException */ public static AFPChain fastaToAfpChain(SequencePair<Sequence<AminoAcidCompound>, AminoAcidCompound> alignment, Structure structure1, Structure structure2) throws StructureException { List<AlignedSequence<Sequence<AminoAcidCompound>, AminoAcidCompound>> seqs = alignment.getAlignedSequences(); StringBuilder sb1 = new StringBuilder(); for (AminoAcidCompound a : seqs.get(0)) { sb1.append(a.getBase()); } try { ProteinSequence seq1 = new ProteinSequence(sb1.toString()); StringBuilder sb2 = new StringBuilder(); for (AminoAcidCompound a : seqs.get(1)) { sb1.append(a.getBase()); } ProteinSequence seq2 = new ProteinSequence(sb2.toString()); LinkedHashMap<String, ProteinSequence> map = new LinkedHashMap<String, ProteinSequence>(); map.put(structure1.getName(), seq1); map.put(structure2.getName(), seq2); return fastaToAfpChain(map, structure1, structure2); } catch (CompoundNotFoundException e) { logger.error("Unexpected error while creating protein sequences: {}. This is most likely a bug.",e.getMessage()); return null; } }
/** * Reads the file {@code fastaFile}, expecting exactly two sequences which give a pairwise alignment. Uses this and two structures to create an AFPChain corresponding to the alignment. Uses a * {@link CasePreservingProteinSequenceCreator} and assumes that a residue is aligned if and only if it is given by an uppercase letter. * * @see #fastaToAfpChain(ProteinSequence, ProteinSequence, Structure, Structure) * @throws IOException * @throws StructureException */ public static AFPChain fastaFileToAfpChain(File fastaFile, Structure structure1, Structure structure2) throws IOException, StructureException { InputStream inStream = new FileInputStream(fastaFile); SequenceCreatorInterface<AminoAcidCompound> creator = new CasePreservingProteinSequenceCreator( AminoAcidCompoundSet.getAminoAcidCompoundSet()); SequenceHeaderParserInterface<ProteinSequence, AminoAcidCompound> headerParser = new GenericFastaHeaderParser<ProteinSequence, AminoAcidCompound>(); FastaReader<ProteinSequence, AminoAcidCompound> fastaReader = new FastaReader<ProteinSequence, AminoAcidCompound>( inStream, headerParser, creator); LinkedHashMap<String, ProteinSequence> sequences = fastaReader.process(); inStream.close(); return fastaToAfpChain(sequences, structure1, structure2); }