snakemake output files as list/table - snakemake

I have written this rule in my snakefile
rule extractfeat:
input:
'/path/to/file/{genome}.gbk'
output:
'{genome}_{locus_tag}_{gene}_{substrate}.fasta'
shell:
'''
extractfeat {input} {output} -value {wildcards.genome}_{wildcards.locus_tag} -type CDS -describe product,locus_tag
'''
I want to get all the output files (by output files I mean file=row) that are written in a separate file that looks like this:
genome locus_tag gene substrate
PalbDSM11370 02121 susC pululan
PalbDSM11370 02122 susD pululan
PalbDSM11370 01210 susC arabinan
PalbDSM11370 01209 susD arabinan
PalbDSM11370 02015 susC bglukan
PalbDSM11370 02016 susD bglukan
PpalDSM17968 00934 susC pululan
PpalDSM17968 00933 susD pululan
PpalDSM17968 02229 susC arabinan
PpalDSM17968 02228 susD arabinan
PpalDSM17968 01622 susC bglukan
PpalDSM17968 01623 susD bglukan
PREVCOP 05864 susC pululan
PREVCOP 05865 susD pululan
PREVCOP 05852 susC arabinan
PREVCOP 05851 susD arabinan
PREVCOP 05099 susC bglukan
PREVCOP 05098 susD bglukan
PREVCOP 03646 susC ksiloglukan
PREVCOP 03645 susD ksiloglukan
Psp.AGR2160 00839 susC ksiloglukan
Thanks

You can add this as another rule, with inputs dependent on all the outputs of your generating rule:
rule tabulate:
input: <ALL THE FASTA FILES>
output: 'table.txt'
run:
wcs = glob_wildcards('{genome}_{locus_tag}_{gene}_{substrate}.fasta')
with open(output[0], 'w') as outfile:
outfile.write('genome\tlocus_tag\tgene\tsubstrate\n') # header
for row in zip(*wcs): # order will match order in wildcard string
outfile.write('\t'.join(row) + '\n')
Or if you already have lists of those wildcards you can write that directly instead of using glob_wildcards.

Related

How can I obtain a field plot instead of getting this particular solution plot only in Maple?

I have a fourth order autonomous ordinary differential equation from which I need to obtain a field plot but I only get a line plot of the particular solution;
EQ1 := (-X(mu)(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1)+X(mu)^2*(diff(X(mu), $(mu, 2)))^2-X(mu)^2*(diff(X(mu), $(mu, 3)))(diff(X(mu), mu)-1)-2(diff(X(mu), mu))(diff(X(mu), mu)-1)^3+(8/3)(diff(X(mu), mu)-1)^3+2X(mu)(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1)^2)((3/2)(diff(X(mu), mu))(diff(X(mu), mu)-1)^2-(diff(X(mu), mu)-1)^2-3X(mu)(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1)+(3/2)X(mu)^2(diff(X(mu), $(mu, 2)))^2-(3/2)X(mu)^2(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1))-((3/2)(diff(X(mu), mu))(diff(X(mu), mu)-1)-(diff(X(mu), mu))+1-(3/2)X(mu)(diff(X(mu), $(mu, 2))))((2(diff(X(mu), mu))(diff(X(mu), mu)-1)-8/3(diff(X(mu), mu)-1)-2X(mu)(diff(X(mu), $(mu, 2))))(2(diff(X(mu), mu))(diff(X(mu), mu)-1)^3-(8/3)(diff(X(mu), mu)-1)^3-2X(mu)(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1)^2-X(mu)(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1)+X(mu)^2(diff(X(mu), $(mu, 2)))^2+X(mu)^2*(diff(X(mu), $(mu, 3)))(diff(X(mu), mu)-1))-2X(mu)(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1)^4+2X(mu)(diff(X(mu), $(mu, 2)))((diff(X(mu), mu)-1)^4+(diff(X(mu), mu)-1)^3-X(mu)(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1)^2)+2X(mu)^2*(diff(X(mu), $(mu, 3)))(diff(X(mu), mu)-1)^3-X(mu)^2(diff(X(mu), $(mu, 3)))(diff(X(mu), mu)-1)^2-2X(mu)(diff(X(mu), $(mu, 2)))((diff(X(mu), mu)-1)^3+(diff(X(mu), mu)-1)^2-X(mu)(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1))+X(mu)(diff(X(mu), mu))(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1)^2+2X(mu)^3*(diff(X(mu), $(mu, 2)))(diff(X(mu), $(mu, 3)))(diff(X(mu), mu)-1)+3X(mu)^2(diff(X(mu), $(mu, 2)))^2*((diff(X(mu), mu)-1)^2+diff(X(mu), mu)-1-X(mu)(diff(X(mu), $(mu, 2))))-X(mu)^2(diff(X(mu), mu))(diff(X(mu), $(mu, 2)))^2(diff(X(mu), mu)-1)-2X(mu)(diff(X(mu), $(mu, 3)))((diff(X(mu), mu)-1)^4+(diff(X(mu), mu)-1)^3-X(mu)(diff(X(mu), $(mu, 2)))(diff(X(mu), mu)-1)^2)-X(mu)^3(diff(X(mu), $(mu, 4)))*(diff(X(mu), mu)-1)^2) = 0
The code I run is;
> with(DEtools);
> DEplot(EQ1, X(mu), mu = 0 .. 1, X = 0 .. 1, {[X(0) = .5, (D(X))(0) = .5, ((D##2)(X))(0) = -.5, ((D##3)(X))(0) = -.5]})
The plot I get which turns out to be for one particular solution is this;
DEplot output

Transform xyz data into surface for Plotly

I have a text file with xyz data called elevation.txt.
29.6615067663688 -98.3933654143067 -0.772875302700243
29.6615067663688 -98.3933757636723 -0.71250410914962
29.6615065076668 -98.3933654143067 -0.757068728178126
29.6615032793597 -98.3933757636723 -0.761051993140882
29.6615067663688 -98.3933647490732 -0.762067716839908
29.6615067663688 -98.3933861130379 -0.6694827930163
29.6615039261939 -98.3933861130379 -0.700458102119342
29.6615067663688 -98.3933964624035 -0.616901857503478
29.661506592754 -98.3933964624035 -0.616901857503478
29.6615067663688 -98.3933969253778 -0.624557146812265
29.6615157595849 -98.3933447155754 -0.765732910338727
29.6615157595849 -98.393355064941 -0.70790301178198
29.6615151056918 -98.3933447155754 -0.764905434184072
29.66151057784 -98.393355064941 -0.775122479690196
29.6615157595849 -98.3933432209641 -0.769340655884378
29.6615157595849 -98.3933654143067 -0.65363454400682
29.6615157595849 -98.3933757636723 -0.607481276569502
29.6615157595849 -98.3933861130379 -0.576043135792759
29.6615157595849 -98.3933964624035 -0.562651209935325
29.6615157595849 -98.3934068117692 -0.574109217270234
29.6615106655842 -98.3934068117692 -0.614824188539728
29.6615157595849 -98.3934151474394 -0.590072189764953
29.6615247528009 -98.3933240168442 -0.716696572671196
29.6615247528009 -98.3933343662098 -0.686067695192037
29.661524433825 -98.3933240168442 -0.716696572671196
29.6615197295622 -98.3933343662098 -0.756041264529586
29.6615247528009 -98.393323315098 -0.727161408607287
29.6615247528009 -98.3933447155754 -0.638302943876681
29.6615247528009 -98.393355064941 -0.595028363722974
29.6615247528009 -98.3933654143067 -0.556618694163944
29.6615247528009 -98.3933757636723 -0.524177330077976
29.6615247528009 -98.3933861130379 -0.502490718796878
29.6615247528009 -98.3933964624035 -0.493857409456894
29.6615247528009 -98.3934068117692 -0.504201213967065
29.6615247528009 -98.3934171611348 -0.537894228748428
29.6615169901716 -98.3934171611348 -0.583259938640822
29.6615247528009 -98.3934272247451 -0.553953638867654
29.661533746017 -98.3933136674785 -0.65337903725532
29.661533746017 -98.3933240168442 -0.606100909015026
29.6615302013555 -98.3933136674785 -0.708003376811007
29.661533746017 -98.3933086036904 -0.674403153398373
29.661533746017 -98.3933343662098 -0.567366118147447
29.661533746017 -98.3933447155754 -0.534873445625749
29.661533746017 -98.393355064941 -0.508345134300043
29.661533746017 -98.3933654143067 -0.4861347257239
29.661533746017 -98.3933757636723 -0.467501455070533
29.661533746017 -98.3933861130379 -0.454825455237369
29.661533746017 -98.3933964624035 -0.450999899418036
29.661533746017 -98.3934068117692 -0.460597462045143
29.661533746017 -98.3934171611348 -0.485507380618685
29.661533746017 -98.3934275105004 -0.527974722804913
29.661525070305 -98.3934275105004 -0.556840681625602
29.661533746017 -98.3934344310752 -0.562287963820779
29.6615427392331 -98.3933033181129 -0.573540650163409
29.6615427392331 -98.3933136674785 -0.534670699160301
29.6615374459315 -98.3933033181129 -0.636710444804976
29.6615427392331 -98.393296254736 -0.60161550624673
29.6615427392331 -98.3933240168442 -0.504091160067674
29.6615427392331 -98.3933343662098 -0.481685184127628
29.6615427392331 -98.3933447155754 -0.464099499361851
29.6615427392331 -98.393355064941 -0.451925941678382
29.6615427392331 -98.3933654143067 -0.442387452656249
29.6615427392331 -98.3933757636723 -0.434505319003051
29.6615427392331 -98.3933861130379 -0.43005283413379
29.6615427392331 -98.3933964624035 -0.430975358433614
29.6615427392331 -98.3934068117692 -0.439570957852027
29.6615427392331 -98.3934171611348 -0.457793169386434
29.6615427392331 -98.3934275105004 -0.486800614061849
29.6615427392331 -98.393437859866 -0.526190392944102
29.6615399177873 -98.393437859866 -0.537130143062313
29.6615427392331 -98.3934387004372 -0.514852475111695
29.6615517324491 -98.3932929687473 -0.505021052994948
29.6615517324491 -98.3933033181129 -0.473109594469918
29.6615456966351 -98.3932929687473 -0.575492082966775
29.6615517324491 -98.3932864360227 -0.521250369085692
29.6615517324491 -98.3933136674785 -0.45228220458921
29.6615517324491 -98.3933240168442 -0.437672906029206
29.6615517324491 -98.3933343662098 -0.428764266490191
29.6615517324491 -98.3933447155754 -0.423388509125061
29.6615517324491 -98.393355064941 -0.42064725625545
29.6615517324491 -98.3933654143067 -0.42059184404286
29.6615517324491 -98.3933757636723 -0.421243310930151
29.6615517324491 -98.3933861130379 -0.423173771259656
29.6615517324491 -98.3933964624035 -0.428077085966038
29.6615517324491 -98.3934068117692 -0.436455120482265
29.6615517324491 -98.3934171611348 -0.449453737366639
29.6615517324491 -98.3934275105004 -0.467176208307023
29.6615517324491 -98.393437859866 -0.491158847783368
29.6615517324491 -98.3934410558634 -0.497183097610673
29.6615607256652 -98.3932826193817 -0.448472127921775
29.6615607256652 -98.3932929687473 -0.423026080899604
29.6615555491089 -98.3932826193817 -0.489444421659471
29.6615607256652 -98.3932774428508 -0.459300103364114
29.6615607256652 -98.3933033181129 -0.41007264067008
29.6615607256652 -98.3933136674785 -0.403379653580657
29.6615607256652 -98.3933240168442 -0.40160597586771
29.6615607256652 -98.3933343662098 -0.402303299508787
29.6615607256652 -98.3933447155754 -0.405026619865623
29.6615607256652 -98.393355064941 -0.409214051179902
29.6615607256652 -98.3933654143067 -0.415145859438724
29.6615607256652 -98.3933757636723 -0.421400604537439
29.6615607256652 -98.3933861130379 -0.428143788263469
29.6615607256652 -98.3933964624035 -0.436987817589795
29.6615607256652 -98.3934068117692 -0.44512400629128
29.6615607256652 -98.3934171611348 -0.454197381772929
29.6615607256652 -98.3934275105004 -0.463704639044295
29.6615607256652 -98.393437859866 -0.475074798026128
29.6615607256652 -98.3934419552231 -0.47966411974312
29.6615697188812 -98.393272270016 -0.406872175697117
29.6615697188812 -98.3932826193817 -0.381486406072099
29.6615665050732 -98.393272270016 -0.423858240303332
29.6615697188812 -98.3932696405537 -0.408051452734071
29.6615697188812 -98.3932929687473 -0.37507324528184
29.6615697188812 -98.3933033181129 -0.376443590970155
29.6615697188812 -98.3933136674785 -0.381102967001596
29.6615697188812 -98.3933240168442 -0.387274655391025
29.6615697188812 -98.3933343662098 -0.39395895958836
29.6615697188812 -98.3933447155754 -0.402001773351628
29.6615697188812 -98.393355064941 -0.410521732197196
29.6615697188812 -98.3933654143067 -0.42095882320541
29.6615697188812 -98.3933757636723 -0.430973881383409
29.6615697188812 -98.3933861130379 -0.441647395592099
29.6615697188812 -98.3933964624035 -0.452005925774586
29.6615697188812 -98.3934068117692 -0.461284006552319
29.6615697188812 -98.3934171611348 -0.467917848494494
29.6615697188812 -98.3934275105004 -0.471415514054002
29.6615697188812 -98.393437859866 -0.473245094507774
29.6615697188812 -98.3934419996021 -0.472659420054981
29.6615787120973 -98.393272270016 -0.355651132143748
29.6615787120973 -98.3932826193817 -0.347239308692565
29.6615787120973 -98.3932632737198 -0.35591381410751
29.6615787120973 -98.3932929687473 -0.35162619014784
29.6615787120973 -98.3933033181129 -0.361200636616275
29.6615787120973 -98.3933136674785 -0.371593174544898
29.6615787120973 -98.3933240168442 -0.382765752356477
29.6615787120973 -98.3933343662098 -0.395095946884567
29.6615787120973 -98.3933447155754 -0.407420962025753
29.6615787120973 -98.393355064941 -0.420616003396162
29.6615787120973 -98.3933654143067 -0.434115117961653
29.6615787120973 -98.3933757636723 -0.447571228535537
29.6615787120973 -98.3933861130379 -0.460083111502854
29.6615787120973 -98.3933964624035 -0.471282501279632
29.6615787120973 -98.3934068117692 -0.479825008942328
29.6615787120973 -98.3934171611348 -0.483889960099589
29.6615787120973 -98.3934275105004 -0.483803486838212
29.6615787120973 -98.393437859866 -0.479388198273796
29.6615787120973 -98.3934408449891 -0.478342909984406
29.6615877053134 -98.3932619206504 -0.352953163198265
29.6615877053134 -98.393272270016 -0.330661192674995
29.661580967234 -98.3932619206504 -0.367248936850558
I read the data using
elevation = np.loadtxt('elevation.txt')
x, y, z = elevation[:, 0], elevation[:, 1], elevation[:, 2]
Because each z point is an unique combination of the corresponding x and y point, they all have the same length, 150.
I want to post this surface using plotly, not matplotlib.
Their documentation says the following:
The data the describes the coordinates of the surface is set in z.
Data in z should be a 2D list. Coordinates in x and y can either be 1D
lists or 2D lists (e.g. to graph parametric surfaces).
However, the shape of z is equal to
(150, )
It needs to be equal to
(150, 150)
How can I achieve this?
The Surface class expects the points to be spaced linearly across both x and y (like those that would be created by np.linspace, examples here). It appears that your data is a set of 3d points which are not linearly spaced out in a grid as Surface expects.
Perhaps a better solution would be to build a 3d scatter plot, or a 3d mesh.

How to use cartopy to create colored US states

I need to create a map where states have different colors depending on a piece of data about that state. I found an example of a US map in the cartopy gallery, but it didn't demonstrate how to refer to the states and access their attributes, and there little else out there:
From the example, I've simplified their code to the following, and would appreciate any help with modifying this to get the face colors of the states to be set according to the magnitude of popdensity for the state.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())
ax.set_extent([-125, -66.5, 20, 50], ccrs.Geodetic())
shapename = 'admin_1_states_provinces_lakes_shp'
states_shp = shpreader.natural_earth(resolution='110m',
category='cultural', name=shapename)
popdensity = {
'New Jersey': 438.00,
'Rhode Island': 387.35,
'Massachusetts': 312.68,
'Connecticut': 271.40,
'Maryland': 209.23,
'New York': 155.18,
'Delaware': 154.87,
'Florida': 114.43,
'Ohio': 107.05,
'Pennsylvania': 105.80,
'Illinois': 86.27,
'California': 83.85,
'Virginia': 69.03,
'Michigan': 67.55,
'Indiana': 65.46,
'North Carolina': 63.80,
'Georgia': 54.59,
'Tennessee': 53.29,
'New Hampshire': 53.20,
'South Carolina': 51.45,
'Louisiana': 39.61,
'Kentucky': 39.28,
'Wisconsin': 38.13,
'Washington': 34.20,
'Alabama': 33.84,
'Missouri': 31.36,
'Texas': 30.75,
'West Virginia': 29.00,
'Vermont': 25.41,
'Minnesota': 23.86,
'Mississippi': 23.42,
'Iowa': 20.22,
'Arkansas': 19.82,
'Oklahoma': 19.40,
'Arizona': 17.43,
'Colorado': 16.01,
'Maine': 15.95,
'Oregon': 13.76,
'Kansas': 12.69,
'Utah': 10.50,
'Nebraska': 8.60,
'Nevada': 7.03,
'Idaho': 6.04,
'New Mexico': 5.79,
'South Dakota': 3.84,
'North Dakota': 3.59,
'Montana': 2.39,
'Wyoming': 1.96}
ax.background_patch.set_visible(False)
ax.outline_patch.set_visible(False)
ax.set_title('State Population Density')
for state in shpreader.Reader(states_shp).geometries():
### I need to replace the following code with code that sets the
### facecolor as a gradient based on the population density above
facecolor = [0.9375, 0.9375, 0.859375]
edgecolor = 'black'
ax.add_geometries([state], ccrs.PlateCarree(),
facecolor=facecolor, edgecolor=edgecolor)
plt.show()
To have access to states' attributes, you need to iterate through .records() rather than .geometries(). Here is a working code based on yours. Read comments in the code's portions that I add / modified for clarification.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1], projection=ccrs.LambertConformal())
ax.set_extent([-125, -66.5, 20, 50], ccrs.Geodetic())
shapename = 'admin_1_states_provinces_lakes_shp'
states_shp = shpreader.natural_earth(resolution='110m',
category='cultural', name=shapename)
popdensity = {
'New Jersey': 438.00,
'Rhode Island': 387.35,
'Massachusetts': 312.68,
'Connecticut': 271.40,
'Maryland': 209.23,
'New York': 155.18,
'Delaware': 154.87,
'Florida': 114.43,
'Ohio': 107.05,
'Pennsylvania': 105.80,
'Illinois': 86.27,
'California': 83.85,
'Virginia': 69.03,
'Michigan': 67.55,
'Indiana': 65.46,
'North Carolina': 63.80,
'Georgia': 54.59,
'Tennessee': 53.29,
'New Hampshire': 53.20,
'South Carolina': 51.45,
'Louisiana': 39.61,
'Kentucky': 39.28,
'Wisconsin': 38.13,
'Washington': 34.20,
'Alabama': 33.84,
'Missouri': 31.36,
'Texas': 30.75,
'West Virginia': 29.00,
'Vermont': 25.41,
'Minnesota': 23.86,
'Mississippi': 23.42,
'Iowa': 20.22,
'Arkansas': 19.82,
'Oklahoma': 19.40,
'Arizona': 17.43,
'Colorado': 16.01,
'Maine': 15.95,
'Oregon': 13.76,
'Kansas': 12.69,
'Utah': 10.50,
'Nebraska': 8.60,
'Nevada': 7.03,
'Idaho': 6.04,
'New Mexico': 5.79,
'South Dakota': 3.84,
'North Dakota': 3.59,
'Montana': 2.39,
'Wyoming': 1.96}
ax.background_patch.set_visible(False)
ax.outline_patch.set_visible(False)
ax.set_title('State Population Density')
#for state in shpreader.Reader(states_shp).geometries():
for astate in shpreader.Reader(states_shp).records():
### You want to replace the following code with code that sets the
### facecolor as a gradient based on the population density above
#facecolor = [0.9375, 0.9375, 0.859375]
edgecolor = 'black'
try:
# use the name of this state to get pop_density
state_dens = popdensity[ astate.attributes['name'] ]
except:
state_dens = 0
# simple scheme to assign color to each state
if state_dens < 40:
facecolor = "lightyellow"
elif state_dens > 200:
facecolor = "red"
else:
facecolor = "pink"
# `astate.geometry` is the polygon to plot
ax.add_geometries([astate.geometry], ccrs.PlateCarree(),
facecolor=facecolor, edgecolor=edgecolor)
plt.show()
The resulting plot:

Parse KML file with vba

I am trying to get 2 pieces of information from a KML file. I have some code (ref:K Buttters) in vba (as I want to add the data to a db). This code works perfectly and returns the coordinates, but I want to also get the data for the name tag. How do I amend the code to get this extra piece of information. information. Thanks
Public Sub readFile()
Dim objXML As MSXML2.DOMDocument60
Dim ns As String
Dim nodelist As IXMLDOMNodeList
Dim coordNode As IXMLDOMNode
Dim XPath As String
Dim Namespace As String
Dim i As Integer
'Create the DomDocument Object
'Set objXML = CreateObject("MSXML2.DOMDocument60")
Set objXML = New MSXML2.DOMDocument60
'Load entire Document before moving on
objXML.async = False
'Don't Validate
objXML.validateOnParse = False
'Read in the file
objXML.Load ("C:\pcsect2.kml")
''''''''''''''''''''''''''''''
' Set up the appropriate (default) namespace
''''''''''''''''''''''''''''''
Namespace = "xmlns:ns='http://www.opengis.net/kml/2.2'"
Call objXML.SetProperty("SelectionNamespaces", Namespace)
Call objXML.SetProperty("SelectionLanguage", "XPath")
''''''''''''''''''''''''''''''
' Define the XPath to get to coordinates
''''''''''''''''''''''''''''''
XPath = "//ns:coordinates"
''''''''''''''''''''''''''''''
' Select All "coordinates" nodes
''''''''''''''''''''''''''''''
Set nodelist = objXML.SelectNodes(XPath)
''''''''''''''''''''''''''''''
' Nodelist contains all the Coordinates...
' note: Under view ... set immediate window to viewable to see the debug.print results...
''''''''''''''''''''''''''''''
For i = 0 To nodelist.Length - 1
Set coordNode = nodelist.NextNode
Debug.Print coordNode.Text
Next i
End Sub
kml file:
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<description><![CDATA[UK postcode polygons]]></description>
<Style id="stylePostcode">
<LineStyle>
<width>3</width>
</LineStyle>
<PolyStyle>
<color>00ff0000</color>
</PolyStyle>
</Style>
<Style id="stylePostcode" xmlns="http://www.opengis.net/kml/2.2">
<LineStyle>
<width>3</width>
</LineStyle>
<PolyStyle>
<color>00ff0000</color>
</PolyStyle>
</Style>
<Folder xmlns="http://www.opengis.net/kml/2.2">
<name>AB10</name>
<description><![CDATA[Postcode sectors in the AB10 postcode district]]></description>
<Placemark>
<name>AB10 1</name>
<styleUrl>#stylePostcode</styleUrl>
<description><![CDATA[AB10 1 postcode sector]]></description>
<MultiGeometry>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-2.09646046762775,57.1512327554617 -2.0962834594401,57.1506128321186 -2.09610247782767
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-2.10596557515499,57.1385921418501 -2.10604057319705,57.1387481221535
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
<Placemark>
<name>AB10 6</name>
<styleUrl>#stylePostcode</styleUrl>
<description><![CDATA[AB10 6 postcode sector</a>]]></description>
<MultiGeometry>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-2.11311153246704,57.1365722876569 -2.11309853402536,57.1365692881912
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-2.1113360807897,57.1427795678869 -2.11112010338182,57.1426645845693
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
<Placemark>
<name>AB10 7</name>
<styleUrl>#stylePostcode</styleUrl>
<description><![CDATA[AB10 7 postcode sector>Details</a>]]></description>
<MultiGeometry>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-2.11383946866067,57.1372032022769 -2.11366248946505,57.1371542105275
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</Folder>
<Style id="stylePostcode" xmlns="http://www.opengis.net/kml/2.2">
<LineStyle>
<width>3</width>
</LineStyle>
<PolyStyle>
<color>00ff0000</color>
</PolyStyle>
</Style>
<Folder xmlns="http://www.opengis.net/kml/2.2">
<name>ZE3</name>
<description><![CDATA[Postcode sectors in the ZE3 postcode district]]></description>
<Placemark>
<name>ZE3 9</name>
<styleUrl>#stylePostcode</styleUrl>
<description><![CDATA[ZE3 9 postcode sector>Details</a>]]></description>
<MultiGeometry>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-1.33983120448744,59.8989006252828 -1.32301876923355,59.9016455421112
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<coordinates>
-1.32885903137389,59.8719950412819 -1.33463302343859,59.8690613106313
</LinearRing>
</outerBoundaryIs>
</Polygon>
</MultiGeometry>
</Placemark>
</Folder>
</Document>
</kml>

XML select and update

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:EligibilityRequest xmlns:ns1="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/mmiscommon" xmlns:ns4="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest" xmlns:ns3="http://xmlns.eohhs.ma.gov/newMMIS/ws/2006/05/MemberInfo" xmlns:ns5="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligresponse">
<ns4:transactionsource id_source="HIX" id_other="00000004353" id_medicaid="100208425271"/>
<ns4:demographic cde_born_to_st_empl=" " cde_born_to_ma=" " ind_pregnancy=" " pct_income_fpl="125" amt_income_fpl="1556.57" amt_indv_income="1638.5" amt_indv_prem="0" mail_adr_zip_code="02532" mail_adr_state="MA" mail_adr_city="Boston" mail_adr_street_2="APT 56" mail_adr_street_1="112 Main ST" res_adr_zip_code="02532" res_adr_state="MA" res_adr_city="Boston" res_adr_street_2="APT 56" res_adr_street_1="112 Main ST" nam_first="John" nam_last="Father" email="MAMedicaid01#hcentive.com" num_phone_day="8575550056" cde_lang_written="RUSSIN" cde_primary_lang="RUSSIN" cde_disability_stat=" " cde_homeless="N" cde_ethnicity="UNKNOW" cde_race="UNKNOW" cde_citizen="C" num_primary_ssn="278938115" cde_sex="M" dte_birth="1978-09-09"/>
<ns4:case amt_family_prem_assist="0" amt_family_mh_prem="0" amt_family_prem="0" hoh_nam_last="Father" hoh_nam_first="John" cde_case_status="2" num_case="00003235C"/>
<ns4:eligibility cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="4" cde_line="00" dte_end_elig="2014-11-13" dte_begin_elig="2014-11-03"/>
<ns4:eligibility cde_open_reason="01" cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="1" cde_line="00" dte_begin_elig="2014-11-03"/>
</ns4:EligibilityRequest>
If you open this attached XML, you can see there is 2 tags of <ns4:eligibility
I have to first select this last sub string, either by regexp_substr or simple str .. means output should be:
<ns4:eligibility cde_open_reason="01" cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="1" cde_line="00" dte_begin_elig="2014-11-03"/>
Then append this string to another xml. same like this. just append in last.
Oracle Setup:
CREATE TABLE XML_IN ( id INT, xml CLOB );
INSERT INTO XML_IN VALUES ( 1, '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns4:EligibilityRequest xmlns:ns1="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/mmiscommon" xmlns:ns4="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest" xmlns:ns3="http://xmlns.eohhs.ma.gov/newMMIS/ws/2006/05/MemberInfo" xmlns:ns5="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligresponse">
<ns4:transactionsource id_source="HIX" id_other="00000004353" id_medicaid="100208425271"/>
<ns4:demographic cde_born_to_st_empl=" " cde_born_to_ma=" " ind_pregnancy=" " pct_income_fpl="125" amt_income_fpl="1556.57" amt_indv_income="1638.5" amt_indv_prem="0" mail_adr_zip_code="02532" mail_adr_state="MA" mail_adr_city="Boston" mail_adr_street_2="APT 56" mail_adr_street_1="112 Main ST" res_adr_zip_code="02532" res_adr_state="MA" res_adr_city="Boston" res_adr_street_2="APT 56" res_adr_street_1="112 Main ST" nam_first="John" nam_last="Father" email="MAMedicaid01#hcentive.com" num_phone_day="8575550056" cde_lang_written="RUSSIN" cde_primary_lang="RUSSIN" cde_disability_stat=" " cde_homeless="N" cde_ethnicity="UNKNOW" cde_race="UNKNOW" cde_citizen="C" num_primary_ssn="278938115" cde_sex="M" dte_birth="1978-09-09"/>
<ns4:case amt_family_prem_assist="0" amt_family_mh_prem="0" amt_family_prem="0" hoh_nam_last="Father" hoh_nam_first="John" cde_case_status="2" num_case="00003235C"/>
<ns4:eligibility cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="4" cde_line="00" dte_end_elig="2014-11-13" dte_begin_elig="2014-11-03"/>
<ns4:eligibility cde_open_reason="01" cde_office="555" cde_region="58" dte_appl="2014-11-13" family_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="1" cde_line="00" dte_begin_elig="2014-11-03"/>
</ns4:EligibilityRequest>');
CREATE TABLE XML_OUT( id INT, xml CLOB );
INSERT INTO XML_OUT VALUES ( 1, '<test><attribute>existing</attribute></test>' );
Query:
MERGE INTO XML_OUT o
USING XML_IN i ON ( o.id = i.id )
WHEN MATCHED THEN
UPDATE
SET xml = APPENDCHILDXML(
XMLType( o.xml ),
'/test',
EXTRACT(
XMLType( i.xml ),
'//ns4:EligibilityRequest/ns4:eligibility[last()]',
'xmlns:ns4="http://newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest"'
)
).getClobVal();
Output:
SELECT * FROM XML_OUT;
ID XML
---------- ----------------------------------------------------------------------
1 <test><attribute>existing</attribute><ns4:eligibility xmlns:ns4="http:
//newmmis.eohhs.ma.gov/serviceobjects/versions/1.0/eligrequest" cde_ope
n_reason="01" cde_office="555" cde_region="58" dte_appl="2014-11-13" fa
mily_size="2" amt_gross_income="1556.57" cde_cat="D1" cde_elig_status="
1" cde_line="00" dte_begin_elig="2014-11-03"/></test>